MemoryBuffer now return an error_code and returns a OwningPtr<MemoryBuffer> via an out parm.

llvm-svn: 121958
This commit is contained in:
Michael J. Spencer 2010-12-16 03:29:14 +00:00
parent fc6f7ab8c7
commit 86f6a9ac6e
24 changed files with 161 additions and 162 deletions

View File

@ -19,6 +19,7 @@
#ifndef LLVM_SUPPORT_IRREADER_H
#define LLVM_SUPPORT_IRREADER_H
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Assembly/Parser.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/MemoryBuffer.h"
@ -57,15 +58,14 @@ namespace llvm {
inline Module *getLazyIRFileModule(const std::string &Filename,
SMDiagnostic &Err,
LLVMContext &Context) {
error_code ec;
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
if (F == 0) {
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
Err = SMDiagnostic(Filename,
"Could not open input file: " + ec.message());
return 0;
}
return getLazyIRModule(F, Err, Context);
return getLazyIRModule(File.take(), Err, Context);
}
/// If the given MemoryBuffer holds a bitcode image, return a Module
@ -95,15 +95,14 @@ namespace llvm {
inline Module *ParseIRFile(const std::string &Filename,
SMDiagnostic &Err,
LLVMContext &Context) {
error_code ec;
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
if (F == 0) {
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
Err = SMDiagnostic(Filename,
"Could not open input file: " + ec.message());
return 0;
}
return ParseIR(F, Err, Context);
return ParseIR(File.take(), Err, Context);
}
}

View File

@ -20,6 +20,7 @@
namespace llvm {
class error_code;
template<class T> class OwningPtr;
/// MemoryBuffer - This interface provides simple read-only access to a block
/// of memory, and provides simple methods for reading files and standard input
@ -61,17 +62,18 @@ public:
/// MemoryBuffer if successful, otherwise returning null. If FileSize is
/// specified, this means that the client knows that the file exists and that
/// it has the specified size.
static MemoryBuffer *getFile(StringRef Filename, error_code &ec,
int64_t FileSize = -1);
static MemoryBuffer *getFile(const char *Filename, error_code &ec,
int64_t FileSize = -1);
static error_code getFile(StringRef Filename, OwningPtr<MemoryBuffer> &result,
int64_t FileSize = -1);
static error_code getFile(const char *Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize = -1);
/// getOpenFile - Given an already-open file descriptor, read the file and
/// return a MemoryBuffer. This takes ownership of the descriptor,
/// immediately closing it after reading the file.
static MemoryBuffer *getOpenFile(int FD, const char *Filename,
error_code &ec,
int64_t FileSize = -1);
static error_code getOpenFile(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize = -1);
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that InputData must be null terminated.
@ -99,18 +101,18 @@ public:
/// getSTDIN - Read all of stdin into a file buffer, and return it.
/// If an error occurs, this returns null and sets ec.
static MemoryBuffer *getSTDIN(error_code &ec);
static error_code getSTDIN(OwningPtr<MemoryBuffer> &result);
/// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
/// if the Filename is "-". If an error occurs, this returns null and sets
/// ec.
static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
error_code &ec,
int64_t FileSize = -1);
static MemoryBuffer *getFileOrSTDIN(const char *Filename,
error_code &ec,
int64_t FileSize = -1);
static error_code getFileOrSTDIN(StringRef Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize = -1);
static error_code getFileOrSTDIN(const char *Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize = -1);
};
} // end namespace llvm

View File

