I don't believe anything functional changed with this patch. Just some
housekeeping. It was big enough that I didn't think it was trivial and
qualified for automatic commit.

---
 bindings/python/tests/cindex/test_cursor.py      |   32 ++++------
 bindings/python/tests/cindex/test_diagnostics.py |   33 +++-------
 bindings/python/tests/cindex/test_location.py    |   78 ++++++++++++----------
 bindings/python/tests/cindex/test_type.py        |   32 +--------
 bindings/python/tests/cindex/util.py             |   65 ++++++++++++++++++
 5 files changed, 130 insertions(+), 110 deletions(-)
 create mode 100644 bindings/python/tests/cindex/util.py
diff --git a/bindings/python/tests/cindex/test_cursor.py b/bindings/python/tests/cindex/test_cursor.py
index d58ca95..c1d7c87 100644
--- a/bindings/python/tests/cindex/test_cursor.py
+++ b/bindings/python/tests/cindex/test_cursor.py
@@ -1,9 +1,12 @@
-from clang.cindex import Index, CursorKind, TypeKind
+from clang.cindex import CursorKind
+from clang.cindex import TypeKind
+from .util import get_cursor
+from .util import get_tu
 
 kInput = """\
 // FIXME: Find nicer way to drop builtins and other cruft.
 int start_decl;
 
 struct s0 {
   int a;
   int b;
@@ -19,19 +22,18 @@ void f0(int a0, int a1) {
 
   for (;;) {
     break;
   }
 }
 """
 
 def test_get_children():
