r215559 - Use std::unique_ptr to simplify memory management a bit.

David Blaikie dblaikie at gmail.com
Wed Aug 13 10:08:50 PDT 2014


On Wed, Aug 13, 2014 at 9:47 AM, Rafael Espindola
<rafael.espindola at gmail.com> wrote:
> Author: rafael
> Date: Wed Aug 13 11:47:00 2014
> New Revision: 215559
>
> URL: http://llvm.org/viewvc/llvm-project?rev=215559&view=rev
> Log:
> Use std::unique_ptr to simplify memory management a bit.
>
> Modified:
>     cfe/trunk/include/clang/Frontend/ASTUnit.h
>     cfe/trunk/lib/Frontend/ASTUnit.cpp
>
> Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=215559&r1=215558&r2=215559&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
> +++ cfe/trunk/include/clang/Frontend/ASTUnit.h Wed Aug 13 11:47:00 2014
> @@ -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<llvm::MemoryBuffer> 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_ptr<llvm::MemoryBuffer >PreambleBuffer;
>
>    /// \brief The number of warnings that occurred while parsing the preamble.
>    ///
>
> Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=215559&r1=215558&r2=215559&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
> +++ cfe/trunk/lib/Frontend/ASTUnit.cpp Wed Aug 13 11:47:00 2014
> @@ -220,7 +220,7 @@ ASTUnit::ASTUnit(bool _MainFileIsAST)
>      OwnsRemappedFileBuffers(true),
>      NumStoredDiagnosticsFromDriver(0),
>      PreambleRebuildCounter(0), SavedMainFileBuffer(nullptr),
> -    PreambleBuffer(nullptr), NumWarningsInPreamble(0),
> +    NumWarningsInPreamble(0),

you removed the initialization of PreambleBuffer, but not
SavedMainFileBuffer - local consistency might be nice, either way? (I
generally err away from initializations that are redundant with their
default behavior)

>      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(SmallV
>  /// \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);

the nullptr parameter is redundant (it's the default argument) - and
the nullptr assignment would've done the right thing here, but there's
reasonable debate about whether "= nullptr" or ".reset()" is the
better way to write this. I'm OK with ".reset()".

>
>    if (!Invocation) {
>      delete OverrideMainBuffer;
> @@ -1127,7 +1123,7 @@ bool ASTUnit::Parse(llvm::MemoryBuffer *
>      checkAndSanitizeDiags(StoredDiagnostics, getSourceManager());
>
>      // Keep track of the override buffer;
> -    SavedMainFileBuffer = OverrideMainBuffer;
> +    SavedMainFileBuffer.reset(OverrideMainBuffer);
>    }
>
>    std::unique_ptr<TopLevelDeclTrackerAction> Act(
> @@ -1514,14 +1510,12 @@ llvm::MemoryBuffer *ASTUnit::getMainBuff
>                                                    + 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;
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits



More information about the cfe-commits mailing list