@ -148,13 +148,13 @@ Archive::Archive(const sys::Path& filename, LLVMContext& C)
bool
Archive::mapToMemory(std::string* ErrMsg) {
error_code ec;
mapfile = MemoryBuffer::getFile(archPath.c_str(), ec);
if (mapfile == 0) {
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFile(archPath.c_str(), File)) {
if (ErrMsg)
*ErrMsg = ec.message();
return true;
}
mapfile = File.take();
base = mapfile->getBufferStart();
return false;
}
@ -218,10 +218,8 @@ bool llvm::GetBitcodeSymbols(const sys::Path& fName,
LLVMContext& Context,
std::vector<std::string>& symbols,
std::string* ErrMsg) {
error_code ec;
std::auto_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getFileOrSTDIN(fName.c_str(), ec));
if (!Buffer.get()) {
OwningPtr<MemoryBuffer> Buffer;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(fName.c_str(), Buffer)) {
if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'" + ": "
+ ec.message();
return true;
@ -246,7 +244,7 @@ llvm::GetBitcodeSymbols(const char *BufPtr, unsigned Length,
std::vector<std::string>& symbols,
std::string* ErrMsg) {
// Get the module.
std::auto_ptr<MemoryBuffer> Buffer(
OwningPtr<MemoryBuffer> Buffer(
MemoryBuffer::getMemBufferCopy(StringRef(BufPtr, Length),ModuleID.c_str()));
Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);

View File

@ -213,13 +213,13 @@ Archive::writeMember(
const char *data = (const char*)member.getData();
MemoryBuffer *mFile = 0;
if (!data) {
error_code ec;
mFile = MemoryBuffer::getFile(member.getPath().c_str(), ec);
if (mFile == 0) {
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFile(member.getPath().c_str(), File)) {
if (ErrMsg)
*ErrMsg = ec.message();
return true;
}
mFile = File.take();
data = mFile->getBufferStart();
fSize = mFile->getBufferSize();
}
@ -411,9 +411,8 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
// Map in the archive we just wrote.
{
error_code ec;
OwningPtr<MemoryBuffer> arch(MemoryBuffer::getFile(TmpArchive.c_str(), ec));
if (arch == 0) {
OwningPtr<MemoryBuffer> arch;
if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
if (ErrMsg)
*ErrMsg = ec.message();
return true;

View File

@ -42,15 +42,14 @@ Module *llvm::ParseAssembly(MemoryBuffer *F,
Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
LLVMContext &Context) {
error_code ec;
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
if (F == 0) {
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
Err = SMDiagnostic(Filename,
"Could not open input file: " + ec.message());
return 0;
}
return ParseAssembly(F, 0, Err, Context);
return ParseAssembly(File.take(), 0, Err, Context);
}
Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,

View File

@ -161,14 +161,13 @@ bool Linker::LinkInFile(const sys::Path &File, bool &is_native) {
// Check for a file of name "-", which means "read standard input"
if (File.str() == "-") {
std::auto_ptr<Module> M;
OwningPtr<MemoryBuffer> Buffer;
error_code ec;
if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN(ec)) {
if (!(ec = MemoryBuffer::getSTDIN(Buffer))) {
if (!Buffer->getBufferSize()) {
delete Buffer;
Error = "standard input is empty";
} else {
M.reset(ParseBitcodeFile(Buffer, Context, &Error));
delete Buffer;
M.reset(ParseBitcodeFile(Buffer.get(), Context, &Error));
if (M.get())
if (!LinkInModule(M.get(), &Error))
return false;

View File

@ -99,14 +99,12 @@ Linker::LoadObject(const sys::Path &FN) {
std::string ParseErrorMessage;
Module *Result = 0;
error_code ec;
std::auto_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getFileOrSTDIN(FN.c_str(), ec));
if (Buffer.get())
Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
else
OwningPtr<MemoryBuffer> Buffer;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(FN.c_str(), Buffer))
ParseErrorMessage = "Error reading file '" + FN.str() + "'" + ": "
+ ec.message();
else
Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
if (Result)
return std::auto_ptr<Module>(Result);

View File

@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Object/ObjectFile.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@ -63,6 +64,8 @@ ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
}
ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
error_code ec;
return createObjectFile(MemoryBuffer::getFile(ObjectPath, ec));
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFile(ObjectPath, File))
return NULL;
return createObjectFile(File.take());
}

View File

@ -464,11 +464,6 @@ static void ExpandResponseFiles(unsigned argc, char** argv,
const sys::FileStatus *FileStat = respFile.getFileStatus();
if (FileStat && FileStat->getSize() != 0) {
// Mmap the response file into memory.
error_code ec;
OwningPtr<MemoryBuffer>
respFilePtr(MemoryBuffer::getFile(respFile.c_str(), ec));
// If we could open the file, parse its contents, otherwise
// pass the @file option verbatim.
@ -477,7 +472,9 @@ static void ExpandResponseFiles(unsigned argc, char** argv,
// itself contain additional @file options; any such options will be
// processed recursively.")
if (respFilePtr != 0) {
// Mmap the response file into memory.
OwningPtr<MemoryBuffer> respFilePtr;
if (!MemoryBuffer::getFile(respFile.c_str(), respFilePtr)) {
ParseCStringVector(newArgv, respFilePtr->getBufferStart());
continue;
}

View File

@ -201,14 +201,14 @@ int llvm::DiffFilesWithTolerance(const sys::PathWithStatus &FileA,
// Now its safe to mmap the files into memory becasue both files
// have a non-zero size.
error_code ec;
OwningPtr<MemoryBuffer> F1(MemoryBuffer::getFile(FileA.c_str(), ec));
if (F1 == 0) {
OwningPtr<MemoryBuffer> F1;
if (error_code ec = MemoryBuffer::getFile(FileA.c_str(), F1)) {
if (Error)
*Error = ec.message();
return 2;
}
OwningPtr<MemoryBuffer> F2(MemoryBuffer::getFile(FileB.c_str(), ec));
if (F2 == 0) {
OwningPtr<MemoryBuffer> F2;
if (error_code ec = MemoryBuffer::getFile(FileB.c_str(), F2)) {
if (Error)
*Error = ec.message();
return 2;

View File

@ -35,6 +35,8 @@
#include <fcntl.h>
using namespace llvm;
namespace { const llvm::error_code success; }
//===----------------------------------------------------------------------===//
// MemoryBuffer implementation itself.
//===----------------------------------------------------------------------===//
@ -143,20 +145,20 @@ MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
/// 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.
MemoryBuffer *MemoryBuffer::getFileOrSTDIN(StringRef Filename,
error_code &ec,
int64_t FileSize) {
error_code MemoryBuffer::getFileOrSTDIN(StringRef Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize) {
if (Filename == "-")
return getSTDIN(ec);
return getFile(Filename, ec, FileSize);
return getSTDIN(result);
return getFile(Filename, result, FileSize);
}
MemoryBuffer *MemoryBuffer::getFileOrSTDIN(const char *Filename,
error_code &ec,
int64_t FileSize) {
error_code MemoryBuffer::getFileOrSTDIN(const char *Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize) {
if (strcmp(Filename, "-") == 0)
return getSTDIN(ec);
return getFile(Filename, ec, FileSize);
return getSTDIN(result);
return getFile(Filename, result, FileSize);
}
//===----------------------------------------------------------------------===//
@ -186,30 +188,32 @@ public:
};
}
MemoryBuffer *MemoryBuffer::getFile(StringRef Filename, error_code &ec,
int64_t FileSize) {
error_code MemoryBuffer::getFile(StringRef Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize) {
// Ensure the path is null terminated.
SmallString<256> PathBuf(Filename.begin(), Filename.end());
return MemoryBuffer::getFile(PathBuf.c_str(), ec, FileSize);
return MemoryBuffer::getFile(PathBuf.c_str(), result, FileSize);
}
MemoryBuffer *MemoryBuffer::getFile(const char *Filename, error_code &ec,
int64_t FileSize) {
error_code MemoryBuffer::getFile(const char *Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize) {
int OpenFlags = O_RDONLY;
#ifdef O_BINARY
OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
#endif
int FD = ::open(Filename, OpenFlags);
if (FD == -1) {
ec = error_code(errno, posix_category());
return 0;
return error_code(errno, posix_category());
}
return getOpenFile(FD, Filename, ec, FileSize);
return getOpenFile(FD, Filename, result, FileSize);
}
MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
error_code &ec, int64_t FileSize) {
error_code MemoryBuffer::getOpenFile(int FD, const char *Filename,
OwningPtr<MemoryBuffer> &result,
int64_t FileSize) {
FileCloser FC(FD); // Close FD on return.
// If we don't know the file size, use fstat to find out. fstat on an open
@ -218,8 +222,7 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
struct stat FileInfo;
// TODO: This should use fstat64 when available.
if (fstat(FD, &FileInfo) == -1) {
ec = error_code(errno, posix_category());
return 0;
return error_code(errno, posix_category());
}
FileSize = FileInfo.st_size;
}
@ -234,8 +237,9 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
if (FileSize >= 4096*4 &&
(FileSize & (sys::Process::GetPageSize()-1)) != 0) {
if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
return GetNamedBuffer<MemoryBufferMMapFile>(StringRef(Pages, FileSize),
Filename);
result.reset(GetNamedBuffer<MemoryBufferMMapFile>(
StringRef(Pages, FileSize), Filename));
return success;
}
}
@ -243,8 +247,7 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
if (!Buf) {
// Failed to create a buffer. The only way it can fail is if
// new(std::nothrow) returns 0.
ec = make_error_code(errc::not_enough_memory);
return 0;
return make_error_code(errc::not_enough_memory);
}
OwningPtr<MemoryBuffer> SB(Buf);
@ -257,26 +260,27 @@ MemoryBuffer *MemoryBuffer::getOpenFile(int FD, const char *Filename,
if (errno == EINTR)
continue;
// Error while reading.
ec = error_code(errno, posix_category());
return 0;
return error_code(errno, posix_category());
} else if (NumRead == 0) {
// We hit EOF early, truncate and terminate buffer.
Buf->BufferEnd = BufPtr;
*BufPtr = 0;
return SB.take();
result.swap(SB);
return success;
}
BytesLeft -= NumRead;
BufPtr += NumRead;
}
return SB.take();
result.swap(SB);
return success;
}
//===----------------------------------------------------------------------===//
// MemoryBuffer::getSTDIN implementation.
//===----------------------------------------------------------------------===//
MemoryBuffer *MemoryBuffer::getSTDIN(error_code &ec) {
error_code MemoryBuffer::getSTDIN(OwningPtr<MemoryBuffer> &result) {
// Read in all of the data from stdin, we cannot mmap stdin.
//
// FIXME: That isn't necessarily true, we should try to mmap stdin and
@ -292,11 +296,11 @@ MemoryBuffer *MemoryBuffer::getSTDIN(error_code &ec) {
ReadBytes = read(0, Buffer.end(), ChunkSize);
if (ReadBytes == -1) {
if (errno == EINTR) continue;
ec = error_code(errno, posix_category());
return 0;
return error_code(errno, posix_category());
}
Buffer.set_size(Buffer.size() + ReadBytes);
} while (ReadBytes != 0);
return getMemBufferCopy(Buffer, "<stdin>");
result.reset(getMemBufferCopy(Buffer, "<stdin>"));
return success;
}

View File

@ -16,6 +16,7 @@
#include "llvm/ADT/Twine.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"
using namespace llvm;
@ -49,18 +50,18 @@ SourceMgr::~SourceMgr() {
/// ~0, otherwise it returns the buffer ID of the stacked file.
unsigned SourceMgr::AddIncludeFile(const std::string &Filename,
SMLoc IncludeLoc) {
error_code ec;
MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str(), ec);
OwningPtr<MemoryBuffer> NewBuf;
MemoryBuffer::getFile(Filename.c_str(), NewBuf);
// 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) {
std::string IncFile = IncludeDirectories[i] + "/" + Filename;
NewBuf = MemoryBuffer::getFile(IncFile.c_str(), ec);
MemoryBuffer::getFile(IncFile.c_str(), NewBuf);
}
if (NewBuf == 0) return ~0U;
return AddNewSourceBuffer(NewBuf, IncludeLoc);
return AddNewSourceBuffer(NewBuf.take(), IncludeLoc);
}

View File

@ -2221,9 +2221,10 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
OwningPtr<MemoryBuffer> MB;
error_code ec;
if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, ec)) {
*OutMemBuf = wrap(MB);
if (!(ec = MemoryBuffer::getFile(Path, MB))) {
*OutMemBuf = wrap(MB.take());
return 0;
}
@ -2233,9 +2234,10 @@ LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
char **OutMessage) {
OwningPtr<MemoryBuffer> MB;
error_code ec;
if (MemoryBuffer *MB = MemoryBuffer::getSTDIN(ec)) {
*OutMemBuf = wrap(MB);
if (!(ec = MemoryBuffer::getSTDIN(MB))) {
*OutMemBuf = wrap(MB.take());
return 0;
}

View File

@ -27,6 +27,7 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Bitcode/BitstreamReader.h"
#include "llvm/Bitcode/LLVMBitCodes.h"
@ -58,7 +59,7 @@ static cl::opt<bool> NoHistogram("disable-histogram",
static cl::opt<bool>
NonSymbolic("non-symbolic",
cl::desc("Emit numberic info in dump even if"
cl::desc("Emit numeric info in dump even if"
" symbolic info is available"));
namespace {
@ -481,11 +482,10 @@ static void PrintSize(uint64_t Bits) {
/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
static int AnalyzeBitcode() {
// Read the input file.
error_code ec;
MemoryBuffer *MemBuf =
MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
OwningPtr<MemoryBuffer> MemBuf;
if (MemBuf == 0)
if (error_code ec =
MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), MemBuf))
return Error("Error reading '" + InputFilename + "': " + ec.message());
if (MemBuf->getBufferSize() & 3)

View File

@ -81,12 +81,13 @@ int main(int argc, char **argv) {
std::string ErrorMessage;
error_code ec;
std::auto_ptr<Module> M;
OwningPtr<MemoryBuffer> BufferPtr;
if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec)) {
M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
delete Buffer;
} else
if (ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr))
ErrorMessage = ec.message();
else
M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage));
MemoryBuffer *Buffer = BufferPtr.take();
if (M.get() == 0) {
errs() << argv[0] << ": ";

View File

@ -168,12 +168,12 @@ static tool_output_file *GetOutputStream() {
}
static int AsLexInput(const char *ProgName) {
error_code ec;
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
if (Buffer == 0) {
OwningPtr<MemoryBuffer> BufferPtr;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
errs() << ProgName << ": " << ec.message() << '\n';
return 1;
}
MemoryBuffer *Buffer = BufferPtr.take();
SourceMgr SrcMgr;
@ -281,12 +281,12 @@ static int AssembleInput(const char *ProgName) {
if (!TheTarget)
return 1;
error_code ec;
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
if (Buffer == 0) {
OwningPtr<MemoryBuffer> BufferPtr;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) {
errs() << ProgName << ": " << ec.message() << '\n';
return 1;
}
MemoryBuffer *Buffer = BufferPtr.take();
SourceMgr SrcMgr;
@ -387,9 +387,8 @@ static int DisassembleInput(const char *ProgName, bool Enhanced) {
if (!TheTarget)
return 0;
error_code ec;
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, ec);
if (Buffer == 0) {
OwningPtr<MemoryBuffer> Buffer;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, Buffer)) {
errs() << ProgName << ": " << ec.message() << '\n';
return 1;
}
@ -400,9 +399,11 @@ static int DisassembleInput(const char *ProgName, bool Enhanced) {
int Res;
if (Enhanced)
Res = Disassembler::disassembleEnhanced(TripleName, *Buffer, Out->os());
Res =
Disassembler::disassembleEnhanced(TripleName, *Buffer.take(), Out->os());
else
Res = Disassembler::disassemble(*TheTarget, TripleName, *Buffer, Out->os());
Res = Disassembler::disassemble(*TheTarget, TripleName,
*Buffer.take(), Out->os());
// Keep output if no errors.
if (Res == 0) Out->keep();

View File

@ -144,10 +144,8 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
sys::Path aPath(Filename);
// Note: Currently we do not support reading an archive from stdin.
if (Filename == "-" || aPath.isBitcodeFile()) {
error_code ec;
std::auto_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getFileOrSTDIN(Filename, ec));
if (Buffer.get() == 0)
OwningPtr<MemoryBuffer> Buffer;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer))
ErrorMessage = ec.message();
Module *Result = 0;
if (Buffer.get())

View File

@ -264,11 +264,11 @@ int main(int argc, char **argv) {
// Read in the bitcode file...
std::string ErrorMessage;
OwningPtr<MemoryBuffer> Buffer;
error_code ec;
Module *M = 0;
if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile, ec)) {
M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
delete Buffer;
if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) {
M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
} else
ErrorMessage = ec.message();
if (M == 0) {

View File

@ -224,10 +224,10 @@ const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
delete _nativeObjectFile;
// read .o file into memory buffer
error_code ec;
_nativeObjectFile = MemoryBuffer::getFile(uniqueObjStr.c_str(), ec);
if (ec)
OwningPtr<MemoryBuffer> BuffPtr;
if (error_code ec = MemoryBuffer::getFile(uniqueObjStr.c_str(),BuffPtr))
errMsg = ec.message();
_nativeObjectFile = BuffPtr.take();
}
// remove temp files

View File

@ -57,18 +57,17 @@ bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
bool LTOModule::isBitcodeFileForTarget(const char *path,
const char *triplePrefix) {
error_code ec;
MemoryBuffer *buffer = MemoryBuffer::getFile(path, ec);
if (buffer == NULL)
OwningPtr<MemoryBuffer> buffer;
if (MemoryBuffer::getFile(path, buffer))
return false;
return isTargetMatch(buffer, triplePrefix);
return isTargetMatch(buffer.take(), triplePrefix);
}
// Takes ownership of buffer.
bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
delete buffer;
return (strncmp(Triple.c_str(), triplePrefix,
return (strncmp(Triple.c_str(), triplePrefix,
strlen(triplePrefix)) == 0);
}
@ -80,9 +79,8 @@ LTOModule::LTOModule(Module *m, TargetMachine *t)
LTOModule *LTOModule::makeLTOModule(const char *path,
std::string &errMsg) {
error_code ec;
OwningPtr<MemoryBuffer> buffer(MemoryBuffer::getFile(path, ec));
if (!buffer) {
OwningPtr<MemoryBuffer> buffer;
if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
errMsg = ec.message();
return NULL;
}

View File

@ -366,10 +366,8 @@ int main(int argc, char **argv) {
// Load the input file.
std::string ErrorStr;
error_code ec;
OwningPtr<MemoryBuffer> InputBuffer(
MemoryBuffer::getFileOrSTDIN(InputFile, ec));
if (!InputBuffer)
OwningPtr<MemoryBuffer> InputBuffer;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFile, InputBuffer))
return Error("unable to read input: '" + ec.message() + "'");
// Construct the Mach-O wrapper object.

View File

@ -16,6 +16,7 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
@ -489,13 +490,14 @@ static MemoryBuffer *CanonicalizeInputFile(MemoryBuffer *MB) {
static bool ReadCheckFile(SourceMgr &SM,
std::vector<CheckString> &CheckStrings) {
// Open the check file, and tell SourceMgr about it.
error_code ec;
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), ec);
if (F == 0) {
OwningPtr<MemoryBuffer> File;
if (error_code ec =
MemoryBuffer::getFileOrSTDIN(CheckFilename.c_str(), File)) {
errs() << "Could not open check file '" << CheckFilename << "': "
<< ec.message() << '\n';
return true;
}
MemoryBuffer *F = File.take();
// If we want to canonicalize whitespace, strip excess whitespace from the
// buffer containing the CHECK lines.
@ -648,13 +650,14 @@ int main(int argc, char **argv) {
return 2;
// Open the file to check and add it to SourceMgr.
error_code ec;
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
if (F == 0) {
OwningPtr<MemoryBuffer> File;
if (error_code ec =
MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
errs() << "Could not open input file '" << InputFilename << "': "
<< ec.message() << '\n';
return true;
}
MemoryBuffer *F = File.take();
// Remove duplicate spaces in the input file if requested.
if (!NoCanonicalizeWhiteSpace)

View File

@ -15,6 +15,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/Signals.h"
@ -43,17 +44,16 @@ int main(int argc, char **argv) {
}
// Get the input data.
error_code ec;
MemoryBuffer *In =
MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
if (In == 0) {
OwningPtr<MemoryBuffer> In;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), In)) {
errs() << argv[0] << ": error: Unable to get input '"
<< InputFilename << "': " << ec.message() << '\n';
return 1;
}
// Get the output data.
MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), ec);
OwningPtr<MemoryBuffer> Out;
MemoryBuffer::getFile(OutputFilename.c_str(), Out);
// If the output exists and the contents match, we are done.
if (Out && In->getBufferSize() == Out->getBufferSize() &&
@ -65,8 +65,6 @@ int main(int argc, char **argv) {
return 0;
}
delete Out;
// Otherwise, overwrite the output.
if (!Quiet)
errs() << argv[0] << ": Updating '" << OutputFilename

View File

@ -37,6 +37,7 @@
#include "ARMDecoderEmitter.h"
#include "SubtargetEmitter.h"
#include "TGParser.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h"
@ -190,13 +191,13 @@ static bool ParseFile(const std::string &Filename,
const std::vector<std::string> &IncludeDirs,
SourceMgr &SrcMgr,
RecordKeeper &Records) {
error_code ec;
MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), ec);
if (F == 0) {
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
errs() << "Could not open input file '" << Filename << "': "
<< ec.message() <<"\n";
return true;
}
MemoryBuffer *F = File.take();
// Tell SrcMgr about this buffer, which is what TGParser will pick up.
SrcMgr.AddNewSourceBuffer(F, SMLoc());