-    index = Index.create()
-    tu = index.parse('t.c', unsaved_files = [('t.c',kInput)])
-    
+    tu = get_tu(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
@@ -61,35 +63,25 @@ def test_get_children():
 
     assert tu_nodes[2].kind == CursorKind.FUNCTION_DECL
     assert tu_nodes[2].spelling == 'f0'
     assert tu_nodes[2].displayname == 'f0(int, int)'
     assert tu_nodes[2].is_definition() == True
 
 def test_underlying_type():
     source = 'typedef int foo;'
-    index = Index.create()
-    tu = index.parse('test.c', unsaved_files=[('test.c', source)])
-    assert tu is not None
-
-    for cursor in tu.cursor.get_children():
-        if cursor.spelling == 'foo':
-            typedef = cursor
-            break
+    tu = get_tu(source)
+    typedef = get_cursor(tu, 'foo')
+    assert typedef is not None
 
     assert typedef.kind.is_declaration()
     underlying = typedef.underlying_typedef_type
     assert underlying.kind == TypeKind.INT
 
 def test_enum_type():
     source = 'enum TEST { FOO=1, BAR=2 };'
-    index = Index.create()
-    tu = index.parse('test.c', unsaved_files=[('test.c', source)])
-    assert tu is not None
-
-    for cursor in tu.cursor.get_children():
-        if cursor.spelling == 'TEST':
-            enum = cursor
-            break
+    tu = get_tu(source)
+    enum = get_cursor(tu, 'TEST')
+    assert enum is not None
 
     assert enum.kind == CursorKind.ENUM_DECL
     enum_type = enum.enum_type
     assert enum_type.kind == TypeKind.UINT
diff --git a/bindings/python/tests/cindex/test_diagnostics.py b/bindings/python/tests/cindex/test_diagnostics.py
index b9872d9..48ab617 100644
--- a/bindings/python/tests/cindex/test_diagnostics.py
+++ b/bindings/python/tests/cindex/test_diagnostics.py
@@ -1,63 +1,51 @@
 from clang.cindex import *
-
-def tu_from_source(source, all_warnings=False):
-    args = []
-    if all_warnings:
-        args = ['-Wall', '-Wextra']
-
-    index = Index.create()
-    tu = index.parse('INPUT.c', args=args,
-            unsaved_files = [('INPUT.c', source)])
-    return tu
+from .util import get_tu
 
 # FIXME: We need support for invalid translation units to test better.
 
 def test_diagnostic_warning():
-    tu = tu_from_source("""int f0() {}\n""")
+    tu = get_tu('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""")
+    tu = get_tu('#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 };""")
+    tu = get_tu('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 == 26
     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 = '
 
 def test_diagnostic_range():
-    index = Index.create()
-    tu = tu_from_source("""void f() { int i = "a" + 1; }""")
+    tu = get_tu('void f() { int i = "a" + 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 == 16
     assert tu.diagnostics[0].spelling.startswith('incompatible pointer to')
     assert len(tu.diagnostics[0].fixits) == 0
     assert len(tu.diagnostics[0].ranges) == 1
     assert tu.diagnostics[0].ranges[0].start.line == 1
@@ -67,31 +55,28 @@ def test_diagnostic_range():
     try:
       tu.diagnostics[0].ranges[1].start.line
     except IndexError:
       assert True
     else:
       assert False
 
 def test_diagnostic_category():
-    # Ensure that category properties work.
-    index = Index.create()
-    tu = tu_from_source("""int f(int i) { return 7; }""", all_warnings=True)
+    """Ensure that category properties work."""
+    tu = get_tu('int f(int i) { return 7; }', all_warnings=True)
     assert len(tu.diagnostics) == 1
     d = tu.diagnostics[0]
 
     assert d.severity == Diagnostic.Warning
     assert d.location.line == 1
     assert d.location.column == 11
 
     assert d.category_number == 2
     assert d.category_name == 'Semantic Issue'
 
 def test_diagnostic_option():
-    # Ensure that category option properties work.
-    index = Index.create()
-    tu = tu_from_source("""int f(int i) { return 7; }""", all_warnings=True)
+    """Ensure that category option properties work."""
+    tu = get_tu('int f(int i) { return 7; }', all_warnings=True)
     assert len(tu.diagnostics) == 1
     d = tu.diagnostics[0]
 
     assert d.option == '-Wunused-parameter'
     assert d.disable_option == '-Wno-unused-parameter'
-
diff --git a/bindings/python/tests/cindex/test_location.py b/bindings/python/tests/cindex/test_location.py
index 1707f01..528676e 100644
--- a/bindings/python/tests/cindex/test_location.py
+++ b/bindings/python/tests/cindex/test_location.py
@@ -1,76 +1,82 @@
-from clang.cindex import Index, File, SourceLocation, SourceRange, Cursor
+from clang.cindex import Cursor
+from clang.cindex import File
+from clang.cindex import SourceLocation
+from clang.cindex import SourceRange
+from .util import get_cursor
+from .util import get_tu
 
 baseInput="int one;\nint two;\n"
 
 def assert_location(loc, line, column, offset):
     assert loc.line == line
     assert loc.column == column
     assert loc.offset == offset
 
 def test_location():
-    index = Index.create()
-    tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)])
+    tu = get_tu(baseInput)
+    one = get_cursor(tu, 'one')
+    two = get_cursor(tu, 'two')
 
-    for n in tu.cursor.get_children():
-        if n.spelling == 'one':
-            assert_location(n.location,line=1,column=5,offset=4)
-        if n.spelling == 'two':
-            assert_location(n.location,line=2,column=5,offset=13)
+    assert one is not None
+    assert two is not None
+
+    assert_location(one.location,line=1,column=5,offset=4)
+    assert_location(two.location,line=2,column=5,offset=13)
 
     # adding a linebreak at top should keep columns same
-    tu = index.parse('t.c', unsaved_files = [('t.c',"\n"+baseInput)])
+    tu = get_tu('\n' + baseInput)
+    one = get_cursor(tu, 'one')
+    two = get_cursor(tu, 'two')
+
+    assert one is not None
+    assert two is not None
 
-    for n in tu.cursor.get_children():
-        if n.spelling == 'one':
-            assert_location(n.location,line=2,column=5,offset=5)
-        if n.spelling == 'two':
-            assert_location(n.location,line=3,column=5,offset=14)
+    assert_location(one.location,line=2,column=5,offset=5)
+    assert_location(two.location,line=3,column=5,offset=14)
 
     # adding a space should affect column on first line only
-    tu = index.parse('t.c', unsaved_files = [('t.c'," "+baseInput)])
+    tu = get_tu(' ' + baseInput)
+    one = get_cursor(tu, 'one')
+    two = get_cursor(tu, 'two')
 
-    for n in tu.cursor.get_children():
-        if n.spelling == 'one':
-            assert_location(n.location,line=1,column=6,offset=5)
-        if n.spelling == 'two':
-            assert_location(n.location,line=2,column=5,offset=14)
+    assert_location(one.location,line=1,column=6,offset=5)
+    assert_location(two.location,line=2,column=5,offset=14)
 
     # define the expected location ourselves and see if it matches
     # the returned location
-    tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)])
+    tu = get_tu(baseInput)
 
     file = File.from_name(tu, 't.c')
     location = SourceLocation.from_position(tu, file, 1, 5)
     cursor = Cursor.from_location(tu, location)
 
-    for n in tu.cursor.get_children():
-        if n.spelling == 'one':
-            assert n == cursor
+    one = get_cursor(tu, 'one')
+    assert one is not None
+    assert one == cursor
 
     # Ensure locations referring to the same entity are equivalent.
     location2 = SourceLocation.from_position(tu, file, 1, 5)
     assert location == location2
     location3 = SourceLocation.from_position(tu, file, 1, 4)
     assert location2 != location3
 
 def test_extent():
