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
This commit is contained in:
Rafael Espindola 2014-08-27 20:03:13 +00:00
parent b2f71836eb
commit 1a7f705fba
16 changed files with 81 additions and 89 deletions

View File

@ -62,9 +62,8 @@ public:
OS.flush();
// Make the data accessible via the ObjectBuffer::Buffer
Buffer.reset(MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()),
"",
false));
Buffer =
MemoryBuffer::getMemBuffer(StringRef(SV.data(), SV.size()), "", false);
}
protected:

View File

@ -98,28 +98,29 @@ public:
/// Open the specified memory range as a MemoryBuffer. Note that InputData
/// must be null terminated if RequiresNullTerminator is true.
static MemoryBuffer *getMemBuffer(StringRef InputData,
StringRef BufferName = "",
bool RequiresNullTerminator = true);
static std::unique_ptr<MemoryBuffer>
getMemBuffer(StringRef InputData, StringRef BufferName = "",
bool RequiresNullTerminator = true);
static std::unique_ptr<MemoryBuffer>
getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator = true);
/// Open the specified memory range as a MemoryBuffer, copying the contents
/// and taking ownership of it. InputData does not have to be null terminated.
static MemoryBuffer *getMemBufferCopy(StringRef InputData,
StringRef BufferName = "");
static std::unique_ptr<MemoryBuffer>
getMemBufferCopy(StringRef InputData, StringRef BufferName = "");
/// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
/// that the caller need not initialize the memory allocated by this method.
/// The memory is owned by the MemoryBuffer object.
static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
static std::unique_ptr<MemoryBuffer>
getNewMemBuffer(size_t Size, StringRef BufferName = "");
/// Allocate a new MemoryBuffer of the specified size that is not initialized.
/// Note that the caller should initialize the memory allocated by this
/// method. The memory is owned by the MemoryBuffer object.
static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
StringRef BufferName = "");
static std::unique_ptr<MemoryBuffer>
getNewUninitMemBuffer(size_t Size, StringRef BufferName = "");
/// Read all of stdin into a file buffer, and return it.
static ErrorOr<std::unique_ptr<MemoryBuffer>> getSTDIN();

View File

@ -110,9 +110,11 @@ void AsmPrinter::EmitInlineAsm(StringRef Str, const MDNode *LocMDNode,
HasDiagHandler = true;
}
std::unique_ptr<MemoryBuffer> Buffer(
isNullTerminated ? MemoryBuffer::getMemBuffer(Str, "<inline asm>")
: MemoryBuffer::getMemBufferCopy(Str, "<inline asm>"));
std::unique_ptr<MemoryBuffer> Buffer;
if (isNullTerminated)
Buffer = MemoryBuffer::getMemBuffer(Str, "<inline asm>");
else
Buffer = MemoryBuffer::getMemBufferCopy(Str, "<inline asm>");
// Tell SrcMgr about this buffer, it takes ownership of the buffer.
SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());

View File

@ -2655,10 +2655,9 @@ LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
const char *BufferName,
LLVMBool RequiresNullTerminator) {
return wrap(MemoryBuffer::getMemBuffer(
StringRef(InputData, InputDataLength),
StringRef(BufferName),
RequiresNullTerminator));
return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
StringRef(BufferName),
RequiresNullTerminator).release());
}
LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
@ -2666,9 +2665,9 @@ LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
size_t InputDataLength,
const char *BufferName) {
return wrap(MemoryBuffer::getMemBufferCopy(
StringRef(InputData, InputDataLength),
StringRef(BufferName)));
return wrap(
MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
StringRef(BufferName)).release());
}
const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {

View File

@ -168,8 +168,7 @@ LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer,
std::unique_ptr<MemoryBuffer>
LTOModule::makeBuffer(const void *mem, size_t length, StringRef name) {
const char *startPtr = (const char*)mem;
return std::unique_ptr<MemoryBuffer>(
MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false));
return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
}
/// objcClassNameFromExpression - Get string that the data pointer points to.

View File

