[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
This commit is contained in:
Argyrios Kyrtzidis 2011-08-17 00:31:25 +00:00
Родитель 0e870622e4
Коммит 4b43b30534
3 изменённых файлов: 51 добавлений и 6 удалений

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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));