[cfe-commits] r98837 - in /cfe/trunk: include/clang-c/Index.h include/clang/Lex/PreprocessingRecord.h test/Index/annotate-tokens-pp.c tools/CIndex/CIndex.cpp tools/CIndex/CXCursor.cpp tools/CIndex/CXCursor.h

Douglas Gregor dgregor at apple.com
Thu Mar 18 11:04:21 PDT 2010


Author: dgregor
Date: Thu Mar 18 13:04:21 2010
New Revision: 98837

URL: http://llvm.org/viewvc/llvm-project?rev=98837&view=rev
Log:
Expose macro definitions as CIndex cursors. These can still only be
generated by clang_annotateTokens().

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Lex/PreprocessingRecord.h
    cfe/trunk/test/Index/annotate-tokens-pp.c
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CXCursor.cpp
    cfe/trunk/tools/CIndex/CXCursor.h

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=98837&r1=98836&r2=98837&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Mar 18 13:04:21 2010
@@ -800,7 +800,8 @@
      
   /* Preprocessing */
   CXCursor_PreprocessingDirective        = 500,
-  CXCursor_MacroInstantiation            = 501,
+  CXCursor_MacroDefinition               = 501,
+  CXCursor_MacroInstantiation            = 502,
   CXCursor_FirstPreprocessing            = CXCursor_PreprocessingDirective,
   CXCursor_LastPreprocessing             = CXCursor_MacroInstantiation
 };

Modified: cfe/trunk/include/clang/Lex/PreprocessingRecord.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessingRecord.h?rev=98837&r1=98836&r2=98837&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/PreprocessingRecord.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessingRecord.h Thu Mar 18 13:04:21 2010
@@ -162,14 +162,14 @@
         Location(Location) { }
     
     /// \brief Retrieve the name of the macro being defined.
-    const IdentifierInfo *getMacroName() const { return Name; }
+    const IdentifierInfo *getName() const { return Name; }
     
     /// \brief Retrieve the location of the macro name in the definition.
     SourceLocation getLocation() const { return Location; }
     
     // Implement isa/cast/dyncast/etc.
-    static bool classof(const PreprocessingDirective *PD) {
-      return PD->getKind() == MacroDefinitionKind;
+    static bool classof(const PreprocessedEntity *PE) {
+      return PE->getKind() == MacroDefinitionKind;
     }
     static bool classof(const MacroDefinition *) { return true; }
   };

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=98837&r1=98836&r2=98837&view=diff
==============================================================================
--- cfe/trunk/test/Index/annotate-tokens-pp.c (original)
+++ cfe/trunk/test/Index/annotate-tokens-pp.c Thu Mar 18 13:04:21 2010
@@ -10,7 +10,7 @@
 // RUN: c-index-test -test-annotate-tokens=%s:2:1:9:1 -I%S/Inputs %s | FileCheck %s
 // 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] preprocessing directive=
+// CHECK: Identifier: "STILL_NOTHING" [2:9 - 2:22] macro definition=STILL_NOTHING
 // CHECK: Identifier: "NOTHING" [2:23 - 2:30] preprocessing directive=
 // CHECK: Punctuation: "(" [2:30 - 2:31] preprocessing directive=
 // CHECK: Identifier: "honk" [2:31 - 2:35] preprocessing directive=
@@ -19,11 +19,11 @@
 // CHECK: Punctuation: ")" [2:42 - 2:43] preprocessing directive=
 // CHECK: Punctuation: "#" [3:1 - 3:2] preprocessing directive=
 // CHECK: Identifier: "define" [3:2 - 3:8] preprocessing directive=
-// CHECK: Identifier: "BAR" [3:9 - 3:12] preprocessing directive=
+// CHECK: Identifier: "BAR" [3:9 - 3:12] macro definition=BAR
 // CHECK: Identifier: "baz" [3:13 - 3:16] preprocessing directive=
 // CHECK: Punctuation: "#" [4:1 - 4:2] preprocessing directive=
 // CHECK: Identifier: "define" [4:2 - 4:8] preprocessing directive=
-// CHECK: Identifier: "WIBBLE" [4:9 - 4:15] preprocessing directive=
+// CHECK: Identifier: "WIBBLE" [4:9 - 4:15] macro definition=WIBBLE
 // CHECK: Punctuation: "(" [4:15 - 4:16] preprocessing directive=
 // CHECK: Identifier: "X" [4:16 - 4:17] preprocessing directive=
 // CHECK: Punctuation: "," [4:17 - 4:18] preprocessing directive=

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=98837&r1=98836&r2=98837&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Thu Mar 18 13:04:21 2010
@@ -1443,6 +1443,10 @@
     return createCXString(getCursorMacroInstantiation(C)->getName()
                                                            ->getNameStart());
 
