[cfe-commits] r171776 - in /cfe/trunk: test/Index/annotate-tokens-pp.c tools/libclang/CIndex.cpp

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon Jan 7 11:16:32 PST 2013


Author: akirtzidis
Date: Mon Jan  7 13:16:32 2013
New Revision: 171776

URL: http://llvm.org/viewvc/llvm-project?rev=171776&view=rev
Log:
[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.

Modified:
    cfe/trunk/test/Index/annotate-tokens-pp.c
    cfe/trunk/tools/libclang/CIndex.cpp

Modified: cfe/trunk/test/Index/annotate-tokens-pp.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/annotate-tokens-pp.c?rev=171776&r1=171775&r2=171776&view=diff
==============================================================================
--- cfe/trunk/test/Index/annotate-tokens-pp.c (original)
+++ cfe/trunk/test/Index/annotate-tokens-pp.c Mon Jan  7 13:16:32 2013
@@ -47,7 +47,7 @@
 // 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

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=171776&r1=171775&r2=171776&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Mon Jan  7 13:16:32 2013
@@ -5307,6 +5307,7 @@
                                        unsigned NumTokens) {
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
 
+  Preprocessor &PP = CXXUnit->getPreprocessor();
   SourceManager &SourceMgr = CXXUnit->getSourceManager();
   std::pair<FileID, unsigned> BeginLocInfo
     = SourceMgr.getDecomposedLoc(RegionOfInterest.getBegin());
@@ -5348,12 +5349,41 @@
       // #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 @@
           MakePreprocessingDirectiveCursor(SourceRange(BeginLoc, EndLoc), TU);
 
       for (; TokIdx <= LastIdx; ++TokIdx)
-        Cursors[TokIdx] = Cursor;
+        updateCursorAnnotation(Cursors[TokIdx], Cursor);
       
       if (finished)
         break;





More information about the cfe-commits mailing list