[C++11] Add overloads for externally used OwningPtr functions.

This will allow external callers of these functions to switch over time
rather than forcing a breaking change all a once. These particular
functions were determined by building clang/lld/lldb.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202959 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ahmed Charles 2014-03-05 10:27:34 +00:00
parent 2e816f0d56
commit fbf6daaec6
9 changed files with 150 additions and 35 deletions

View File

@ -123,6 +123,11 @@ public:
void *DisInfo,
MCContext *Ctx,
OwningPtr<MCRelocationInfo> &RelInfo);
void setupForSymbolicDisassembly(LLVMOpInfoCallback GetOpInfo,
LLVMSymbolLookupCallback SymbolLookUp,
void *DisInfo,
MCContext *Ctx,
std::unique_ptr<MCRelocationInfo> &RelInfo);
LLVMOpInfoCallback getLLVMOpInfoCallback() const { return GetOpInfo; }
LLVMSymbolLookupCallback getLLVMSymbolLookupCallback() const {

View File

@ -91,9 +91,13 @@ public:
error_code getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
bool FullPath = false) const;
error_code getMemoryBuffer(std::unique_ptr<MemoryBuffer> &Result,
bool FullPath = false) const;
error_code getAsBinary(OwningPtr<Binary> &Result,
LLVMContext *Context = 0) const;
error_code getAsBinary(std::unique_ptr<Binary> &Result,
LLVMContext *Context = 0) const;
};
class child_iterator {

View File

@ -43,6 +43,9 @@ public:
static error_code create(StringRef FilePath, size_t Size,
OwningPtr<FileOutputBuffer> &Result,
unsigned Flags = 0);
static error_code create(StringRef FilePath, size_t Size,
std::unique_ptr<FileOutputBuffer> &Result,
unsigned Flags = 0);
/// Returns a pointer to the start of the buffer.
uint8_t *getBufferStart() {
@ -83,7 +86,7 @@ private:
FileOutputBuffer(llvm::sys::fs::mapped_file_region *R,
StringRef Path, StringRef TempPath);
OwningPtr<llvm::sys::fs::mapped_file_region> Region;
std::unique_ptr<llvm::sys::fs::mapped_file_region> Region;
SmallString<128> FinalPath;
SmallString<128> TempPath;
};

View File

@ -19,6 +19,7 @@
#include "llvm/Support/CBindingWrapping.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
#include <memory>
namespace llvm {
@ -66,7 +67,11 @@ public:
/// MemoryBuffer if successful, otherwise returning null. If FileSize is
/// specified, this means that the client knows that the file exists and that
/// it has the specified size.
static error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &result,
static error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &Result,
int64_t FileSize = -1,
bool RequiresNullTerminator = true);
static error_code getFile(Twine Filename,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize = -1,
bool RequiresNullTerminator = true);
@ -76,6 +81,9 @@ public:
static error_code getOpenFileSlice(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset);
static error_code getOpenFileSlice(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset);
/// Given an already-open file descriptor, read the file and return a
/// MemoryBuffer.
@ -83,6 +91,10 @@ public:
OwningPtr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator = true);
static error_code getOpenFile(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator = true);
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that InputData must be null terminated if RequiresNullTerminator is true.
@ -111,14 +123,18 @@ public:
/// getSTDIN - Read all of stdin into a file buffer, and return it.
/// If an error occurs, this returns null and sets ec.
static error_code getSTDIN(OwningPtr<MemoryBuffer> &result);
static error_code getSTDIN(OwningPtr<MemoryBuffer> &Result);
static error_code getSTDIN(std::unique_ptr<MemoryBuffer> &Result);
/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
/// if the Filename is "-". If an error occurs, this returns null and sets
/// ec.
static error_code getFileOrSTDIN(StringRef Filename,
OwningPtr<MemoryBuffer> &result,
OwningPtr<MemoryBuffer> &Result,
int64_t FileSize = -1);
static error_code getFileOrSTDIN(StringRef Filename,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize = -1);
//===--------------------------------------------------------------------===//

View File

