[cfe-commits] r165729 - in /cfe/trunk: include/clang/Lex/Preprocessor.h include/clang/Serialization/ASTReader.h lib/Lex/PPMacroExpansion.cpp lib/Serialization/ASTReader.cpp

Douglas Gregor dgregor at apple.com
Thu Oct 11 10:41:55 PDT 2012


Author: dgregor
Date: Thu Oct 11 12:41:54 2012
New Revision: 165729

URL: http://llvm.org/viewvc/llvm-project?rev=165729&view=rev
Log:
Introduce a simple "hint" scheme to eliminate the quadratic behavior
associated with deserializing macro history for an identifier.

Modified:
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/include/clang/Serialization/ASTReader.h
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Serialization/ASTReader.cpp

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=165729&r1=165728&r2=165729&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Thu Oct 11 12:41:54 2012
@@ -519,7 +519,8 @@
   /// \brief Specify a macro for this identifier.
   void setMacroInfo(IdentifierInfo *II, MacroInfo *MI);
   /// \brief Add a MacroInfo that was loaded from an AST file.
-  void addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI);
+  void addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI,
+                          MacroInfo *Hint = 0);
   /// \brief Make the given MacroInfo, that was loaded from an AST file and
   /// previously hidden, visible.
   void makeLoadedMacroInfoVisible(IdentifierInfo *II, MacroInfo *MI);

Modified: cfe/trunk/include/clang/Serialization/ASTReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTReader.h?rev=165729&r1=165728&r2=165729&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTReader.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTReader.h Thu Oct 11 12:41:54 2012
@@ -1433,7 +1433,7 @@
                                                     unsigned LocalID);
 
   /// \brief Retrieve the macro with the given ID.
-  MacroInfo *getMacro(serialization::MacroID ID);
+  MacroInfo *getMacro(serialization::MacroID ID, MacroInfo *Hint = 0);
 
   /// \brief Retrieve the global macro ID corresponding to the given local
   /// ID within the given module file.
@@ -1582,7 +1582,7 @@
   Expr *ReadSubExpr();
 
   /// \brief Reads the macro record located at the given offset.
-  void ReadMacroRecord(ModuleFile &F, uint64_t Offset);
+  void ReadMacroRecord(ModuleFile &F, uint64_t Offset, MacroInfo *Hint = 0);
 
   /// \brief Determine the global preprocessed entity ID that corresponds to
   /// the given local ID within the given module.

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=165729&r1=165728&r2=165729&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Thu Oct 11 12:41:54 2012
@@ -55,7 +55,8 @@
     II->setChangedSinceDeserialization();
 }
 
-void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
+void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI,
+                                      MacroInfo *Hint) {
   assert(MI && "Missing macro?");
   assert(MI->isFromAST() && "Macro is not from an AST?");
   assert(!MI->getPreviousDefinition() && "Macro already in chain?");
@@ -105,8 +106,7 @@
   }
 
   // The macro is not a definition; put it at the end of the list.
-  // FIXME: Adding macro history is quadratic, but a hint could fix this.
-  MacroInfo *Prev = StoredMI;
+  MacroInfo *Prev = Hint? Hint : StoredMI;
   while (Prev->getPreviousDefinition())
     Prev = Prev->getPreviousDefinition();
   Prev->setPreviousDefinition(MI);

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=165729&r1=165728&r2=165729&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Thu Oct 11 12:41:54 2012
@@ -1241,7 +1241,8 @@
   }
 }
 
-void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
+void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
+                                MacroInfo *Hint) {
   llvm::BitstreamCursor &Stream = F.MacroCursor;
 
   // Keep track of where we are in the stream, then jump back there
@@ -1370,7 +1371,7 @@
       MI->setHidden(Hidden);
 
       // Finally, install the macro.
-      PP.addLoadedMacroInfo(II, MI);
+      PP.addLoadedMacroInfo(II, MI, Hint);
 
       // Remember that we saw this macro last so that we add the tokens that
       // form its body to it.
@@ -5795,7 +5796,7 @@
   return LocalID + I->second;
 }
 
-MacroInfo *ASTReader::getMacro(MacroID ID) {
+MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
   if (ID == 0)
     return 0;
 
@@ -5811,7 +5812,7 @@
     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
     ModuleFile *M = I->second;
     unsigned Index = ID - M->BaseMacroID;
-    ReadMacroRecord(*M, M->MacroOffsets[Index]);
+    ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
   }
 
   return MacrosLoaded[ID];
@@ -6512,9 +6513,10 @@
     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
       // FIXME: std::move here
       SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
+      MacroInfo *Hint = 0;
       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx !=  NumIDs;
            ++IDIdx) {
-        getMacro(GlobalIDs[IDIdx]);
+        Hint = getMacro(GlobalIDs[IDIdx], Hint);
       }
     }
     PendingMacroIDs.clear();





More information about the cfe-commits mailing list