diff --git a/include/llvm/MC/MCDisassembler.h b/include/llvm/MC/MCDisassembler.h index 8fd64df2e96..f82ebca525c 100644 --- a/include/llvm/MC/MCDisassembler.h +++ b/include/llvm/MC/MCDisassembler.h @@ -123,6 +123,11 @@ public: void *DisInfo, MCContext *Ctx, OwningPtr &RelInfo); + void setupForSymbolicDisassembly(LLVMOpInfoCallback GetOpInfo, + LLVMSymbolLookupCallback SymbolLookUp, + void *DisInfo, + MCContext *Ctx, + std::unique_ptr &RelInfo); LLVMOpInfoCallback getLLVMOpInfoCallback() const { return GetOpInfo; } LLVMSymbolLookupCallback getLLVMSymbolLookupCallback() const { diff --git a/include/llvm/Object/Archive.h b/include/llvm/Object/Archive.h index 2ba7bf23ac1..4fae76fdd52 100644 --- a/include/llvm/Object/Archive.h +++ b/include/llvm/Object/Archive.h @@ -91,9 +91,13 @@ public: error_code getMemoryBuffer(OwningPtr &Result, bool FullPath = false) const; + error_code getMemoryBuffer(std::unique_ptr &Result, + bool FullPath = false) const; error_code getAsBinary(OwningPtr &Result, LLVMContext *Context = 0) const; + error_code getAsBinary(std::unique_ptr &Result, + LLVMContext *Context = 0) const; }; class child_iterator { diff --git a/include/llvm/Support/FileOutputBuffer.h b/include/llvm/Support/FileOutputBuffer.h index cbc9c467d23..1884a242b3e 100644 --- a/include/llvm/Support/FileOutputBuffer.h +++ b/include/llvm/Support/FileOutputBuffer.h @@ -43,6 +43,9 @@ public: static error_code create(StringRef FilePath, size_t Size, OwningPtr &Result, unsigned Flags = 0); + static error_code create(StringRef FilePath, size_t Size, + std::unique_ptr &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 Region; + std::unique_ptr Region; SmallString<128> FinalPath; SmallString<128> TempPath; }; diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h index ea10a70f096..1910d44240a 100644 --- a/include/llvm/Support/MemoryBuffer.h +++ b/include/llvm/Support/MemoryBuffer.h @@ -19,6 +19,7 @@ #include "llvm/Support/CBindingWrapping.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/DataTypes.h" +#include 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 &result, + static error_code getFile(Twine Filename, OwningPtr &Result, + int64_t FileSize = -1, + bool RequiresNullTerminator = true); + static error_code getFile(Twine Filename, + std::unique_ptr &Result, int64_t FileSize = -1, bool RequiresNullTerminator = true); @@ -76,6 +81,9 @@ public: static error_code getOpenFileSlice(int FD, const char *Filename, OwningPtr &Result, uint64_t MapSize, int64_t Offset); + static error_code getOpenFileSlice(int FD, const char *Filename, + std::unique_ptr &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 &Result, uint64_t FileSize, bool RequiresNullTerminator = true); + static error_code getOpenFile(int FD, const char *Filename, + std::unique_ptr &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 &result); + static error_code getSTDIN(OwningPtr &Result); + static error_code getSTDIN(std::unique_ptr &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 &result, + OwningPtr &Result, + int64_t FileSize = -1); + static error_code getFileOrSTDIN(StringRef Filename, + std::unique_ptr &Result, int64_t FileSize = -1); //===--------------------------------------------------------------------===// diff --git a/lib/MC/MCDisassembler.cpp b/lib/MC/MCDisassembler.cpp index 7c29fd36a3b..e365c37c32b 100644 --- a/lib/MC/MCDisassembler.cpp +++ b/lib/MC/MCDisassembler.cpp @@ -33,6 +33,18 @@ MCDisassembler::setupForSymbolicDisassembly( SymbolLookUp, DisInfo)); } +void +MCDisassembler::setupForSymbolicDisassembly( + LLVMOpInfoCallback GetOpInfo, + LLVMSymbolLookupCallback SymbolLookUp, + void *DisInfo, + MCContext *Ctx, + std::unique_ptr &RelInfo) { + OwningPtr 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, diff --git a/lib/Object/Archive.cpp b/lib/Object/Archive.cpp index 99bc4a5a3b3..999bf28bb38 100644 --- a/lib/Object/Archive.cpp +++ b/lib/Object/Archive.cpp @@ -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 &Result, +error_code Archive::Child::getMemoryBuffer(std::unique_ptr &Result, bool FullPath) const { StringRef Name; if (error_code ec = getName(Name)) @@ -182,25 +183,41 @@ error_code Archive::Child::getMemoryBuffer(OwningPtr &Result, return error_code::success(); } -error_code Archive::Child::getAsBinary(OwningPtr &Result, +error_code Archive::Child::getMemoryBuffer(OwningPtr &Result, + bool FullPath) const { + std::unique_ptr MB; + error_code ec = getMemoryBuffer(MB, FullPath); + Result = std::move(MB); + return ec; +} + +error_code Archive::Child::getAsBinary(std::unique_ptr &Result, LLVMContext *Context) const { - OwningPtr ret; - OwningPtr Buff; + std::unique_ptr ret; + std::unique_ptr Buff; if (error_code ec = getMemoryBuffer(Buff)) return ec; - ErrorOr BinaryOrErr = createBinary(Buff.take(), Context); + ErrorOr 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 &Result, + LLVMContext *Context) const { + std::unique_ptr B; + error_code ec = getAsBinary(B, Context); + Result = std::move(B); + return ec; +} + ErrorOr Archive::create(MemoryBuffer *Source) { error_code EC; - OwningPtr Ret(new Archive(Source, EC)); + std::unique_ptr Ret(new Archive(Source, EC)); if (EC) return EC; - return Ret.take(); + return Ret.release(); } Archive::Archive(MemoryBuffer *source, error_code &ec) diff --git a/lib/Support/FileOutputBuffer.cpp b/lib/Support/FileOutputBuffer.cpp index c01778f9600..8f2c9fcce08 100644 --- a/lib/Support/FileOutputBuffer.cpp +++ b/lib/Support/FileOutputBuffer.cpp @@ -33,7 +33,7 @@ FileOutputBuffer::~FileOutputBuffer() { error_code FileOutputBuffer::create(StringRef FilePath, size_t Size, - OwningPtr &Result, + std::unique_ptr &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 MappedFile(new mapped_file_region( + std::unique_ptr 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 &Result, + unsigned Flags) { + std::unique_ptr 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); diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index ecae7695e1f..c7f76d3d7e7 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -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 &result, + std::unique_ptr &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 &Result, + int64_t FileSize) { + std::unique_ptr 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 &result) { + std::unique_ptr &Result) { const ssize_t ChunkSize = 4096*4; SmallString 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 &result, int64_t FileSize, + std::unique_ptr &Result, + int64_t FileSize, bool RequiresNullTerminator); error_code MemoryBuffer::getFile(Twine Filename, - OwningPtr &result, + std::unique_ptr &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 &Result, + int64_t FileSize, + bool RequiresNullTerminator) { + std::unique_ptr 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 &Result, + std::unique_ptr &Result, uint64_t FileSize, uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator); static error_code getFileAux(const char *Filename, - OwningPtr &result, int64_t FileSize, + std::unique_ptr &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 &result, + std::unique_ptr &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 SB(Buf); + std::unique_ptr SB(Buf); char *BufPtr = const_cast(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 &Result, + std::unique_ptr &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 &Result, + uint64_t FileSize, + bool RequiresNullTerminator) { + std::unique_ptr 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 &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 &Result, uint64_t MapSize, int64_t Offset) { - return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false); + std::unique_ptr 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 &result) { +error_code MemoryBuffer::getSTDIN(std::unique_ptr &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, "", result); + return getMemoryBufferForStream(0, "", Result); +} + +error_code MemoryBuffer::getSTDIN(OwningPtr &Result) { + std::unique_ptr MB; + error_code ec = getSTDIN(MB); + Result = std::move(MB); + return ec; } diff --git a/unittests/Support/MemoryBufferTest.cpp b/unittests/Support/MemoryBufferTest.cpp index 5bd8ee673e2..43b7e0deeb6 100644 --- a/unittests/Support/MemoryBufferTest.cpp +++ b/unittests/Support/MemoryBufferTest.cpp @@ -78,7 +78,7 @@ TEST_F(MemoryBufferTest, NullTerminator4K) { } OF.close(); - OwningPtr MB; + OwningBuffer MB; error_code EC = MemoryBuffer::getFile(TestPath.c_str(), MB); ASSERT_FALSE(EC);