From: Rafael Espindola Date: Wed, 13 Aug 2014 16:47:00 +0000 (+0000) Subject: Use std::unique_ptr to simplify memory management a bit. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=48bd21767dd3ed3723d272df1420e78c6f8f5f1e;p=clang Use std::unique_ptr to simplify memory management a bit. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@215559 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/ASTUnit.h b/include/clang/Frontend/ASTUnit.h index d31be7e5cb..c350261ea2 100644 --- a/include/clang/Frontend/ASTUnit.h +++ b/include/clang/Frontend/ASTUnit.h @@ -272,12 +272,12 @@ private: /// \brief When non-NULL, this is the buffer used to store the contents of /// the main file when it has been padded for use with the precompiled /// preamble. - llvm::MemoryBuffer *SavedMainFileBuffer; + std::unique_ptr SavedMainFileBuffer; /// \brief When non-NULL, this is the buffer used to store the /// contents of the preamble when it has been padded to build the /// precompiled preamble. - llvm::MemoryBuffer *PreambleBuffer; + std::unique_ptrPreambleBuffer; /// \brief The number of warnings that occurred while parsing the preamble. /// diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index a29edc0e3e..bc8d268f48 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -220,7 +220,7 @@ ASTUnit::ASTUnit(bool _MainFileIsAST) OwnsRemappedFileBuffers(true), NumStoredDiagnosticsFromDriver(0), PreambleRebuildCounter(0), SavedMainFileBuffer(nullptr), - PreambleBuffer(nullptr), NumWarningsInPreamble(0), + NumWarningsInPreamble(0), ShouldCacheCodeCompletionResults(false), IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false), CompletionCacheTopLevelHashValue(0), @@ -251,9 +251,6 @@ ASTUnit::~ASTUnit() { for (const auto &RB : PPOpts.RemappedFileBuffers) delete RB.second; } - - delete SavedMainFileBuffer; - delete PreambleBuffer; ClearCachedCompletionResults(); @@ -1029,8 +1026,7 @@ static void checkAndSanitizeDiags(SmallVectorImpl & /// \returns True if a failure occurred that causes the ASTUnit not to /// contain any translation-unit information, false otherwise. bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { - delete SavedMainFileBuffer; - SavedMainFileBuffer = nullptr; + SavedMainFileBuffer.reset(nullptr); if (!Invocation) { delete OverrideMainBuffer; @@ -1127,7 +1123,7 @@ bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { checkAndSanitizeDiags(StoredDiagnostics, getSourceManager()); // Keep track of the override buffer; - SavedMainFileBuffer = OverrideMainBuffer; + SavedMainFileBuffer.reset(OverrideMainBuffer); } std::unique_ptr Act( @@ -1514,14 +1510,12 @@ llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble( + NewPreamble.second.first); PreambleEndsAtStartOfLine = NewPreamble.second.second; - delete PreambleBuffer; - PreambleBuffer - = llvm::MemoryBuffer::getMemBufferCopy( - NewPreamble.first->getBuffer().slice(0, Preamble.size()), MainFilename); + PreambleBuffer.reset(llvm::MemoryBuffer::getMemBufferCopy( + NewPreamble.first->getBuffer().slice(0, Preamble.size()), MainFilename)); // Remap the main source file to the preamble buffer. StringRef MainFilePath = FrontendOpts.Inputs[0].getFile(); - PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer); + PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer.get()); // Tell the compiler invocation to generate a temporary precompiled header. FrontendOpts.ProgramAction = frontend::GeneratePCH;