-    index = Index.create()
-    tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)])
-
-    for n in tu.cursor.get_children():
-        if n.spelling == 'one':
-            assert_location(n.extent.start,line=1,column=1,offset=0)
-            assert_location(n.extent.end,line=1,column=8,offset=7)
-            assert baseInput[n.extent.start.offset:n.extent.end.offset] == "int one"
-        if n.spelling == 'two':
-            assert_location(n.extent.start,line=2,column=1,offset=9)
-            assert_location(n.extent.end,line=2,column=8,offset=16)
-            assert baseInput[n.extent.start.offset:n.extent.end.offset] == "int two"
+    tu = get_tu(baseInput)
+    one = get_cursor(tu, 'one')
+    two = get_cursor(tu, 'two')
+
+    assert_location(one.extent.start,line=1,column=1,offset=0)
+    assert_location(one.extent.end,line=1,column=8,offset=7)
+    assert baseInput[one.extent.start.offset:one.extent.end.offset] == "int one"
+
+    assert_location(two.extent.start,line=2,column=1,offset=9)
+    assert_location(two.extent.end,line=2,column=8,offset=16)
+    assert baseInput[two.extent.start.offset:two.extent.end.offset] == "int two"
 
     file = File.from_name(tu, 't.c')
     location1 = SourceLocation.from_position(tu, file, 1, 1)
     location2 = SourceLocation.from_position(tu, file, 1, 8)
 
     range1 = SourceRange.from_locations(location1, location2)
     range2 = SourceRange.from_locations(location1, location2)
     assert range1 == range2
diff --git a/bindings/python/tests/cindex/test_type.py b/bindings/python/tests/cindex/test_type.py
index e4cd245..ed85210 100644
--- a/bindings/python/tests/cindex/test_type.py
+++ b/bindings/python/tests/cindex/test_type.py
@@ -1,13 +1,14 @@
-from clang.cindex import Cursor
 from clang.cindex import CursorKind
 from clang.cindex import Index
 from clang.cindex import TypeKind
 from nose.tools import raises
+from .util import get_cursor
+from .util import get_tu
 
 kInput = """\
 
 typedef int I;
 
 struct teststruct {
   int a;
   I b;
@@ -16,45 +17,16 @@ struct teststruct {
   signed long e;
   const int f;
   int *g;
   int ***h;
 };
 
 """
 
-def get_tu(source=kInput, lang='c'):
-    name = 't.c'
-    if lang == 'cpp':
-        name = 't.cpp'
-
-    index = Index.create()
-    tu = index.parse(name, unsaved_files=[(name, source)])
-    assert tu is not None
-    return tu
-
-def get_cursor(source, spelling):
-    children = []
-    if isinstance(source, Cursor):
-        children = source.get_children()
-    else:
-        # Assume TU
-        children = source.cursor.get_children()
-
-    for cursor in children:
-        if cursor.spelling == spelling:
-            return cursor
-
-        # Recurse into children.
-        result = get_cursor(cursor, spelling)
-        if result is not None:
-            return result
-
-    return None
-
 def test_a_struct():
     tu = get_tu(kInput)
 
     teststruct = get_cursor(tu, 'teststruct')
     assert teststruct is not None, "Could not find teststruct."
     fields = list(teststruct.get_children())
     assert all(x.kind == CursorKind.FIELD_DECL for x in fields)
 
diff --git a/bindings/python/tests/cindex/util.py b/bindings/python/tests/cindex/util.py
new file mode 100644
index 0000000..388b269
--- /dev/null
+++ b/bindings/python/tests/cindex/util.py
@@ -0,0 +1,65 @@
+# This file provides common utility functions for the test suite.
+
+from clang.cindex import Cursor
+from clang.cindex import Index
+
+def get_tu(source, lang='c', all_warnings=False):
+    """Obtain a translation unit from source and language.
+
+    By default, the translation unit is created from source file "t.<ext>"
+    where <ext> is the default file extension for the specified language. By
+    default it is C, so "t.c" is the default file name.
+
+    Supported languages are {c, cpp, objc}.
+
+    all_warnings is a convenience argument to enable all compiler warnings.
+    """
+    name = 't.c'
+    if lang == 'cpp':
+        name = 't.cpp'
+    elif lang == 'objc':
+        name = 't.m'
+    elif lang != 'c':
+        raise Exception('Unknown language: %s' % lang)
+
+    args = []
+    if all_warnings:
+        args = ['-Wall', '-Wextra']
+
+    index = Index.create()
+    tu = index.parse(name, args=args, unsaved_files=[(name, source)])
+    assert tu is not None
+    return tu
+
+def get_cursor(source, spelling):
+    """Obtain a cursor from a source object.
+
+    This provides a convenient search mechanism to find a cursor with specific
+    spelling within a source. The first argument can be either a
+    TranslationUnit or Cursor instance.
+
+    If the cursor is not found, None is returned.
+    """
+    children = []
+    if isinstance(source, Cursor):
+        children = source.get_children()
+    else:
+        # Assume TU
+        children = source.cursor.get_children()
+
+    for cursor in children:
+        if cursor.spelling == spelling:
+            return cursor
+
+        # Recurse into children.
+        result = get_cursor(cursor, spelling)
+        if result is not None:
+            return result
+
+    return None
+
+
+__all__ = [
+    'get_cursor',
+    'get_tu',
+]
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to