@ -33,6 +33,18 @@ MCDisassembler::setupForSymbolicDisassembly(
SymbolLookUp, DisInfo));
}
void
MCDisassembler::setupForSymbolicDisassembly(
LLVMOpInfoCallback GetOpInfo,
LLVMSymbolLookupCallback SymbolLookUp,
void *DisInfo,
MCContext *Ctx,
std::unique_ptr<MCRelocationInfo> &RelInfo) {
OwningPtr<MCRelocationInfo> MCRI;
setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx, MCRI);
RelInfo = MCRI.take_unique();
}
bool MCDisassembler::tryAddingSymbolicOperand(MCInst &Inst, int64_t Value,
uint64_t Address, bool IsBranch,
uint64_t Offset,

View File

@ -13,6 +13,7 @@
#include "llvm/Object/Archive.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Endian.h"
@ -168,7 +169,7 @@ error_code Archive::Child::getName(StringRef &Result) const {
return object_error::success;
}
error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
error_code Archive::Child::getMemoryBuffer(std::unique_ptr<MemoryBuffer> &Result,
bool FullPath) const {
StringRef Name;
if (error_code ec = getName(Name))
@ -182,25 +183,41 @@ error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
return error_code::success();
}
error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result,
error_code Archive::Child::getMemoryBuffer(OwningPtr<MemoryBuffer> &Result,
bool FullPath) const {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getMemoryBuffer(MB, FullPath);
Result = std::move(MB);
return ec;
}
error_code Archive::Child::getAsBinary(std::unique_ptr<Binary> &Result,
LLVMContext *Context) const {
OwningPtr<Binary> ret;
OwningPtr<MemoryBuffer> Buff;
std::unique_ptr<Binary> ret;
std::unique_ptr<MemoryBuffer> Buff;
if (error_code ec = getMemoryBuffer(Buff))
return ec;
ErrorOr<Binary *> BinaryOrErr = createBinary(Buff.take(), Context);
ErrorOr<Binary *> BinaryOrErr = createBinary(Buff.release(), Context);
if (error_code EC = BinaryOrErr.getError())
return EC;
Result.reset(BinaryOrErr.get());
return object_error::success;
}
error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result,
LLVMContext *Context) const {
std::unique_ptr<Binary> B;
error_code ec = getAsBinary(B, Context);
Result = std::move(B);
return ec;
}
ErrorOr<Archive*> Archive::create(MemoryBuffer *Source) {
error_code EC;
OwningPtr<Archive> Ret(new Archive(Source, EC));
std::unique_ptr<Archive> Ret(new Archive(Source, EC));
if (EC)
return EC;
return Ret.take();
return Ret.release();
}
Archive::Archive(MemoryBuffer *source, error_code &ec)

View File

