From 1fca78a9b1d78ac2e05174e8b8b0eda5fe4ab8b3 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 15 Jan 2014 19:37:43 +0000 Subject: [PATCH] Return an ErrorOr from createBinary. I did write a version returning ErrorOr >, but it is too cumbersome to use without std::move. I will keep the patch locally and submit when we switch to c++11. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199326 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/Binary.h | 12 +++--- lib/Object/Archive.cpp | 7 ++-- lib/Object/Binary.cpp | 56 +++++++++++-------------- tools/llvm-nm/llvm-nm.cpp | 15 ++++--- tools/llvm-objdump/llvm-objdump.cpp | 7 ++-- tools/llvm-readobj/llvm-readobj.cpp | 5 ++- tools/llvm-size/llvm-size.cpp | 7 ++-- tools/llvm-symbolizer/LLVMSymbolize.cpp | 21 ++++++---- tools/macho-dump/macho-dump.cpp | 5 ++- 9 files changed, 69 insertions(+), 66 deletions(-) diff --git a/include/llvm/Object/Binary.h b/include/llvm/Object/Binary.h index a3f5625cc9b..6a73e0a5b1e 100644 --- a/include/llvm/Object/Binary.h +++ b/include/llvm/Object/Binary.h @@ -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 &Result); - -error_code createBinary(StringRef Path, OwningPtr &Result); +/// to the Binary if successful. If an error is returned, +/// Source is destroyed by createBinary before returning. +ErrorOr createBinary(MemoryBuffer *Source); +ErrorOr createBinary(StringRef Path); } } diff --git a/lib/Object/Archive.cpp b/lib/Object/Archive.cpp index 71efca2b186..f91752e0ab0 100644 --- a/lib/Object/Archive.cpp +++ b/lib/Object/Archive.cpp @@ -187,9 +187,10 @@ error_code Archive::Child::getAsBinary(OwningPtr &Result) const { OwningPtr Buff; if (error_code ec = getMemoryBuffer(Buff)) return ec; - if (error_code ec = createBinary(Buff.take(), ret)) - return ec; - Result.swap(ret); + ErrorOr BinaryOrErr = createBinary(Buff.take()); + if (error_code EC = BinaryOrErr.getError()) + return EC; + Result.reset(BinaryOrErr.get()); return object_error::success; } diff --git a/lib/Object/Binary.cpp b/lib/Object/Binary.cpp index 4d5242789be..f1da11ceab5 100644 --- a/lib/Object/Binary.cpp +++ b/lib/Object/Binary.cpp @@ -42,30 +42,26 @@ StringRef Binary::getFileName() const { return Data->getBufferIdentifier(); } -error_code object::createBinary(MemoryBuffer *Source, - OwningPtr &Result) { +ErrorOr object::createBinary(MemoryBuffer *Source) { OwningPtr 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 ret(new Archive(scopedSource.take(), ec)); - if (ec) return ec; - Result.swap(ret); - return object_error::success; + OwningPtr 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 ret( - ObjectFile::createELFObjectFile(scopedSource.take())); - if (!ret) + OwningPtr 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 ret( - ObjectFile::createMachOObjectFile(scopedSource.take())); - if (!ret) + OwningPtr 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 ret(new MachOUniversalBinary(scopedSource.take(), ec)); - if (ec) return ec; - Result.swap(ret); - return object_error::success; + OwningPtr 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 ret( + OwningPtr 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 &Result) { +ErrorOr object::createBinary(StringRef Path) { OwningPtr 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()); } diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index 6822c9dbd45..b5f6321f8bc 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -580,9 +580,10 @@ static void DumpSymbolNamesFromFile(std::string &Filename) { delete Result; } } else if (magic == sys::fs::file_magic::archive) { - OwningPtr arch; - if (error(object::createBinary(Buffer.take(), arch), Filename)) + ErrorOr BinaryOrErr = object::createBinary(Buffer.take()); + if (error(BinaryOrErr.getError(), Filename)) return; + OwningPtr arch(BinaryOrErr.get()); if (object::Archive *a = dyn_cast(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 Bin; - if (error(object::createBinary(Buffer.take(), Bin), Filename)) + ErrorOr BinaryOrErr = object::createBinary(Buffer.take()); + if (error(BinaryOrErr.getError(), Filename)) return; + OwningPtr Bin(BinaryOrErr.get()); object::MachOUniversalBinary *UB = cast(Bin.get()); @@ -647,9 +649,10 @@ static void DumpSymbolNamesFromFile(std::string &Filename) { } } } else if (magic.is_object()) { - OwningPtr obj; - if (error(object::createBinary(Buffer.take(), obj), Filename)) + ErrorOr BinaryOrErr = object::createBinary(Buffer.take()); + if (error(BinaryOrErr.getError(), Filename)) return; + OwningPtr obj(BinaryOrErr.get()); if (object::ObjectFile *o = dyn_cast(obj.get())) DumpSymbolNamesFromObject(o); } else { diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index 9bc092e1881..ba21ab125f3 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -833,11 +833,12 @@ static void DumpInput(StringRef file) { } // Attempt to open the binary. - OwningPtr binary; - if (error_code ec = createBinary(file, binary)) { - errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n"; + ErrorOr BinaryOrErr = createBinary(file); + if (error_code EC = BinaryOrErr.getError()) { + errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n"; return; } + OwningPtr binary(BinaryOrErr.get()); if (Archive *a = dyn_cast(binary.get())) DumpArchive(a); diff --git a/tools/llvm-readobj/llvm-readobj.cpp b/tools/llvm-readobj/llvm-readobj.cpp index 2d9f3e50ce6..916c658ddde 100644 --- a/tools/llvm-readobj/llvm-readobj.cpp +++ b/tools/llvm-readobj/llvm-readobj.cpp @@ -260,11 +260,12 @@ static void dumpInput(StringRef File) { } // Attempt to open the binary. - OwningPtr Binary; - if (error_code EC = createBinary(File, Binary)) { + ErrorOr BinaryOrErr = createBinary(File); + if (error_code EC = BinaryOrErr.getError()) { reportError(File, EC); return; } + OwningPtr Binary(BinaryOrErr.get()); if (Archive *Arc = dyn_cast(Binary.get())) dumpArchive(Arc); diff --git a/tools/llvm-size/llvm-size.cpp b/tools/llvm-size/llvm-size.cpp index 3de6605285b..cb805c26338 100644 --- a/tools/llvm-size/llvm-size.cpp +++ b/tools/llvm-size/llvm-size.cpp @@ -244,11 +244,12 @@ static void PrintFileSectionSizes(StringRef file) { } // Attempt to open the binary. - OwningPtr binary; - if (error_code ec = createBinary(file, binary)) { - errs() << ToolName << ": " << file << ": " << ec.message() << ".\n"; + ErrorOr BinaryOrErr = createBinary(file); + if (error_code EC = BinaryOrErr.getError()) { + errs() << ToolName << ": " << file << ": " << EC.message() << ".\n"; return; } + OwningPtr binary(BinaryOrErr.get()); if (Archive *a = dyn_cast(binary.get())) { // This is an archive. Iterate over each member and display its sizes. diff --git a/tools/llvm-symbolizer/LLVMSymbolize.cpp b/tools/llvm-symbolizer/LLVMSymbolize.cpp index 751453c27fb..c522c0d3de6 100644 --- a/tools/llvm-symbolizer/LLVMSymbolize.cpp +++ b/tools/llvm-symbolizer/LLVMSymbolize.cpp @@ -301,9 +301,9 @@ LLVMSymbolizer::getOrCreateBinary(const std::string &Path) { return I->second; Binary *Bin = 0; Binary *DbgBin = 0; - OwningPtr ParsedBinary; - OwningPtr ParsedDbgBinary; - if (!error(createBinary(Path, ParsedBinary))) { + ErrorOr BinaryOrErr = createBinary(Path); + if (!error(BinaryOrErr.getError())) { + OwningPtr 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); + } } } } diff --git a/tools/macho-dump/macho-dump.cpp b/tools/macho-dump/macho-dump.cpp index 0dfbd5fa09a..4e7a0b875f1 100644 --- a/tools/macho-dump/macho-dump.cpp +++ b/tools/macho-dump/macho-dump.cpp @@ -379,9 +379,10 @@ int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n"); - OwningPtr Binary; - if (error_code EC = createBinary(InputFile, Binary)) + ErrorOr BinaryOrErr = createBinary(InputFile); + if (error_code EC = BinaryOrErr.getError()) return Error("unable to read input: '" + EC.message() + "'"); + OwningPtr Binary(BinaryOrErr.get()); const MachOObjectFile *InputObject = dyn_cast(Binary.get()); if (!InputObject)