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

Douglas Gregor dgregor at apple.com
Thu Mar 18 11:23:03 PDT 2010


Author: dgregor
Date: Thu Mar 18 13:23:03 2010
New Revision: 98842

URL: http://llvm.org/viewvc/llvm-project?rev=98842&view=rev
Log:
Explicitly link macro instantiations to macro definitions in the
preprocessing record. Use that link with clang_getCursorReferenced()
and clang_getCursorDefinition() to match instantiations of a macro to
the definition of the macro.

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

Modified: cfe/trunk/include/clang/Lex/PreprocessingRecord.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessingRecord.h?rev=98842&r1=98841&r2=98842&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/PreprocessingRecord.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessingRecord.h Thu Mar 18 13:23:03 2010
@@ -16,6 +16,7 @@
 
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/PointerUnion.h"
 #include "llvm/Support/Allocator.h"
 #include <vector>
@@ -34,6 +35,8 @@
                      unsigned) throw();
 
 namespace clang {
+  class MacroDefinition;
+
   /// \brief Base class that describes a preprocessed entity, which may be a
   /// preprocessor directive or macro instantiation.
   class PreprocessedEntity {
@@ -108,22 +111,20 @@
     /// \brief The name of the macro being instantiation.
     IdentifierInfo *Name;
     
-    /// \brief The location of the definition of the macro being instantiated.
-    SourceLocation DefinitionLocation;
+    /// \brief The definition of this macro.
+    MacroDefinition *Definition;
     
   public:
     MacroInstantiation(IdentifierInfo *Name, SourceRange Range,
-                       SourceLocation DefinitionLocation)
+                       MacroDefinition *Definition)
       : PreprocessedEntity(MacroInstantiationKind, Range), Name(Name), 
-        DefinitionLocation(DefinitionLocation) { }
+        Definition(Definition) { }
     
     /// \brief The name of the macro being instantiated.
     IdentifierInfo *getName() const { return Name; }
     
-    /// \brief The location of the definition of the macro being instantiated.
-    /// FIXME: Could we just provide MacroInfo pointers instead, by teaching
-    /// the preprocessor to hold on to them when we care to keep them around?
-    SourceLocation getDefinitionLocation() const { return DefinitionLocation; }
+    /// \brief The definition of the macro being instantiated.
+    MacroDefinition *getDefinition() const { return Definition; }
 
     // Implement isa/cast/dyncast/etc.
     static bool classof(const PreprocessedEntity *PE) {
@@ -212,6 +213,9 @@
     /// \brief The preprocessing record this action will populate.
     PreprocessingRecord &Record;
     
+    /// \brief Mapping from MacroInfo structures to their definitions.
+    llvm::DenseMap<const MacroInfo *, MacroDefinition *> MacroDefinitions;
+
   public:
     explicit PopulatePreprocessingRecord(PreprocessingRecord &Record)
       : Record(Record) { }

Modified: cfe/trunk/lib/Lex/PreprocessingRecord.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PreprocessingRecord.cpp?rev=98842&r1=98841&r2=98842&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PreprocessingRecord.cpp (original)
+++ cfe/trunk/lib/Lex/PreprocessingRecord.cpp Thu Mar 18 13:23:03 2010
@@ -26,12 +26,14 @@
   Record.addPreprocessedEntity(
                         new (Record) MacroInstantiation(Id.getIdentifierInfo(),
                                                        Id.getLocation(),
-                                                      MI->getDefinitionLoc()));
+                                                        MacroDefinitions[MI]));
 }
 
 void PopulatePreprocessingRecord::MacroDefined(const IdentifierInfo *II, 
                                                const MacroInfo *MI) {
   SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
-  Record.addPreprocessedEntity(
-                  new (Record) MacroDefinition(II, MI->getDefinitionLoc(), R));
+  MacroDefinition *Def
+    = new (Record) MacroDefinition(II, MI->getDefinitionLoc(), R);
+  MacroDefinitions[MI] = Def;
+  Record.addPreprocessedEntity(Def);
 }

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=98842&r1=98841&r2=98842&view=diff
==============================================================================
--- cfe/trunk/test/Index/annotate-tokens-pp.c (original)
+++ cfe/trunk/test/Index/annotate-tokens-pp.c Thu Mar 18 13:23:03 2010
@@ -32,14 +32,14 @@
 // CHECK: Identifier: "X" [4:22 - 4:23] preprocessing directive=
 // CHECK: Punctuation: "##" [4:23 - 4:25] preprocessing directive=
 // CHECK: Identifier: "Y" [4:25 - 4:26] preprocessing directive=
-// CHECK: Identifier: "NOTHING" [5:1 - 5:8] macro instantiation=NOTHING
+// CHECK: Identifier: "NOTHING" [5:1 - 5:8] macro instantiation=NOTHING:1:9
 // CHECK: Punctuation: "(" [5:8 - 5:9]
 // CHECK: Identifier: "more" [5:9 - 5:13]
 // CHECK: Punctuation: "," [5:13 - 5:14]
 // CHECK: Identifier: "junk" [5:14 - 5:18]
 // CHECK: Punctuation: ")" [5:18 - 5:19]
 // CHECK: Keyword: "float" [5:20 - 5:25]
-// CHECK: Identifier: "WIBBLE" [5:26 - 5:32] macro instantiation=WIBBLE
+// CHECK: Identifier: "WIBBLE" [5:26 - 5:32] macro instantiation=WIBBLE:4:9
 // CHECK: Punctuation: "(" [5:32 - 5:33]
 // CHECK: Keyword: "int" [5:33 - 5:36]
 // CHECK: Punctuation: "," [5:36 - 5:37]
@@ -47,8 +47,8 @@
 // CHECK: Punctuation: ")" [5:43 - 5:44]
 // CHECK: Punctuation: ";" [5:44 - 5:45]
 // CHECK: Keyword: "int" [6:1 - 6:4]
-// CHECK: Identifier: "BAR" [6:5 - 6:8] macro instantiation=BAR
-// CHECK: Identifier: "STILL_NOTHING" [6:9 - 6:22] macro instantiation=STILL_NOTHING
+// CHECK: Identifier: "BAR" [6:5 - 6:8] macro instantiation=BAR:3:9
+// CHECK: Identifier: "STILL_NOTHING" [6:9 - 6:22] macro instantiation=STILL_NOTHING:2:9
 // CHECK: Punctuation: ";" [6:22 - 6:23]
 // CHECK: Punctuation: "#" [7:1 - 7:2] preprocessing directive=
 // CHECK: Identifier: "include" [7:2 - 7:9] preprocessing directive=

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=98842&r1=98841&r2=98842&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Thu Mar 18 13:23:03 2010
@@ -1765,6 +1765,11 @@
     return clang_getNullCursor();
   }
 
+  if (C.kind == CXCursor_MacroInstantiation) {
+    if (MacroDefinition *Def = getCursorMacroInstantiation(C)->getDefinition())
+      return MakeMacroDefinitionCursor(Def, CXXUnit);
+  }
+
   if (!clang_isReference(C.kind))
     return clang_getNullCursor();
 
@@ -1803,6 +1808,9 @@
     WasReference = true;
   }
 
+  if (C.kind == CXCursor_MacroInstantiation)
+    return clang_getCursorReferenced(C);
+
   if (!clang_isDeclaration(C.kind))
     return clang_getNullCursor();
 





More information about the cfe-commits mailing list