Revert Clang r172620 and r172629, which caused a hang when building
complicated modules (<rdar://problem/13038265>). Unfortunately, this
un-fixes <rdar://problem/13016031>.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172783 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp
index d71bce5..81d3cea 100644
--- a/lib/Serialization/ASTReader.cpp
+++ b/lib/Serialization/ASTReader.cpp
@@ -525,9 +525,13 @@
// If this identifier is a macro, deserialize the macro
// definition.
if (hadMacroDefinition) {
+ SmallVector<MacroID, 4> MacroIDs;
+ while (uint32_t LocalID = ReadUnalignedLE32(d)) {
+ MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
+ DataLen -= 4;
+ }
DataLen -= 4;
- uint32_t LocalID = ReadUnalignedLE32(d);
- Reader.addMacroIDForDeserialization(II, Reader.getGlobalMacroID(F,LocalID));
+ Reader.setIdentifierIsMacro(II, MacroIDs);
}
Reader.SetIdentifierInfo(ID, II);
@@ -1057,7 +1061,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
@@ -1069,6 +1074,24 @@
SmallVector<IdentifierInfo*, 16> MacroArgs;
MacroInfo *Macro = 0;
+ // RAII object to add the loaded macro information once we're done
+ // adding tokens.
+ struct AddLoadedMacroInfoRAII {
+ Preprocessor &PP;
+ MacroInfo *Hint;
+ MacroInfo *MI;
+ IdentifierInfo *II;
+
+ AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
+ : PP(PP), Hint(Hint), MI(), II() { }
+ ~AddLoadedMacroInfoRAII( ) {
+ if (MI) {
+ // Finally, install the macro.
+ PP.addLoadedMacroInfo(II, MI, Hint);
+ }
+ }
+ } AddLoadedMacroInfo(PP, Hint);
+
while (true) {
unsigned Code = Stream.ReadCode();
switch (Code) {
@@ -1123,8 +1146,6 @@
SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
MacroInfo *MI = PP.AllocateMacroInfo(Loc);
MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
- MacroInfo *PrevMI = getMacro(getGlobalMacroID(F, Record[NextIndex++]));
- MI->setPreviousDefinition(PrevMI);
// Record this macro.
MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
@@ -1209,6 +1230,10 @@
}
MI->setHidden(Hidden);
+ // Make sure we install the macro once we're done.
+ AddLoadedMacroInfo.MI = MI;
+ AddLoadedMacroInfo.II = II;
+
// Remember that we saw this macro last so that we add the tokens that
// form its body to it.
Macro = MI;
@@ -1316,13 +1341,10 @@
return HFI;
}
-void ASTReader::addMacroIDForDeserialization(IdentifierInfo *II, MacroID ID){
+void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
II->setHadMacroDefinition(true);
assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
- SmallVector<serialization::MacroID, 2> &MacroIDs = PendingMacroIDs[II];
- assert(std::find(MacroIDs.begin(), MacroIDs.end(), ID) == MacroIDs.end() &&
- "Already added the macro ID for deserialization");
- MacroIDs.push_back(ID);
+ PendingMacroIDs[II].append(IDs.begin(), IDs.end());
}
void ASTReader::ReadDefinedMacros() {
@@ -6136,7 +6158,7 @@
return LocalID + I->second;
}
-MacroInfo *ASTReader::getMacro(MacroID ID) {
+MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
if (ID == 0)
return 0;
@@ -6152,7 +6174,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];
@@ -6851,16 +6873,13 @@
PendingDeclChains.clear();
// Load any pending macro definitions.
- // Note that new macros may be added while deserializing a macro.
for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
- PendingMacroIDsMap::iterator PMIt = PendingMacroIDs.begin() + I;
- IdentifierInfo *II = PMIt->first;
- SmallVector<serialization::MacroID, 2> MacroIDs;
- MacroIDs.swap(PMIt->second);
- for (SmallVectorImpl<serialization::MacroID>::iterator
- MIt = MacroIDs.begin(), ME = MacroIDs.end(); MIt != ME; ++MIt) {
- MacroInfo *MI = getMacro(*MIt);
- PP.addLoadedMacroInfo(II, MI);
+ // 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) {
+ Hint = getMacro(GlobalIDs[IDIdx], Hint);
}
}
PendingMacroIDs.clear();
diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp
index 8c14b5f..36e1bb3 100644
--- a/lib/Serialization/ASTWriter.cpp
+++ b/lib/Serialization/ASTWriter.cpp
@@ -1796,10 +1796,12 @@
// Construct the list of macro definitions that need to be serialized.
SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
MacrosToEmit;
+ llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
E = PP.macro_end(Chain == 0);
I != E; ++I) {
if (!IsModule || I->second->isPublic()) {
+ MacroDefinitionsSeen.insert(I->first);
MacrosToEmit.push_back(std::make_pair(I->first, I->second));
}
}
@@ -1852,12 +1854,6 @@
Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
AddSourceLocation(MI->getDefinitionLoc(), Record);
AddSourceLocation(MI->getDefinitionEndLoc(), Record);
- MacroInfo *PrevMI = MI->getPreviousDefinition();
- // Serialize only the part of the definition chain that is local.
- // The chain will be synthesized across modules by the ASTReader.
- if (Chain && PrevMI && PrevMI->isFromAST())
- PrevMI = 0;
- addMacroRef(PrevMI, Record);
AddSourceLocation(MI->getUndefLoc(), Record);
Record.push_back(MI->isUsed());
Record.push_back(MI->isPublic());
@@ -2739,8 +2735,14 @@
if (isInterestingIdentifier(II, Macro)) {
DataLen += 2; // 2 bytes for builtin ID
DataLen += 2; // 2 bytes for flags
- if (hadMacroDefinition(II, Macro))
+ if (hadMacroDefinition(II, Macro)) {
+ for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
+ if (Writer.getMacroRef(M) != 0)
+ DataLen += 4;
+ }
+
DataLen += 4;
+ }
for (IdentifierResolver::iterator D = IdResolver.begin(II),
DEnd = IdResolver.end();
@@ -2785,8 +2787,13 @@
clang::io::Emit16(Out, Bits);
if (HadMacroDefinition) {
- // Write the macro ID associated with this identifier.
- clang::io::Emit32(Out, Writer.getMacroRef(Macro));
+ // Write all of the macro IDs associated with this identifier.
+ for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
+ if (MacroID ID = Writer.getMacroRef(M))
+ clang::io::Emit32(Out, ID);
+ }
+
+ clang::io::Emit32(Out, 0);
}
// Emit the declaration IDs in reverse order, because the