Skip to content

Commit

Permalink
Update the MemoryBuffer API to use ErrorOr.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212405 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Jul 6, 2014
1 parent 04e43e6 commit 7cba2a9
Show file tree
Hide file tree
Showing 35 changed files with 297 additions and 280 deletions.
47 changes: 20 additions & 27 deletions include/llvm/Support/MemoryBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -60,19 +61,17 @@ class MemoryBuffer {
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.
Expand All @@ -81,22 +80,19 @@ class MemoryBuffer {
/// \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.
///
/// \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.
Expand All @@ -123,16 +119,13 @@ class MemoryBuffer {
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.
Expand Down
9 changes: 5 additions & 4 deletions lib/AsmParser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 12 additions & 16 deletions lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions lib/IR/GCOV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions lib/IRReader/IRReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

//===----------------------------------------------------------------------===//
Expand Down
9 changes: 5 additions & 4 deletions lib/LTO/LTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 10 additions & 9 deletions lib/LTO/LTOModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions lib/Object/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
7 changes: 4 additions & 3 deletions lib/Object/ObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
5 changes: 4 additions & 1 deletion lib/ProfileData/InstrProfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 4 additions & 2 deletions lib/Support/CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions lib/Support/FileUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions lib/Support/LockFileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 7cba2a9

Please sign in to comment.