diff options
Diffstat (limited to 'bindings/python/tests/cindex/util.py')
-rw-r--r-- | bindings/python/tests/cindex/util.py | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/bindings/python/tests/cindex/util.py b/bindings/python/tests/cindex/util.py index 388b269..2323839 100644 --- a/bindings/python/tests/cindex/util.py +++ b/bindings/python/tests/cindex/util.py @@ -1,7 +1,7 @@ # This file provides common utility functions for the test suite. from clang.cindex import Cursor -from clang.cindex import Index +from clang.cindex import TranslationUnit def get_tu(source, lang='c', all_warnings=False): """Obtain a translation unit from source and language. @@ -15,21 +15,20 @@ def get_tu(source, lang='c', all_warnings=False): all_warnings is a convenience argument to enable all compiler warnings. """ name = 't.c' + args = [] if lang == 'cpp': name = 't.cpp' + args.append('-std=c++11') elif lang == 'objc': name = 't.m' elif lang != 'c': raise Exception('Unknown language: %s' % lang) - args = [] if all_warnings: - args = ['-Wall', '-Wextra'] + args += ['-Wall', '-Wextra'] - index = Index.create() - tu = index.parse(name, args=args, unsaved_files=[(name, source)]) - assert tu is not None - return tu + return TranslationUnit.from_source(name, args, unsaved_files=[(name, + source)]) def get_cursor(source, spelling): """Obtain a cursor from a source object. @@ -57,9 +56,38 @@ def get_cursor(source, spelling): return result return None + +def get_cursors(source, spelling): + """Obtain all cursors from a source object with a specific spelling. + + This provides a convenient search mechanism to find all cursors with specific + spelling within a source. The first argument can be either a + TranslationUnit or Cursor instance. + + If no cursors are found, an empty list is returned. + """ + cursors = [] + 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: + cursors.append(cursor) + + # Recurse into children. + cursors.extend(get_cursors(cursor, spelling)) + + return cursors + + __all__ = [ 'get_cursor', + 'get_cursors', 'get_tu', ] |