aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafael Espindola <rafael.espindola@gmail.com>2014-07-06 17:43:13 +0000
committerRafael Espindola <rafael.espindola@gmail.com>2014-07-06 17:43:13 +0000
commitadf21f2a5634f2b33b6aa2dffae67df50c72a9f2 (patch)
treeab58d9d0ff55fb26656a0152b43539b19657b9dc
parent178cadafc1a7fc576b6f4eb86a598c24247ccad7 (diff)
downloadllvm-adf21f2a5634f2b33b6aa2dffae67df50c72a9f2.zip
llvm-adf21f2a5634f2b33b6aa2dffae67df50c72a9f2.tar.gz
llvm-adf21f2a5634f2b33b6aa2dffae67df50c72a9f2.tar.bz2
Update the MemoryBuffer API to use ErrorOr.
llvm-svn: 212405
-rw-r--r--llvm/include/llvm/Support/MemoryBuffer.h47
-rw-r--r--llvm/lib/AsmParser/Parser.cpp9
-rw-r--r--llvm/lib/IR/Core.cpp28
-rw-r--r--llvm/lib/IR/GCOV.cpp8
-rw-r--r--llvm/lib/IRReader/IRReader.cpp18
-rw-r--r--llvm/lib/LTO/LTOCodeGenerator.cpp9
-rw-r--r--llvm/lib/LTO/LTOModule.cpp19
-rw-r--r--llvm/lib/Object/Binary.cpp7
-rw-r--r--llvm/lib/Object/ObjectFile.cpp7
-rw-r--r--llvm/lib/ProfileData/InstrProfReader.cpp5
-rw-r--r--llvm/lib/Support/CommandLine.cpp6
-rw-r--r--llvm/lib/Support/FileUtilities.cpp15
-rw-r--r--llvm/lib/Support/LockFileManager.cpp6
-rw-r--r--llvm/lib/Support/MemoryBuffer.cpp117
-rw-r--r--llvm/lib/Support/SourceMgr.cpp16
-rw-r--r--llvm/lib/TableGen/Main.cpp11
-rw-r--r--llvm/lib/Transforms/Scalar/SampleProfile.cpp7
-rw-r--r--llvm/lib/Transforms/Utils/SpecialCaseList.cpp7
-rw-r--r--llvm/tools/gold/gold-plugin.cpp9
-rw-r--r--llvm/tools/lli/lli.cpp18
-rw-r--r--llvm/tools/llvm-ar/llvm-ar.cpp15
-rw-r--r--llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp9
-rw-r--r--llvm/tools/llvm-cov/llvm-cov.cpp20
-rw-r--r--llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp9
-rw-r--r--llvm/tools/llvm-mc/llvm-mc.cpp10
-rw-r--r--llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp9
-rw-r--r--llvm/tools/llvm-nm/llvm-nm.cpp6
-rw-r--r--llvm/tools/llvm-objdump/MachODump.cpp19
-rw-r--r--llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp47
-rw-r--r--llvm/tools/llvm-symbolizer/LLVMSymbolize.cpp7
-rw-r--r--llvm/tools/lto/lto.cpp6
-rw-r--r--llvm/tools/yaml2obj/yaml2obj.cpp7
-rw-r--r--llvm/unittests/Support/MemoryBufferTest.cpp15
-rw-r--r--llvm/utils/FileCheck/FileCheck.cpp23
-rw-r--r--llvm/utils/yaml-bench/YAMLBench.cpp6
35 files changed, 297 insertions, 280 deletions
diff --git a/llvm/include/llvm/Support/MemoryBuffer.h b/llvm/include/llvm/Support/MemoryBuffer.h
index 8c742c6..147be47 100644
--- a/llvm/include/llvm/Support/MemoryBuffer.h
+++ b/llvm/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 <memory>
#include <system_error>
@@ -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<MemoryBuffer> &Result,
- int64_t FileSize = -1,
- bool RequiresNullTerminator = true,
- bool IsVolatileSize = false);
+ static ErrorOr<std::unique_ptr<MemoryBuffer>>
+ 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<MemoryBuffer> &Result,
- uint64_t MapSize, int64_t Offset,
- bool IsVolatileSize = false);
+ static ErrorOr<std::unique_ptr<MemoryBuffer>>
+ 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<MemoryBuffer> &Result,
- uint64_t FileSize,
- bool RequiresNullTerminator = true,
- bool IsVolatileSize = false);
+ static ErrorOr<std::unique_ptr<MemoryBuffer>>
+ 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<MemoryBuffer> &Result);
+ /// Read all of stdin into a file buffer, and return it.
+ static ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> &Result,
- int64_t FileSize = -1);
+ /// Open the specified file as a MemoryBuffer, or open stdin if the Filename
+ /// is "-".
+ static ErrorOr<std::unique_ptr<MemoryBuffer>>
+ getFileOrSTDIN(StringRef Filename, int64_t FileSize = -1);
//===--------------------------------------------------------------------===//
// Provided for performance analysis.
diff --git a/llvm/lib/AsmParser/Parser.cpp b/llvm/lib/AsmParser/Parser.cpp
index 2138fde..91bb51c 100644
--- a/llvm/lib/AsmParser/Parser.cpp
+++ b/llvm/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<MemoryBuffer> File;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 0cb781c..87099a6 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -2606,28 +2606,24 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
- std::unique_ptr<MemoryBuffer> MB;
- std::error_code ec;
- if (!(ec = MemoryBuffer::getFile(Path, MB))) {
- *OutMemBuf = wrap(MB.release());
- return 0;
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> MB;
- std::error_code ec;
- if (!(ec = MemoryBuffer::getSTDIN(MB))) {
- *OutMemBuf = wrap(MB.release());
- return 0;
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/lib/IR/GCOV.cpp b/llvm/lib/IR/GCOV.cpp
index b4c7977..1667401 100644
--- a/llvm/lib/IR/GCOV.cpp
+++ b/llvm/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<std::unique_ptr<MemoryBuffer>> 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/llvm/lib/IRReader/IRReader.cpp b/llvm/lib/IRReader/IRReader.cpp
index 00c6c0db..f8d2f5a 100644
--- a/llvm/lib/IRReader/IRReader.cpp
+++ b/llvm/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<MemoryBuffer> File;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> File;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/lib/LTO/LTOCodeGenerator.cpp b/llvm/lib/LTO/LTOCodeGenerator.cpp
index 4b51166..335197a 100644
--- a/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/llvm/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<MemoryBuffer> BuffPtr;
- if (std::error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
- errMsg = ec.message();
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/lib/LTO/LTOModule.cpp b/llvm/lib/LTO/LTOModule.cpp
index 5b3057177..844c0f2 100644
--- a/llvm/lib/LTO/LTOModule.cpp
+++ b/llvm/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<MemoryBuffer> buffer;
- if (std::error_code ec = MemoryBuffer::getFile(path, buffer)) {
- errMsg = ec.message();
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> buffer;
- if (std::error_code ec =
- MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
- errMsg = ec.message();
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/lib/Object/Binary.cpp b/llvm/lib/Object/Binary.cpp
index 8d79a96..9f6a685 100644
--- a/llvm/lib/Object/Binary.cpp
+++ b/llvm/lib/Object/Binary.cpp
@@ -75,8 +75,9 @@ ErrorOr<Binary *> object::createBinary(std::unique_ptr<MemoryBuffer> &Buffer,
}
ErrorOr<Binary *> object::createBinary(StringRef Path) {
- std::unique_ptr<MemoryBuffer> File;
- if (std::error_code EC = MemoryBuffer::getFileOrSTDIN(Path, File))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
+ MemoryBuffer::getFileOrSTDIN(Path);
+ if (std::error_code EC = FileOrErr.getError())
return EC;
- return createBinary(File);
+ return createBinary(FileOrErr.get());
}
diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp
index 591ed28..f5488c6 100644
--- a/llvm/lib/Object/ObjectFile.cpp
+++ b/llvm/lib/Object/ObjectFile.cpp
@@ -83,8 +83,9 @@ ObjectFile::createObjectFile(std::unique_ptr<MemoryBuffer> &Object,
}
ErrorOr<ObjectFile *> ObjectFile::createObjectFile(StringRef ObjectPath) {
- std::unique_ptr<MemoryBuffer> File;
- if (std::error_code EC = MemoryBuffer::getFile(ObjectPath, File))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
+ MemoryBuffer::getFile(ObjectPath);
+ if (std::error_code EC = FileOrErr.getError())
return EC;
- return createObjectFile(File);
+ return createObjectFile(FileOrErr.get());
}
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index d0493d3..0b36728 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -23,8 +23,11 @@ using namespace llvm;
static std::error_code
setupMemoryBuffer(std::string Path, std::unique_ptr<MemoryBuffer> &Buffer) {
- if (std::error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<unsigned>::max())
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index c2b739f..87348f7 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/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<const char *> &NewArgv) {
- std::unique_ptr<MemoryBuffer> MemBuf;
- if (MemoryBuffer::getFile(FName, MemBuf))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
+ MemoryBuffer::getFile(FName);
+ if (!MemBufOrErr)
return false;
+ std::unique_ptr<MemoryBuffer> 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/llvm/lib/Support/FileUtilities.cpp b/llvm/lib/Support/FileUtilities.cpp
index 0d26baf..8a23491 100644
--- a/llvm/lib/Support/FileUtilities.cpp
+++ b/llvm/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<MemoryBuffer> F1;
- if (std::error_code ec = MemoryBuffer::getFile(NameA, F1)) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> F1OrErr = MemoryBuffer::getFile(NameA);
+ if (std::error_code EC = F1OrErr.getError()) {
if (Error)
- *Error = ec.message();
+ *Error = EC.message();
return 2;
}
- std::unique_ptr<MemoryBuffer> F2;
- if (std::error_code ec = MemoryBuffer::getFile(NameB, F2)) {
+ std::unique_ptr<MemoryBuffer> F1 = std::move(F1OrErr.get());
+
+ ErrorOr<std::unique_ptr<MemoryBuffer>> F2OrErr = MemoryBuffer::getFile(NameB);
+ if (std::error_code EC = F2OrErr.getError()) {
if (Error)
- *Error = ec.message();
+ *Error = EC.message();
return 2;
}
+ std::unique_ptr<MemoryBuffer> 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/llvm/lib/Support/LockFileManager.cpp b/llvm/lib/Support/LockFileManager.cpp
index 681bae2..3f224e0 100644
--- a/llvm/lib/Support/LockFileManager.cpp
+++ b/llvm/lib/Support/LockFileManager.cpp
@@ -33,11 +33,13 @@ Optional<std::pair<std::string, int> >
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<MemoryBuffer> MB;
- if (MemoryBuffer::getFile(LockFileName, MB)) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
+ MemoryBuffer::getFile(LockFileName);
+ if (!MBOrErr) {
sys::fs::remove(LockFileName);
return None;
}
+ std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
StringRef Hostname;
StringRef PIDStr;
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index abf4f60..5f4b7da 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/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<MemoryBuffer> &Result,
- int64_t FileSize) {
+ErrorOr<std::unique_ptr<MemoryBuffer>>
+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<MemoryBuffer> &Result) {
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+getMemoryBufferForStream(int FD, StringRef BufferName) {
const ssize_t ChunkSize = 4096*4;
SmallString<ChunkSize> 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<MemoryBuffer> Ret(
+ MemoryBuffer::getMemBufferCopy(Buffer, BufferName));
+ return std::move(Ret);
}
-static std::error_code getFileAux(const char *Filename,
- std::unique_ptr<MemoryBuffer> &Result,
- int64_t FileSize, bool RequiresNullTerminator,
- bool IsVolatileSize);
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+getFileAux(const char *Filename, int64_t FileSize, bool RequiresNullTerminator,
+ bool IsVolatileSize);
-std::error_code MemoryBuffer::getFile(Twine Filename,
- std::unique_ptr<MemoryBuffer> &Result,
- int64_t FileSize,
- bool RequiresNullTerminator,
- bool IsVolatileSize) {
+ErrorOr<std::unique_ptr<MemoryBuffer>>
+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<MemoryBuffer> &Result,
- uint64_t FileSize, uint64_t MapSize,
- int64_t Offset,
- bool RequiresNullTerminator,
- bool IsVolatileSize);
-
-static std::error_code getFileAux(const char *Filename,
- std::unique_ptr<MemoryBuffer> &Result,
- int64_t FileSize, bool RequiresNullTerminator,
- bool IsVolatileSize) {
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+getOpenFileImpl(int FD, const char *Filename, uint64_t FileSize,
+ uint64_t MapSize, int64_t Offset, bool RequiresNullTerminator,
+ bool IsVolatileSize);
+
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+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<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> &Result,
- uint64_t FileSize, uint64_t MapSize,
- int64_t Offset,
- bool RequiresNullTerminator,
- bool IsVolatileSize) {
+static ErrorOr<std::unique_ptr<MemoryBuffer>>
+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<MemoryBuffer> 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<MemoryBuffer> &Result,
- uint64_t FileSize,
- bool RequiresNullTerminator,
- bool IsVolatileSize) {
- return getOpenFileImpl(FD, Filename, Result, FileSize, FileSize, 0,
+ErrorOr<std::unique_ptr<MemoryBuffer>>
+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<MemoryBuffer> &Result,
- uint64_t MapSize, int64_t Offset, bool IsVolatileSize) {
- return getOpenFileImpl(FD, Filename, Result, -1, MapSize, Offset, false,
+ErrorOr<std::unique_ptr<MemoryBuffer>>
+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<MemoryBuffer> &Result) {
+ErrorOr<std::unique_ptr<MemoryBuffer>> 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, "<stdin>", Result);
+ return getMemoryBufferForStream(0, "<stdin>");
}
diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp
index a9323d0..595c6ee 100644
--- a/llvm/lib/Support/SourceMgr.cpp
+++ b/llvm/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<MemoryBuffer> NewBuf;
IncludedFile = Filename;
- MemoryBuffer::getFile(IncludedFile.c_str(), NewBuf);
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/lib/TableGen/Main.cpp b/llvm/lib/TableGen/Main.cpp
index 191307a..e317fbf 100644
--- a/llvm/lib/TableGen/Main.cpp
+++ b/llvm/lib/TableGen/Main.cpp
@@ -81,13 +81,14 @@ int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
RecordKeeper Records;
// Parse the input file.
- std::unique_ptr<MemoryBuffer> File;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, File)) {
- errs() << "Could not open input file '" << InputFilename << "': "
- << ec.message() <<"\n";
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/lib/Transforms/Scalar/SampleProfile.cpp b/llvm/lib/Transforms/Scalar/SampleProfile.cpp
index 888e252..73c97ff 100644
--- a/llvm/lib/Transforms/Scalar/SampleProfile.cpp
+++ b/llvm/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<MemoryBuffer> Buffer;
- std::error_code EC = MemoryBuffer::getFile(Filename, Buffer);
- if (EC) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
line_iterator LineIt(*Buffer, '#');
// Read the profile of each function. Since each function may be
diff --git a/llvm/lib/Transforms/Utils/SpecialCaseList.cpp b/llvm/lib/Transforms/Utils/SpecialCaseList.cpp
index 45a2b61..3d76a17 100644
--- a/llvm/lib/Transforms/Utils/SpecialCaseList.cpp
+++ b/llvm/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<MemoryBuffer> File;
- if (std::error_code EC = MemoryBuffer::getFile(Path, File)) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
index 6d52f62..f424e46 100644
--- a/llvm/tools/gold/gold-plugin.cpp
+++ b/llvm/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<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index f69fb43..48828c1 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/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<MemoryBuffer> IRObjectBuffer;
- MemoryBuffer::getFile(CacheName.c_str(), IRObjectBuffer, -1, false);
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> ArBuf;
- std::error_code ec;
- ec = MemoryBuffer::getFileOrSTDIN(ExtraArchives[i], ArBuf);
- if (ec) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp
index 4bd586b..f638e55 100644
--- a/llvm/tools/llvm-ar/llvm-ar.cpp
+++ b/llvm/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<std::unique_ptr<MemoryBuffer>> 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<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
@@ -934,8 +934,9 @@ int ar_main(char **argv) {
static int performOperation(ArchiveOperation Operation) {
// Create or open the archive object.
- std::unique_ptr<MemoryBuffer> Buf;
- std::error_code EC = MemoryBuffer::getFile(ArchiveName, Buf, -1, false);
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
index fe2b235..15567cf 100644
--- a/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
+++ b/llvm/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<MemoryBuffer> MemBuf;
-
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, MemBuf))
- return Error("Error reading '" + InputFilename + "': " + ec.message());
+ ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
+ MemoryBuffer::getFileOrSTDIN(InputFilename);
+ if (std::error_code EC = MemBufOrErr.getError())
+ return Error("Error reading '" + InputFilename + "': " + EC.message());
+ std::unique_ptr<MemoryBuffer> 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/llvm/tools/llvm-cov/llvm-cov.cpp b/llvm/tools/llvm-cov/llvm-cov.cpp
index 04de443..18cc1b1 100644
--- a/llvm/tools/llvm-cov/llvm-cov.cpp
+++ b/llvm/tools/llvm-cov/llvm-cov.cpp
@@ -97,27 +97,29 @@ void reportCoverage(StringRef SourceFile) {
: InputGCDA;
GCOVFile GF;
- std::unique_ptr<MemoryBuffer> GCNO_Buff;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(GCNO, GCNO_Buff)) {
- errs() << GCNO << ": " << ec.message() << "\n";
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> 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<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index 0eb6e4f..f44b0e3 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/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<MemoryBuffer> Buff;
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<ObjectFile *> ObjOrErr(ObjectFile::createObjectFile(Buff));
+ ErrorOr<ObjectFile *> ObjOrErr(ObjectFile::createObjectFile(Buff.get()));
if (std::error_code EC = ObjOrErr.getError()) {
errs() << Filename << ": " << EC.message() << '\n';
return;
diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp
index 72294ca..4c5b230 100644
--- a/llvm/tools/llvm-mc/llvm-mc.cpp
+++ b/llvm/tools/llvm-mc/llvm-mc.cpp
@@ -367,13 +367,13 @@ int main(int argc, char **argv) {
if (!TheTarget)
return 1;
- std::unique_ptr<MemoryBuffer> BufferPtr;
- if (std::error_code ec =
- MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
- errs() << ProgName << ": " << ec.message() << '\n';
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp b/llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp
index cbb0d51..a878f11 100644
--- a/llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp
+++ b/llvm/tools/llvm-mcmarkup/llvm-mcmarkup.cpp
@@ -135,12 +135,13 @@ MarkupTag MarkupParser::parseTag() {
}
static void parseMCMarkup(StringRef Filename) {
- std::unique_ptr<MemoryBuffer> BufferPtr;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, BufferPtr)) {
- errs() << ToolName << ": " << ec.message() << '\n';
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp
index d391854..8a89dd8 100644
--- a/llvm/tools/llvm-nm/llvm-nm.cpp
+++ b/llvm/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<MemoryBuffer> Buffer;
- if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
+ MemoryBuffer::getFileOrSTDIN(Filename);
+ if (error(BufferOrErr.getError(), Filename))
return;
+ std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
LLVMContext &Context = getGlobalContext();
ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer, &Context);
diff --git a/llvm/tools/llvm-objdump/MachODump.cpp b/llvm/tools/llvm-objdump/MachODump.cpp
index 74fdfef..4b46ac4 100644
--- a/llvm/tools/llvm-objdump/MachODump.cpp
+++ b/llvm/tools/llvm-objdump/MachODump.cpp
@@ -195,15 +195,15 @@ static void DisassembleInputMachO2(StringRef Filename,
MachOObjectFile *MachOOF);
void llvm::DisassembleInputMachO(StringRef Filename) {
- std::unique_ptr<MemoryBuffer> Buff;
-
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
- errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n";
+ ErrorOr<std::unique_ptr<MemoryBuffer>> Buff =
+ MemoryBuffer::getFileOrSTDIN(Filename);
+ if (std::error_code EC = Buff.getError()) {
+ errs() << "llvm-objdump: " << Filename << ": " << EC.message() << "\n";
return;
}
std::unique_ptr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile *>(
- 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<MemoryBuffer> Buf;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile, Buf)) {
- errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n';
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp b/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp
index fcda9e7f..45734f4 100644
--- a/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp
+++ b/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp
@@ -175,14 +175,16 @@ static int printLineInfoForInput() {
RuntimeDyld Dyld(&MemMgr);
// Load the input memory buffer.
- std::unique_ptr<MemoryBuffer> InputBuffer;
- std::unique_ptr<ObjectImage> LoadedObject;
- if (std::error_code ec =
- MemoryBuffer::getFileOrSTDIN(InputFileList[i], InputBuffer))
- return Error("unable to read input: '" + ec.message() + "'");
+ ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
+ MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
+ if (std::error_code EC = InputBuffer.getError())
+ return Error("unable to read input: '" + EC.message() + "'");
+
+ std::unique_ptr<ObjectImage> 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<MemoryBuffer> InputBuffer;
+ ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
+ MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
+ if (std::error_code EC = InputBuffer.getError())
+ return Error("unable to read input: '" + EC.message() + "'");
std::unique_ptr<ObjectImage> 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<MemoryBuffer> CheckerFileBuf;
- if (std::error_code EC =
- MemoryBuffer::getFileOrSTDIN(CheckerFileName, CheckerFileBuf))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> InputBuffer;
- std::unique_ptr<ObjectImage> LoadedObject;
- if (std::error_code ec =
- MemoryBuffer::getFileOrSTDIN(InputFileList[i], InputBuffer))
- return Error("unable to read input: '" + ec.message() + "'");
+ ErrorOr<std::unique_ptr<MemoryBuffer>> InputBuffer =
+ MemoryBuffer::getFileOrSTDIN(InputFileList[i]);
+ if (std::error_code EC = InputBuffer.getError())
+ return Error("unable to read input: '" + EC.message() + "'");
+ std::unique_ptr<ObjectImage> 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/llvm/tools/llvm-symbolizer/LLVMSymbolize.cpp b/llvm/tools/llvm-symbolizer/LLVMSymbolize.cpp
index ebbae39..c1d39ef 100644
--- a/llvm/tools/llvm-symbolizer/LLVMSymbolize.cpp
+++ b/llvm/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<MemoryBuffer> MB;
- if (MemoryBuffer::getFileOrSTDIN(Path, MB))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/lto/lto.cpp b/llvm/tools/lto/lto.cpp
index b3f5938..b401f9a 100644
--- a/llvm/tools/lto/lto.cpp
+++ b/llvm/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<MemoryBuffer> buffer;
- if (MemoryBuffer::getFile(path, buffer))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/tools/yaml2obj/yaml2obj.cpp b/llvm/tools/yaml2obj/yaml2obj.cpp
index dd41951..945fad1 100644
--- a/llvm/tools/yaml2obj/yaml2obj.cpp
+++ b/llvm/tools/yaml2obj/yaml2obj.cpp
@@ -91,8 +91,9 @@ int main(int argc, char **argv) {
return 1;
}
- std::unique_ptr<MemoryBuffer> Buf;
- if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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/llvm/unittests/Support/MemoryBufferTest.cpp b/llvm/unittests/Support/MemoryBufferTest.cpp
index 98af5c4..93bf301 100644
--- a/llvm/unittests/Support/MemoryBufferTest.cpp
+++ b/llvm/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<OwningBuffer> 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<OwningBuffer> 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/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index 978e05f..d88cf36 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/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<CheckString> &CheckStrings) {
- std::unique_ptr<MemoryBuffer> File;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(CheckFilename, File)) {
- errs() << "Could not open check file '" << CheckFilename << "': "
- << ec.message() << '\n';
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> File;
- if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, File)) {
- errs() << "Could not open input file '" << InputFilename << "': "
- << ec.message() << '\n';
+ ErrorOr<std::unique_ptr<MemoryBuffer>> 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<MemoryBuffer> File = std::move(FileOrErr.get());
if (File->getBufferSize() == 0) {
errs() << "FileCheck error: '" << InputFilename << "' is empty.\n";
diff --git a/llvm/utils/yaml-bench/YAMLBench.cpp b/llvm/utils/yaml-bench/YAMLBench.cpp
index 39b8f72..e88ce5d 100644
--- a/llvm/utils/yaml-bench/YAMLBench.cpp
+++ b/llvm/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<MemoryBuffer> Buf;
- if (MemoryBuffer::getFileOrSTDIN(Input, Buf))
+ ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
+ MemoryBuffer::getFileOrSTDIN(Input);
+ if (!BufOrErr)
return 1;
+ std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
llvm::SourceMgr sm;
if (DumpTokens) {