@ -33,7 +33,7 @@ FileOutputBuffer::~FileOutputBuffer() {
error_code FileOutputBuffer::create(StringRef FilePath,
size_t Size,
OwningPtr<FileOutputBuffer> &Result,
std::unique_ptr<FileOutputBuffer> &Result,
unsigned Flags) {
// If file already exists, it must be a regular file (to be mappable).
sys::fs::file_status Stat;
@ -73,18 +73,28 @@ error_code FileOutputBuffer::create(StringRef FilePath,
if (EC)
return EC;
OwningPtr<mapped_file_region> MappedFile(new mapped_file_region(
std::unique_ptr<mapped_file_region> MappedFile(new mapped_file_region(
FD, true, mapped_file_region::readwrite, Size, 0, EC));
if (EC)
return EC;
Result.reset(new FileOutputBuffer(MappedFile.get(), FilePath, TempFilePath));
if (Result)
MappedFile.take();
MappedFile.release();
return error_code::success();
}
error_code FileOutputBuffer::create(StringRef FilePath,
size_t Size,
OwningPtr<FileOutputBuffer> &Result,
unsigned Flags) {
std::unique_ptr<FileOutputBuffer> FOB;
error_code ec = create(FilePath, Size, FOB, Flags);
Result = std::move(FOB);
return ec;
}
error_code FileOutputBuffer::commit(int64_t NewSmallerSize) {
// Unmap buffer, letting OS flush dirty pages to file on disk.
Region.reset(0);

View File

@ -166,13 +166,23 @@ MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
/// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN)
/// returns an empty buffer.
error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
OwningPtr<MemoryBuffer> &result,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize) {
if (Filename == "-")
return getSTDIN(result);
return getFile(Filename, result, FileSize);
return getSTDIN(Result);
return getFile(Filename, Result, FileSize);
}
error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
OwningPtr<MemoryBuffer> &Result,
int64_t FileSize) {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getFileOrSTDIN(Filename, MB, FileSize);
Result = std::move(MB);
return ec;
}
//===----------------------------------------------------------------------===//
// MemoryBuffer::getFile implementation.
//===----------------------------------------------------------------------===//
@ -220,7 +230,7 @@ public:
static error_code getMemoryBufferForStream(int FD,
StringRef BufferName,
OwningPtr<MemoryBuffer> &result) {
std::unique_ptr<MemoryBuffer> &Result) {
const ssize_t ChunkSize = 4096*4;
SmallString<ChunkSize> Buffer;
ssize_t ReadBytes;
@ -235,39 +245,50 @@ static error_code getMemoryBufferForStream(int FD,
Buffer.set_size(Buffer.size() + ReadBytes);
} while (ReadBytes != 0);
result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
Result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
return error_code::success();
}
static error_code getFileAux(const char *Filename,
OwningPtr<MemoryBuffer> &result, int64_t FileSize,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize,
bool RequiresNullTerminator);
error_code MemoryBuffer::getFile(Twine Filename,
OwningPtr<MemoryBuffer> &result,
std::unique_ptr<MemoryBuffer> &Result,
int64_t FileSize,
bool RequiresNullTerminator) {
// Ensure the path is null terminated.
SmallString<256> PathBuf;
StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf);
return getFileAux(NullTerminatedName.data(), result, FileSize,
return getFileAux(NullTerminatedName.data(), Result, FileSize,
RequiresNullTerminator);
}
error_code MemoryBuffer::getFile(Twine Filename,
OwningPtr<MemoryBuffer> &Result,
int64_t FileSize,
bool RequiresNullTerminator) {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getFile(Filename, MB, FileSize, RequiresNullTerminator);
Result = std::move(MB);
return ec;
}
static error_code getOpenFileImpl(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize, uint64_t MapSize,
int64_t Offset, bool RequiresNullTerminator);
static error_code getFileAux(const char *Filename,
OwningPtr<MemoryBuffer> &result, int64_t FileSize,
std::unique_ptr<MemoryBuffer> &Result, int64_t FileSize,
bool RequiresNullTerminator) {
int FD;
error_code EC = sys::fs::openFileForRead(Filename, FD);
if (EC)
return EC;
error_code ret = getOpenFileImpl(FD, Filename, result, FileSize, FileSize, 0,
error_code ret = getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
RequiresNullTerminator);
close(FD);
return ret;
@ -325,7 +346,7 @@ static bool shouldUseMmap(int FD,
}
static error_code getOpenFileImpl(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &result,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize, uint64_t MapSize,
int64_t Offset, bool RequiresNullTerminator) {
static int PageSize = sys::process::get_self()->page_size();
@ -346,7 +367,7 @@ static error_code getOpenFileImpl(int FD, const char *Filename,
sys::fs::file_type Type = Status.type();
if (Type != sys::fs::file_type::regular_file &&
Type != sys::fs::file_type::block_file)
return getMemoryBufferForStream(FD, Filename, result);
return getMemoryBufferForStream(FD, Filename, Result);
FileSize = Status.getSize();
}
@ -356,7 +377,7 @@ static error_code getOpenFileImpl(int FD, const char *Filename,
if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator,
PageSize)) {
error_code EC;
result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile(
RequiresNullTerminator, FD, MapSize, Offset, EC));
if (!EC)
return error_code::success();
@ -369,7 +390,7 @@ static error_code getOpenFileImpl(int FD, const char *Filename,
return make_error_code(errc::not_enough_memory);
}
OwningPtr<MemoryBuffer> SB(Buf);
std::unique_ptr<MemoryBuffer> SB(Buf);
char *BufPtr = const_cast<char*>(SB->getBufferStart());
size_t BytesLeft = MapSize;
@ -400,34 +421,61 @@ static error_code getOpenFileImpl(int FD, const char *Filename,
BufPtr += NumRead;
}
result.swap(SB);
Result.swap(SB);
return error_code::success();
}
error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator) {
return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
RequiresNullTerminator);
}
error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t FileSize,
bool RequiresNullTerminator) {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getOpenFileImpl(FD, Filename, MB, FileSize, FileSize, 0,
RequiresNullTerminator);
Result = std::move(MB);
return ec;
}
error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
std::unique_ptr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset) {
return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false);
}
error_code MemoryBuffer::getOpenFileSlice(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &Result,
uint64_t MapSize, int64_t Offset) {
return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false);
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getOpenFileImpl(FD, Filename, MB, -1, MapSize, Offset, false);
Result = std::move(MB);
return ec;
}
//===----------------------------------------------------------------------===//
// MemoryBuffer::getSTDIN implementation.
//===----------------------------------------------------------------------===//
error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) {
error_code MemoryBuffer::getSTDIN(std::unique_ptr<MemoryBuffer> &Result) {
// Read in all of the data from stdin, we cannot mmap stdin.
//
// FIXME: That isn't necessarily true, we should try to mmap stdin and
// fallback if it fails.
sys::ChangeStdinToBinary();
return getMemoryBufferForStream(0, "<stdin>", result);
return getMemoryBufferForStream(0, "<stdin>", Result);
}
error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &Result) {
std::unique_ptr<MemoryBuffer> MB;
error_code ec = getSTDIN(MB);
Result = std::move(MB);
return ec;
}

View File

@ -78,7 +78,7 @@ TEST_F(MemoryBufferTest, NullTerminator4K) {
}
OF.close();
OwningPtr<MemoryBuffer> MB;
OwningBuffer MB;
error_code EC = MemoryBuffer::getFile(TestPath.c_str(), MB);
ASSERT_FALSE(EC);