Skip to content
Snippets Groups Projects
llvm5-0012-libclang-WIP-Fix-get_tokens-in-macro-expansion.patch 2.37 KiB
Newer Older
From 8566bbfaf8a34bf088cacf632b647922257d7d5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johann=20Kl=C3=A4hn?= <johann.klaehn@kip.uni-heidelberg.de>
Date: Mon, 31 Jul 2017 14:09:52 +0200
Subject: [PATCH 12/12] [libclang] WIP: Fix get_tokens in macro expansion

---
 bindings/python/tests/cindex/test_cursor.py | 15 +++++++++++++++
 tools/libclang/CIndex.cpp                   |  4 ++--
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/tools/clang/bindings/python/tests/cindex/test_cursor.py b/tools/clang/bindings/python/tests/cindex/test_cursor.py
index 43606b605c..5c6f9fb320 100644
--- a/tools/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/tools/clang/bindings/python/tests/cindex/test_cursor.py
@@ -523,6 +523,21 @@ def test_get_tokens_with_whitespace():
     assert ''.join(t.spelling for t in tokens) == source
     assert len(tokens) == 27
 
+def test_get_tokens_in_macro():
+    """regression test"""
+    source = "#define IMPL(name) struct name { name(int v = 0); }; \n IMPL(X)"
+    tu = get_tu(source, lang="cpp")
+    ctor = get_cursors(tu, "X")[1]
+    assert ctor.kind == CursorKind.CONSTRUCTOR
+    p = next(ctor.get_children())
+    assert p.kind == CursorKind.PARM_DECL
+    children = list(p.get_children())
+    assert len(children) == 1
+    expr = children[0]
+    tokens = list(expr.get_tokens())
+    assert len(tokens) == 1
+    assert tokens[0].spelling == "0"
+
 def test_get_arguments():
     tu = get_tu('void foo(int i, int j);')
     foo = get_cursor(tu, 'foo')
diff --git a/tools/clang/tools/libclang/CIndex.cpp b/tools/clang/tools/libclang/CIndex.cpp
index f32611b8d7..5849c7dfde 100644
--- a/tools/clang/tools/libclang/CIndex.cpp
+++ b/tools/clang/tools/libclang/CIndex.cpp
@@ -144,8 +144,8 @@ CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
   // We want the last character in this location, so we will adjust the
   // location accordingly.
   SourceLocation EndLoc = R.getEnd();
-  if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc))
-    EndLoc = SM.getExpansionRange(EndLoc).second;
+  // if (EndLoc.isValid() && EndLoc.isMacroID() && !SM.isMacroArgExpansion(EndLoc))
+  //   EndLoc = SM.getExpansionRange(EndLoc).second;
   if (R.isTokenRange() && EndLoc.isValid()) {
     unsigned Length = Lexer::MeasureTokenLength(SM.getSpellingLoc(EndLoc),
                                                 SM, LangOpts);
-- 
2.13.0