Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 95ca0fb

Browse files
committed
Explicitly pass ownership of the MemoryBuffer to AddNewSourceBuffer using std::unique_ptr
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216223 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent fdbf61d commit 95ca0fb

File tree

12 files changed

+54
-56
lines changed

12 files changed

+54
-56
lines changed

include/llvm/Support/SourceMgr.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/StringRef.h"
2121
#include "llvm/ADT/Twine.h"
22+
#include "llvm/Support/MemoryBuffer.h"
2223
#include "llvm/Support/SMLoc.h"
2324
#include <string>
2425

2526
namespace llvm {
26-
class MemoryBuffer;
2727
class SourceMgr;
2828
class SMDiagnostic;
2929
class SMFixIt;
@@ -47,7 +47,7 @@ class SourceMgr {
4747
private:
4848
struct SrcBuffer {
4949
/// The memory buffer for the file.
50-
MemoryBuffer *Buffer;
50+
std::unique_ptr<MemoryBuffer> Buffer;
5151

5252
/// This is the location of the parent include, or null if at the top level.
5353
SMLoc IncludeLoc;
@@ -96,7 +96,7 @@ class SourceMgr {
9696

9797
const MemoryBuffer *getMemoryBuffer(unsigned i) const {
9898
assert(isValidBufferID(i));
99-
return Buffers[i - 1].Buffer;
99+
return Buffers[i - 1].Buffer.get();
100100
}
101101

102102
unsigned getNumBuffers() const {
@@ -115,11 +115,12 @@ class SourceMgr {
115115

116116
/// Add a new source buffer to this source manager. This takes ownership of
117117
/// the memory buffer.
118-
unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
118+
unsigned AddNewSourceBuffer(std::unique_ptr<MemoryBuffer> F,
119+
SMLoc IncludeLoc) {
119120
SrcBuffer NB;
120-
NB.Buffer = F;
121+
NB.Buffer = std::move(F);
121122
NB.IncludeLoc = IncludeLoc;
122-
Buffers.push_back(NB);
123+
Buffers.push_back(std::move(NB));
123124
return Buffers.size();
124125
}
125126

lib/AsmParser/Parser.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ bool llvm::parseAssemblyInto(std::unique_ptr<MemoryBuffer> F, Module &M,
2525
SMDiagnostic &Err) {
2626
SourceMgr SM;
2727
StringRef Buf = F->getBuffer();
28-
SM.AddNewSourceBuffer(F.release(), SMLoc());
28+
SM.AddNewSourceBuffer(std::move(F), SMLoc());
2929

3030
return LLParser(Buf, SM, Err, &M).Run();
3131
}

lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,12 @@ void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
110110
HasDiagHandler = true;
111111
}
112112

113-
MemoryBuffer *Buffer;
114-
if (isNullTerminated)
115-
Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
116-
else
117-
Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
113+
std::unique_ptr<MemoryBuffer> Buffer(
114+
isNullTerminated ? MemoryBuffer::getMemBuffer(Str, "<inline asm>")
115+
: MemoryBuffer::getMemBufferCopy(Str, "<inline asm>"));
118116

119117
// Tell SrcMgr about this buffer, it takes ownership of the buffer.
120-
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
118+
SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
121119

122120
std::unique_ptr<MCAsmParser> Parser(
123121
createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));

lib/MC/MCParser/AsmParser.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -2123,8 +2123,8 @@ bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
21232123
// instantiation.
21242124
OS << ".endmacro\n";
21252125

2126-
MemoryBuffer *Instantiation =
2127-
MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
2126+
std::unique_ptr<MemoryBuffer> Instantiation(
2127+
MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"));
21282128

21292129
// Create the macro instantiation object and add to the current macro
21302130
// instantiation stack.
@@ -2134,7 +2134,7 @@ bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
21342134
ActiveMacros.push_back(MI);
21352135

21362136
// Jump to the macro instantiation and prime the lexer.
2137-
CurBuffer = SrcMgr.AddNewSourceBuffer(Instantiation, SMLoc());
2137+
CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
21382138
Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
21392139
Lex();
21402140

@@ -4310,8 +4310,8 @@ void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
43104310
raw_svector_ostream &OS) {
43114311
OS << ".endr\n";
43124312

4313-
MemoryBuffer *Instantiation =
4314-
MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
4313+
std::unique_ptr<MemoryBuffer> Instantiation(
4314+
MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"));
43154315

43164316
// Create the macro instantiation object and add to the current macro
43174317
// instantiation stack.
@@ -4321,7 +4321,7 @@ void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
43214321
ActiveMacros.push_back(MI);
43224322

43234323
// Jump to the macro instantiation and prime the lexer.
4324-
CurBuffer = SrcMgr.AddNewSourceBuffer(Instantiation, SMLoc());
4324+
CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
43254325
Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
43264326
Lex();
43274327
}

lib/Object/IRObjectFile.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ IRObjectFile::IRObjectFile(MemoryBufferRef Object, std::unique_ptr<Module> Mod)
7575

7676
std::unique_ptr<MemoryBuffer> Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
7777
SourceMgr SrcMgr;
78-
SrcMgr.AddNewSourceBuffer(Buffer.release(), SMLoc());
78+
SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
7979
std::unique_ptr<MCAsmParser> Parser(
8080
createMCAsmParser(SrcMgr, MCCtx, *Streamer, *MAI));
8181

lib/Support/SourceMgr.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ SourceMgr::~SourceMgr() {
4242
// Delete the line # cache if allocated.
4343
if (LineNoCacheTy *Cache = getCache(LineNoCache))
4444
delete Cache;
45-
46-
while (!Buffers.empty()) {
47-
delete Buffers.back().Buffer;
48-
Buffers.pop_back();
49-
}
5045
}
5146

5247
unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
@@ -67,7 +62,7 @@ unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
6762
if (!NewBufOrErr)
6863
return 0;
6964

70-
return AddNewSourceBuffer(NewBufOrErr.get().release(), IncludeLoc);
65+
return AddNewSourceBuffer(std::move(*NewBufOrErr), IncludeLoc);
7166
}
7267

7368
unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {

lib/Support/YAMLParser.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,10 @@ Scanner::Scanner(StringRef Input, SourceMgr &sm)
708708
, IsStartOfStream(true)
709709
, IsSimpleKeyAllowed(true)
710710
, Failed(false) {
711-
InputBuffer = MemoryBuffer::getMemBuffer(Input, "YAML");
712-
SM.AddNewSourceBuffer(InputBuffer, SMLoc());
711+
std::unique_ptr<MemoryBuffer> InputBufferOwner(
712+
MemoryBuffer::getMemBuffer(Input, "YAML"));
713+
InputBuffer = InputBufferOwner.get();
714+
SM.AddNewSourceBuffer(std::move(InputBufferOwner), SMLoc());
713715
Current = InputBuffer->getBufferStart();
714716
End = InputBuffer->getBufferEnd();
715717
}
@@ -719,7 +721,7 @@ Scanner::Scanner(std::unique_ptr<MemoryBuffer> Buffer, SourceMgr &SM_)
719721
Current(InputBuffer->getBufferStart()), End(InputBuffer->getBufferEnd()),
720722
Indent(-1), Column(0), Line(0), FlowLevel(0), IsStartOfStream(true),
721723
IsSimpleKeyAllowed(true), Failed(false) {
722-
SM.AddNewSourceBuffer(Buffer.release(), SMLoc());
724+
SM.AddNewSourceBuffer(std::move(Buffer), SMLoc());
723725
}
724726

725727
Token &Scanner::peekNext() {

lib/TableGen/Main.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
8888
<< "': " << EC.message() << "\n";
8989
return 1;
9090
}
91-
MemoryBuffer *F = FileOrErr.get().release();
9291

9392
// Tell SrcMgr about this buffer, which is what TGParser will pick up.
94-
SrcMgr.AddNewSourceBuffer(F, SMLoc());
93+
SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
9594

9695
// Record the location of the include directory so that the lexer can find
9796
// it later.

tools/llvm-mc/llvm-mc.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,12 @@ int main(int argc, char **argv) {
373373
errs() << ProgName << ": " << EC.message() << '\n';
374374
return 1;
375375
}
376-
MemoryBuffer *Buffer = BufferPtr->release();
376+
MemoryBuffer *Buffer = BufferPtr->get();
377377

378378
SourceMgr SrcMgr;
379379

380380
// Tell SrcMgr about this buffer, which is what the parser will pick up.
381-
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
381+
SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
382382

383383
// Record the location of the include directories so that the lexer can find
384384
// it later.

tools/llvm-mcmarkup/llvm-mcmarkup.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,15 @@ static void parseMCMarkup(StringRef Filename) {
141141
errs() << ToolName << ": " << EC.message() << '\n';
142142
return;
143143
}
144-
MemoryBuffer *Buffer = BufferPtr->release();
144+
std::unique_ptr<MemoryBuffer> &Buffer = BufferPtr.get();
145145

146146
SourceMgr SrcMgr;
147147

148+
StringRef InputSource = Buffer->getBuffer();
149+
148150
// Tell SrcMgr about this buffer, which is what the parser will pick up.
149-
SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
151+
SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
150152

151-
StringRef InputSource = Buffer->getBuffer();
152153
MarkupLexer Lex(InputSource);
153154
MarkupParser Parser(Lex, SrcMgr);
154155

unittests/Support/SourceMgrTest.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class SourceMgrTest : public testing::Test {
2323
std::string Output;
2424

2525
void setMainBuffer(StringRef Text, StringRef BufferName) {
26-
MemoryBuffer *MainBuffer = MemoryBuffer::getMemBuffer(Text, BufferName);
27-
MainBufferID = SM.AddNewSourceBuffer(MainBuffer, llvm::SMLoc());
26+
std::unique_ptr<MemoryBuffer> MainBuffer(
27+
MemoryBuffer::getMemBuffer(Text, BufferName));
28+
MainBufferID = SM.AddNewSourceBuffer(std::move(MainBuffer), llvm::SMLoc());
2829
}
2930

3031
SMLoc getLoc(unsigned Offset) {

utils/FileCheck/FileCheck.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,9 @@ struct CheckString {
636636
///
637637
/// \param PreserveHorizontal Don't squash consecutive horizontal whitespace
638638
/// characters to a single space.
639-
static MemoryBuffer *CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB,
640-
bool PreserveHorizontal) {
639+
static std::unique_ptr<MemoryBuffer>
640+
CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB,
641+
bool PreserveHorizontal) {
641642
SmallString<128> NewFile;
642643
NewFile.reserve(MB->getBufferSize());
643644

@@ -662,8 +663,8 @@ static MemoryBuffer *CanonicalizeInputFile(std::unique_ptr<MemoryBuffer> MB,
662663
++Ptr;
663664
}
664665

665-
return MemoryBuffer::getMemBufferCopy(NewFile.str(),
666-
MB->getBufferIdentifier());
666+
return std::unique_ptr<MemoryBuffer>(
667+
MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier()));
667668
}
668669

669670
static bool IsPartOfWord(char c) {
@@ -838,25 +839,25 @@ static bool ReadCheckFile(SourceMgr &SM,
838839

839840
// If we want to canonicalize whitespace, strip excess whitespace from the
840841
// buffer containing the CHECK lines. Remove DOS style line endings.
841-
MemoryBuffer *F = CanonicalizeInputFile(std::move(FileOrErr.get()),
842-
NoCanonicalizeWhiteSpace);
843-
844-
SM.AddNewSourceBuffer(F, SMLoc());
842+
std::unique_ptr<MemoryBuffer> F =
843+
CanonicalizeInputFile(std::move(*FileOrErr), NoCanonicalizeWhiteSpace);
845844

846845
// Find all instances of CheckPrefix followed by : in the file.
847846
StringRef Buffer = F->getBuffer();
848847

848+
SM.AddNewSourceBuffer(std::move(F), SMLoc());
849+
849850
std::vector<Pattern> ImplicitNegativeChecks;
850851
for (const auto &PatternString : ImplicitCheckNot) {
851852
// Create a buffer with fake command line content in order to display the
852853
// command line option responsible for the specific implicit CHECK-NOT.
853854
std::string Prefix = std::string("-") + ImplicitCheckNot.ArgStr + "='";
854855
std::string Suffix = "'";
855-
MemoryBuffer *CmdLine = MemoryBuffer::getMemBufferCopy(
856-
Prefix + PatternString + Suffix, "command line");
856+
std::unique_ptr<MemoryBuffer> CmdLine(MemoryBuffer::getMemBufferCopy(
857+
Prefix + PatternString + Suffix, "command line"));
857858
StringRef PatternInBuffer =
858859
CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
859-
SM.AddNewSourceBuffer(CmdLine, SMLoc());
860+
SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
860861

861862
ImplicitNegativeChecks.push_back(Pattern(Check::CheckNot));
862863
ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer,
@@ -1272,18 +1273,18 @@ int main(int argc, char **argv) {
12721273

12731274
// Remove duplicate spaces in the input file if requested.
12741275
// Remove DOS style line endings.
1275-
MemoryBuffer *F =
1276-
CanonicalizeInputFile(std::move(File), NoCanonicalizeWhiteSpace);
1277-
1278-
SM.AddNewSourceBuffer(F, SMLoc());
1279-
1280-
/// VariableTable - This holds all the current filecheck variables.
1281-
StringMap<StringRef> VariableTable;
1276+
std::unique_ptr<MemoryBuffer> F =
1277+
CanonicalizeInputFile(std::move(File), NoCanonicalizeWhiteSpace);
12821278

12831279
// Check that we have all of the expected strings, in order, in the input
12841280
// file.
12851281
StringRef Buffer = F->getBuffer();
12861282

1283+
SM.AddNewSourceBuffer(std::move(F), SMLoc());
1284+
1285+
/// VariableTable - This holds all the current filecheck variables.
1286+
StringMap<StringRef> VariableTable;
1287+
12871288
bool hasError = false;
12881289

12891290
unsigned i = 0, j = 0, e = CheckStrings.size();

0 commit comments

Comments
 (0)