llvm.org GIT mirror llvm / 95ca0fb
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 David Blaikie 5 years ago
12 changed file(s) with 55 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
1818 #include "llvm/ADT/ArrayRef.h"
1919 #include "llvm/ADT/StringRef.h"
2020 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/MemoryBuffer.h"
2122 #include "llvm/Support/SMLoc.h"
2223 #include
2324
2425 namespace llvm {
25 class MemoryBuffer;
2626 class SourceMgr;
2727 class SMDiagnostic;
2828 class SMFixIt;
4646 private:
4747 struct SrcBuffer {
4848 /// The memory buffer for the file.
49 MemoryBuffer *Buffer;
49 std::unique_ptr Buffer;
5050
5151 /// This is the location of the parent include, or null if at the top level.
5252 SMLoc IncludeLoc;
9595
9696 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
9797 assert(isValidBufferID(i));
98 return Buffers[i - 1].Buffer;
98 return Buffers[i - 1].Buffer.get();
9999 }
100100
101101 unsigned getNumBuffers() const {
114114
115115 /// Add a new source buffer to this source manager. This takes ownership of
116116 /// the memory buffer.
117 unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
117 unsigned AddNewSourceBuffer(std::unique_ptr F,
118 SMLoc IncludeLoc) {
118119 SrcBuffer NB;
119 NB.Buffer = F;
120 NB.Buffer = std::move(F);
120121 NB.IncludeLoc = IncludeLoc;
121 Buffers.push_back(NB);
122 Buffers.push_back(std::move(NB));
122123 return Buffers.size();
123124 }
124125
2424 SMDiagnostic &Err) {
2525 SourceMgr SM;
2626 StringRef Buf = F->getBuffer();
27 SM.AddNewSourceBuffer(F.release(), SMLoc());
27 SM.AddNewSourceBuffer(std::move(F), SMLoc());
2828
2929 return LLParser(Buf, SM, Err, &M).Run();
3030 }
109109 HasDiagHandler = true;
110110 }
111111
112 MemoryBuffer *Buffer;
113 if (isNullTerminated)
114 Buffer = MemoryBuffer::getMemBuffer(Str, "");
115 else
116 Buffer = MemoryBuffer::getMemBufferCopy(Str, "");
112 std::unique_ptr Buffer(
113 isNullTerminated ? MemoryBuffer::getMemBuffer(Str, "")
114 : MemoryBuffer::getMemBufferCopy(Str, ""));
117115
118116 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
119 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
117 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
120118
121119 std::unique_ptr Parser(
122120 createMCAsmParser(SrcMgr, OutContext, OutStreamer, *MAI));
21222122 // instantiation.
21232123 OS << ".endmacro\n";
21242124
2125 MemoryBuffer *Instantiation =
2126 MemoryBuffer::getMemBufferCopy(OS.str(), "");
2125 std::unique_ptr Instantiation(
2126 MemoryBuffer::getMemBufferCopy(OS.str(), ""));
21272127
21282128 // Create the macro instantiation object and add to the current macro
21292129 // instantiation stack.
21332133 ActiveMacros.push_back(MI);
21342134
21352135 // Jump to the macro instantiation and prime the lexer.
2136 CurBuffer = SrcMgr.AddNewSourceBuffer(Instantiation, SMLoc());
2136 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
21372137 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
21382138 Lex();
21392139
43094309 raw_svector_ostream &OS) {
43104310 OS << ".endr\n";
43114311
4312 MemoryBuffer *Instantiation =
4313 MemoryBuffer::getMemBufferCopy(OS.str(), "");
4312 std::unique_ptr Instantiation(
4313 MemoryBuffer::getMemBufferCopy(OS.str(), ""));
43144314
43154315 // Create the macro instantiation object and add to the current macro
43164316 // instantiation stack.
43204320 ActiveMacros.push_back(MI);
43214321
43224322 // Jump to the macro instantiation and prime the lexer.
4323 CurBuffer = SrcMgr.AddNewSourceBuffer(Instantiation, SMLoc());
4323 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
43244324 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
43254325 Lex();
43264326 }
7474
7575 std::unique_ptr Buffer(MemoryBuffer::getMemBuffer(InlineAsm));
7676 SourceMgr SrcMgr;
77 SrcMgr.AddNewSourceBuffer(Buffer.release(), SMLoc());
77 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
7878 std::unique_ptr Parser(
7979 createMCAsmParser(SrcMgr, MCCtx, *Streamer, *MAI));
8080
4141 // Delete the line # cache if allocated.
4242 if (LineNoCacheTy *Cache = getCache(LineNoCache))
4343 delete Cache;
44
45 while (!Buffers.empty()) {
46 delete Buffers.back().Buffer;
47 Buffers.pop_back();
48 }
4944 }
5045
5146 unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
6661 if (!NewBufOrErr)
6762 return 0;
6863
69 return AddNewSourceBuffer(NewBufOrErr.get().release(), IncludeLoc);
64 return AddNewSourceBuffer(std::move(*NewBufOrErr), IncludeLoc);
7065 }
7166
7267 unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const {
707707 , IsStartOfStream(true)
708708 , IsSimpleKeyAllowed(true)
709709 , Failed(false) {
710 InputBuffer = MemoryBuffer::getMemBuffer(Input, "YAML");
711 SM.AddNewSourceBuffer(InputBuffer, SMLoc());
710 std::unique_ptr InputBufferOwner(
711 MemoryBuffer::getMemBuffer(Input, "YAML"));
712 InputBuffer = InputBufferOwner.get();
713 SM.AddNewSourceBuffer(std::move(InputBufferOwner), SMLoc());
712714 Current = InputBuffer->getBufferStart();
713715 End = InputBuffer->getBufferEnd();
714716 }
718720 Current(InputBuffer->getBufferStart()), End(InputBuffer->getBufferEnd()),
719721 Indent(-1), Column(0), Line(0), FlowLevel(0), IsStartOfStream(true),
720722 IsSimpleKeyAllowed(true), Failed(false) {
721 SM.AddNewSourceBuffer(Buffer.release(), SMLoc());
723 SM.AddNewSourceBuffer(std::move(Buffer), SMLoc());
722724 }
723725
724726 Token &Scanner::peekNext() {
8787 << "': " << EC.message() << "\n";
8888 return 1;
8989 }
90 MemoryBuffer *F = FileOrErr.get().release();
9190
9291 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
93 SrcMgr.AddNewSourceBuffer(F, SMLoc());
92 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
9493
9594 // Record the location of the include directory so that the lexer can find
9695 // it later.
372372 errs() << ProgName << ": " << EC.message() << '\n';
373373 return 1;
374374 }
375 MemoryBuffer *Buffer = BufferPtr->release();
375 MemoryBuffer *Buffer = BufferPtr->get();
376376
377377 SourceMgr SrcMgr;
378378
379379 // Tell SrcMgr about this buffer, which is what the parser will pick up.
380 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
380 SrcMgr.AddNewSourceBuffer(std::move(*BufferPtr), SMLoc());
381381
382382 // Record the location of the include directories so that the lexer can find
383383 // it later.
140140 errs() << ToolName << ": " << EC.message() << '\n';
141141 return;
142142 }
143 MemoryBuffer *Buffer = BufferPtr->release();
143 std::unique_ptr &Buffer = BufferPtr.get();
144144
145145 SourceMgr SrcMgr;
146146
147 StringRef InputSource = Buffer->getBuffer();
148
147149 // Tell SrcMgr about this buffer, which is what the parser will pick up.
148 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
149
150 StringRef InputSource = Buffer->getBuffer();
150 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
151
151152 MarkupLexer Lex(InputSource);
152153 MarkupParser Parser(Lex, SrcMgr);
153154
2222 std::string Output;
2323
2424 void setMainBuffer(StringRef Text, StringRef BufferName) {
25 MemoryBuffer *MainBuffer = MemoryBuffer::getMemBuffer(Text, BufferName);
26 MainBufferID = SM.AddNewSourceBuffer(MainBuffer, llvm::SMLoc());
25 std::unique_ptr MainBuffer(
26 MemoryBuffer::getMemBuffer(Text, BufferName));
27 MainBufferID = SM.AddNewSourceBuffer(std::move(MainBuffer), llvm::SMLoc());
2728 }
2829
2930 SMLoc getLoc(unsigned Offset) {
635635 ///
636636 /// \param PreserveHorizontal Don't squash consecutive horizontal whitespace
637637 /// characters to a single space.
638 static MemoryBuffer *CanonicalizeInputFile(std::unique_ptr MB,
639 bool PreserveHorizontal) {
638 static std::unique_ptr
639 CanonicalizeInputFile(std::unique_ptr MB,
640 bool PreserveHorizontal) {
640641 SmallString<128> NewFile;
641642 NewFile.reserve(MB->getBufferSize());
642643
661662 ++Ptr;
662663 }
663664
664 return MemoryBuffer::getMemBufferCopy(NewFile.str(),
665 MB->getBufferIdentifier());
665 return std::unique_ptr(
666 MemoryBuffer::getMemBufferCopy(NewFile.str(), MB->getBufferIdentifier()));
666667 }
667668
668669 static bool IsPartOfWord(char c) {
837838
838839 // If we want to canonicalize whitespace, strip excess whitespace from the
839840 // buffer containing the CHECK lines. Remove DOS style line endings.
840 MemoryBuffer *F = CanonicalizeInputFile(std::move(FileOrErr.get()),
841 NoCanonicalizeWhiteSpace);
842
843 SM.AddNewSourceBuffer(F, SMLoc());
841 std::unique_ptr F =
842 CanonicalizeInputFile(std::move(*FileOrErr), NoCanonicalizeWhiteSpace);
844843
845844 // Find all instances of CheckPrefix followed by : in the file.
846845 StringRef Buffer = F->getBuffer();
846
847 SM.AddNewSourceBuffer(std::move(F), SMLoc());
847848
848849 std::vector ImplicitNegativeChecks;
849850 for (const auto &PatternString : ImplicitCheckNot) {
851852 // command line option responsible for the specific implicit CHECK-NOT.
852853 std::string Prefix = std::string("-") + ImplicitCheckNot.ArgStr + "='";
853854 std::string Suffix = "'";
854 MemoryBuffer *CmdLine = MemoryBuffer::getMemBufferCopy(
855 Prefix + PatternString + Suffix, "command line");
855 std::unique_ptr CmdLine(MemoryBuffer::getMemBufferCopy(
856 Prefix + PatternString + Suffix, "command line"));
856857 StringRef PatternInBuffer =
857858 CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
858 SM.AddNewSourceBuffer(CmdLine, SMLoc());
859 SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());
859860
860861 ImplicitNegativeChecks.push_back(Pattern(Check::CheckNot));
861862 ImplicitNegativeChecks.back().ParsePattern(PatternInBuffer,
12711272
12721273 // Remove duplicate spaces in the input file if requested.
12731274 // Remove DOS style line endings.
1274 MemoryBuffer *F =
1275 CanonicalizeInputFile(std::move(File), NoCanonicalizeWhiteSpace);
1276
1277 SM.AddNewSourceBuffer(F, SMLoc());
1278
1279 /// VariableTable - This holds all the current filecheck variables.
1280 StringMap VariableTable;
1275 std::unique_ptr F =
1276 CanonicalizeInputFile(std::move(File), NoCanonicalizeWhiteSpace);
12811277
12821278 // Check that we have all of the expected strings, in order, in the input
12831279 // file.
12841280 StringRef Buffer = F->getBuffer();
1281
1282 SM.AddNewSourceBuffer(std::move(F), SMLoc());
1283
1284 /// VariableTable - This holds all the current filecheck variables.
1285 StringMap VariableTable;
12851286
12861287 bool hasError = false;
12871288