diff --git a/include/llvm/Support/MemoryBuffer.h b/include/llvm/Support/MemoryBuffer.h index 8c742c686b1..147be47e1c8 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 "llvm/Support/ErrorOr.h" #include #include @@ -60,19 +61,17 @@ public: return "Unknown buffer"; } - /// getFile - Open the specified file as a MemoryBuffer, returning a new - /// 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. + /// Open the specified file as a MemoryBuffer, returning a new 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. /// /// \param IsVolatileSize Set to true to indicate that the file size may be /// changing, e.g. when libclang tries to parse while the user is /// editing/updating the file. - static std::error_code getFile(Twine Filename, - std::unique_ptr &Result, - int64_t FileSize = -1, - bool RequiresNullTerminator = true, - bool IsVolatileSize = false); + static ErrorOr> + getFile(Twine Filename, int64_t FileSize = -1, + bool RequiresNullTerminator = true, bool IsVolatileSize = false); /// Given an already-open file descriptor, map some slice of it into a /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize. @@ -81,10 +80,9 @@ public: /// \param IsVolatileSize Set to true to indicate that the file size may be /// changing, e.g. when libclang tries to parse while the user is /// editing/updating the file. - static std::error_code getOpenFileSlice(int FD, const char *Filename, - std::unique_ptr &Result, - uint64_t MapSize, int64_t Offset, - bool IsVolatileSize = false); + static ErrorOr> + getOpenFileSlice(int FD, const char *Filename, uint64_t MapSize, + int64_t Offset, bool IsVolatileSize = false); /// Given an already-open file descriptor, read the file and return a /// MemoryBuffer. @@ -92,11 +90,9 @@ public: /// \param IsVolatileSize Set to true to indicate that the file size may be /// changing, e.g. when libclang tries to parse while the user is /// editing/updating the file. - static std::error_code getOpenFile(int FD, const char *Filename, - std::unique_ptr &Result, - uint64_t FileSize, - bool RequiresNullTerminator = true, - bool IsVolatileSize = false); + static ErrorOr> + getOpenFile(int FD, const char *Filename, uint64_t FileSize, + bool RequiresNullTerminator = true, bool IsVolatileSize = false); /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note /// that InputData must be null terminated if RequiresNullTerminator is true. @@ -123,16 +119,13 @@ public: static MemoryBuffer *getNewUninitMemBuffer(size_t Size, StringRef BufferName = ""); - /// getSTDIN - Read all of stdin into a file buffer, and return it. - /// If an error occurs, this returns null and sets ec. - static std::error_code getSTDIN(std::unique_ptr &Result); + /// Read all of stdin into a file buffer, and return it. + static ErrorOr> getSTDIN(); - /// 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 std::error_code getFileOrSTDIN(StringRef Filename, - std::unique_ptr &Result, - int64_t FileSize = -1); + /// Open the specified file as a MemoryBuffer, or open stdin if the Filename + /// is "-". + static ErrorOr> + getFileOrSTDIN(StringRef Filename, int64_t FileSize = -1); //===--------------------------------------------------------------------===// // Provided for performance analysis. diff --git a/lib/AsmParser/Parser.cpp b/lib/AsmParser/Parser.cpp index 2138fde5eb2..91bb51c2422 100644 --- a/lib/AsmParser/Parser.cpp +++ b/lib/AsmParser/Parser.cpp @@ -41,14 +41,15 @@ Module *llvm::ParseAssembly(MemoryBuffer *F, Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err, LLVMContext &Context) { - std::unique_ptr File; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) { + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(Filename); + if (std::error_code EC = FileOrErr.getError()) { Err = SMDiagnostic(Filename, SourceMgr::DK_Error, - "Could not open input file: " + ec.message()); + "Could not open input file: " + EC.message()); return nullptr; } - return ParseAssembly(File.release(), nullptr, Err, Context); + return ParseAssembly(FileOrErr.get().release(), nullptr, Err, Context); } Module *llvm::ParseAssemblyString(const char *AsmString, Module *M, diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 0cb781c2c6b..87099a6c4e1 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -2606,28 +2606,24 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( LLVMMemoryBufferRef *OutMemBuf, char **OutMessage) { - std::unique_ptr MB; - std::error_code ec; - if (!(ec = MemoryBuffer::getFile(Path, MB))) { - *OutMemBuf = wrap(MB.release()); - return 0; + ErrorOr> MBOrErr = MemoryBuffer::getFile(Path); + if (std::error_code EC = MBOrErr.getError()) { + *OutMessage = strdup(EC.message().c_str()); + return 1; } - - *OutMessage = strdup(ec.message().c_str()); - return 1; + *OutMemBuf = wrap(MBOrErr.get().release()); + return 0; } LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, char **OutMessage) { - std::unique_ptr MB; - std::error_code ec; - if (!(ec = MemoryBuffer::getSTDIN(MB))) { - *OutMemBuf = wrap(MB.release()); - return 0; + ErrorOr> MBOrErr = MemoryBuffer::getSTDIN(); + if (std::error_code EC = MBOrErr.getError()) { + *OutMessage = strdup(EC.message().c_str()); + return 1; } - - *OutMessage = strdup(ec.message().c_str()); - return 1; + *OutMemBuf = wrap(MBOrErr.get().release()); + return 0; } LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( diff --git a/lib/IR/GCOV.cpp b/lib/IR/GCOV.cpp index b4c7977d52e..1667401f88d 100644 --- a/lib/IR/GCOV.cpp +++ b/lib/IR/GCOV.cpp @@ -438,11 +438,15 @@ class LineConsumer { StringRef Remaining; public: LineConsumer(StringRef Filename) { - if (std::error_code EC = MemoryBuffer::getFileOrSTDIN(Filename, Buffer)) { + ErrorOr> BufferOrErr = + MemoryBuffer::getFileOrSTDIN(Filename); + if (std::error_code EC = BufferOrErr.getError()) { errs() << Filename << ": " << EC.message() << "\n"; Remaining = ""; - } else + } else { + Buffer = std::move(BufferOrErr.get()); Remaining = Buffer->getBuffer(); + } } bool empty() { return Remaining.empty(); } void printNext(raw_ostream &OS, uint32_t LineNum) { diff --git a/lib/IRReader/IRReader.cpp b/lib/IRReader/IRReader.cpp index 00c6c0dbf43..f8d2f5a9bd8 100644 --- a/lib/IRReader/IRReader.cpp +++ b/lib/IRReader/IRReader.cpp @@ -51,14 +51,15 @@ static Module *getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err, Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err, LLVMContext &Context) { - std::unique_ptr File; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) { + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(Filename); + if (std::error_code EC = FileOrErr.getError()) { Err = SMDiagnostic(Filename, SourceMgr::DK_Error, - "Could not open input file: " + ec.message()); + "Could not open input file: " + EC.message()); return nullptr; } - return getLazyIRModule(File.release(), Err, Context); + return getLazyIRModule(FileOrErr.get().release(), Err, Context); } Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, @@ -85,14 +86,15 @@ Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err, LLVMContext &Context) { - std::unique_ptr File; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) { + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(Filename); + if (std::error_code EC = FileOrErr.getError()) { Err = SMDiagnostic(Filename, SourceMgr::DK_Error, - "Could not open input file: " + ec.message()); + "Could not open input file: " + EC.message()); return nullptr; } - return ParseIR(File.get(), Err, Context); + return ParseIR(FileOrErr.get().get(), Err, Context); } //===----------------------------------------------------------------------===// diff --git a/lib/LTO/LTOCodeGenerator.cpp b/lib/LTO/LTOCodeGenerator.cpp index 4b51166a755..335197a1906 100644 --- a/lib/LTO/LTOCodeGenerator.cpp +++ b/lib/LTO/LTOCodeGenerator.cpp @@ -236,13 +236,14 @@ const void* LTOCodeGenerator::compile(size_t* length, delete NativeObjectFile; // read .o file into memory buffer - std::unique_ptr BuffPtr; - if (std::error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) { - errMsg = ec.message(); + ErrorOr> BufferOrErr = + MemoryBuffer::getFile(name, -1, false); + if (std::error_code EC = BufferOrErr.getError()) { + errMsg = EC.message(); sys::fs::remove(NativeObjectPath); return nullptr; } - NativeObjectFile = BuffPtr.release(); + NativeObjectFile = BufferOrErr.get().release(); // remove temp files sys::fs::remove(NativeObjectPath); diff --git a/lib/LTO/LTOModule.cpp b/lib/LTO/LTOModule.cpp index 5b3057177e2..844c0f2d8e1 100644 --- a/lib/LTO/LTOModule.cpp +++ b/lib/LTO/LTOModule.cpp @@ -69,12 +69,13 @@ bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer, LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options, std::string &errMsg) { - std::unique_ptr buffer; - if (std::error_code ec = MemoryBuffer::getFile(path, buffer)) { - errMsg = ec.message(); + ErrorOr> BufferOrErr = + MemoryBuffer::getFile(path); + if (std::error_code EC = BufferOrErr.getError()) { + errMsg = EC.message(); return nullptr; } - return makeLTOModule(std::move(buffer), options, errMsg); + return makeLTOModule(std::move(BufferOrErr.get()), options, errMsg); } LTOModule *LTOModule::createFromOpenFile(int fd, const char *path, size_t size, @@ -87,13 +88,13 @@ LTOModule *LTOModule::createFromOpenFileSlice(int fd, const char *path, size_t map_size, off_t offset, TargetOptions options, std::string &errMsg) { - std::unique_ptr buffer; - if (std::error_code ec = - MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) { - errMsg = ec.message(); + ErrorOr> BufferOrErr = + MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset); + if (std::error_code EC = BufferOrErr.getError()) { + errMsg = EC.message(); return nullptr; } - return makeLTOModule(std::move(buffer), options, errMsg); + return makeLTOModule(std::move(BufferOrErr.get()), options, errMsg); } LTOModule *LTOModule::createFromBuffer(const void *mem, size_t length, diff --git a/lib/Object/Binary.cpp b/lib/Object/Binary.cpp index 8d79a96662f..9f6a68556f9 100644 --- a/lib/Object/Binary.cpp +++ b/lib/Object/Binary.cpp @@ -75,8 +75,9 @@ ErrorOr object::createBinary(std::unique_ptr &Buffer, } ErrorOr object::createBinary(StringRef Path) { - std::unique_ptr File; - if (std::error_code EC = MemoryBuffer::getFileOrSTDIN(Path, File)) + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(Path); + if (std::error_code EC = FileOrErr.getError()) return EC; - return createBinary(File); + return createBinary(FileOrErr.get()); } diff --git a/lib/Object/ObjectFile.cpp b/lib/Object/ObjectFile.cpp index 591ed28262e..f5488c6d52d 100644 --- a/lib/Object/ObjectFile.cpp +++ b/lib/Object/ObjectFile.cpp @@ -83,8 +83,9 @@ ObjectFile::createObjectFile(std::unique_ptr &Object, } ErrorOr ObjectFile::createObjectFile(StringRef ObjectPath) { - std::unique_ptr File; - if (std::error_code EC = MemoryBuffer::getFile(ObjectPath, File)) + ErrorOr> FileOrErr = + MemoryBuffer::getFile(ObjectPath); + if (std::error_code EC = FileOrErr.getError()) return EC; - return createObjectFile(File); + return createObjectFile(FileOrErr.get()); } diff --git a/lib/ProfileData/InstrProfReader.cpp b/lib/ProfileData/InstrProfReader.cpp index d0493d34c20..0b367282e14 100644 --- a/lib/ProfileData/InstrProfReader.cpp +++ b/lib/ProfileData/InstrProfReader.cpp @@ -23,8 +23,11 @@ using namespace llvm; static std::error_code setupMemoryBuffer(std::string Path, std::unique_ptr &Buffer) { - if (std::error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer)) + ErrorOr> BufferOrErr = + MemoryBuffer::getFileOrSTDIN(Path); + if (std::error_code EC = BufferOrErr.getError()) return EC; + Buffer = std::move(BufferOrErr.get()); // Sanity check the file. if (Buffer->getBufferSize() > std::numeric_limits::max()) diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index c2b739fa736..87348f7342e 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -631,9 +631,11 @@ void cl::TokenizeWindowsCommandLine(StringRef Src, StringSaver &Saver, static bool ExpandResponseFile(const char *FName, StringSaver &Saver, TokenizerCallback Tokenizer, SmallVectorImpl &NewArgv) { - std::unique_ptr MemBuf; - if (MemoryBuffer::getFile(FName, MemBuf)) + ErrorOr> MemBufOrErr = + MemoryBuffer::getFile(FName); + if (!MemBufOrErr) return false; + std::unique_ptr MemBuf = std::move(MemBufOrErr.get()); StringRef Str(MemBuf->getBufferStart(), MemBuf->getBufferSize()); // If we have a UTF-16 byte order mark, convert to UTF-8 for parsing. diff --git a/lib/Support/FileUtilities.cpp b/lib/Support/FileUtilities.cpp index 0d26bafd771..8a234917827 100644 --- a/lib/Support/FileUtilities.cpp +++ b/lib/Support/FileUtilities.cpp @@ -176,18 +176,21 @@ int llvm::DiffFilesWithTolerance(StringRef NameA, std::string *Error) { // Now its safe to mmap the files into memory because both files // have a non-zero size. - std::unique_ptr F1; - if (std::error_code ec = MemoryBuffer::getFile(NameA, F1)) { + ErrorOr> F1OrErr = MemoryBuffer::getFile(NameA); + if (std::error_code EC = F1OrErr.getError()) { if (Error) - *Error = ec.message(); + *Error = EC.message(); return 2; } - std::unique_ptr F2; - if (std::error_code ec = MemoryBuffer::getFile(NameB, F2)) { + std::unique_ptr F1 = std::move(F1OrErr.get()); + + ErrorOr> F2OrErr = MemoryBuffer::getFile(NameB); + if (std::error_code EC = F2OrErr.getError()) { if (Error) - *Error = ec.message(); + *Error = EC.message(); return 2; } + std::unique_ptr F2 = std::move(F2OrErr.get()); // Okay, now that we opened the files, scan them for the first difference. const char *File1Start = F1->getBufferStart(); diff --git a/lib/Support/LockFileManager.cpp b/lib/Support/LockFileManager.cpp index 681bae2ba18..3f224e0c588 100644 --- a/lib/Support/LockFileManager.cpp +++ b/lib/Support/LockFileManager.cpp @@ -33,11 +33,13 @@ Optional > LockFileManager::readLockFile(StringRef LockFileName) { // Read the owning host and PID out of the lock file. If it appears that the // owning process is dead, the lock file is invalid. - std::unique_ptr MB; - if (MemoryBuffer::getFile(LockFileName, MB)) { + ErrorOr> MBOrErr = + MemoryBuffer::getFile(LockFileName); + if (!MBOrErr) { sys::fs::remove(LockFileName); return None; } + std::unique_ptr MB = std::move(MBOrErr.get()); StringRef Hostname; StringRef PIDStr; diff --git a/lib/Support/MemoryBuffer.cpp b/lib/Support/MemoryBuffer.cpp index abf4f60b27a..5f4b7daae53 100644 --- a/lib/Support/MemoryBuffer.cpp +++ b/lib/Support/MemoryBuffer.cpp @@ -152,18 +152,11 @@ MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) { return SB; } - -/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin -/// if the Filename is "-". If an error occurs, this returns null and fills -/// in *ErrStr with a reason. If stdin is empty, this API (unlike getSTDIN) -/// returns an empty buffer. -std::error_code -MemoryBuffer::getFileOrSTDIN(StringRef Filename, - std::unique_ptr &Result, - int64_t FileSize) { +ErrorOr> +MemoryBuffer::getFileOrSTDIN(StringRef Filename, int64_t FileSize) { if (Filename == "-") - return getSTDIN(Result); - return getFile(Filename, Result, FileSize); + return getSTDIN(); + return getFile(Filename, FileSize); } @@ -212,9 +205,8 @@ public: }; } -static std::error_code -getMemoryBufferForStream(int FD, StringRef BufferName, - std::unique_ptr &Result) { +static ErrorOr> +getMemoryBufferForStream(int FD, StringRef BufferName) { const ssize_t ChunkSize = 4096*4; SmallString Buffer; ssize_t ReadBytes; @@ -229,48 +221,43 @@ getMemoryBufferForStream(int FD, StringRef BufferName, Buffer.set_size(Buffer.size() + ReadBytes); } while (ReadBytes != 0); - Result.reset(MemoryBuffer::getMemBufferCopy(Buffer, BufferName)); - return std::error_code(); + std::unique_ptr Ret( + MemoryBuffer::getMemBufferCopy(Buffer, BufferName)); + return std::move(Ret); } -static std::error_code getFileAux(const char *Filename, - std::unique_ptr &Result, - int64_t FileSize, bool RequiresNullTerminator, - bool IsVolatileSize); +static ErrorOr> +getFileAux(const char *Filename, int64_t FileSize, bool RequiresNullTerminator, + bool IsVolatileSize); -std::error_code MemoryBuffer::getFile(Twine Filename, - std::unique_ptr &Result, - int64_t FileSize, - bool RequiresNullTerminator, - bool IsVolatileSize) { +ErrorOr> +MemoryBuffer::getFile(Twine Filename, int64_t FileSize, + bool RequiresNullTerminator, bool IsVolatileSize) { // Ensure the path is null terminated. SmallString<256> PathBuf; StringRef NullTerminatedName = Filename.toNullTerminatedStringRef(PathBuf); - return getFileAux(NullTerminatedName.data(), Result, FileSize, - RequiresNullTerminator, IsVolatileSize); + return getFileAux(NullTerminatedName.data(), FileSize, RequiresNullTerminator, + IsVolatileSize); } -static std::error_code getOpenFileImpl(int FD, const char *Filename, - std::unique_ptr &Result, - uint64_t FileSize, uint64_t MapSize, - int64_t Offset, - bool RequiresNullTerminator, - bool IsVolatileSize); +static ErrorOr> +getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize, + uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, + bool IsVolatileSize); -static std::error_code getFileAux(const char *Filename, - std::unique_ptr &Result, - int64_t FileSize, bool RequiresNullTerminator, - bool IsVolatileSize) { +static ErrorOr> +getFileAux(const char *Filename, int64_t FileSize, bool RequiresNullTerminator, + bool IsVolatileSize) { int FD; std::error_code EC = sys::fs::openFileForRead(Filename, FD); if (EC) return EC; - std::error_code ret = - getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0, + ErrorOr> Ret = + getOpenFileImpl(FD, Filename, FileSize, FileSize, 0, RequiresNullTerminator, IsVolatileSize); close(FD); - return ret; + return Ret; } static bool shouldUseMmap(int FD, @@ -321,12 +308,10 @@ static bool shouldUseMmap(int FD, return true; } -static std::error_code getOpenFileImpl(int FD, const char *Filename, - std::unique_ptr &Result, - uint64_t FileSize, uint64_t MapSize, - int64_t Offset, - bool RequiresNullTerminator, - bool IsVolatileSize) { +static ErrorOr> +getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize, + uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator, + bool IsVolatileSize) { static int PageSize = sys::process::get_self()->page_size(); // Default is to map the full file. @@ -345,7 +330,7 @@ static std::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); FileSize = Status.getSize(); } @@ -355,10 +340,11 @@ static std::error_code getOpenFileImpl(int FD, const char *Filename, if (shouldUseMmap(FD, FileSize, MapSize, Offset, RequiresNullTerminator, PageSize, IsVolatileSize)) { std::error_code EC; - Result.reset(new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile( - RequiresNullTerminator, FD, MapSize, Offset, EC)); + std::unique_ptr Result( + new (NamedBufferAlloc(Filename)) + MemoryBufferMMapFile(RequiresNullTerminator, FD, MapSize, Offset, EC)); if (!EC) - return std::error_code(); + return std::move(Result); } MemoryBuffer *Buf = MemoryBuffer::getNewUninitMemBuffer(MapSize, Filename); @@ -397,36 +383,29 @@ static std::error_code getOpenFileImpl(int FD, const char *Filename, BufPtr += NumRead; } - Result.swap(SB); - return std::error_code(); + return std::move(SB); } -std::error_code MemoryBuffer::getOpenFile(int FD, const char *Filename, - std::unique_ptr &Result, - uint64_t FileSize, - bool RequiresNullTerminator, - bool IsVolatileSize) { - return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0, +ErrorOr> +MemoryBuffer::getOpenFile(int FD, const char *Filename, uint64_t FileSize, + bool RequiresNullTerminator, bool IsVolatileSize) { + return getOpenFileImpl(FD, Filename, FileSize, FileSize, 0, RequiresNullTerminator, IsVolatileSize); } -std::error_code MemoryBuffer::getOpenFileSlice( - int FD, const char *Filename, std::unique_ptr &Result, - uint64_t MapSize, int64_t Offset, bool IsVolatileSize) { - return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false, +ErrorOr> +MemoryBuffer::getOpenFileSlice(int FD, const char *Filename, uint64_t MapSize, + int64_t Offset, bool IsVolatileSize) { + return getOpenFileImpl(FD, Filename, -1, MapSize, Offset, false, IsVolatileSize); } -//===----------------------------------------------------------------------===// -// MemoryBuffer::getSTDIN implementation. -//===----------------------------------------------------------------------===// - -std::error_code MemoryBuffer::getSTDIN(std::unique_ptr &Result) { +ErrorOr> MemoryBuffer::getSTDIN() { // 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, ""); } diff --git a/lib/Support/SourceMgr.cpp b/lib/Support/SourceMgr.cpp index a9323d080f6..595c6eec110 100644 --- a/lib/Support/SourceMgr.cpp +++ b/lib/Support/SourceMgr.cpp @@ -52,20 +52,22 @@ SourceMgr::~SourceMgr() { size_t SourceMgr::AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc, std::string &IncludedFile) { - std::unique_ptr NewBuf; IncludedFile = Filename; - MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf); + ErrorOr> NewBufOrErr = + MemoryBuffer::getFile(IncludedFile.c_str()); // If the file didn't exist directly, see if it's in an include path. - for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) { - IncludedFile = IncludeDirectories[i] + sys::path::get_separator().data() + Filename; - MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf); + for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBufOrErr; + ++i) { + IncludedFile = + IncludeDirectories[i] + sys::path::get_separator().data() + Filename; + NewBufOrErr = MemoryBuffer::getFile(IncludedFile.c_str()); } - if (!NewBuf) + if (!NewBufOrErr) return 0; - return AddNewSourceBuffer(NewBuf.release(), IncludeLoc); + return AddNewSourceBuffer(NewBufOrErr.get().release(), IncludeLoc); } unsigned SourceMgr::FindBufferContainingLoc(SMLoc Loc) const { diff --git a/lib/TableGen/Main.cpp b/lib/TableGen/Main.cpp index 191307a66bc..e317fbfa373 100644 --- a/lib/TableGen/Main.cpp +++ b/lib/TableGen/Main.cpp @@ -81,13 +81,14 @@ int TableGenMain(char *argv0, TableGenMainFn *MainFn) { RecordKeeper Records; // Parse the input file. - std::unique_ptr File; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, File)) { - errs() << "Could not open input file '" << InputFilename << "': " - << ec.message() <<"\n"; + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(InputFilename); + if (std::error_code EC = FileOrErr.getError()) { + errs() << "Could not open input file '" << InputFilename + << "': " << EC.message() << "\n"; return 1; } - MemoryBuffer *F = File.release(); + MemoryBuffer *F = FileOrErr.get().release(); // Tell SrcMgr about this buffer, which is what TGParser will pick up. SrcMgr.AddNewSourceBuffer(F, SMLoc()); diff --git a/lib/Transforms/Scalar/SampleProfile.cpp b/lib/Transforms/Scalar/SampleProfile.cpp index 888e2527383..73c97ffeef4 100644 --- a/lib/Transforms/Scalar/SampleProfile.cpp +++ b/lib/Transforms/Scalar/SampleProfile.cpp @@ -450,13 +450,14 @@ void SampleModuleProfile::dump() { /// /// \returns true if the file was loaded successfully, false otherwise. bool SampleModuleProfile::loadText() { - std::unique_ptr Buffer; - std::error_code EC = MemoryBuffer::getFile(Filename, Buffer); - if (EC) { + ErrorOr> BufferOrErr = + MemoryBuffer::getFile(Filename); + if (std::error_code EC = BufferOrErr.getError()) { std::string Msg(EC.message()); M.getContext().diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg)); return false; } + std::unique_ptr Buffer = std::move(BufferOrErr.get()); line_iterator LineIt(*Buffer, '#'); // Read the profile of each function. Since each function may be diff --git a/lib/Transforms/Utils/SpecialCaseList.cpp b/lib/Transforms/Utils/SpecialCaseList.cpp index 45a2b618a71..3d76a17abd9 100644 --- a/lib/Transforms/Utils/SpecialCaseList.cpp +++ b/lib/Transforms/Utils/SpecialCaseList.cpp @@ -54,12 +54,13 @@ SpecialCaseList *SpecialCaseList::create( const StringRef Path, std::string &Error) { if (Path.empty()) return new SpecialCaseList(); - std::unique_ptr File; - if (std::error_code EC = MemoryBuffer::getFile(Path, File)) { + ErrorOr> FileOrErr = + MemoryBuffer::getFile(Path); + if (std::error_code EC = FileOrErr.getError()) { Error = (Twine("Can't open file '") + Path + "': " + EC.message()).str(); return nullptr; } - return create(File.get(), Error); + return create(FileOrErr.get().get(), Error); } SpecialCaseList *SpecialCaseList::create( diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index 6d52f62b210..f424e46eb46 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -280,11 +280,14 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, if (file->offset) { offset = file->offset; } - if (std::error_code ec = MemoryBuffer::getOpenFileSlice( - file->fd, file->name, buffer, file->filesize, offset)) { - (*message)(LDPL_ERROR, ec.message().c_str()); + ErrorOr> BufferOrErr = + MemoryBuffer::getOpenFileSlice(file->fd, file->name, file->filesize, + offset); + if (std::error_code EC = BufferOrErr.getError()) { + (*message)(LDPL_ERROR, EC.message().c_str()); return LDPS_ERR; } + buffer = std::move(BufferOrErr.get()); view = buffer->getBufferStart(); } diff --git a/tools/lli/lli.cpp b/tools/lli/lli.cpp index f69fb435c68..48828c1d68a 100644 --- a/tools/lli/lli.cpp +++ b/tools/lli/lli.cpp @@ -285,8 +285,8 @@ public: if (!getCacheFilename(ModuleID, CacheName)) return nullptr; // Load the object from the cache filename - std::unique_ptr IRObjectBuffer; - MemoryBuffer::getFile(CacheName.c_str(), IRObjectBuffer, -1, false); + ErrorOr> IRObjectBuffer = + MemoryBuffer::getFile(CacheName.c_str(), -1, false); // If the file isn't there, that's OK. if (!IRObjectBuffer) return nullptr; @@ -294,7 +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 MemoryBuffer::getMemBufferCopy(IRObjectBuffer->getBuffer()); + return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer()); } private: @@ -538,15 +538,15 @@ int main(int argc, char **argv, char * const *envp) { } for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) { - std::unique_ptr ArBuf; - std::error_code ec; - ec = MemoryBuffer::getFileOrSTDIN(ExtraArchives[i], ArBuf); - if (ec) { + ErrorOr> ArBuf = + MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]); + if (!ArBuf) { Err.print(argv[0], errs()); return 1; } - object::Archive *Ar = new object::Archive(std::move(ArBuf), ec); - if (ec || !Ar) { + std::error_code EC; + object::Archive *Ar = new object::Archive(std::move(ArBuf.get()), EC); + if (EC || !Ar) { Err.print(argv[0], errs()); return 1; } diff --git a/tools/llvm-ar/llvm-ar.cpp b/tools/llvm-ar/llvm-ar.cpp index 4bd586be451..f638e55c5c7 100644 --- a/tools/llvm-ar/llvm-ar.cpp +++ b/tools/llvm-ar/llvm-ar.cpp @@ -770,10 +770,10 @@ static void performWriteOperation(ArchiveOperation Operation, const char *Filename = Member.getNew(); int FD = Member.getFD(); const sys::fs::file_status &Status = Member.getStatus(); - failIfError(MemoryBuffer::getOpenFile(FD, Filename, MemberBuffer, - Status.getSize(), false), - Filename); - + ErrorOr> MemberBufferOrErr = + MemoryBuffer::getOpenFile(FD, Filename, Status.getSize(), false); + failIfError(MemberBufferOrErr.getError(), Filename); + MemberBuffer = std::move(MemberBufferOrErr.get()); } else { object::Archive::child_iterator OldMember = Member.getOld(); ErrorOr> MemberBufferOrErr = @@ -934,8 +934,9 @@ int ar_main(char **argv) { static int performOperation(ArchiveOperation Operation) { // Create or open the archive object. - std::unique_ptr Buf; - std::error_code EC = MemoryBuffer::getFile(ArchiveName, Buf, -1, false); + ErrorOr> Buf = + MemoryBuffer::getFile(ArchiveName, -1, false); + std::error_code EC = Buf.getError(); if (EC && EC != errc::no_such_file_or_directory) { errs() << ToolName << ": error opening '" << ArchiveName << "': " << EC.message() << "!\n"; @@ -943,7 +944,7 @@ static int performOperation(ArchiveOperation Operation) { } if (!EC) { - object::Archive Archive(std::move(Buf), EC); + object::Archive Archive(std::move(Buf.get()), EC); if (EC) { errs() << ToolName << ": error loading '" << ArchiveName diff --git a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp index fe2b235e53c..15567cf3123 100644 --- a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp +++ b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp @@ -478,10 +478,11 @@ static void PrintSize(uint64_t Bits) { /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. static int AnalyzeBitcode() { // Read the input file. - std::unique_ptr MemBuf; - - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, MemBuf)) - return Error("Error reading '" + InputFilename + "': " + ec.message()); + ErrorOr> MemBufOrErr = + MemoryBuffer::getFileOrSTDIN(InputFilename); + if (std::error_code EC = MemBufOrErr.getError()) + return Error("Error reading '" + InputFilename + "': " + EC.message()); + std::unique_ptr MemBuf = std::move(MemBufOrErr.get()); if (MemBuf->getBufferSize() & 3) return Error("Bitcode stream should be a multiple of 4 bytes in length"); diff --git a/tools/llvm-cov/llvm-cov.cpp b/tools/llvm-cov/llvm-cov.cpp index 04de4431059..18cc1b110e2 100644 --- a/tools/llvm-cov/llvm-cov.cpp +++ b/tools/llvm-cov/llvm-cov.cpp @@ -97,27 +97,29 @@ void reportCoverage(StringRef SourceFile) { : InputGCDA; GCOVFile GF; - std::unique_ptr GCNO_Buff; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(GCNO, GCNO_Buff)) { - errs() << GCNO << ": " << ec.message() << "\n"; + ErrorOr> GCNO_Buff = + MemoryBuffer::getFileOrSTDIN(GCNO); + if (std::error_code EC = GCNO_Buff.getError()) { + errs() << GCNO << ": " << EC.message() << "\n"; return; } - GCOVBuffer GCNO_GB(GCNO_Buff.get()); + GCOVBuffer GCNO_GB(GCNO_Buff.get().get()); if (!GF.readGCNO(GCNO_GB)) { errs() << "Invalid .gcno File!\n"; return; } - std::unique_ptr GCDA_Buff; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(GCDA, GCDA_Buff)) { - if (ec != errc::no_such_file_or_directory) { - errs() << GCDA << ": " << ec.message() << "\n"; + ErrorOr> GCDA_Buff = + MemoryBuffer::getFileOrSTDIN(GCDA); + if (std::error_code EC = GCDA_Buff.getError()) { + if (EC != errc::no_such_file_or_directory) { + errs() << GCDA << ": " << EC.message() << "\n"; return; } // Clear the filename to make it clear we didn't read anything. GCDA = "-"; } else { - GCOVBuffer GCDA_GB(GCDA_Buff.get()); + GCOVBuffer GCDA_GB(GCDA_Buff.get().get()); if (!GF.readGCDA(GCDA_GB)) { errs() << "Invalid .gcda File!\n"; return; diff --git a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index 0eb6e4f4304..f44b0e3ef5e 100644 --- a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -66,14 +66,15 @@ DumpType("debug-dump", cl::init(DIDT_All), clEnumValEnd)); static void DumpInput(const StringRef &Filename) { - std::unique_ptr Buff; + ErrorOr> Buff = + MemoryBuffer::getFileOrSTDIN(Filename); - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { - errs() << Filename << ": " << ec.message() << "\n"; + if (std::error_code EC = Buff.getError()) { + errs() << Filename << ": " << EC.message() << "\n"; return; } - ErrorOr ObjOrErr(ObjectFile::createObjectFile(Buff)); + ErrorOr ObjOrErr(ObjectFile::createObjectFile(Buff.get())); if (std::error_code EC = ObjOrErr.getError()) { errs() << Filename << ": " << EC.message() << '\n'; return; diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index 72294caef3b..4c5b230573b 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -367,13 +367,13 @@ int main(int argc, char **argv) { if (!TheTarget) return 1; - std::unique_ptr BufferPtr; - if (std::error_code ec = - MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) { - errs() << ProgName << ": " << ec.message() << '\n'; + ErrorOr> BufferPtr = + MemoryBuffer::getFileOrSTDIN(InputFilename); + if (std::error_code EC = BufferPtr.getError()) { + errs() << ProgName << ": " << EC.message() << '\n'; return 1; } - MemoryBuffer *Buffer = BufferPtr.release(); + MemoryBuffer *Buffer = BufferPtr->release(); SourceMgr SrcMgr; diff --git a/tools/llvm-mcmarkup/llvm-mcmarkup.cpp b/tools/llvm-mcmarkup/llvm-mcmarkup.cpp index cbb0d51dd3d..a878f1157d5 100644 --- a/tools/llvm-mcmarkup/llvm-mcmarkup.cpp +++ b/tools/llvm-mcmarkup/llvm-mcmarkup.cpp @@ -135,12 +135,13 @@ MarkupTag MarkupParser::parseTag() { } static void parseMCMarkup(StringRef Filename) { - std::unique_ptr BufferPtr; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, BufferPtr)) { - errs() << ToolName << ": " << ec.message() << '\n'; + ErrorOr> BufferPtr = + MemoryBuffer::getFileOrSTDIN(Filename); + if (std::error_code EC = BufferPtr.getError()) { + errs() << ToolName << ": " << EC.message() << '\n'; return; } - MemoryBuffer *Buffer = BufferPtr.release(); + MemoryBuffer *Buffer = BufferPtr->release(); SourceMgr SrcMgr; diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index d3918547d84..8a89dd80dba 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -805,9 +805,11 @@ static bool checkMachOAndArchFlags(SymbolicFile *O, std::string &Filename) { } static void dumpSymbolNamesFromFile(std::string &Filename) { - std::unique_ptr Buffer; - if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename)) + ErrorOr> BufferOrErr = + MemoryBuffer::getFileOrSTDIN(Filename); + if (error(BufferOrErr.getError(), Filename)) return; + std::unique_ptr Buffer = std::move(BufferOrErr.get()); LLVMContext &Context = getGlobalContext(); ErrorOr BinaryOrErr = createBinary(Buffer, &Context); diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 74fdfef67ce..4b46ac4fc0b 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -195,15 +195,15 @@ static void DisassembleInputMachO2(StringRef Filename, MachOObjectFile *MachOOF); void llvm::DisassembleInputMachO(StringRef Filename) { - std::unique_ptr Buff; - - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { - errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n"; + ErrorOr> Buff = + MemoryBuffer::getFileOrSTDIN(Filename); + if (std::error_code EC = Buff.getError()) { + errs() << "llvm-objdump: " << Filename << ": " << EC.message() << "\n"; return; } std::unique_ptr MachOOF(static_cast( - ObjectFile::createMachOObjectFile(Buff).get())); + ObjectFile::createMachOObjectFile(Buff.get()).get())); DisassembleInputMachO2(Filename, MachOOF.get()); } @@ -288,12 +288,13 @@ static void DisassembleInputMachO2(StringRef Filename, // A separate DSym file path was specified, parse it as a macho file, // get the sections and supply it to the section name parsing machinery. if (!DSYMFile.empty()) { - std::unique_ptr Buf; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile, Buf)) { - errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n'; + ErrorOr> Buf = + MemoryBuffer::getFileOrSTDIN(DSYMFile); + if (std::error_code EC = Buf.getError()) { + errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n'; return; } - DbgObj = ObjectFile::createMachOObjectFile(Buf).get(); + DbgObj = ObjectFile::createMachOObjectFile(Buf.get()).get(); } // Setup the DIContext diff --git a/tools/llvm-rtdyld/llvm-rtdyld.cpp b/tools/llvm-rtdyld/llvm-rtdyld.cpp index fcda9e7f8a8..45734f4b7ba 100644 --- a/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -175,14 +175,16 @@ static int printLineInfoForInput() { RuntimeDyld Dyld(&MemMgr); // Load the input memory buffer. - std::unique_ptr InputBuffer; - std::unique_ptr LoadedObject; - if (std::error_code ec = - MemoryBuffer::getFileOrSTDIN(InputFileList[i], InputBuffer)) - return Error("unable to read input: '" + ec.message() + "'"); + ErrorOr> InputBuffer = + MemoryBuffer::getFileOrSTDIN(InputFileList[i]); + if (std::error_code EC = InputBuffer.getError()) + return Error("unable to read input: '" + EC.message() + "'"); + + std::unique_ptr LoadedObject; // Load the object file - LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.release()))); + LoadedObject.reset( + Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); if (!LoadedObject) { return Error(Dyld.getErrorString()); } @@ -236,14 +238,14 @@ static int executeInput() { InputFileList.push_back("-"); for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { // Load the input memory buffer. - std::unique_ptr InputBuffer; + ErrorOr> InputBuffer = + MemoryBuffer::getFileOrSTDIN(InputFileList[i]); + if (std::error_code EC = InputBuffer.getError()) + return Error("unable to read input: '" + EC.message() + "'"); std::unique_ptr LoadedObject; - if (std::error_code ec = - MemoryBuffer::getFileOrSTDIN(InputFileList[i], InputBuffer)) - return Error("unable to read input: '" + ec.message() + "'"); - // Load the object file - LoadedObject.reset(Dyld.loadObject(new ObjectBuffer(InputBuffer.release()))); + LoadedObject.reset( + Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); if (!LoadedObject) { return Error(Dyld.getErrorString()); } @@ -285,13 +287,14 @@ static int executeInput() { static int checkAllExpressions(RuntimeDyldChecker &Checker) { for (const auto& CheckerFileName : CheckFiles) { - std::unique_ptr CheckerFileBuf; - if (std::error_code EC = - MemoryBuffer::getFileOrSTDIN(CheckerFileName, CheckerFileBuf)) + ErrorOr> CheckerFileBuf = + MemoryBuffer::getFileOrSTDIN(CheckerFileName); + if (std::error_code EC = CheckerFileBuf.getError()) return Error("unable to read input '" + CheckerFileName + "': " + EC.message()); - if (!Checker.checkAllRulesInBuffer("# rtdyld-check:", CheckerFileBuf.get())) + if (!Checker.checkAllRulesInBuffer("# rtdyld-check:", + CheckerFileBuf.get().get())) return Error("some checks in '" + CheckerFileName + "' failed"); } return 0; @@ -350,15 +353,15 @@ static int linkAndVerify() { InputFileList.push_back("-"); for(unsigned i = 0, e = InputFileList.size(); i != e; ++i) { // Load the input memory buffer. - std::unique_ptr InputBuffer; - std::unique_ptr LoadedObject; - if (std::error_code ec = - MemoryBuffer::getFileOrSTDIN(InputFileList[i], InputBuffer)) - return Error("unable to read input: '" + ec.message() + "'"); + ErrorOr> InputBuffer = + MemoryBuffer::getFileOrSTDIN(InputFileList[i]); + if (std::error_code EC = InputBuffer.getError()) + return Error("unable to read input: '" + EC.message() + "'"); + std::unique_ptr LoadedObject; // Load the object file LoadedObject.reset( - Dyld.loadObject(new ObjectBuffer(InputBuffer.release()))); + Dyld.loadObject(new ObjectBuffer(InputBuffer.get().release()))); if (!LoadedObject) { return Error(Dyld.getErrorString()); } diff --git a/tools/llvm-symbolizer/LLVMSymbolize.cpp b/tools/llvm-symbolizer/LLVMSymbolize.cpp index ebbae39a57a..c1d39efc1b8 100644 --- a/tools/llvm-symbolizer/LLVMSymbolize.cpp +++ b/tools/llvm-symbolizer/LLVMSymbolize.cpp @@ -220,10 +220,11 @@ static std::string getDarwinDWARFResourceForPath(const std::string &Path) { } static bool checkFileCRC(StringRef Path, uint32_t CRCHash) { - std::unique_ptr MB; - if (MemoryBuffer::getFileOrSTDIN(Path, MB)) + ErrorOr> MB = + MemoryBuffer::getFileOrSTDIN(Path); + if (!MB) return false; - return !zlib::isAvailable() || CRCHash == zlib::crc32(MB->getBuffer()); + return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer()); } static bool findDebugBinary(const std::string &OrigPath, diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index b3f5938b00d..b401f9a7f19 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -88,10 +88,10 @@ bool lto_module_is_object_file(const char* path) { bool lto_module_is_object_file_for_target(const char* path, const char* target_triplet_prefix) { - std::unique_ptr buffer; - if (MemoryBuffer::getFile(path, buffer)) + ErrorOr> Buffer = MemoryBuffer::getFile(path); + if (!Buffer) return false; - return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix); + return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix); } bool lto_module_is_object_file_in_memory(const void* mem, size_t length) { diff --git a/tools/yaml2obj/yaml2obj.cpp b/tools/yaml2obj/yaml2obj.cpp index dd41951e7db..945fad1384a 100644 --- a/tools/yaml2obj/yaml2obj.cpp +++ b/tools/yaml2obj/yaml2obj.cpp @@ -91,8 +91,9 @@ int main(int argc, char **argv) { return 1; } - std::unique_ptr Buf; - if (MemoryBuffer::getFileOrSTDIN(Input, Buf)) + ErrorOr> Buf = + MemoryBuffer::getFileOrSTDIN(Input); + if (!Buf) return 1; ConvertFuncPtr Convert = nullptr; @@ -105,7 +106,7 @@ int main(int argc, char **argv) { return 1; } - yaml::Input YIn(Buf->getBuffer()); + yaml::Input YIn(Buf.get()->getBuffer()); int Res = convertYAML(YIn, Out->os(), Convert); if (Res == 0) diff --git a/unittests/Support/MemoryBufferTest.cpp b/unittests/Support/MemoryBufferTest.cpp index 98af5c4f316..93bf301267e 100644 --- a/unittests/Support/MemoryBufferTest.cpp +++ b/unittests/Support/MemoryBufferTest.cpp @@ -77,11 +77,11 @@ TEST_F(MemoryBufferTest, NullTerminator4K) { } OF.close(); - OwningBuffer MB; - std::error_code EC = MemoryBuffer::getFile(TestPath.c_str(), MB); + ErrorOr MB = MemoryBuffer::getFile(TestPath.c_str()); + std::error_code EC = MB.getError(); ASSERT_FALSE(EC); - const char *BufData = MB->getBufferStart(); + const char *BufData = MB.get()->getBufferStart(); EXPECT_EQ('f', BufData[4095]); EXPECT_EQ('\0', BufData[4096]); } @@ -146,15 +146,16 @@ void MemoryBufferTest::testGetOpenFileSlice(bool Reopen) { EXPECT_FALSE(sys::fs::openFileForRead(TestPath.c_str(), TestFD)); } - OwningBuffer Buf; - std::error_code EC = - MemoryBuffer::getOpenFileSlice(TestFD, TestPath.c_str(), Buf, + ErrorOr Buf = + MemoryBuffer::getOpenFileSlice(TestFD, TestPath.c_str(), 40000, // Size 80000 // Offset ); + + std::error_code EC = Buf.getError(); EXPECT_FALSE(EC); - StringRef BufData = Buf->getBuffer(); + StringRef BufData = Buf.get()->getBuffer(); EXPECT_EQ(BufData.size(), 40000U); EXPECT_EQ(BufData[0], '0'); EXPECT_EQ(BufData[9], '9'); diff --git a/utils/FileCheck/FileCheck.cpp b/utils/FileCheck/FileCheck.cpp index 978e05f4f2c..d88cf367c9d 100644 --- a/utils/FileCheck/FileCheck.cpp +++ b/utils/FileCheck/FileCheck.cpp @@ -820,17 +820,18 @@ static StringRef FindFirstMatchingPrefix(StringRef &Buffer, /// Returns true in case of an error, false otherwise. static bool ReadCheckFile(SourceMgr &SM, std::vector &CheckStrings) { - std::unique_ptr File; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(CheckFilename, File)) { - errs() << "Could not open check file '" << CheckFilename << "': " - << ec.message() << '\n'; + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(CheckFilename); + if (std::error_code EC = FileOrErr.getError()) { + errs() << "Could not open check file '" << CheckFilename + << "': " << EC.message() << '\n'; return true; } // If we want to canonicalize whitespace, strip excess whitespace from the // buffer containing the CHECK lines. Remove DOS style line endings. - MemoryBuffer *F = - CanonicalizeInputFile(File.release(), NoCanonicalizeWhiteSpace); + MemoryBuffer *F = CanonicalizeInputFile(FileOrErr.get().release(), + NoCanonicalizeWhiteSpace); SM.AddNewSourceBuffer(F, SMLoc()); @@ -1223,12 +1224,14 @@ int main(int argc, char **argv) { return 2; // Open the file to check and add it to SourceMgr. - std::unique_ptr File; - if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, File)) { - errs() << "Could not open input file '" << InputFilename << "': " - << ec.message() << '\n'; + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(InputFilename); + if (std::error_code EC = FileOrErr.getError()) { + errs() << "Could not open input file '" << InputFilename + << "': " << EC.message() << '\n'; return 2; } + std::unique_ptr File = std::move(FileOrErr.get()); if (File->getBufferSize() == 0) { errs() << "FileCheck error: '" << InputFilename << "' is empty.\n"; diff --git a/utils/yaml-bench/YAMLBench.cpp b/utils/yaml-bench/YAMLBench.cpp index 39b8f72ecc5..e88ce5daeba 100644 --- a/utils/yaml-bench/YAMLBench.cpp +++ b/utils/yaml-bench/YAMLBench.cpp @@ -188,9 +188,11 @@ static std::string createJSONText(size_t MemoryMB, unsigned ValueSize) { int main(int argc, char **argv) { llvm::cl::ParseCommandLineOptions(argc, argv); if (Input.getNumOccurrences()) { - std::unique_ptr Buf; - if (MemoryBuffer::getFileOrSTDIN(Input, Buf)) + ErrorOr> BufOrErr = + MemoryBuffer::getFileOrSTDIN(Input); + if (!BufOrErr) return 1; + std::unique_ptr Buf = std::move(BufOrErr.get()); llvm::SourceMgr sm; if (DumpTokens) {