diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 47d274220a0b..aa84b23fe8b7 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -397,6 +397,14 @@ private: // Cached tokens state. /// allocation. MacroInfoChain *MICache; + struct DeserializedMacroInfoChain { + MacroInfo MI; + unsigned OwningModuleID; // MUST be immediately after the MacroInfo object + // so it can be accessed by MacroInfo::getOwningModuleID(). + DeserializedMacroInfoChain *Next; + }; + DeserializedMacroInfoChain *DeserialMIChainHead; + public: Preprocessor(IntrusiveRefCntPtr PPOpts, DiagnosticsEngine &diags, LangOptions &opts, diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 3cd40eacf8ab..50a0cb55f736 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -61,9 +61,12 @@ MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L, unsigned SubModuleID) { LLVM_STATIC_ASSERT(llvm::AlignOf::Alignment >= sizeof(SubModuleID), "alignment for MacroInfo is less than the ID"); - MacroInfo *MI = - (MacroInfo*)BP.Allocate(sizeof(MacroInfo) + sizeof(SubModuleID), - llvm::AlignOf::Alignment); + DeserializedMacroInfoChain *MIChain = + BP.Allocate(); + MIChain->Next = DeserialMIChainHead; + DeserialMIChainHead = MIChain; + + MacroInfo *MI = &MIChain->MI; new (MI) MacroInfo(L); MI->FromASTFile = true; MI->setOwningModuleID(SubModuleID); diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 53c45dca01f9..09f827991dc3 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -67,7 +67,8 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr PPOpts, CodeComplete(0), CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0), SkipMainFilePreamble(0, true), CurPPLexer(0), CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), - MacroArgCache(0), Record(0), MIChainHead(0), MICache(0) { + MacroArgCache(0), Record(0), MIChainHead(0), MICache(0), + DeserialMIChainHead(0) { OwnsHeaderSearch = OwnsHeaders; ScratchBuf = new ScratchBuffer(SourceMgr); @@ -153,6 +154,9 @@ Preprocessor::~Preprocessor() { for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) delete TokenLexerCache[i]; + for (DeserializedMacroInfoChain *I = DeserialMIChainHead ; I ; I = I->Next) + I->MI.Destroy(); + // Free any cached MacroArgs. for (MacroArgs *ArgList = MacroArgCache; ArgList; ) ArgList = ArgList->deallocate();