summaryrefslogtreecommitdiffstats
path: root/bindings
diff options
context:
space:
mode:
authorJonathan Coe <jbcoe@me.com>2017-09-21 20:48:43 +0000
committerJonathan Coe <jbcoe@me.com>2017-09-21 20:48:43 +0000
commitc64b5a7bdc0717570cc76501450bb6279f016b0e (patch)
treeb2516f38182f9f7f87d7153d23dc7a76f3635597 /bindings
parent3a1d0b688c3f3851973a70c2da751aeaba733413 (diff)
[libclang] Keep track of TranslationUnit instance when annotating tokens
Summary: Previously the `_tu` was not propagated to the returned cursor, leading to errors when calling any method on that cursor (e.g. `cursor.referenced`). Reviewers: jbcoe, rsmith Reviewed By: jbcoe Subscribers: cfe-commits Tags: #clang Patch by jklaehn (Johann Klähn) Differential Revision: https://reviews.llvm.org/D36953 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313913 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings')
-rw-r--r--bindings/python/clang/cindex.py1
-rw-r--r--bindings/python/tests/cindex/test_cursor.py22
2 files changed, 23 insertions, 0 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index 3eb2d70504..6a53bc0c47 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -3216,6 +3216,7 @@ class Token(Structure):
def cursor(self):
"""The Cursor this Token corresponds to."""
cursor = Cursor()
+ cursor._tu = self._tu
conf.lib.clang_annotateTokens(self._tu, byref(self), 1, byref(cursor))
diff --git a/bindings/python/tests/cindex/test_cursor.py b/bindings/python/tests/cindex/test_cursor.py
index 4787ea931e..e7c40c771e 100644
--- a/bindings/python/tests/cindex/test_cursor.py
+++ b/bindings/python/tests/cindex/test_cursor.py
@@ -395,6 +395,28 @@ def test_get_tokens():
assert tokens[0].spelling == 'int'
assert tokens[1].spelling == 'foo'
+def test_get_token_cursor():
+ """Ensure we can map tokens to cursors."""
+ tu = get_tu('class A {}; int foo(A var = A());', lang='cpp')
+ foo = get_cursor(tu, 'foo')
+
+ for cursor in foo.walk_preorder():
+ if cursor.kind.is_expression() and not cursor.kind.is_statement():
+ break
+ else:
+ assert False, "Could not find default value expression"
+
+ tokens = list(cursor.get_tokens())
+ assert len(tokens) == 4, [t.spelling for t in tokens]
+ assert tokens[0].spelling == '='
+ assert tokens[1].spelling == 'A'
+ assert tokens[2].spelling == '('
+ assert tokens[3].spelling == ')'
+ t_cursor = tokens[1].cursor
+ assert t_cursor.kind == CursorKind.TYPE_REF
+ r_cursor = t_cursor.referenced # should not raise an exception
+ assert r_cursor.kind == CursorKind.CLASS_DECL
+
def test_get_arguments():
tu = get_tu('void foo(int i, int j);')
foo = get_cursor(tu, 'foo')