From: Argyrios Kyrtzidis Date: Mon, 7 Jan 2013 19:16:32 +0000 (+0000) Subject: [libclang] When annotating preprocessor tokens, if we are in a macro definition, X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3453bf7da9ac88cd2421b7fdccebf5cd2b8a9d87;p=clang [libclang] When annotating preprocessor tokens, if we are in a macro definition, check if the token was ever a macro name and annotate it if that's the case. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171776 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/test/Index/annotate-tokens-pp.c b/test/Index/annotate-tokens-pp.c index 2ebc58fc60..7da2d6f582 100644 --- a/test/Index/annotate-tokens-pp.c +++ b/test/Index/annotate-tokens-pp.c @@ -47,7 +47,7 @@ struct A // CHECK: Punctuation: "#" [2:1 - 2:2] preprocessing directive= // CHECK: Identifier: "define" [2:2 - 2:8] preprocessing directive= // CHECK: Identifier: "STILL_NOTHING" [2:9 - 2:22] macro definition=STILL_NOTHING -// CHECK: Identifier: "NOTHING" [2:23 - 2:30] macro definition=STILL_NOTHING +// CHECK: Identifier: "NOTHING" [2:23 - 2:30] macro expansion=NOTHING:1:9 // CHECK: Punctuation: "(" [2:30 - 2:31] macro definition=STILL_NOTHING // CHECK: Identifier: "honk" [2:31 - 2:35] macro definition=STILL_NOTHING // CHECK: Punctuation: "," [2:35 - 2:36] macro definition=STILL_NOTHING diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 797344eada..97fabab4a5 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -5307,6 +5307,7 @@ static void annotatePreprocessorTokens(CXTranslationUnit TU, unsigned NumTokens) { ASTUnit *CXXUnit = static_cast(TU->TUData); + Preprocessor &PP = CXXUnit->getPreprocessor(); SourceManager &SourceMgr = CXXUnit->getSourceManager(); std::pair BeginLocInfo = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin()); @@ -5348,12 +5349,41 @@ static void annotatePreprocessorTokens(CXTranslationUnit TU, // #undefs, to provide specific cursor kinds for those. SourceLocation BeginLoc = Tok.getLocation(); + if (lexNext(Lex, Tok, NextIdx, NumTokens)) + break; + + MacroInfo *MI = 0; + if (Tok.is(tok::raw_identifier) && + StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == "define") { + if (lexNext(Lex, Tok, NextIdx, NumTokens)) + break; + + if (Tok.is(tok::raw_identifier)) { + StringRef Name(Tok.getRawIdentifierData(), Tok.getLength()); + IdentifierInfo &II = PP.getIdentifierTable().get(Name); + SourceLocation MappedTokLoc = + CXXUnit->mapLocationToPreamble(Tok.getLocation()); + MI = getMacroInfo(II, MappedTokLoc, TU); + } + } + bool finished = false; do { if (lexNext(Lex, Tok, NextIdx, NumTokens)) { finished = true; break; } + // If we are in a macro definition, check if the token was ever a + // macro name and annotate it if that's the case. + if (MI) { + SourceLocation SaveLoc = Tok.getLocation(); + Tok.setLocation(CXXUnit->mapLocationToPreamble(SaveLoc)); + MacroDefinition *MacroDef = checkForMacroInMacroDefinition(MI,Tok,TU); + Tok.setLocation(SaveLoc); + if (MacroDef) + Cursors[NextIdx-1] = MakeMacroExpansionCursor(MacroDef, + Tok.getLocation(), TU); + } } while (!Tok.isAtStartOfLine()); unsigned LastIdx = finished ? NextIdx-1 : NextIdx-2; @@ -5364,7 +5394,7 @@ static void annotatePreprocessorTokens(CXTranslationUnit TU, MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU); for (; TokIdx <= LastIdx; ++TokIdx) - Cursors[TokIdx] = Cursor; + updateCursorAnnotation(Cursors[TokIdx], Cursor); if (finished) break;