diff options
Diffstat (limited to 'contrib/llvm/tools/clang/bindings/python/tests')
13 files changed, 250 insertions, 0 deletions
diff --git a/contrib/llvm/tools/clang/bindings/python/tests/__init__.py b/contrib/llvm/tools/clang/bindings/python/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/__init__.py diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header1.h b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header1.h new file mode 100644 index 0000000..b4eacbe --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header1.h @@ -0,0 +1,6 @@ +#ifndef HEADER1 +#define HEADER1 + +#include "header3.h" + +#endif diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header2.h b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header2.h new file mode 100644 index 0000000..c4eddc0 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header2.h @@ -0,0 +1,6 @@ +#ifndef HEADER2 +#define HEADER2 + +#include "header3.h" + +#endif diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header3.h b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header3.h new file mode 100644 index 0000000..6dca764 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/header3.h @@ -0,0 +1,3 @@ +// Not a guarded header! + +void f(); diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/hello.cpp b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/hello.cpp new file mode 100644 index 0000000..7ef086e --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/hello.cpp @@ -0,0 +1,6 @@ +#include "stdio.h" + +int main(int argc, char* argv[]) { + printf("hello world\n"); + return 0; +} diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/include.cpp b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/include.cpp new file mode 100644 index 0000000..60cfdaa --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/include.cpp @@ -0,0 +1,5 @@ +#include "header1.h" +#include "header2.h" +#include "header1.h" + +int main() { } diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/parse_arguments.c b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/parse_arguments.c new file mode 100644 index 0000000..7196486 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/INPUTS/parse_arguments.c @@ -0,0 +1,2 @@ +int DECL_ONE = 1; +int DECL_TWO = 2; diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/__init__.py b/contrib/llvm/tools/clang/bindings/python/tests/cindex/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/__init__.py diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_cursor.py b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_cursor.py new file mode 100644 index 0000000..a653ba7 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_cursor.py @@ -0,0 +1,59 @@ +from clang.cindex import Index, CursorKind + +kInput = """\ +// FIXME: Find nicer way to drop builtins and other cruft. +int start_decl; + +struct s0 { + int a; + int b; +}; + +struct s1; + +void f0(int a0, int a1) { + int l0, l1; + + if (a0) + return; + + for (;;) { + break; + } +} +""" + +def test_get_children(): + index = Index.create() + tu = index.parse('t.c', unsaved_files = [('t.c',kInput)]) + + # Skip until past start_decl. + it = tu.cursor.get_children() + while it.next().spelling != 'start_decl': + pass + + tu_nodes = list(it) + + assert len(tu_nodes) == 3 + + assert tu_nodes[0].kind == CursorKind.STRUCT_DECL + assert tu_nodes[0].spelling == 's0' + assert tu_nodes[0].is_definition() == True + assert tu_nodes[0].location.file.name == 't.c' + assert tu_nodes[0].location.line == 4 + assert tu_nodes[0].location.column == 8 + + s0_nodes = list(tu_nodes[0].get_children()) + assert len(s0_nodes) == 2 + assert s0_nodes[0].kind == CursorKind.FIELD_DECL + assert s0_nodes[0].spelling == 'a' + assert s0_nodes[1].kind == CursorKind.FIELD_DECL + assert s0_nodes[1].spelling == 'b' + + assert tu_nodes[1].kind == CursorKind.STRUCT_DECL + assert tu_nodes[1].spelling == 's1' + assert tu_nodes[1].is_definition() == False + + assert tu_nodes[2].kind == CursorKind.FUNCTION_DECL + assert tu_nodes[2].spelling == 'f0' + assert tu_nodes[2].is_definition() == True diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_cursor_kind.py b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_cursor_kind.py new file mode 100644 index 0000000..bdfa318 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_cursor_kind.py @@ -0,0 +1,27 @@ +from clang.cindex import CursorKind + +def test_name(): + assert CursorKind.UNEXPOSED_DECL.name is 'UNEXPOSED_DECL' + +def test_get_all_kinds(): + assert CursorKind.UNEXPOSED_DECL in CursorKind.get_all_kinds() + assert CursorKind.TRANSLATION_UNIT in CursorKind.get_all_kinds() + +def test_kind_groups(): + """Check that every kind classifies to exactly one group.""" + + assert CursorKind.UNEXPOSED_DECL.is_declaration() + assert CursorKind.TYPE_REF.is_reference() + assert CursorKind.DECL_REF_EXPR.is_expression() + assert CursorKind.UNEXPOSED_STMT.is_statement() + assert CursorKind.INVALID_FILE.is_invalid() + + for k in CursorKind.get_all_kinds(): + group = [n for n in ('is_declaration', 'is_reference', 'is_expression', + 'is_statement', 'is_invalid') + if getattr(k, n)()] + + if k == CursorKind.TRANSLATION_UNIT: + assert len(group) == 0 + else: + assert len(group) == 1 diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_diagnostics.py b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_diagnostics.py new file mode 100644 index 0000000..8518765 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_diagnostics.py @@ -0,0 +1,48 @@ +from clang.cindex import * + +def tu_from_source(source): + index = Index.create() + tu = index.parse('INPUT.c', unsaved_files = [('INPUT.c', source)]) + # FIXME: Remove the need for this. + tu.index = index + return tu + +# FIXME: We need support for invalid translation units to test better. + +def test_diagnostic_warning(): + tu = tu_from_source("""int f0() {}\n""") + assert len(tu.diagnostics) == 1 + assert tu.diagnostics[0].severity == Diagnostic.Warning + assert tu.diagnostics[0].location.line == 1 + assert tu.diagnostics[0].location.column == 11 + assert (tu.diagnostics[0].spelling == + 'control reaches end of non-void function') + +def test_diagnostic_note(): + # FIXME: We aren't getting notes here for some reason. + index = Index.create() + tu = tu_from_source("""#define A x\nvoid *A = 1;\n""") + assert len(tu.diagnostics) == 1 + assert tu.diagnostics[0].severity == Diagnostic.Warning + assert tu.diagnostics[0].location.line == 2 + assert tu.diagnostics[0].location.column == 7 + assert 'incompatible' in tu.diagnostics[0].spelling +# assert tu.diagnostics[1].severity == Diagnostic.Note +# assert tu.diagnostics[1].location.line == 1 +# assert tu.diagnostics[1].location.column == 11 +# assert tu.diagnostics[1].spelling == 'instantiated from' + +def test_diagnostic_fixit(): + index = Index.create() + tu = tu_from_source("""struct { int f0; } x = { f0 : 1 };""") + assert len(tu.diagnostics) == 1 + assert tu.diagnostics[0].severity == Diagnostic.Warning + assert tu.diagnostics[0].location.line == 1 + assert tu.diagnostics[0].location.column == 31 + assert tu.diagnostics[0].spelling.startswith('use of GNU old-style') + assert len(tu.diagnostics[0].fixits) == 1 + assert tu.diagnostics[0].fixits[0].range.start.line == 1 + assert tu.diagnostics[0].fixits[0].range.start.column == 26 + assert tu.diagnostics[0].fixits[0].range.end.line == 1 + assert tu.diagnostics[0].fixits[0].range.end.column == 30 + assert tu.diagnostics[0].fixits[0].value == '.f0 = ' diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_index.py b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_index.py new file mode 100644 index 0000000..dc173f0 --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_index.py @@ -0,0 +1,15 @@ +from clang.cindex import * +import os + +kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS') + +def test_create(): + index = Index.create() + +# FIXME: test Index.read + +def test_parse(): + index = Index.create() + assert isinstance(index, Index) + tu = index.parse(os.path.join(kInputsDir, 'hello.cpp')) + assert isinstance(tu, TranslationUnit) diff --git a/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_translation_unit.py b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_translation_unit.py new file mode 100644 index 0000000..3c05c3f --- /dev/null +++ b/contrib/llvm/tools/clang/bindings/python/tests/cindex/test_translation_unit.py @@ -0,0 +1,73 @@ +from clang.cindex import * +import os + +kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS') + +def test_spelling(): + path = os.path.join(kInputsDir, 'hello.cpp') + index = Index.create() + tu = index.parse(path) + assert tu.spelling == path + +def test_cursor(): + path = os.path.join(kInputsDir, 'hello.cpp') + index = Index.create() + tu = index.parse(path) + c = tu.cursor + assert isinstance(c, Cursor) + assert c.kind is CursorKind.TRANSLATION_UNIT + +def test_parse_arguments(): + path = os.path.join(kInputsDir, 'parse_arguments.c') + index = Index.create() + tu = index.parse(path, ['-DDECL_ONE=hello', '-DDECL_TWO=hi']) + spellings = [c.spelling for c in tu.cursor.get_children()] + assert spellings[-2] == 'hello' + assert spellings[-1] == 'hi' + +def test_unsaved_files(): + index = Index.create() + # FIXME: Why can't we just use "fake.h" here (instead of /tmp/fake.h)? + tu = index.parse('fake.c', unsaved_files = [ + ('fake.c', """ +#include "/tmp/fake.h" +int x; +int SOME_DEFINE; +"""), + ('/tmp/fake.h', """ +#define SOME_DEFINE y +""") + ]) + spellings = [c.spelling for c in tu.cursor.get_children()] + assert spellings[-2] == 'x' + assert spellings[-1] == 'y' + +def test_unsaved_files_2(): + import StringIO + index = Index.create() + tu = index.parse('fake.c', unsaved_files = [ + ('fake.c', StringIO.StringIO('int x;'))]) + spellings = [c.spelling for c in tu.cursor.get_children()] + assert spellings[-1] == 'x' + + +def test_includes(): + def eq(expected, actual): + if not actual.is_input_file: + return expected[0] == actual.source.name and \ + expected[1] == actual.include.name + else: + return expected[1] == actual.include.name + + src = os.path.join(kInputsDir, 'include.cpp') + h1 = os.path.join(kInputsDir, "header1.h") + h2 = os.path.join(kInputsDir, "header2.h") + h3 = os.path.join(kInputsDir, "header3.h") + inc = [(None, src), (src, h1), (h1, h3), (src, h2), (h2, h3)] + + index = Index.create() + tu = index.parse(src) + for i in zip(inc, tu.get_includes()): + assert eq(i[0], i[1]) + + |