summaryrefslogtreecommitdiffstats
path: root/bindings
diff options
context:
space:
mode:
authorEli Bendersky <eliben@google.com>2014-05-29 02:35:27 +0000
committerEli Bendersky <eliben@google.com>2014-05-29 02:35:27 +0000
commit27fd28cbfaa5a6c6b26ed41bf060042e61993122 (patch)
tree4d2fc5a886786375244b04bddb523221c13d10df /bindings
parenteca0bcfaee4c16fe21552234b0c44dfb978974e5 (diff)
Implement a convenience recursive walk method over a cursor and its descendants.
Before r160106 there was a way to recursively visit all descendants of a cursor via Cursor_visit, but it was removed. Since then, every user needs to reimplement the recursive descent into get_children. Adding a walk_preorder() method to Cursor that conveniently implements recursive walking in a Pythonic way. This also greatly simplifies get_cursor and get_cursors in tests/cindex/util.py (walk_preorder is now tested through these utility functions, since they are used in many tests). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209793 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings')
-rw-r--r--bindings/python/clang/cindex.py10
-rw-r--r--bindings/python/tests/cindex/util.py40
2 files changed, 21 insertions, 29 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index 779fa14c88..e03f7e6c4e 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -1391,6 +1391,16 @@ class Cursor(Structure):
children)
return iter(children)
+ def walk_preorder(self):
+ """Depth-first preorder walk over the cursor and its descendants.
+
+ Yields cursors.
+ """
+ yield self
+ for child in self.get_children():
+ for descendant in child.walk_preorder():
+ yield descendant
+
def get_tokens(self):
"""Obtain Token instances formulating that compose this Cursor.
diff --git a/bindings/python/tests/cindex/util.py b/bindings/python/tests/cindex/util.py
index 8614b02ad2..c53ba7c81b 100644
--- a/bindings/python/tests/cindex/util.py
+++ b/bindings/python/tests/cindex/util.py
@@ -39,52 +39,34 @@ def get_cursor(source, spelling):
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:
+ # Convenience for calling on a TU.
+ root_cursor = source if isinstance(source, Cursor) else source.cursor
+
+ for cursor in root_cursor.walk_preorder():
if cursor.spelling == spelling:
return cursor
- # Recurse into children.
- result = get_cursor(cursor, spelling)
- if result is not None:
- 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
+ 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.
"""
+ # Convenience for calling on a TU.
+ root_cursor = source if isinstance(source, Cursor) else source.cursor
+
cursors = []
- children = []
- if isinstance(source, Cursor):
- children = source.get_children()
- else:
- # Assume TU
- children = source.cursor.get_children()
-
- for cursor in children:
+ for cursor in root_cursor.walk_preorder():
if cursor.spelling == spelling:
cursors.append(cursor)
- # Recurse into children.
- cursors.extend(get_cursors(cursor, spelling))
-
return cursors
-
-
__all__ = [
'get_cursor',