@ -2118,8 +2118,8 @@ bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
// instantiation.
OS << ".endmacro\n";
std::unique_ptr<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.
@ -4304,8 +4304,8 @@ void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
raw_svector_ostream &OS) {
OS << ".endr\n";
std::unique_ptr<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.

View File

@ -94,13 +94,12 @@ public:
};
}
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that InputData must be a null terminated if RequiresNullTerminator is true!
MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
StringRef BufferName,
bool RequiresNullTerminator) {
return new (NamedBufferAlloc(BufferName))
std::unique_ptr<MemoryBuffer>
MemoryBuffer::getMemBuffer(StringRef InputData, StringRef BufferName,
bool RequiresNullTerminator) {
auto *Ret = new (NamedBufferAlloc(BufferName))
MemoryBufferMem(InputData, RequiresNullTerminator);
return std::unique_ptr<MemoryBuffer>(Ret);
}
std::unique_ptr<MemoryBuffer>
@ -109,24 +108,19 @@ MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
Ref.getBuffer(), Ref.getBufferIdentifier(), RequiresNullTerminator));
}
/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
/// copying the contents and taking ownership of it. This has no requirements
/// on EndPtr[0].
MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
StringRef BufferName) {
MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
if (!Buf) return nullptr;
std::unique_ptr<MemoryBuffer>
MemoryBuffer::getMemBufferCopy(StringRef InputData, StringRef BufferName) {
std::unique_ptr<MemoryBuffer> Buf =
getNewUninitMemBuffer(InputData.size(), BufferName);
if (!Buf)
return nullptr;
memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
InputData.size());
return Buf;
}
/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
/// that is not initialized. Note that the caller should initialize the
/// memory allocated by this method. The memory is owned by the MemoryBuffer
/// object.
MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
StringRef BufferName) {
std::unique_ptr<MemoryBuffer>
MemoryBuffer::getNewUninitMemBuffer(size_t Size, StringRef BufferName) {
// Allocate space for the MemoryBuffer, the data and the name. It is important
// that MemoryBuffer and data are aligned so PointerIntPair works with them.
// TODO: Is 16-byte alignment enough? We copy small object files with large
@ -135,7 +129,8 @@ MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1, 16);
size_t RealLen = AlignedStringLen + Size + 1;
char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
if (!Mem) return nullptr;
if (!Mem)
return nullptr;
// The name is stored after the class itself.
CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
@ -144,15 +139,15 @@ MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
char *Buf = Mem + AlignedStringLen;
Buf[Size] = 0; // Null terminate buffer.
return new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
auto *Ret = new (Mem) MemoryBufferMem(StringRef(Buf, Size), true);
return std::unique_ptr<MemoryBuffer>(Ret);
}
/// getNewMemBuffer - Allocate a new zero-initialized MemoryBuffer of the
/// specified size. Note that the caller need not initialize the memory
/// allocated by this method. The memory is owned by the MemoryBuffer object.
MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
if (!SB) return nullptr;
std::unique_ptr<MemoryBuffer>
MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
std::unique_ptr<MemoryBuffer> SB = getNewUninitMemBuffer(Size, BufferName);
if (!SB)
return nullptr;
memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
return SB;
}
@ -226,9 +221,7 @@ getMemoryBufferForStream(int FD, StringRef BufferName) {
Buffer.set_size(Buffer.size() + ReadBytes);
} while (ReadBytes != 0);
std::unique_ptr<MemoryBuffer> Ret(
MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
return std::move(Ret);
return MemoryBuffer::getMemBufferCopy(Buffer, BufferName);
}
static ErrorOr<std::unique_ptr<MemoryBuffer>>
@ -360,15 +353,15 @@ getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize,
return std::move(Result);
}
MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
std::unique_ptr<MemoryBuffer> Buf =
MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
if (!Buf) {
// Failed to create a buffer. The only way it can fail is if
// new(std::nothrow) returns 0.
return make_error_code(errc::not_enough_memory);
}
std::unique_ptr<MemoryBuffer> SB(Buf);
char *BufPtr = const_cast<char*>(SB->getBufferStart());
char *BufPtr = const_cast<char *>(Buf->getBufferStart());
size_t BytesLeft = MapSize;
#ifndef HAVE_PREAD
@ -396,7 +389,7 @@ getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize,
BufPtr += NumRead;
}
return std::move(SB);
return std::move(Buf);
}
ErrorOr<std::unique_ptr<MemoryBuffer>>

View File

@ -276,8 +276,8 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
message(LDPL_ERROR, "Failed to get a view of %s", file->name);
return LDPS_ERR;
}
buffer.reset(MemoryBuffer::getMemBuffer(
StringRef((char *)view, file->filesize), "", false));
buffer = MemoryBuffer::getMemBuffer(StringRef((char *)view, file->filesize),
"", false);
} else {
int64_t offset = 0;
// Gold has found what might be IR part-way inside of a file, such as

View File

@ -294,8 +294,7 @@ public:
// because the file has probably just been mmapped. Instead we make
// a copy. The filed-based buffer will be released when it goes
// out of scope.
return std::unique_ptr<MemoryBuffer>(
MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer()));
return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
}
private:

View File