+  if (C.kind == CXCursor_MacroDefinition)
+    return createCXString(getCursorMacroDefinition(C)->getName()
+                                                           ->getNameStart());
+
   if (clang_isDeclaration(C.kind))
     return getDeclSpelling(getCursorDecl(C));
 
@@ -1527,6 +1531,8 @@
      return createCXString("attribute(iboutlet)");
   case CXCursor_PreprocessingDirective:
     return createCXString("preprocessing directive");
+  case CXCursor_MacroDefinition:
+    return createCXString("macro definition");
   case CXCursor_MacroInstantiation:
     return createCXString("macro instantiation");
   }
@@ -1665,6 +1671,11 @@
       = cxcursor::getCursorMacroInstantiation(C)->getSourceRange().getBegin();
     return cxloc::translateSourceLocation(getCursorContext(C), L);
   }
+
+  if (C.kind == CXCursor_MacroDefinition) {
+    SourceLocation L = cxcursor::getCursorMacroDefinition(C)->getLocation();
+    return cxloc::translateSourceLocation(getCursorContext(C), L);
+  }
   
   if (!getCursorDecl(C))
     return clang_getNullLocation();
@@ -1726,6 +1737,11 @@
     SourceRange R = cxcursor::getCursorMacroInstantiation(C)->getSourceRange();
     return cxloc::translateSourceRange(getCursorContext(C), R);
   }
+
+  if (C.kind == CXCursor_MacroDefinition) {
+    SourceRange R = cxcursor::getCursorMacroDefinition(C)->getSourceRange();
+    return cxloc::translateSourceRange(getCursorContext(C), R);
+  }
   
   if (!getCursorDecl(C))
     return clang_getNullRange();
@@ -2387,7 +2403,15 @@
         continue;
       }
 
-      // FIXME: expose other preprocessed entities.
+      if (MacroDefinition *MD = dyn_cast<MacroDefinition>(Entity)) {
+        SourceLocation Loc = MD->getLocation();
+        if (Loc.isFileID()) {
+          Annotated[Loc.getRawEncoding()] 
+            = MakeMacroDefinitionCursor(MD, CXXUnit);
+        }
+
+        continue;
+      }
     }
   }
   

Modified: cfe/trunk/tools/CIndex/CXCursor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CXCursor.cpp?rev=98837&r1=98836&r2=98837&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CXCursor.cpp (original)
+++ cfe/trunk/tools/CIndex/CXCursor.cpp Thu Mar 18 13:04:21 2010
@@ -314,6 +314,16 @@
                                       reinterpret_cast<uintptr_t> (C.data[1])));
 }
 
+CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, ASTUnit *TU) {
+  CXCursor C = { CXCursor_MacroDefinition, { MI, 0, TU } };
+  return C;
+}
+
+MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
+  assert(C.kind == CXCursor_MacroDefinition);
+  return static_cast<MacroDefinition *>(C.data[0]);
+}
+
 CXCursor cxcursor::MakeMacroInstantiationCursor(MacroInstantiation *MI, 
                                                 ASTUnit *TU) {
   CXCursor C = { CXCursor_MacroInstantiation, { MI, 0, TU } };

Modified: cfe/trunk/tools/CIndex/CXCursor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CXCursor.h?rev=98837&r1=98836&r2=98837&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CXCursor.h (original)
+++ cfe/trunk/tools/CIndex/CXCursor.h Thu Mar 18 13:04:21 2010
@@ -25,6 +25,7 @@
 class Attr;
 class Decl;
 class Expr;
+class MacroDefinition;
 class MacroInstantiation;
 class NamedDecl;
 class ObjCInterfaceDecl;
@@ -80,6 +81,13 @@
 /// \brief Unpack a given preprocessing directive to retrieve its source range.
 SourceRange getCursorPreprocessingDirective(CXCursor C);
 
+/// \brief Create a macro definition cursor.
+CXCursor MakeMacroDefinitionCursor(MacroDefinition *, ASTUnit *TU);
+
+/// \brief Unpack a given macro definition cursor to retrieve its
+/// source range.
+MacroDefinition *getCursorMacroDefinition(CXCursor C);
+
 /// \brief Create a macro instantiation cursor.
 CXCursor MakeMacroInstantiationCursor(MacroInstantiation *, ASTUnit *TU);
 





More information about the cfe-commits mailing list