[cfe-commits] r137796 - in /cfe/trunk: test/Index/get-cursor-macro-args.h test/Index/get-cursor-macro-args.m tools/libclang/CIndex.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Tue Aug 16 17:31:25 PDT 2011


Author: akirtzidis
Date: Tue Aug 16 19:31:25 2011
New Revision: 137796

URL: http://llvm.org/viewvc/llvm-project?rev=137796&view=rev
Log:
[libclang] Make clang_getCursor able to handle locations that point inside macro arguments.

e.g. for:

\define INVOKE(METHOD, CLASS) [CLASS METHOD]

void test2() {
  INVOKE(meth, MyClass);
}

Pointing at 'meth' will give a CXCursor_ObjCMessageExpr and pointing at 'MyClass'
will give a CXCursor_ObjCClassRef.

Added:
    cfe/trunk/test/Index/get-cursor-macro-args.h
    cfe/trunk/test/Index/get-cursor-macro-args.m
Modified:
    cfe/trunk/tools/libclang/CIndex.cpp

Added: cfe/trunk/test/Index/get-cursor-macro-args.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/get-cursor-macro-args.h?rev=137796&view=auto
==============================================================================
--- cfe/trunk/test/Index/get-cursor-macro-args.h (added)
+++ cfe/trunk/test/Index/get-cursor-macro-args.h Tue Aug 16 19:31:25 2011
@@ -0,0 +1,16 @@
+ at interface MyClass
++(void)meth;
+ at end
+
+#define MACRO2(x) x
+#define MACRO(x) MACRO2(x)
+
+void test() {
+  MACRO([MyClass meth]);
+}
+
+#define INVOKE(METHOD, CLASS) [CLASS METHOD]
+
+void test2() {
+  INVOKE(meth, MyClass);
+}

Added: cfe/trunk/test/Index/get-cursor-macro-args.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/get-cursor-macro-args.m?rev=137796&view=auto
==============================================================================
--- cfe/trunk/test/Index/get-cursor-macro-args.m (added)
+++ cfe/trunk/test/Index/get-cursor-macro-args.m Tue Aug 16 19:31:25 2011
@@ -0,0 +1,19 @@
+// Test without PCH
+// RUN: c-index-test -cursor-at=%S/get-cursor-macro-args.h:9:12 \
+// RUN:              -cursor-at=%S/get-cursor-macro-args.h:9:21 \
+// RUN:              -cursor-at=%S/get-cursor-macro-args.h:15:12 \
+// RUN:              -cursor-at=%S/get-cursor-macro-args.h:15:20 \
+// RUN:       %s -include get-cursor-macro-args.h | FileCheck %s
+
+// Test with PCH
+// RUN: c-index-test -write-pch %t.pch -x objective-c-header %S/get-cursor-macro-args.h
+// RUN: c-index-test -cursor-at=%S/get-cursor-macro-args.h:9:12 \
+// RUN:              -cursor-at=%S/get-cursor-macro-args.h:9:21 \
+// RUN:              -cursor-at=%S/get-cursor-macro-args.h:15:12 \
+// RUN:              -cursor-at=%S/get-cursor-macro-args.h:15:20 \
+// RUN:       %s -include-pch %t.pch | FileCheck %s
+
+// CHECK:      ObjCClassRef=MyClass:1:12
+// CHECK-NEXT: ObjCMessageExpr=meth:2:1
+// CHECK-NEXT: ObjCMessageExpr=meth:2:1
+// CHECK-NEXT: ObjCClassRef=MyClass:1:12

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=137796&r1=137795&r2=137796&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Tue Aug 16 19:31:25 2011
@@ -3476,17 +3476,27 @@
 
 struct GetCursorData {
   SourceLocation TokenBeginLoc;
+  bool PointsAtMacroArgExpansion;
   CXCursor &BestCursor;
 
-  GetCursorData(SourceLocation tokenBegin, CXCursor &outputCursor)
-    : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) { }
+  GetCursorData(SourceManager &SM,
+                SourceLocation tokenBegin, CXCursor &outputCursor)
+    : TokenBeginLoc(tokenBegin), BestCursor(outputCursor) {
+    PointsAtMacroArgExpansion = SM.isMacroArgExpansion(tokenBegin);
+  }
 };
 
-enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
-                                         CXCursor parent,
-                                         CXClientData client_data) {
+static enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
+                                                CXCursor parent,
+                                                CXClientData client_data) {
   GetCursorData *Data = static_cast<GetCursorData *>(client_data);
   CXCursor *BestCursor = &Data->BestCursor;
+
+  // If we point inside a macro argument we should provide info of what the
+  // token is so use the actual cursor, don't replace it with a macro expansion
+  // cursor.
+  if (cursor.kind == CXCursor_MacroExpansion && Data->PointsAtMacroArgExpansion)
+    return CXChildVisit_Recurse;
   
   if (clang_isDeclaration(cursor.kind)) {
     // Avoid having the synthesized methods override the property decls.
@@ -3552,7 +3562,7 @@
     // FIXME: Would be great to have a "hint" cursor, then walk from that
     // hint cursor upward until we find a cursor whose source range encloses
     // the region of interest, rather than starting from the translation unit.
-    GetCursorData ResultData(SLoc, Result);
+    GetCursorData ResultData(CXXUnit->getSourceManager(), SLoc, Result);
     CXCursor Parent = clang_getTranslationUnitCursor(TU);
     CursorVisitor CursorVis(TU, GetCursorVisitor, &ResultData,
                             Decl::MaxPCHLevel, true, SourceLocation(SLoc));





More information about the cfe-commits mailing list