@ -51,8 +51,8 @@ static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
SmallString<1024> &Mem,
const char *Assembly) {
writeModuleToBuffer(parseAssembly(Assembly), Mem);
std::unique_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getMemBuffer(Mem.str(), "test", false));
std::unique_ptr<MemoryBuffer> Buffer =
MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
return std::unique_ptr<Module>(ModuleOrErr.get());
}

View File

@ -632,9 +632,9 @@ std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
ExecutionEngine *getJITFromBitcode(
LLVMContext &Context, const std::string &Bitcode, Module *&M) {
// c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
std::unique_ptr<MemoryBuffer> BitcodeBuffer(
MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test"));
ErrorOr<Module*> ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context);
std::unique_ptr<MemoryBuffer> BitcodeBuffer =
MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context);
if (std::error_code EC = ModuleOrErr.getError()) {
ADD_FAILURE() << EC.message();
return nullptr;

View File

@ -41,8 +41,7 @@ public:
return nullptr;
// Our test cache wants to maintain ownership of its object buffers
// so we make a copy here for the execution engine.
return std::unique_ptr<MemoryBuffer>(
MemoryBuffer::getMemBufferCopy(BufferFound->getBuffer()));
return MemoryBuffer::getMemBufferCopy(BufferFound->getBuffer());
}
// Test-harness-specific functions
@ -65,8 +64,8 @@ public:
private:
MemoryBuffer *copyBuffer(MemoryBufferRef Buf) {
// Create a local copy of the buffer.
std::unique_ptr<MemoryBuffer> NewBuffer(
MemoryBuffer::getMemBufferCopy(Buf.getBuffer()));
std::unique_ptr<MemoryBuffer> NewBuffer =
MemoryBuffer::getMemBufferCopy(Buf.getBuffer());
MemoryBuffer *Ret = NewBuffer.get();
AllocatedBuffers.push_back(std::move(NewBuffer));
return Ret;

View File

@ -95,19 +95,19 @@ TEST(LineIteratorTest, EmptyBuffers) {
EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
Buffer.reset(MemoryBuffer::getMemBuffer("\n\n\n"));
Buffer = MemoryBuffer::getMemBuffer("\n\n\n");
EXPECT_TRUE(line_iterator(*Buffer).is_at_eof());
EXPECT_EQ(line_iterator(), line_iterator(*Buffer));
Buffer.reset(MemoryBuffer::getMemBuffer("# foo\n"
"\n"
"# bar"));
Buffer = MemoryBuffer::getMemBuffer("# foo\n"
"\n"
"# bar");
EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof());
EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#'));
Buffer.reset(MemoryBuffer::getMemBuffer("\n"
"# baz\n"
"\n"));
Buffer = MemoryBuffer::getMemBuffer("\n"
"# baz\n"
"\n");
EXPECT_TRUE(line_iterator(*Buffer, '#').is_at_eof());
EXPECT_EQ(line_iterator(), line_iterator(*Buffer, '#'));
}

View File

@ -23,8 +23,8 @@ public:
std::string Output;
void setMainBuffer(StringRef Text, StringRef BufferName) {
std::unique_ptr<MemoryBuffer> MainBuffer(
MemoryBuffer::getMemBuffer(Text, BufferName));
std::unique_ptr<MemoryBuffer> MainBuffer =
MemoryBuffer::getMemBuffer(Text, BufferName);
MainBufferID = SM.AddNewSourceBuffer(std::move(MainBuffer), llvm::SMLoc());
}

View File

@ -210,8 +210,8 @@ TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
// When we construct a YAML stream over a named buffer,
// we get its ID as filename in diagnostics.
std::unique_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getMemBuffer("[]", "buffername.yaml"));
std::unique_ptr<MemoryBuffer> Buffer =
MemoryBuffer::getMemBuffer("[]", "buffername.yaml");
yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());

View File

@ -839,8 +839,8 @@ 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.
std::unique_ptr<MemoryBuffer> F =
CanonicalizeInputFile(std::move(*FileOrErr), NoCanonicalizeWhiteSpace);
std::unique_ptr<MemoryBuffer> F = CanonicalizeInputFile(
std::move(FileOrErr.get()), NoCanonicalizeWhiteSpace);
// Find all instances of CheckPrefix followed by : in the file.
StringRef Buffer = F->getBuffer();
@ -853,8 +853,9 @@ static bool ReadCheckFile(SourceMgr &SM,
// command line option responsible for the specific implicit CHECK-NOT.
std::string Prefix = std::string("-") + ImplicitCheckNot.ArgStr + "='";
std::string Suffix = "'";
std::unique_ptr<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(std::move(CmdLine), SMLoc());