[llvm] r216223 - Explicitly pass ownership of the MemoryBuffer to AddNewSourceBuffer using std::unique_ptr

David Blaikie dblaikie at gmail.com
Thu Aug 21 13:44:56 PDT 2014


Author: dblaikie
Date: Thu Aug 21 15:44:56 2014
New Revision: 216223

URL: http://llvm.org/viewvc/llvm-project?rev=216223&view=rev
Log:
Explicitly pass ownership of the MemoryBuffer to AddNewSourceBuffer using std::unique_ptr

Modified:
    llvm/trunk/include/llvm/Support/SourceMgr.h
    llvm/trunk/lib/AsmParser/Parser.cpp
    llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp
    llvm/trunk/lib/Object/IRObjectFile.cpp
    llvm/trunk/lib/Support/SourceMgr.cpp
    llvm/trunk/lib/Support/YAMLParser.cpp
    llvm/trunk/lib/TableGen/Main.cpp
    llvm/trunk/tools/llvm-mc/llvm-mc.cpp
    llvm/trunk/tools/llvm-mcmarkup/llvm-mcmarkup.cpp
    llvm/trunk/unittests/Support/SourceMgrTest.cpp
    llvm/trunk/utils/FileCheck/FileCheck.cpp

Modified: llvm/trunk/include/llvm/Support/SourceMgr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/SourceMgr.h?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/SourceMgr.h (original)
+++ llvm/trunk/include/llvm/Support/SourceMgr.h Thu Aug 21 15:44:56 2014
@@ -19,11 +19,11 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SMLoc.h"
 #include <string>
 
 namespace llvm {
-  class MemoryBuffer;
   class SourceMgr;
   class SMDiagnostic;
   class SMFixIt;
@@ -47,7 +47,7 @@ public:
 private:
   struct SrcBuffer {
     /// The memory buffer for the file.
-    MemoryBuffer *Buffer;
+    std::unique_ptr<MemoryBuffer> Buffer;
 
     /// This is the location of the parent include, or null if at the top level.
     SMLoc IncludeLoc;
@@ -96,7 +96,7 @@ public:
 
   const MemoryBuffer *getMemoryBuffer(unsigned i) const {
     assert(isValidBufferID(i));
-    return Buffers[i - 1].Buffer;
+    return Buffers[i - 1].Buffer.get();
   }
 
   unsigned getNumBuffers() const {
@@ -115,11 +115,12 @@ public:
 
   /// Add a new source buffer to this source manager. This takes ownership of
   /// the memory buffer.
-  unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
+  unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
+                              SMLoc IncludeLoc) {
     SrcBuffer NB;
-    NB.Buffer = F;
+    NB.Buffer = std::move(F);
     NB.IncludeLoc = IncludeLoc;
-    Buffers.push_back(NB);
+    Buffers.push_back(std::move(NB));
     return Buffers.size();
   }
 

Modified: llvm/trunk/lib/AsmParser/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/Parser.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/Parser.cpp (original)
+++ llvm/trunk/lib/AsmParser/Parser.cpp Thu Aug 21 15:44:56 2014
@@ -25,7 +25,7 @@ bool llvm::parseAssemblyInto(std::unique
                              SMDiagnostic &Err) {
   SourceMgr SM;
   StringRef Buf = F->getBuffer();
-  SM.AddNewSourceBuffer(F.release(), SMLoc());
+  SM.AddNewSourceBuffer(std::move(F), SMLoc());
 
   return LLParser(Buf, SM, Err, &M).Run();
 }

Modified: llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp Thu Aug 21 15:44:56 2014
@@ -110,14 +110,12 @@ void AsmPrinter::EmitInlineAsm(StringRef
     HasDiagHandler = true;
   }
 
-  MemoryBuffer *Buffer;
-  if (isNullTerminated)
-    Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
-  else
-    Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
+  std::unique_ptr<MemoryBuffer> Buffer(
+      isNullTerminated ? MemoryBuffer::getMemBuffer(Str, "<inline asm>")
+                       : MemoryBuffer::getMemBufferCopy(Str, "<inline asm>"));
 
   // Tell SrcMgr about this buffer, it takes ownership of the buffer.
-  SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
+  SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
 
   std::unique_ptr<MCAsmParser> Parser(
       createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Thu Aug 21 15:44:56 2014
@@ -2123,8 +2123,8 @@ bool AsmParser::handleMacroEntry(const M
   // instantiation.
   OS << ".endmacro\n";
 
-  MemoryBuffer *Instantiation =
-      MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
+  std::unique_ptr<MemoryBuffer> Instantiation(
+      MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"));
 
   // Create the macro instantiation object and add to the current macro
   // instantiation stack.
@@ -2134,7 +2134,7 @@ bool AsmParser::handleMacroEntry(const M
   ActiveMacros.push_back(MI);
 
   // Jump to the macro instantiation and prime the lexer.
-  CurBuffer = SrcMgr.AddNewSourceBuffer(Instantiation, SMLoc());
+  CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
   Lex();
 
@@ -4310,8 +4310,8 @@ void AsmParser::instantiateMacroLikeBody
                                          raw_svector_ostream &OS) {
   OS << ".endr\n";
 
-  MemoryBuffer *Instantiation =
-      MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
+  std::unique_ptr<MemoryBuffer> Instantiation(
+      MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"));
 
   // Create the macro instantiation object and add to the current macro
   // instantiation stack.
@@ -4321,7 +4321,7 @@ void AsmParser::instantiateMacroLikeBody
   ActiveMacros.push_back(MI);
 
   // Jump to the macro instantiation and prime the lexer.
-  CurBuffer = SrcMgr.AddNewSourceBuffer(Instantiation, SMLoc());
+  CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
   Lex();
 }

Modified: llvm/trunk/lib/Object/IRObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/IRObjectFile.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/lib/Object/IRObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/IRObjectFile.cpp Thu Aug 21 15:44:56 2014
@@ -75,7 +75,7 @@ IRObjectFile::IRObjectFile(MemoryBufferR
 
   std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
   SourceMgr SrcMgr;
-  SrcMgr.AddNewSourceBuffer(Buffer.release(), SMLoc());
+  SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
   std::unique_ptr<MCAsmParser> Parser(
       createMCAsmParser(SrcMgr, MCCtx, *Streamer, *MAI));
 

Modified: llvm/trunk/lib/Support/SourceMgr.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SourceMgr.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SourceMgr.cpp (original)
+++ llvm/trunk/lib/Support/SourceMgr.cpp Thu Aug 21 15:44:56 2014
@@ -42,11 +42,6 @@ SourceMgr::~SourceMgr() {
   // Delete the line # cache if allocated.
   if (LineNoCacheTy *Cache = getCache(LineNoCache))
     delete Cache;
-
-  while (!Buffers.empty()) {
-    delete Buffers.back().Buffer;
-    Buffers.pop_back();
-  }
 }
 
 unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
@@ -67,7 +62,7 @@ unsigned SourceMgr::AddIncludeFile(const
   if (!NewBufOrErr)
     return 0;
 
-  return AddNewSourceBuffer(NewBufOrErr.get().release(), IncludeLoc);
+  return AddNewSourceBuffer(std::move(*NewBufOrErr), IncludeLoc);
 }
 
 unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {

Modified: llvm/trunk/lib/Support/YAMLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLParser.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/lib/Support/YAMLParser.cpp (original)
+++ llvm/trunk/lib/Support/YAMLParser.cpp Thu Aug 21 15:44:56 2014
@@ -708,8 +708,10 @@ Scanner::Scanner(StringRef Input, Source
   , IsStartOfStream(true)
   , IsSimpleKeyAllowed(true)
   , Failed(false) {
-  InputBuffer = MemoryBuffer::getMemBuffer(Input, "YAML");
-  SM.AddNewSourceBuffer(InputBuffer, SMLoc());
+  std::unique_ptr<MemoryBuffer> InputBufferOwner(
+      MemoryBuffer::getMemBuffer(Input, "YAML"));
+  InputBuffer = InputBufferOwner.get();
+  SM.AddNewSourceBuffer(std::move(InputBufferOwner), SMLoc());
   Current = InputBuffer->getBufferStart();
   End = InputBuffer->getBufferEnd();
 }
@@ -719,7 +721,7 @@ Scanner::Scanner(std::unique_ptr<MemoryB
       Current(InputBuffer->getBufferStart()), End(InputBuffer->getBufferEnd()),
       Indent(-1), Column(0), Line(0), FlowLevel(0), IsStartOfStream(true),
       IsSimpleKeyAllowed(true), Failed(false) {
-  SM.AddNewSourceBuffer(Buffer.release(), SMLoc());
+  SM.AddNewSourceBuffer(std::move(Buffer), SMLoc());
 }
 
 Token &Scanner::peekNext() {

Modified: llvm/trunk/lib/TableGen/Main.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/TableGen/Main.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/lib/TableGen/Main.cpp (original)
+++ llvm/trunk/lib/TableGen/Main.cpp Thu Aug 21 15:44:56 2014
@@ -88,10 +88,9 @@ int TableGenMain(char *argv0, TableGenMa
            << "': " << EC.message() << "\n";
     return 1;
   }
-  MemoryBuffer *F = FileOrErr.get().release();
 
   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
-  SrcMgr.AddNewSourceBuffer(F, SMLoc());
+  SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
 
   // Record the location of the include directory so that the lexer can find
   // it later.

Modified: llvm/trunk/tools/llvm-mc/llvm-mc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/llvm-mc.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mc/llvm-mc.cpp (original)
+++ llvm/trunk/tools/llvm-mc/llvm-mc.cpp Thu Aug 21 15:44:56 2014
@@ -373,12 +373,12 @@ int main(int argc, char **argv) {
     errs() << ProgName << ": " << EC.message() << '\n';
     return 1;
   }
-  MemoryBuffer *Buffer = BufferPtr->release();
+  MemoryBuffer *Buffer = BufferPtr->get();
 
   SourceMgr SrcMgr;
 
   // Tell SrcMgr about this buffer, which is what the parser will pick up.
-  SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
+  SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
 
   // Record the location of the include directories so that the lexer can find
   // it later.

Modified: llvm/trunk/tools/llvm-mcmarkup/llvm-mcmarkup.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mcmarkup/llvm-mcmarkup.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mcmarkup/llvm-mcmarkup.cpp (original)
+++ llvm/trunk/tools/llvm-mcmarkup/llvm-mcmarkup.cpp Thu Aug 21 15:44:56 2014
@@ -141,14 +141,15 @@ static void parseMCMarkup(StringRef File
     errs() << ToolName << ": " << EC.message() << '\n';
     return;
   }
-  MemoryBuffer *Buffer = BufferPtr->release();
+  std::unique_ptr<MemoryBuffer> &Buffer = BufferPtr.get();
 
   SourceMgr SrcMgr;
 
+  StringRef InputSource = Buffer->getBuffer();
+
   // Tell SrcMgr about this buffer, which is what the parser will pick up.
-  SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
+  SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
 
-  StringRef InputSource = Buffer->getBuffer();
   MarkupLexer Lex(InputSource);
   MarkupParser Parser(Lex, SrcMgr);
 

Modified: llvm/trunk/unittests/Support/SourceMgrTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/SourceMgrTest.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/SourceMgrTest.cpp (original)
+++ llvm/trunk/unittests/Support/SourceMgrTest.cpp Thu Aug 21 15:44:56 2014
@@ -23,8 +23,9 @@ public:
   std::string Output;
 
   void setMainBuffer(StringRef Text, StringRef BufferName) {
-    MemoryBuffer *MainBuffer = MemoryBuffer::getMemBuffer(Text, BufferName);
-    MainBufferID = SM.AddNewSourceBuffer(MainBuffer, llvm::SMLoc());
+    std::unique_ptr<MemoryBuffer> MainBuffer(
+        MemoryBuffer::getMemBuffer(Text, BufferName));
+    MainBufferID = SM.AddNewSourceBuffer(std::move(MainBuffer), llvm::SMLoc());
   }
 
   SMLoc getLoc(unsigned Offset) {

Modified: llvm/trunk/utils/FileCheck/FileCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/FileCheck/FileCheck.cpp?rev=216223&r1=216222&r2=216223&view=diff
==============================================================================
--- llvm/trunk/utils/FileCheck/FileCheck.cpp (original)
+++ llvm/trunk/utils/FileCheck/FileCheck.cpp Thu Aug 21 15:44:56 2014
@@ -636,8 +636,9 @@ struct CheckString {
 ///
 /// \param PreserveHorizontal Don't squash consecutive horizontal whitespace
 /// characters to a single space.
-static MemoryBuffer *CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB,
-                                           bool PreserveHorizontal) {
+static std::unique_ptr<MemoryBuffer>
+CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB,
+                      bool PreserveHorizontal) {
   SmallString<128> NewFile;
   NewFile.reserve(MB->getBufferSize());
 
@@ -662,8 +663,8 @@ static MemoryBuffer *CanonicalizeInputFi
       ++Ptr;
   }
 
-  return MemoryBuffer::getMemBufferCopy(NewFile.str(),
-                                        MB->getBufferIdentifier());
+  return std::unique_ptr<MemoryBuffer>(
+      MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier()));
 }
 
 static bool IsPartOfWord(char c) {
@@ -838,25 +839,25 @@ static bool ReadCheckFile(SourceMgr &SM,
 
   // If we want to canonicalize whitespace, strip excess whitespace from the
   // buffer containing the CHECK lines. Remove DOS style line endings.
-  MemoryBuffer *F = CanonicalizeInputFile(std::move(FileOrErr.get()),
-                                          NoCanonicalizeWhiteSpace);
-
-  SM.AddNewSourceBuffer(F, SMLoc());
+  std::unique_ptr<MemoryBuffer> F =
+      CanonicalizeInputFile(std::move(*FileOrErr), NoCanonicalizeWhiteSpace);
 
   // Find all instances of CheckPrefix followed by : in the file.
   StringRef Buffer = F->getBuffer();
 
+  SM.AddNewSourceBuffer(std::move(F), SMLoc());
+
   std::vector<Pattern> ImplicitNegativeChecks;
   for (const auto &PatternString : ImplicitCheckNot) {
     // Create a buffer with fake command line content in order to display the
     // command line option responsible for the specific implicit CHECK-NOT.
     std::string Prefix = std::string("-") + ImplicitCheckNot.ArgStr + "='";
     std::string Suffix = "'";
-    MemoryBuffer *CmdLine = MemoryBuffer::getMemBufferCopy(
-        Prefix + PatternString + Suffix, "command line");
+    std::unique_ptr<MemoryBuffer> CmdLine(MemoryBuffer::getMemBufferCopy(
+        Prefix + PatternString + Suffix, "command line"));
     StringRef PatternInBuffer =
         CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
-    SM.AddNewSourceBuffer(CmdLine, SMLoc());
+    SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
 
     ImplicitNegativeChecks.push_back(Pattern(Check::CheckNot));
     ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer,
@@ -1272,18 +1273,18 @@ int main(int argc, char **argv) {
 
   // Remove duplicate spaces in the input file if requested.
   // Remove DOS style line endings.
-  MemoryBuffer *F =
-    CanonicalizeInputFile(std::move(File), NoCanonicalizeWhiteSpace);
-
-  SM.AddNewSourceBuffer(F, SMLoc());
-
-  /// VariableTable - This holds all the current filecheck variables.
-  StringMap<StringRef> VariableTable;
+  std::unique_ptr<MemoryBuffer> F =
+      CanonicalizeInputFile(std::move(File), NoCanonicalizeWhiteSpace);
 
   // Check that we have all of the expected strings, in order, in the input
   // file.
   StringRef Buffer = F->getBuffer();
 
+  SM.AddNewSourceBuffer(std::move(F), SMLoc());
+
+  /// VariableTable - This holds all the current filecheck variables.
+  StringMap<StringRef> VariableTable;
+
   bool hasError = false;
 
   unsigned i = 0, j = 0, e = CheckStrings.size();





More information about the llvm-commits mailing list