From 1961f14cf965e63d944d8888a089067cde142d10 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Thu, 21 Aug 2014 20:44:56 +0000 Subject: [PATCH] Explicitly pass ownership of the MemoryBuffer to AddNewSourceBuffer using std::unique_ptr llvm-svn: 216223 --- llvm/include/llvm/Support/SourceMgr.h | 13 ++++--- llvm/lib/AsmParser/Parser.cpp | 2 +- .../AsmPrinter/AsmPrinterInlineAsm.cpp | 10 ++--- llvm/lib/MC/MCParser/AsmParser.cpp | 12 +++--- llvm/lib/Object/IRObjectFile.cpp | 2 +- llvm/lib/Support/SourceMgr.cpp | 7 +--- llvm/lib/Support/YAMLParser.cpp | 8 ++-- llvm/lib/TableGen/Main.cpp | 3 +- llvm/tools/llvm-mc/llvm-mc.cpp | 4 +- llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp | 9 +++-- llvm/unittests/Support/SourceMgrTest.cpp | 5 ++- llvm/utils/FileCheck/FileCheck.cpp | 37 ++++++++++--------- 12 files changed, 55 insertions(+), 57 deletions(-) diff --git a/llvm/include/llvm/Support/SourceMgr.h b/llvm/include/llvm/Support/SourceMgr.h index 4717553bd0de..19dad6b6ac53 100644 --- a/llvm/include/llvm/Support/SourceMgr.h +++ b/llvm/include/llvm/Support/SourceMgr.h @@ -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 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 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 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(); } diff --git a/llvm/lib/AsmParser/Parser.cpp b/llvm/lib/AsmParser/Parser.cpp index 9bc9b241666e..d2384bd978d0 100644 --- a/llvm/lib/AsmParser/Parser.cpp +++ b/llvm/lib/AsmParser/Parser.cpp @@ -25,7 +25,7 @@ bool llvm::parseAssemblyInto(std::unique_ptr F, Module &M, 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(); } diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp index 7d0cb6b45d1c..35a2842c70fa 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp @@ -110,14 +110,12 @@ void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode, HasDiagHandler = true; } - MemoryBuffer *Buffer; - if (isNullTerminated) - Buffer = MemoryBuffer::getMemBuffer(Str, ""); - else - Buffer = MemoryBuffer::getMemBufferCopy(Str, ""); + std::unique_ptr Buffer( + isNullTerminated ? MemoryBuffer::getMemBuffer(Str, "") + : MemoryBuffer::getMemBufferCopy(Str, "")); // Tell SrcMgr about this buffer, it takes ownership of the buffer. - SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); + SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc()); std::unique_ptr Parser( createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI)); diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index aafc5e1850e9..5e44266e26eb 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -2123,8 +2123,8 @@ bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { // instantiation. OS << ".endmacro\n"; - MemoryBuffer *Instantiation = - MemoryBuffer::getMemBufferCopy(OS.str(), ""); + std::unique_ptr Instantiation( + MemoryBuffer::getMemBufferCopy(OS.str(), "")); // Create the macro instantiation object and add to the current macro // instantiation stack. @@ -2134,7 +2134,7 @@ bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { 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(MCAsmMacro *M, SMLoc DirectiveLoc, raw_svector_ostream &OS) { OS << ".endr\n"; - MemoryBuffer *Instantiation = - MemoryBuffer::getMemBufferCopy(OS.str(), ""); + std::unique_ptr Instantiation( + MemoryBuffer::getMemBufferCopy(OS.str(), "")); // Create the macro instantiation object and add to the current macro // instantiation stack. @@ -4321,7 +4321,7 @@ void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, 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(); } diff --git a/llvm/lib/Object/IRObjectFile.cpp b/llvm/lib/Object/IRObjectFile.cpp index d6b4b6bd2575..2ef359c7f00d 100644 --- a/llvm/lib/Object/IRObjectFile.cpp +++ b/llvm/lib/Object/IRObjectFile.cpp @@ -75,7 +75,7 @@ IRObjectFile::IRObjectFile(MemoryBufferRef Object, std::unique_ptr Mod) std::unique_ptr Buffer(MemoryBuffer::getMemBuffer(InlineAsm)); SourceMgr SrcMgr; - SrcMgr.AddNewSourceBuffer(Buffer.release(), SMLoc()); + SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc()); std::unique_ptr Parser( createMCAsmParser(SrcMgr, MCCtx, *Streamer, *MAI)); diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp index 003cb56e6cb5..f3a422c9d27c 100644 --- a/llvm/lib/Support/SourceMgr.cpp +++ b/llvm/lib/Support/SourceMgr.cpp @@ -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 std::string &Filename, if (!NewBufOrErr) return 0; - return AddNewSourceBuffer(NewBufOrErr.get().release(), IncludeLoc); + return AddNewSourceBuffer(std::move(*NewBufOrErr), IncludeLoc); } unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const { diff --git a/llvm/lib/Support/YAMLParser.cpp b/llvm/lib/Support/YAMLParser.cpp index 01cd926297a9..07a326ee554f 100644 --- a/llvm/lib/Support/YAMLParser.cpp +++ b/llvm/lib/Support/YAMLParser.cpp @@ -708,8 +708,10 @@ Scanner::Scanner(StringRef Input, SourceMgr &sm) , IsStartOfStream(true) , IsSimpleKeyAllowed(true) , Failed(false) { - InputBuffer = MemoryBuffer::getMemBuffer(Input, "YAML"); - SM.AddNewSourceBuffer(InputBuffer, SMLoc()); + std::unique_ptr 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 Buffer, SourceMgr &SM_) 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() { diff --git a/llvm/lib/TableGen/Main.cpp b/llvm/lib/TableGen/Main.cpp index e317fbfa373d..d9c5601a5da4 100644 --- a/llvm/lib/TableGen/Main.cpp +++ b/llvm/lib/TableGen/Main.cpp @@ -88,10 +88,9 @@ int TableGenMain(char *argv0, TableGenMainFn *MainFn) { << "': " << 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. diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp index 4c5b230573b2..42cde6e16f4c 100644 --- a/llvm/tools/llvm-mc/llvm-mc.cpp +++ b/llvm/tools/llvm-mc/llvm-mc.cpp @@ -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. diff --git a/llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp b/llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp index a878f1157d57..56543139d6fa 100644 --- a/llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp +++ b/llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp @@ -141,14 +141,15 @@ static void parseMCMarkup(StringRef Filename) { errs() << ToolName << ": " << EC.message() << '\n'; return; } - MemoryBuffer *Buffer = BufferPtr->release(); + std::unique_ptr &Buffer = BufferPtr.get(); SourceMgr SrcMgr; - // Tell SrcMgr about this buffer, which is what the parser will pick up. - SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); - StringRef InputSource = Buffer->getBuffer(); + + // Tell SrcMgr about this buffer, which is what the parser will pick up. + SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc()); + MarkupLexer Lex(InputSource); MarkupParser Parser(Lex, SrcMgr); diff --git a/llvm/unittests/Support/SourceMgrTest.cpp b/llvm/unittests/Support/SourceMgrTest.cpp index 2b69fe984445..26310c11ddba 100644 --- a/llvm/unittests/Support/SourceMgrTest.cpp +++ b/llvm/unittests/Support/SourceMgrTest.cpp @@ -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 MainBuffer( + MemoryBuffer::getMemBuffer(Text, BufferName)); + MainBufferID = SM.AddNewSourceBuffer(std::move(MainBuffer), llvm::SMLoc()); } SMLoc getLoc(unsigned Offset) { diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp index e8365fc248af..9203c4cf33c8 100644 --- a/llvm/utils/FileCheck/FileCheck.cpp +++ b/llvm/utils/FileCheck/FileCheck.cpp @@ -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 MB, - bool PreserveHorizontal) { +static std::unique_ptr +CanonicalizeInputFile(std::unique_ptr MB, + bool PreserveHorizontal) { SmallString<128> NewFile; NewFile.reserve(MB->getBufferSize()); @@ -662,8 +663,8 @@ static MemoryBuffer *CanonicalizeInputFile(std::unique_ptr MB, ++Ptr; } - return MemoryBuffer::getMemBufferCopy(NewFile.str(), - MB->getBufferIdentifier()); + return std::unique_ptr( + 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 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 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 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 VariableTable; + std::unique_ptr 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 VariableTable; + bool hasError = false; unsigned i = 0, j = 0, e = CheckStrings.size();