llvm.org GIT mirror llvm / 1a7f705
Return a std::unique_ptr when creating a new MemoryBuffer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216583 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
16 changed file(s) with 83 addition(s) and 91 deletion(s). Raw diff Collapse all Expand all
6161 OS.flush();
6262
6363 // Make the data accessible via the ObjectBuffer::Buffer
64 Buffer.reset(MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()),
65 "",
66 false));
64 Buffer =
65 MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()), "", false);
6766 }
6867
6968 protected:
9797
9898 /// Open the specified memory range as a MemoryBuffer. Note that InputData
9999 /// must be null terminated if RequiresNullTerminator is true.
100 static MemoryBuffer *getMemBuffer(StringRef InputData,
101 StringRef BufferName = "",
102 bool RequiresNullTerminator = true);
100 static std::unique_ptr
101 getMemBuffer(StringRef InputData, StringRef BufferName = "",
102 bool RequiresNullTerminator = true);
103103
104104 static std::unique_ptr
105105 getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
106106
107107 /// Open the specified memory range as a MemoryBuffer, copying the contents
108108 /// and taking ownership of it. InputData does not have to be null terminated.
109 static MemoryBuffer *getMemBufferCopy(StringRef InputData,
110 StringRef BufferName = "");
109 static std::unique_ptr
110 getMemBufferCopy(StringRef InputData, StringRef BufferName = "");
111111
112112 /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
113113 /// that the caller need not initialize the memory allocated by this method.
114114 /// The memory is owned by the MemoryBuffer object.
115 static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
115 static std::unique_ptr
116 getNewMemBuffer(size_t Size, StringRef BufferName = "");
116117
117118 /// Allocate a new MemoryBuffer of the specified size that is not initialized.
118119 /// Note that the caller should initialize the memory allocated by this
119120 /// method. The memory is owned by the MemoryBuffer object.
120 static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
121 StringRef BufferName = "");
121 static std::unique_ptr
122 getNewUninitMemBuffer(size_t Size, StringRef BufferName = "");
122123
123124 /// Read all of stdin into a file buffer, and return it.
124125 static ErrorOr> getSTDIN();
109109 HasDiagHandler = true;
110110 }
111111
112 std::unique_ptr Buffer(
113 isNullTerminated ? MemoryBuffer::getMemBuffer(Str, "")
114 : MemoryBuffer::getMemBufferCopy(Str, ""));
112 std::unique_ptr Buffer;
113 if (isNullTerminated)
114 Buffer = MemoryBuffer::getMemBuffer(Str, "");
115 else
116 Buffer = MemoryBuffer::getMemBufferCopy(Str, "");
115117
116118 // Tell SrcMgr about this buffer, it takes ownership of the buffer.
117119 SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
26542654 const char *BufferName,
26552655 LLVMBool RequiresNullTerminator) {
26562656
2657 return wrap(MemoryBuffer::getMemBuffer(
2658 StringRef(InputData, InputDataLength),
2659 StringRef(BufferName),
2660 RequiresNullTerminator));
2657 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
2658 StringRef(BufferName),
2659 RequiresNullTerminator).release());
26612660 }
26622661
26632662 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
26652664 size_t InputDataLength,
26662665 const char *BufferName) {
26672666
2668 return wrap(MemoryBuffer::getMemBufferCopy(
2669 StringRef(InputData, InputDataLength),
2670 StringRef(BufferName)));
2667 return wrap(
2668 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
2669 StringRef(BufferName)).release());
26712670 }
26722671
26732672 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
167167 std::unique_ptr
168168 LTOModule::makeBuffer(const void *mem, size_t length, StringRef name) {
169169 const char *startPtr = (const char*)mem;
170 return std::unique_ptr(
171 MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false));
170 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
172171 }
173172
174173 /// objcClassNameFromExpression - Get string that the data pointer points to.
21172117 // instantiation.
21182118 OS << ".endmacro\n";
21192119
2120 std::unique_ptr Instantiation(
2121 MemoryBuffer::getMemBufferCopy(OS.str(), ""));
2120 std::unique_ptr Instantiation =
2121 MemoryBuffer::getMemBufferCopy(OS.str(), "");
21222122
21232123 // Create the macro instantiation object and add to the current macro
21242124 // instantiation stack.
43034303 raw_svector_ostream &OS) {
43044304 OS << ".endr\n";
43054305
4306 std::unique_ptr Instantiation(
4307 MemoryBuffer::getMemBufferCopy(OS.str(), ""));
4306 std::unique_ptr Instantiation =
4307 MemoryBuffer::getMemBufferCopy(OS.str(), "");
43084308
43094309 // Create the macro instantiation object and add to the current macro
43104310 // instantiation stack.
9393 };
9494 }
9595
96 /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
97 /// that InputData must be a null terminated if RequiresNullTerminator is true!
98 MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
99 StringRef BufferName,
100 bool RequiresNullTerminator) {
101 return new (NamedBufferAlloc(BufferName))
96 std::unique_ptr
97 MemoryBuffer::getMemBuffer(StringRef InputData, StringRef BufferName,
98 bool RequiresNullTerminator) {
99 auto *Ret = new (NamedBufferAlloc(BufferName))
102100 MemoryBufferMem(InputData, RequiresNullTerminator);
101 return std::unique_ptr(Ret);
103102 }
104103
105104 std::unique_ptr
108107 Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
109108 }
110109
111 /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
112 /// copying the contents and taking ownership of it. This has no requirements
113 /// on EndPtr[0].
114 MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
115 StringRef BufferName) {
116 MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
117 if (!Buf) return nullptr;
110 std::unique_ptr
111 MemoryBuffer::getMemBufferCopy(StringRef InputData, StringRef BufferName) {
112 std::unique_ptr Buf =
113 getNewUninitMemBuffer(InputData.size(), BufferName);
114 if (!Buf)
115 return nullptr;
118116 memcpy(const_cast(Buf->getBufferStart()), InputData.data(),
119117 InputData.size());
120118 return Buf;
121119 }
122120
123 /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
124 /// that is not initialized. Note that the caller should initialize the
125 /// memory allocated by this method. The memory is owned by the MemoryBuffer
126 /// object.
127 MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
128 StringRef BufferName) {
121 std::unique_ptr
122 MemoryBuffer::getNewUninitMemBuffer(size_t Size, StringRef BufferName) {
129123 // Allocate space for the MemoryBuffer, the data and the name. It is important
130124 // that MemoryBuffer and data are aligned so PointerIntPair works with them.
131125 // TODO: Is 16-byte alignment enough? We copy small object files with large
134128 RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 16);
135129 size_t RealLen = AlignedStringLen + Size + 1;
136130 char *Mem = static_cast(operator new(RealLen, std::nothrow));
137 if (!Mem) return nullptr;
131 if (!Mem)
132 return nullptr;
138133
139134 // The name is stored after the class itself.
140135 CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
143138 char *Buf = Mem + AlignedStringLen;
144139 Buf[Size] = 0; // Null terminate buffer.
145140
146 return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
147 }
148
149 /// getNewMemBuffer - Allocate a new zero-initialized MemoryBuffer of the
150 /// specified size. Note that the caller need not initialize the memory
151 /// allocated by this method. The memory is owned by the MemoryBuffer object.
152 MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
153 MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
154 if (!SB) return nullptr;
141 auto *Ret = new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
142 return std::unique_ptr(Ret);
143 }
144
145 std::unique_ptr
146 MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
147 std::unique_ptr SB = getNewUninitMemBuffer(Size, BufferName);
148 if (!SB)
149 return nullptr;
155150 memset(const_cast(SB->getBufferStart()), 0, Size);
156151 return SB;
157152 }
225220 Buffer.set_size(Buffer.size() + ReadBytes);
226221 } while (ReadBytes != 0);
227222
228 std::unique_ptr Ret(
229 MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
230 return std::move(Ret);
223 return MemoryBuffer::getMemBufferCopy(Buffer, BufferName);
231224 }
232225
233226 static ErrorOr>
359352 return std::move(Result);
360353 }
361354
362 MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
355 std::unique_ptr Buf =
356 MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
363357 if (!Buf) {
364358 // Failed to create a buffer. The only way it can fail is if
365359 // new(std::nothrow) returns 0.
366360 return make_error_code(errc::not_enough_memory);
367361 }
368362
369 std::unique_ptr SB(Buf);
370 char *BufPtr = const_cast*>(SB->getBufferStart());
363 char *BufPtr = const_cast *>(Buf->getBufferStart());
371364
372365 size_t BytesLeft = MapSize;
373366 #ifndef HAVE_PREAD
395388 BufPtr += NumRead;
396389 }
397390
398 return std::move(SB);
391 return std::move(Buf);
399392 }
400393
401394 ErrorOr>
275275 message(LDPL_ERROR, "Failed to get a view of %s", file->name);
276276 return LDPS_ERR;
277277 }
278 buffer.reset(MemoryBuffer::getMemBuffer(
279 StringRef((char *)view, file->filesize), "", false));
278 buffer = MemoryBuffer::getMemBuffer(StringRef((char *)view, file->filesize),
279 "", false);
280280 } else {
281281 int64_t offset = 0;
282282 // Gold has found what might be IR part-way inside of a file, such as
293293 // because the file has probably just been mmapped. Instead we make
294294 // a copy. The filed-based buffer will be released when it goes
295295 // out of scope.
296 return std::unique_ptr(
297 MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer()));
296 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
298297 }
299298
300299 private:
5050 SmallString<1024> &Mem,
5151 const char *Assembly) {
5252 writeModuleToBuffer(parseAssembly(Assembly), Mem);
53 std::unique_ptr Buffer(
54 MemoryBuffer::getMemBuffer(Mem.str(), "test", false));
53 std::unique_ptr Buffer =
54 MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
5555 ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
5656 return std::unique_ptr(ModuleOrErr.get());
5757 }
631631 ExecutionEngine *getJITFromBitcode(
632632 LLVMContext &Context, const std::string &Bitcode, Module *&M) {
633633 // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
634 std::unique_ptr BitcodeBuffer(
635 MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test"));
636 ErrorOr ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context);
634 std::unique_ptr BitcodeBuffer =
635 MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
636 ErrorOr ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context);
637637 if (std::error_code EC = ModuleOrErr.getError()) {
638638 ADD_FAILURE() << EC.message();
639639 return nullptr;
4040 return nullptr;
4141 // Our test cache wants to maintain ownership of its object buffers
4242 // so we make a copy here for the execution engine.
43 return std::unique_ptr(
44 MemoryBuffer::getMemBufferCopy(BufferFound->getBuffer()));
43 return MemoryBuffer::getMemBufferCopy(BufferFound->getBuffer());
4544 }
4645
4746 // Test-harness-specific functions
6463 private:
6564 MemoryBuffer *copyBuffer(MemoryBufferRef Buf) {
6665 // Create a local copy of the buffer.
67 std::unique_ptr NewBuffer(
68 MemoryBuffer::getMemBufferCopy(Buf.getBuffer()));
66 std::unique_ptr NewBuffer =
67 MemoryBuffer::getMemBufferCopy(Buf.getBuffer());
6968 MemoryBuffer *Ret = NewBuffer.get();
7069 AllocatedBuffers.push_back(std::move(NewBuffer));
7170 return Ret;
9494 EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
9595 EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
9696
97 Buffer.reset(MemoryBuffer::getMemBuffer("\n\n\n"));
97 Buffer = MemoryBuffer::getMemBuffer("\n\n\n");
9898 EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
9999 EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
100100
101 Buffer.reset(MemoryBuffer::getMemBuffer("# foo\n"
102 "\n"
103 "# bar"));
101 Buffer = MemoryBuffer::getMemBuffer("# foo\n"
102 "\n"
103 "# bar");
104104 EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof());
105105 EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#'));
106106
107 Buffer.reset(MemoryBuffer::getMemBuffer("\n"
108 "# baz\n"
109 "\n"));
107 Buffer = MemoryBuffer::getMemBuffer("\n"
108 "# baz\n"
109 "\n");
110110 EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof());
111111 EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#'));
112112 }
2222 std::string Output;
2323
2424 void setMainBuffer(StringRef Text, StringRef BufferName) {
25 std::unique_ptr MainBuffer(
26 MemoryBuffer::getMemBuffer(Text, BufferName));
25 std::unique_ptr MainBuffer =
26 MemoryBuffer::getMemBuffer(Text, BufferName);
2727 MainBufferID = SM.AddNewSourceBuffer(std::move(MainBuffer), llvm::SMLoc());
2828 }
2929
209209
210210 // When we construct a YAML stream over a named buffer,
211211 // we get its ID as filename in diagnostics.
212 std::unique_ptr Buffer(
213 MemoryBuffer::getMemBuffer("[]", "buffername.yaml"));
212 std::unique_ptr Buffer =
213 MemoryBuffer::getMemBuffer("[]", "buffername.yaml");
214214 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
215215 Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
216216 EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
838838
839839 // If we want to canonicalize whitespace, strip excess whitespace from the
840840 // buffer containing the CHECK lines. Remove DOS style line endings.
841 std::unique_ptr F =
842 CanonicalizeInputFile(std::move(*FileOrErr), NoCanonicalizeWhiteSpace);
841 std::unique_ptr F = CanonicalizeInputFile(
842 std::move(FileOrErr.get()), NoCanonicalizeWhiteSpace);
843843
844844 // Find all instances of CheckPrefix followed by : in the file.
845845 StringRef Buffer = F->getBuffer();
852852 // command line option responsible for the specific implicit CHECK-NOT.
853853 std::string Prefix = std::string("-") + ImplicitCheckNot.ArgStr + "='";
854854 std::string Suffix = "'";
855 std::unique_ptr CmdLine(MemoryBuffer::getMemBufferCopy(
856 Prefix + PatternString + Suffix, "command line"));
855 std::unique_ptr CmdLine = MemoryBuffer::getMemBufferCopy(
856 Prefix + PatternString + Suffix, "command line");
857
857858 StringRef PatternInBuffer =
858859 CmdLine->getBuffer().substr(Prefix.size(), PatternString.size());
859860 SM.AddNewSourceBuffer(std::move(CmdLine), SMLoc());