summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2013-04-30 05:05:35 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2013-04-30 05:05:35 +0000
commit3e25b990f1e2ba2a9a63dde5fd111492a7f6194a (patch)
treeb964dbf9e494ae7b36325e8cb929468d8570de49
parenta7efaf99ac662af11890aae74d84da8d31ab2fe0 (diff)
downloadclang-3e25b990f1e2ba2a9a63dde5fd111492a7f6194a.tar.gz
clang-3e25b990f1e2ba2a9a63dde5fd111492a7f6194a.tar.bz2
clang-3e25b990f1e2ba2a9a63dde5fd111492a7f6194a.tar.xz
[PCH] Fix memory leak related to deserialized MacroInfo objects.
Deserialized MacroInfos were not destroyed and if their SmallVector did heap allocation, it was leaked. rdar://13768967 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@180771 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Lex/Preprocessor.h8
-rw-r--r--lib/Lex/PPDirectives.cpp9
-rw-r--r--lib/Lex/Preprocessor.cpp6
3 files changed, 19 insertions, 4 deletions
diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h
index 47d274220a..aa84b23fe8 100644
--- a/include/clang/Lex/Preprocessor.h
+++ b/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<PreprocessorOptions> PPOpts,
DiagnosticsEngine &diags, LangOptions &opts,
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp
index 3cd40eacf8..50a0cb55f7 100644
--- a/lib/Lex/PPDirectives.cpp
+++ b/lib/Lex/PPDirectives.cpp
@@ -61,9 +61,12 @@ MacroInfo *Preprocessor::AllocateDeserializedMacroInfo(SourceLocation L,
unsigned SubModuleID) {
LLVM_STATIC_ASSERT(llvm::AlignOf<MacroInfo>::Alignment >= sizeof(SubModuleID),
"alignment for MacroInfo is less than the ID");
- MacroInfo *MI =
- (MacroInfo*)BP.Allocate(sizeof(MacroInfo) + sizeof(SubModuleID),
- llvm::AlignOf<MacroInfo>::Alignment);
+ DeserializedMacroInfoChain *MIChain =
+ BP.Allocate<DeserializedMacroInfoChain>();
+ MIChain->Next = DeserialMIChainHead;
+ DeserialMIChainHead = MIChain;
+
+ MacroInfo *MI = &MIChain->MI;
new (MI) MacroInfo(L);
MI->FromASTFile = true;
MI->setOwningModuleID(SubModuleID);
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index 53c45dca01..09f827991d 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -67,7 +67,8 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> 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();