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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -244,11 +244,12 @@ static void PrintFileSectionSizes(StringRef file) {
} }
// Attempt to open the binary. // Attempt to open the binary.
OwningPtr<Binary> binary; ErrorOr<Binary *> BinaryOrErr = createBinary(file);
if (error_code ec = createBinary(file, binary)) { if (error_code EC = BinaryOrErr.getError()) {
errs() << ToolName << ": " << file << ": " << ec.message() << ".\n"; errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
return; return;
} }
OwningPtr<Binary> binary(BinaryOrErr.get());
if (Archive *a = dyn_cast<Archive>(binary.get())) { if (Archive *a = dyn_cast<Archive>(binary.get())) {
// This is an archive. Iterate over each member and display its sizes. // 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; return I->second;
Binary *Bin = 0; Binary *Bin = 0;
Binary *DbgBin = 0; Binary *DbgBin = 0;
OwningPtr<Binary> ParsedBinary; ErrorOr<Binary *> BinaryOrErr = createBinary(Path);
OwningPtr<Binary> ParsedDbgBinary; if (!error(BinaryOrErr.getError())) {
if (!error(createBinary(Path, ParsedBinary))) { OwningPtr<Binary> ParsedBinary(BinaryOrErr.get());
// Check if it's a universal binary. // Check if it's a universal binary.
Bin = ParsedBinary.take(); Bin = ParsedBinary.take();
ParsedBinariesAndObjects.push_back(Bin); ParsedBinariesAndObjects.push_back(Bin);
@ -312,9 +312,10 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
// resource directory. // resource directory.
const std::string &ResourcePath = const std::string &ResourcePath =
getDarwinDWARFResourceForPath(Path); 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)) { if (EC != errc::no_such_file_or_directory && !error(EC)) {
DbgBin = ParsedDbgBinary.take(); DbgBin = BinaryOrErr.get();
ParsedBinariesAndObjects.push_back(DbgBin); ParsedBinariesAndObjects.push_back(DbgBin);
} }
} }
@ -324,10 +325,12 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) {
uint32_t CRCHash; uint32_t CRCHash;
std::string DebugBinaryPath; std::string DebugBinaryPath;
if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) && if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) &&
findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath) && findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
!error(createBinary(DebugBinaryPath, ParsedDbgBinary))) { BinaryOrErr = createBinary(DebugBinaryPath);
DbgBin = ParsedDbgBinary.take(); if (!error(BinaryOrErr.getError())) {
ParsedBinariesAndObjects.push_back(DbgBin); 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"); cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
OwningPtr<Binary> Binary; ErrorOr<Binary *> BinaryOrErr = createBinary(InputFile);
if (error_code EC = createBinary(InputFile, Binary)) if (error_code EC = BinaryOrErr.getError())
return Error("unable to read input: '" + EC.message() + "'"); return Error("unable to read input: '" + EC.message() + "'");
OwningPtr<Binary> Binary(BinaryOrErr.get());
const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get()); const MachOObjectFile *InputObject = dyn_cast<MachOObjectFile>(Binary.get());
if (!InputObject) if (!InputObject)