[PATCH] D36953: [libclang] Keep track of TranslationUnit instance when annotating tokens
This revision was automatically updated to reflect the committed changes. Closed by commit rL313913: [libclang] Keep track of TranslationUnit instance when annotating tokens (authored by jbcoe). Changed prior to commit: https://reviews.llvm.org/D36953?vs=111959=116262#toc Repository: rL LLVM https://reviews.llvm.org/D36953 Files: cfe/trunk/bindings/python/clang/cindex.py cfe/trunk/bindings/python/tests/cindex/test_cursor.py Index: cfe/trunk/bindings/python/clang/cindex.py === --- cfe/trunk/bindings/python/clang/cindex.py +++ cfe/trunk/bindings/python/clang/cindex.py @@ -3216,6 +3216,7 @@ 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)) Index: cfe/trunk/bindings/python/tests/cindex/test_cursor.py === --- cfe/trunk/bindings/python/tests/cindex/test_cursor.py +++ cfe/trunk/bindings/python/tests/cindex/test_cursor.py @@ -395,6 +395,28 @@ 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') Index: cfe/trunk/bindings/python/clang/cindex.py === --- cfe/trunk/bindings/python/clang/cindex.py +++ cfe/trunk/bindings/python/clang/cindex.py @@ -3216,6 +3216,7 @@ 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)) Index: cfe/trunk/bindings/python/tests/cindex/test_cursor.py === --- cfe/trunk/bindings/python/tests/cindex/test_cursor.py +++ cfe/trunk/bindings/python/tests/cindex/test_cursor.py @@ -395,6 +395,28 @@ 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') ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D36953: [libclang] Keep track of TranslationUnit instance when annotating tokens
jklaehn added a comment. In https://reviews.llvm.org/D36953#877367, @jbcoe wrote: > Do you need someone to commit this change for you? > > I'm happy to do so if you don't have commit access. Yes that would be great, thanks! https://reviews.llvm.org/D36953 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D36953: [libclang] Keep track of TranslationUnit instance when annotating tokens
jbcoe added a comment. Do you need someone to commit this change for you? I'm happy to do so if you don't have commit access. https://reviews.llvm.org/D36953 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D36953: [libclang] Keep track of TranslationUnit instance when annotating tokens
jklaehn created this revision. jklaehn added a project: clang. Previously the `_tu` was not propagated to the returned cursor, leading to errors when calling any method on that cursor (e.g. `cursor.referenced`). https://reviews.llvm.org/D36953 Files: bindings/python/clang/cindex.py bindings/python/tests/cindex/test_cursor.py Index: bindings/python/tests/cindex/test_cursor.py === --- bindings/python/tests/cindex/test_cursor.py +++ bindings/python/tests/cindex/test_cursor.py @@ -395,6 +395,28 @@ 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') Index: bindings/python/clang/cindex.py === --- bindings/python/clang/cindex.py +++ bindings/python/clang/cindex.py @@ -3193,6 +3193,7 @@ 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)) Index: bindings/python/tests/cindex/test_cursor.py === --- bindings/python/tests/cindex/test_cursor.py +++ bindings/python/tests/cindex/test_cursor.py @@ -395,6 +395,28 @@ 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') Index: bindings/python/clang/cindex.py === --- bindings/python/clang/cindex.py +++ bindings/python/clang/cindex.py @@ -3193,6 +3193,7 @@ 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)) ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits