]> granicus.if.org Git - clang/commitdiff
Introduce a simple "hint" scheme to eliminate the quadratic behavior
authorDouglas Gregor <dgregor@apple.com>
Thu, 11 Oct 2012 17:41:54 +0000 (17:41 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 11 Oct 2012 17:41:54 +0000 (17:41 +0000)
associated with deserializing macro history for an identifier.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165729 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Lex/Preprocessor.h
include/clang/Serialization/ASTReader.h
lib/Lex/PPMacroExpansion.cpp
lib/Serialization/ASTReader.cpp

index 2c0814c9107351e9f09d0aa80eb04ec1deb8e510..7888a26fcbda7bee7d021e80f592e0cd25d22ce1 100644 (file)
@@ -519,7 +519,8 @@ public:
   /// \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);
index 2a9afc92e9b80924a4a964ff1229c9f8314a3093..4e8531a820ebd652d9008ac40dec59a1aec12af6 100644 (file)
@@ -1433,7 +1433,7 @@ public:
                                                     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 @@ public:
   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.
index 8db74f28d414691ec71ebc5bc71a67e9234f2131..c7178166507a5efe0eb98f62add32201bdd0c906 100644 (file)
@@ -55,7 +55,8 @@ void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
     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 @@ void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
   }
 
   // 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);
index 736f082d6f38f61628eb4f21bbf3f5d274af2206..5657ee23bc12bca976bee664d122d7f06129e3a6 100644 (file)
@@ -1241,7 +1241,8 @@ bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
   }
 }
 
-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 @@ void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
       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 @@ IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
   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 @@ MacroInfo *ASTReader::getMacro(MacroID ID) {
     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 @@ void ASTReader::finishPendingActions() {
     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();