diff options
Diffstat (limited to 'bindings/python/tests')
19 files changed, 285 insertions, 14 deletions
diff --git a/bindings/python/tests/CMakeLists.txt b/bindings/python/tests/CMakeLists.txt new file mode 100644 index 000000000000..7af6503f1588 --- /dev/null +++ b/bindings/python/tests/CMakeLists.txt @@ -0,0 +1,46 @@ +# Test target to run Python test suite from main build. + +add_custom_target(check-clang-python + COMMAND ${CMAKE_COMMAND} -E env + CLANG_LIBRARY_PATH=$<TARGET_FILE_DIR:libclang> + ${PYTHON_EXECUTABLE} -m unittest discover + DEPENDS libclang + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..) + +set(RUN_PYTHON_TESTS TRUE) +set_target_properties(check-clang-python PROPERTIES FOLDER "Clang tests") + +# Tests require libclang.so which is only built with LLVM_ENABLE_PIC=ON +if(NOT LLVM_ENABLE_PIC) + set(RUN_PYTHON_TESTS FALSE) +endif() + +# Do not try to run if libclang was built with ASan because +# the sanitizer library will likely be loaded too late to perform +# interception and will then fail. +# We could use LD_PRELOAD/DYLD_INSERT_LIBRARIES but this isn't +# portable so its easier just to not run the tests when building +# with ASan. +list(FIND LLVM_USE_SANITIZER "Address" LLVM_USE_ASAN_INDEX) +if(NOT LLVM_USE_ASAN_INDEX EQUAL -1) + set(RUN_PYTHON_TESTS FALSE) +endif() + +# Tests fail on Windows, and need someone knowledgeable to fix. +# It's not clear whether it's a test or a valid binding problem. +if(WIN32) + set(RUN_PYTHON_TESTS FALSE) +endif() + +# AArch64 and Hexagon have known test failures that need to be +# addressed. +# SystemZ has broken Python/FFI interface: +# https://reviews.llvm.org/D52840#1265716 +if(${LLVM_NATIVE_ARCH} MATCHES "^(AArch64|Hexagon|SystemZ)$") + set(RUN_PYTHON_TESTS FALSE) +endif() + +if(RUN_PYTHON_TESTS) + set_property(GLOBAL APPEND PROPERTY + LLVM_ADDITIONAL_TEST_TARGETS check-clang-python) +endif() diff --git a/bindings/python/tests/cindex/test_access_specifiers.py b/bindings/python/tests/cindex/test_access_specifiers.py index 2f6144be082c..e36424f240aa 100644 --- a/bindings/python/tests/cindex/test_access_specifiers.py +++ b/bindings/python/tests/cindex/test_access_specifiers.py @@ -1,3 +1,7 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) from clang.cindex import AccessSpecifier from clang.cindex import Cursor diff --git a/bindings/python/tests/cindex/test_cdb.py b/bindings/python/tests/cindex/test_cdb.py index 64651af31732..589fc72856bd 100644 --- a/bindings/python/tests/cindex/test_cdb.py +++ b/bindings/python/tests/cindex/test_cdb.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import CompilationDatabase from clang.cindex import CompilationDatabaseError from clang.cindex import CompileCommands @@ -6,6 +11,8 @@ import os import gc import unittest import sys +from .util import skip_if_no_fspath +from .util import str_to_path kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS') @@ -26,17 +33,19 @@ class TestCDB(unittest.TestCase): """Check we can load a compilation database""" cdb = CompilationDatabase.fromDirectory(kInputsDir) - def test_lookup_fail(self): - """Check file lookup failure""" - cdb = CompilationDatabase.fromDirectory(kInputsDir) - self.assertIsNone(cdb.getCompileCommands('file_do_not_exist.cpp')) - def test_lookup_succeed(self): """Check we get some results if the file exists in the db""" cdb = CompilationDatabase.fromDirectory(kInputsDir) cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp') self.assertNotEqual(len(cmds), 0) + @skip_if_no_fspath + def test_lookup_succeed_pathlike(self): + """Same as test_lookup_succeed, but with PathLikes""" + cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir)) + cmds = cdb.getCompileCommands(str_to_path('/home/john.doe/MyProject/project.cpp')) + self.assertNotEqual(len(cmds), 0) + def test_all_compilecommand(self): """Check we get all results from the db""" cdb = CompilationDatabase.fromDirectory(kInputsDir) diff --git a/bindings/python/tests/cindex/test_code_completion.py b/bindings/python/tests/cindex/test_code_completion.py index a56bb304cd71..e0b41577aeb3 100644 --- a/bindings/python/tests/cindex/test_code_completion.py +++ b/bindings/python/tests/cindex/test_code_completion.py @@ -1,6 +1,13 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import TranslationUnit import unittest +from .util import skip_if_no_fspath +from .util import str_to_path class TestCodeCompletion(unittest.TestCase): @@ -38,6 +45,32 @@ void f() { ] self.check_completion_results(cr, expected) + @skip_if_no_fspath + def test_code_complete_pathlike(self): + files = [(str_to_path('fake.c'), """ +/// Aaa. +int test1; + +/// Bbb. +void test2(void); + +void f() { + +} +""")] + + tu = TranslationUnit.from_source(str_to_path('fake.c'), ['-std=c99'], unsaved_files=files, + options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION) + + cr = tu.codeComplete(str_to_path('fake.c'), 9, 1, unsaved_files=files, include_brief_comments=True) + + expected = [ + "{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.", + "{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.", + "{'return', TypedText} || Priority: 40 || Availability: Available || Brief comment: None" + ] + self.check_completion_results(cr, expected) + def test_code_complete_availability(self): files = [('fake.cpp', """ class P { @@ -61,11 +94,11 @@ void f(P x, Q y) { cr = tu.codeComplete('fake.cpp', 12, 5, unsaved_files=files) expected = [ - "{'const', TypedText} || Priority: 40 || Availability: Available || Brief comment: None", - "{'volatile', TypedText} || Priority: 40 || Availability: Available || Brief comment: None", + "{'const', TypedText} || Priority: 50 || Availability: Available || Brief comment: None", + "{'volatile', TypedText} || Priority: 50 || Availability: Available || Brief comment: None", "{'operator', TypedText} || Priority: 40 || Availability: Available || Brief comment: None", - "{'P', TypedText} | {'::', Text} || Priority: 75 || Availability: Available || Brief comment: None", - "{'Q', TypedText} | {'::', Text} || Priority: 75 || Availability: Available || Brief comment: None" + "{'P', TypedText} || Priority: 50 || Availability: Available || Brief comment: None", + "{'Q', TypedText} || Priority: 50 || Availability: Available || Brief comment: None" ] self.check_completion_results(cr, expected) diff --git a/bindings/python/tests/cindex/test_comment.py b/bindings/python/tests/cindex/test_comment.py index d6c6d8e5c5b0..73fb617ae168 100644 --- a/bindings/python/tests/cindex/test_comment.py +++ b/bindings/python/tests/cindex/test_comment.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import TranslationUnit from tests.cindex.util import get_cursor diff --git a/bindings/python/tests/cindex/test_cursor.py b/bindings/python/tests/cindex/test_cursor.py index f5733fd15879..ef875e972474 100644 --- a/bindings/python/tests/cindex/test_cursor.py +++ b/bindings/python/tests/cindex/test_cursor.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + import ctypes import gc import unittest diff --git a/bindings/python/tests/cindex/test_cursor_kind.py b/bindings/python/tests/cindex/test_cursor_kind.py index f1ee753ef8b1..e6b9558b3cc1 100644 --- a/bindings/python/tests/cindex/test_cursor_kind.py +++ b/bindings/python/tests/cindex/test_cursor_kind.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import CursorKind import unittest diff --git a/bindings/python/tests/cindex/test_diagnostics.py b/bindings/python/tests/cindex/test_diagnostics.py index 78b327daa72c..c17d5b28efe9 100644 --- a/bindings/python/tests/cindex/test_diagnostics.py +++ b/bindings/python/tests/cindex/test_diagnostics.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import * from .util import get_tu @@ -46,7 +51,7 @@ class TestDiagnostics(unittest.TestCase): self.assertEqual(tu.diagnostics[0].fixits[0].value, '.f0 = ') def test_diagnostic_range(self): - tu = get_tu('void f() { int i = "a" + 1; }') + tu = get_tu('void f() { int i = "a"; }') self.assertEqual(len(tu.diagnostics), 1) self.assertEqual(tu.diagnostics[0].severity, Diagnostic.Warning) self.assertEqual(tu.diagnostics[0].location.line, 1) @@ -58,7 +63,7 @@ class TestDiagnostics(unittest.TestCase): self.assertEqual(tu.diagnostics[0].ranges[0].start.line, 1) self.assertEqual(tu.diagnostics[0].ranges[0].start.column, 20) self.assertEqual(tu.diagnostics[0].ranges[0].end.line, 1) - self.assertEqual(tu.diagnostics[0].ranges[0].end.column, 27) + self.assertEqual(tu.diagnostics[0].ranges[0].end.column, 23) with self.assertRaises(IndexError): tu.diagnostics[0].ranges[1].start.line diff --git a/bindings/python/tests/cindex/test_exception_specification_kind.py b/bindings/python/tests/cindex/test_exception_specification_kind.py index 80b3639a8ab3..6c13f70fb256 100644 --- a/bindings/python/tests/cindex/test_exception_specification_kind.py +++ b/bindings/python/tests/cindex/test_exception_specification_kind.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + import clang.cindex from clang.cindex import ExceptionSpecificationKind from .util import get_tu diff --git a/bindings/python/tests/cindex/test_file.py b/bindings/python/tests/cindex/test_file.py index 98f6575262cc..a146fe5c9239 100644 --- a/bindings/python/tests/cindex/test_file.py +++ b/bindings/python/tests/cindex/test_file.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import Index, File import unittest diff --git a/bindings/python/tests/cindex/test_index.py b/bindings/python/tests/cindex/test_index.py index cfdf98e628ac..46aafcf222e7 100644 --- a/bindings/python/tests/cindex/test_index.py +++ b/bindings/python/tests/cindex/test_index.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import * import os import unittest diff --git a/bindings/python/tests/cindex/test_linkage.py b/bindings/python/tests/cindex/test_linkage.py index 6b482f8081b3..cdd97fc2df0d 100644 --- a/bindings/python/tests/cindex/test_linkage.py +++ b/bindings/python/tests/cindex/test_linkage.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import LinkageKind from clang.cindex import Cursor from clang.cindex import TranslationUnit diff --git a/bindings/python/tests/cindex/test_location.py b/bindings/python/tests/cindex/test_location.py index cbc32deb4bd5..fbe9770d7ebc 100644 --- a/bindings/python/tests/cindex/test_location.py +++ b/bindings/python/tests/cindex/test_location.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import Cursor from clang.cindex import File from clang.cindex import SourceLocation diff --git a/bindings/python/tests/cindex/test_tls_kind.py b/bindings/python/tests/cindex/test_tls_kind.py index fbc3418a64ef..c828ac83a468 100644 --- a/bindings/python/tests/cindex/test_tls_kind.py +++ b/bindings/python/tests/cindex/test_tls_kind.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import TLSKind from clang.cindex import Cursor from clang.cindex import TranslationUnit diff --git a/bindings/python/tests/cindex/test_token_kind.py b/bindings/python/tests/cindex/test_token_kind.py index 700f95a64624..904e007cafc5 100644 --- a/bindings/python/tests/cindex/test_token_kind.py +++ b/bindings/python/tests/cindex/test_token_kind.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import TokenKind import unittest diff --git a/bindings/python/tests/cindex/test_tokens.py b/bindings/python/tests/cindex/test_tokens.py index c93353dc9da2..dd6d3a3259ed 100644 --- a/bindings/python/tests/cindex/test_tokens.py +++ b/bindings/python/tests/cindex/test_tokens.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from clang.cindex import CursorKind from clang.cindex import Index from clang.cindex import SourceLocation diff --git a/bindings/python/tests/cindex/test_translation_unit.py b/bindings/python/tests/cindex/test_translation_unit.py index d3ee535f4d0d..f3e770a93611 100644 --- a/bindings/python/tests/cindex/test_translation_unit.py +++ b/bindings/python/tests/cindex/test_translation_unit.py @@ -1,6 +1,12 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + from contextlib import contextmanager import gc import os +import sys import tempfile import unittest @@ -15,6 +21,8 @@ from clang.cindex import TranslationUnitLoadError from clang.cindex import TranslationUnit from .util import get_cursor from .util import get_tu +from .util import skip_if_no_fspath +from .util import str_to_path kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS') @@ -31,6 +39,17 @@ def save_tu(tu): yield t.name +@contextmanager +def save_tu_pathlike(tu): + """Convenience API to save a TranslationUnit to a file. + + Returns the filename it was saved to. + """ + with tempfile.NamedTemporaryFile() as t: + tu.save(str_to_path(t.name)) + yield t.name + + class TestTranslationUnit(unittest.TestCase): def test_spelling(self): path = os.path.join(kInputsDir, 'hello.cpp') @@ -75,15 +94,31 @@ int SOME_DEFINE; self.assertEqual(spellings[-1], 'y') def test_unsaved_files_2(self): - try: - from StringIO import StringIO - except: + if sys.version_info.major >= 3: from io import StringIO + else: + from io import BytesIO as StringIO tu = TranslationUnit.from_source('fake.c', unsaved_files = [ ('fake.c', StringIO('int x;'))]) spellings = [c.spelling for c in tu.cursor.get_children()] self.assertEqual(spellings[-1], 'x') + @skip_if_no_fspath + def test_from_source_accepts_pathlike(self): + tu = TranslationUnit.from_source(str_to_path('fake.c'), ['-Iincludes'], unsaved_files = [ + (str_to_path('fake.c'), """ +#include "fake.h" + int x; + int SOME_DEFINE; + """), + (str_to_path('includes/fake.h'), """ +#define SOME_DEFINE y + """) + ]) + spellings = [c.spelling for c in tu.cursor.get_children()] + self.assertEqual(spellings[-2], 'x') + self.assertEqual(spellings[-1], 'y') + def assert_normpaths_equal(self, path1, path2): """ Compares two paths for equality after normalizing them with os.path.normpath @@ -130,6 +165,16 @@ int SOME_DEFINE; self.assertTrue(os.path.exists(path)) self.assertGreater(os.path.getsize(path), 0) + @skip_if_no_fspath + def test_save_pathlike(self): + """Ensure TranslationUnit.save() works with PathLike filename.""" + + tu = get_tu('int foo();') + + with save_tu_pathlike(tu) as path: + self.assertTrue(os.path.exists(path)) + self.assertGreater(os.path.getsize(path), 0) + def test_save_translation_errors(self): """Ensure that saving to an invalid directory raises.""" @@ -162,6 +207,22 @@ int SOME_DEFINE; # Just in case there is an open file descriptor somewhere. del tu2 + @skip_if_no_fspath + def test_load_pathlike(self): + """Ensure TranslationUnits can be constructed from saved files - + PathLike variant.""" + tu = get_tu('int foo();') + self.assertEqual(len(tu.diagnostics), 0) + with save_tu(tu) as path: + tu2 = TranslationUnit.from_ast_file(filename=str_to_path(path)) + self.assertEqual(len(tu2.diagnostics), 0) + + foo = get_cursor(tu2, 'foo') + self.assertIsNotNone(foo) + + # Just in case there is an open file descriptor somewhere. + del tu2 + def test_index_parse(self): path = os.path.join(kInputsDir, 'hello.cpp') index = Index.create() @@ -180,6 +241,19 @@ int SOME_DEFINE; with self.assertRaises(Exception): f = tu.get_file('foobar.cpp') + @skip_if_no_fspath + def test_get_file_pathlike(self): + """Ensure tu.get_file() works appropriately with PathLike filenames.""" + + tu = get_tu('int foo();') + + f = tu.get_file(str_to_path('t.c')) + self.assertIsInstance(f, File) + self.assertEqual(f.name, 't.c') + + with self.assertRaises(Exception): + f = tu.get_file(str_to_path('foobar.cpp')) + def test_get_source_location(self): """Ensure tu.get_source_location() works.""" diff --git a/bindings/python/tests/cindex/test_type.py b/bindings/python/tests/cindex/test_type.py index 4dec0583c7b0..bcdbeff9d99c 100644 --- a/bindings/python/tests/cindex/test_type.py +++ b/bindings/python/tests/cindex/test_type.py @@ -1,3 +1,8 @@ +import os +from clang.cindex import Config +if 'CLANG_LIBRARY_PATH' in os.environ: + Config.set_library_path(os.environ['CLANG_LIBRARY_PATH']) + import gc import unittest @@ -436,3 +441,28 @@ class TestType(unittest.TestCase): self.assertIsNotNone(testInteger, "Could not find testInteger.") self.assertEqual(testInteger.type.get_address_space(), 2) + + def test_template_arguments(self): + source = """ + class Foo { + }; + template <typename T> + class Template { + }; + Template<Foo> instance; + int bar; + """ + tu = get_tu(source, lang='cpp') + + # Varible with a template argument. + cursor = get_cursor(tu, 'instance') + cursor_type = cursor.type + self.assertEqual(cursor.kind, CursorKind.VAR_DECL) + self.assertEqual(cursor_type.spelling, 'Template<Foo>') + self.assertEqual(cursor_type.get_num_template_arguments(), 1) + template_type = cursor_type.get_template_argument_type(0) + self.assertEqual(template_type.spelling, 'Foo') + + # Variable without a template argument. + cursor = get_cursor(tu, 'bar') + self.assertEqual(cursor.get_num_template_arguments(), -1) diff --git a/bindings/python/tests/cindex/util.py b/bindings/python/tests/cindex/util.py index c53ba7c81bde..57e17941c558 100644 --- a/bindings/python/tests/cindex/util.py +++ b/bindings/python/tests/cindex/util.py @@ -1,5 +1,15 @@ # This file provides common utility functions for the test suite. +import os +HAS_FSPATH = hasattr(os, 'fspath') + +if HAS_FSPATH: + from pathlib import Path as str_to_path +else: + str_to_path = None + +import unittest + from clang.cindex import Cursor from clang.cindex import TranslationUnit @@ -68,8 +78,13 @@ def get_cursors(source, spelling): return cursors +skip_if_no_fspath = unittest.skipUnless(HAS_FSPATH, + "Requires file system path protocol / Python 3.6+") + __all__ = [ 'get_cursor', 'get_cursors', 'get_tu', + 'skip_if_no_fspath', + 'str_to_path', ] |