1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 10:42:39 +01:00

Return an ErrorOr<Binary *> from createBinary.

I did write a version returning ErrorOr<OwningPtr<Binary> >, but it is too
cumbersome to use without std::move. I will keep the patch locally and submit
when we switch to c++11.

llvm-svn: 199326
This commit is contained in:
Rafael Espindola 2014-01-15 19:37:43 +00:00
parent 463134c711
commit 966babad9e
9 changed files with 69 additions and 66 deletions

View File

@ -14,8 +14,8 @@
#ifndef LLVM_OBJECT_BINARY_H
#define LLVM_OBJECT_BINARY_H
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Object/Error.h"
#include "llvm/Support/ErrorOr.h"
namespace llvm {
@ -113,13 +113,11 @@ public:
/// @brief Create a Binary from Source, autodetecting the file type.
///
/// @param Source The data to create the Binary from. Ownership is transferred
/// to Result if successful. If an error is returned, Source is destroyed
/// by createBinary before returning.
/// @param Result A pointer to the resulting Binary if no error occured.
error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result);
error_code createBinary(StringRef Path, OwningPtr<Binary> &Result);
/// to the Binary if successful. If an error is returned,
/// Source is destroyed by createBinary before returning.
ErrorOr<Binary *> createBinary(MemoryBuffer *Source);
ErrorOr<Binary *> createBinary(StringRef Path);
}
}

View File

@ -187,9 +187,10 @@ error_code Archive::Child::getAsBinary(OwningPtr<Binary> &Result) const {
OwningPtr<MemoryBuffer> Buff;
if (error_code ec = getMemoryBuffer(Buff))
return ec;
if (error_code ec = createBinary(Buff.take(), ret))
return ec;
Result.swap(ret);
ErrorOr<Binary *> BinaryOrErr = createBinary(Buff.take());
if (error_code EC = BinaryOrErr.getError())
return EC;
Result.reset(BinaryOrErr.get());
return object_error::success;
}

View File

