From b2a5bf25f995aa7eaee09db1f21f3c3d770d6e46 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 9 Dec 2014 21:05:36 +0000 Subject: [PATCH] Return ErrorOr> form getAsArchive. This is the same return type of Archive::create. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223827 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/MachOUniversal.h | 2 +- lib/Object/MachOUniversal.cpp | 24 ++++++++++-------------- tools/llvm-nm/llvm-nm.cpp | 14 ++++++++------ tools/llvm-objdump/MachODump.cpp | 14 ++++++++------ tools/llvm-size/llvm-size.cpp | 15 +++++++++------ 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/include/llvm/Object/MachOUniversal.h b/include/llvm/Object/MachOUniversal.h index 46cf3fba80ea..9cd9b3a515dd 100644 --- a/include/llvm/Object/MachOUniversal.h +++ b/include/llvm/Object/MachOUniversal.h @@ -58,7 +58,7 @@ class MachOUniversalBinary : public Binary { ErrorOr> getAsObjectFile() const; - std::error_code getAsArchive(std::unique_ptr &Result) const; + ErrorOr> getAsArchive() const; }; class object_iterator { diff --git a/lib/Object/MachOUniversal.cpp b/lib/Object/MachOUniversal.cpp index 77aeb63eb823..cf564c4cc2f6 100644 --- a/lib/Object/MachOUniversal.cpp +++ b/lib/Object/MachOUniversal.cpp @@ -79,20 +79,16 @@ MachOUniversalBinary::ObjectForArch::getAsObjectFile() const { return object_error::parse_failed; } -std::error_code MachOUniversalBinary::ObjectForArch::getAsArchive( - std::unique_ptr &Result) const { - if (Parent) { - StringRef ParentData = Parent->getData(); - StringRef ObjectData = ParentData.substr(Header.offset, Header.size); - StringRef ObjectName = Parent->getFileName(); - MemoryBufferRef ObjBuffer(ObjectData, ObjectName); - ErrorOr> Obj = Archive::create(ObjBuffer); - if (std::error_code EC = Obj.getError()) - return EC; - Result = std::move(Obj.get()); - return object_error::success; - } - return object_error::parse_failed; +ErrorOr> +MachOUniversalBinary::ObjectForArch::getAsArchive() const { + if (!Parent) + return object_error::parse_failed; + + StringRef ParentData = Parent->getData(); + StringRef ObjectData = ParentData.substr(Header.offset, Header.size); + StringRef ObjectName = Parent->getFileName(); + MemoryBufferRef ObjBuffer(ObjectData, ObjectName); + return Archive::create(ObjBuffer); } void MachOUniversalBinary::anchor() { } diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index be2c4fad948f..021bc1b063cd 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -1069,7 +1069,6 @@ static void dumpSymbolNamesFromFile(std::string &Filename) { ArchFound = true; ErrorOr> ObjOrErr = I->getAsObjectFile(); - std::unique_ptr A; std::string ArchiveName; std::string ArchitectureName; ArchiveName.clear(); @@ -1086,7 +1085,9 @@ static void dumpSymbolNamesFromFile(std::string &Filename) { } dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName); - } else if (!I->getAsArchive(A)) { + } else if (ErrorOr> AOrErr = + I->getAsArchive()) { + std::unique_ptr &A = *AOrErr; for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); AI != AE; ++AI) { @@ -1133,13 +1134,14 @@ static void dumpSymbolNamesFromFile(std::string &Filename) { I != E; ++I) { if (HostArchName == I->getArchTypeName()) { ErrorOr> ObjOrErr = I->getAsObjectFile(); - std::unique_ptr A; std::string ArchiveName; ArchiveName.clear(); if (ObjOrErr) { ObjectFile &Obj = *ObjOrErr.get(); dumpSymbolNamesFromObject(Obj, false); - } else if (!I->getAsArchive(A)) { + } else if (ErrorOr> AOrErr = + I->getAsArchive()) { + std::unique_ptr &A = *AOrErr; for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); AI != AE; ++AI) { @@ -1170,7 +1172,6 @@ static void dumpSymbolNamesFromFile(std::string &Filename) { E = UB->end_objects(); I != E; ++I) { ErrorOr> ObjOrErr = I->getAsObjectFile(); - std::unique_ptr A; std::string ArchiveName; std::string ArchitectureName; ArchiveName.clear(); @@ -1189,7 +1190,8 @@ static void dumpSymbolNamesFromFile(std::string &Filename) { outs() << ":\n"; } dumpSymbolNamesFromObject(Obj, false, ArchiveName, ArchitectureName); - } else if (!I->getAsArchive(A)) { + } else if (ErrorOr> AOrErr = I->getAsArchive()) { + std::unique_ptr &A = *AOrErr; for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); AI != AE; ++AI) { ErrorOr> ChildOrErr = diff --git a/tools/llvm-objdump/MachODump.cpp b/tools/llvm-objdump/MachODump.cpp index 1539fc134197..c07fa3fcbec2 100644 --- a/tools/llvm-objdump/MachODump.cpp +++ b/tools/llvm-objdump/MachODump.cpp @@ -340,7 +340,6 @@ void llvm::DisassembleInputMachO(StringRef Filename) { ArchFound = true; ErrorOr> ObjOrErr = I->getAsObjectFile(); - std::unique_ptr A; std::string ArchitectureName = ""; if (ArchFlags.size() > 1) ArchitectureName = I->getArchTypeName(); @@ -348,7 +347,9 @@ void llvm::DisassembleInputMachO(StringRef Filename) { ObjectFile &O = *ObjOrErr.get(); if (MachOObjectFile *MachOOF = dyn_cast(&O)) DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName); - } else if (!I->getAsArchive(A)) { + } else if (ErrorOr> AOrErr = + I->getAsArchive()) { + std::unique_ptr &A = *AOrErr; outs() << "Archive : " << Filename; if (!ArchitectureName.empty()) outs() << " (architecture " << ArchitectureName << ")"; @@ -384,14 +385,15 @@ void llvm::DisassembleInputMachO(StringRef Filename) { I != E; ++I) { if (HostArchName == I->getArchTypeName()) { ErrorOr> ObjOrErr = I->getAsObjectFile(); - std::unique_ptr A; std::string ArchiveName; ArchiveName.clear(); if (ObjOrErr) { ObjectFile &O = *ObjOrErr.get(); if (MachOObjectFile *MachOOF = dyn_cast(&O)) DisassembleInputMachO2(Filename, MachOOF); - } else if (!I->getAsArchive(A)) { + } else if (ErrorOr> AOrErr = + I->getAsArchive()) { + std::unique_ptr &A = *AOrErr; outs() << "Archive : " << Filename << "\n"; for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); @@ -415,7 +417,6 @@ void llvm::DisassembleInputMachO(StringRef Filename) { E = UB->end_objects(); I != E; ++I) { ErrorOr> ObjOrErr = I->getAsObjectFile(); - std::unique_ptr A; std::string ArchitectureName = ""; if (moreThanOneArch) ArchitectureName = I->getArchTypeName(); @@ -423,7 +424,8 @@ void llvm::DisassembleInputMachO(StringRef Filename) { ObjectFile &Obj = *ObjOrErr.get(); if (MachOObjectFile *MachOOF = dyn_cast(&Obj)) DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName); - } else if (!I->getAsArchive(A)) { + } else if (ErrorOr> AOrErr = I->getAsArchive()) { + std::unique_ptr &A = *AOrErr; outs() << "Archive : " << Filename; if (!ArchitectureName.empty()) outs() << " (architecture " << ArchitectureName << ")"; diff --git a/tools/llvm-size/llvm-size.cpp b/tools/llvm-size/llvm-size.cpp index 59a5f20a9c79..f50336c6f807 100644 --- a/tools/llvm-size/llvm-size.cpp +++ b/tools/llvm-size/llvm-size.cpp @@ -484,7 +484,6 @@ static void PrintFileSectionSizes(StringRef file) { if (ArchFlags[i] == I->getArchTypeName()) { ArchFound = true; ErrorOr> UO = I->getAsObjectFile(); - std::unique_ptr UA; if (UO) { if (ObjectFile *o = dyn_cast(&*UO.get())) { MachOObjectFile *MachO = dyn_cast(o); @@ -503,7 +502,9 @@ static void PrintFileSectionSizes(StringRef file) { outs() << "\n"; } } - } else if (!I->getAsArchive(UA)) { + } else if (ErrorOr> AOrErr = + I->getAsArchive()) { + std::unique_ptr &UA = *AOrErr; // This is an archive. Iterate over each member and display its // sizes. for (object::Archive::child_iterator i = UA->child_begin(), @@ -560,7 +561,6 @@ static void PrintFileSectionSizes(StringRef file) { I != E; ++I) { if (HostArchName == I->getArchTypeName()) { ErrorOr> UO = I->getAsObjectFile(); - std::unique_ptr UA; if (UO) { if (ObjectFile *o = dyn_cast(&*UO.get())) { MachOObjectFile *MachO = dyn_cast(o); @@ -579,7 +579,9 @@ static void PrintFileSectionSizes(StringRef file) { outs() << "\n"; } } - } else if (!I->getAsArchive(UA)) { + } else if (ErrorOr> AOrErr = + I->getAsArchive()) { + std::unique_ptr &UA = *AOrErr; // This is an archive. Iterate over each member and display its // sizes. for (object::Archive::child_iterator i = UA->child_begin(), @@ -623,7 +625,6 @@ static void PrintFileSectionSizes(StringRef file) { E = UB->end_objects(); I != E; ++I) { ErrorOr> UO = I->getAsObjectFile(); - std::unique_ptr UA; if (UO) { if (ObjectFile *o = dyn_cast(&*UO.get())) { MachOObjectFile *MachO = dyn_cast(o); @@ -643,7 +644,9 @@ static void PrintFileSectionSizes(StringRef file) { outs() << "\n"; } } - } else if (!I->getAsArchive(UA)) { + } else if (ErrorOr> AOrErr = + I->getAsArchive()) { + std::unique_ptr &UA = *AOrErr; // This is an archive. Iterate over each member and display its sizes. for (object::Archive::child_iterator i = UA->child_begin(), e = UA->child_end();