aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2011-08-17 00:31:25 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2011-08-17 00:31:25 +0000
commit4b43b305342ae2e49d473d0fa6152e5d0c343765 (patch)
treeb77f664e77a5220ef41041e78d99aa49d6ed5741
parent0e870622e4d4b2ecb7bc6ffd2c97f74fd14220b6 (diff)
[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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@137796 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--test/Index/get-cursor-macro-args.h16
-rw-r--r--test/Index/get-cursor-macro-args.m19
-rw-r--r--tools/libclang/CIndex.cpp22
3 files changed, 51 insertions, 6 deletions
diff --git a/test/Index/get-cursor-macro-args.h b/test/Index/get-cursor-macro-args.h
new file mode 100644
index 0000000000..40ec8dc0b8
--- /dev/null
+++ b/test/Index/get-cursor-macro-args.h
@@ -0,0 +1,16 @@
+@interface MyClass
++(void)meth;
+@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);
+}
diff --git a/test/Index/get-cursor-macro-args.m b/test/Index/get-cursor-macro-args.m
new file mode 100644
index 0000000000..4e0ac78fe0
--- /dev/null
+++ b/test/Index/get-cursor-macro-args.m
@@ -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
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index 404b9d3488..5ba4ad06c5 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -3476,17 +3476,27 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
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 @@ CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
// 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));