@ -42,30 +42,26 @@ StringRef Binary::getFileName() const {
return Data->getBufferIdentifier();
}
error_code object::createBinary(MemoryBuffer *Source,
OwningPtr<Binary> &Result) {
ErrorOr<Binary *> object::createBinary(MemoryBuffer *Source) {
OwningPtr<MemoryBuffer> scopedSource(Source);
if (!Source)
return make_error_code(errc::invalid_argument);
sys::fs::file_magic type = sys::fs::identify_magic(Source->getBuffer());
error_code ec;
error_code EC;
switch (type) {
case sys::fs::file_magic::archive: {
OwningPtr<Binary> ret(new Archive(scopedSource.take(), ec));
if (ec) return ec;
Result.swap(ret);
return object_error::success;
OwningPtr<Binary> Ret(new Archive(scopedSource.take(), EC));
if (EC)
return EC;
return Ret.take();
}
case sys::fs::file_magic::elf_relocatable:
case sys::fs::file_magic::elf_executable:
case sys::fs::file_magic::elf_shared_object:
case sys::fs::file_magic::elf_core: {
OwningPtr<Binary> ret(
ObjectFile::createELFObjectFile(scopedSource.take()));
if (!ret)
OwningPtr<Binary> Ret(
ObjectFile::createELFObjectFile(scopedSource.take()));
if (!Ret)
return object_error::invalid_file_type;
Result.swap(ret);
return object_error::success;
return Ret.take();
}
case sys::fs::file_magic::macho_object:
case sys::fs::file_magic::macho_executable:
@ -77,28 +73,26 @@ error_code object::createBinary(MemoryBuffer *Source,
case sys::fs::file_magic::macho_bundle:
case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
case sys::fs::file_magic::macho_dsym_companion: {
OwningPtr<Binary> ret(
ObjectFile::createMachOObjectFile(scopedSource.take()));
if (!ret)
OwningPtr<Binary> Ret(
ObjectFile::createMachOObjectFile(scopedSource.take()));
if (!Ret)
return object_error::invalid_file_type;
Result.swap(ret);
return object_error::success;
return Ret.take();
}
case sys::fs::file_magic::macho_universal_binary: {
OwningPtr<Binary> ret(new MachOUniversalBinary(scopedSource.take(), ec));
if (ec) return ec;
Result.swap(ret);
return object_error::success;
OwningPtr<Binary> Ret(new MachOUniversalBinary(scopedSource.take(), EC));
if (EC)
return EC;
return Ret.take();
}
case sys::fs::file_magic::coff_object:
case sys::fs::file_magic::coff_import_library:
case sys::fs::file_magic::pecoff_executable: {
OwningPtr<Binary> ret(
OwningPtr<Binary> Ret(
ObjectFile::createCOFFObjectFile(scopedSource.take()));
if (!ret)
if (!Ret)
return object_error::invalid_file_type;
Result.swap(ret);
return object_error::success;
return Ret.take();
}
case sys::fs::file_magic::unknown:
case sys::fs::file_magic::bitcode:
@ -110,9 +104,9 @@ error_code object::createBinary(MemoryBuffer *Source,
llvm_unreachable("Unexpected Binary File Type");
}
error_code object::createBinary(StringRef Path, OwningPtr<Binary> &Result) {
ErrorOr<Binary *> object::createBinary(StringRef Path) {
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Path, File))
return ec;
return createBinary(File.take(), Result);
if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, File))
return EC;
return createBinary(File.take());
}

View File

@ -580,9 +580,10 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
delete Result;
}
} else if (magic == sys::fs::file_magic::archive) {
OwningPtr<Binary> arch;
if (error(object::createBinary(Buffer.take(), arch), Filename))
ErrorOr<Binary *> BinaryOrErr = object::createBinary(Buffer.take());
if (error(BinaryOrErr.getError(), Filename))
return;
OwningPtr<Binary> arch(BinaryOrErr.get());
if (object::Archive *a = dyn_cast<object::Archive>(arch.get())) {
if (ArchiveMap) {
@ -630,9 +631,10 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
}
}
} else if (magic == sys::fs::file_magic::macho_universal_binary) {
OwningPtr<Binary> Bin;
if (error(object::createBinary(Buffer.take(), Bin), Filename))
ErrorOr<Binary *> BinaryOrErr = object::createBinary(Buffer.take());
if (error(BinaryOrErr.getError(), Filename))
return;
OwningPtr<Binary> Bin(BinaryOrErr.get());
object::MachOUniversalBinary *UB =
cast<object::MachOUniversalBinary>(Bin.get());
@ -647,9 +649,10 @@ static void DumpSymbolNamesFromFile(std::string &Filename) {
}
}
} else if (magic.is_object()) {
OwningPtr<Binary> obj;
if (error(object::createBinary(Buffer.take(), obj), Filename))
ErrorOr<Binary *> BinaryOrErr = object::createBinary(Buffer.take());
if (error(BinaryOrErr.getError(), Filename))
return;
OwningPtr<Binary> obj(BinaryOrErr.get());
if (object::ObjectFile *o = dyn_cast<ObjectFile>(obj.get()))
DumpSymbolNamesFromObject(o);
} else {

View File

@ -833,11 +833,12 @@ static void DumpInput(StringRef file) {
}
// Attempt to open the binary.
OwningPtr<Binary> binary;
if (error_code ec = createBinary(file, binary)) {
errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
ErrorOr<Binary *> BinaryOrErr = createBinary(file);
if (error_code EC = BinaryOrErr.getError()) {
errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
return;
}
OwningPtr<Binary> binary(BinaryOrErr.get());
if (Archive *a = dyn_cast<Archive>(binary.get()))
DumpArchive(a);

View File

@ -260,11 +260,12 @@ static void dumpInput(StringRef File) {
}
// Attempt to open the binary.
OwningPtr<Binary> Binary;
if (error_code EC = createBinary(File, Binary)) {
ErrorOr<Binary *> BinaryOrErr = createBinary(File);
if (error_code EC = BinaryOrErr.getError()) {
reportError(File, EC);
return;
}
OwningPtr<Binary> Binary(BinaryOrErr.get());
if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
dumpArchive(Arc);

View File

@ -244,11 +244,12 @@ static void PrintFileSectionSizes(StringRef file) {
}
// Attempt to open the binary.
OwningPtr<Binary> binary;
if (error_code ec = createBinary(file, binary)) {
errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
ErrorOr<Binary *> BinaryOrErr = createBinary(file);
if (error_code EC = BinaryOrErr.getError()) {
errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
return;
}
OwningPtr<Binary> binary(BinaryOrErr.get());
if (Archive *a = dyn_cast<Archive>(binary.get())) {
// This is an archive. Iterate over each member and display its sizes.

View File

@ -301,9 +301,9 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
return I->second;
Binary *Bin = 0;
Binary *DbgBin = 0;
OwningPtr<Binary> ParsedBinary;
OwningPtr<Binary> ParsedDbgBinary;
if (!error(createBinary(Path, ParsedBinary))) {
ErrorOr<Binary *> BinaryOrErr = createBinary(Path);
if (!error(BinaryOrErr.getError())) {
OwningPtr<Binary> ParsedBinary(BinaryOrErr.get());
// Check if it's a universal binary.
Bin = ParsedBinary.take();
ParsedBinariesAndObjects.push_back(Bin);
@ -312,9 +312,10 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
// resource directory.
const std::string &ResourcePath =
getDarwinDWARFResourceForPath(Path);
error_code EC = createBinary(ResourcePath, ParsedDbgBinary);
BinaryOrErr = createBinary(ResourcePath);
error_code EC = BinaryOrErr.getError();
if (EC != errc::no_such_file_or_directory && !error(EC)) {
DbgBin = ParsedDbgBinary.take();
DbgBin = BinaryOrErr.get();
ParsedBinariesAndObjects.push_back(DbgBin);
}
}
@ -324,10 +325,12 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
uint32_t CRCHash;
std::string DebugBinaryPath;
if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) &&
findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath) &&
!error(createBinary(DebugBinaryPath, ParsedDbgBinary))) {
DbgBin = ParsedDbgBinary.take();
ParsedBinariesAndObjects.push_back(DbgBin);
findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
BinaryOrErr = createBinary(DebugBinaryPath);
if (!error(BinaryOrErr.getError())) {
DbgBin = BinaryOrErr.get();
ParsedBinariesAndObjects.push_back(DbgBin);
}
}
}
}

View File

@ -379,9 +379,10 @@ int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
OwningPtr<Binary> Binary;
if (error_code EC = createBinary(InputFile, Binary))
ErrorOr<Binary *> BinaryOrErr = createBinary(InputFile);
if (error_code EC = BinaryOrErr.getError())
return Error("unable to read input: '" + EC.message() + "'");
OwningPtr<Binary> Binary(BinaryOrErr.get());
const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get());
if (!InputObject)