Skip to content

Commit

Permalink
Return ErrorOr<std::unique_ptr<Archive>> form getAsArchive.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
espindola committed Dec 9, 2014
1 parent 86704cc commit b2a5bf2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion include/llvm/Object/MachOUniversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MachOUniversalBinary : public Binary {

ErrorOr<std::unique_ptr<MachOObjectFile>> getAsObjectFile() const;

std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const;
ErrorOr<std::unique_ptr<Archive>> getAsArchive() const;
};

class object_iterator {
Expand Down
24 changes: 10 additions & 14 deletions lib/Object/MachOUniversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,16 @@ MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
return object_error::parse_failed;
}

std::error_code MachOUniversalBinary::ObjectForArch::getAsArchive(
std::unique_ptr<Archive> &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<std::unique_ptr<Archive>> 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<std::unique_ptr<Archive>>
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() { }
Expand Down
14 changes: 8 additions & 6 deletions tools/llvm-nm/llvm-nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,6 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
ArchFound = true;
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
I->getAsObjectFile();
std::unique_ptr<Archive> A;
std::string ArchiveName;
std::string ArchitectureName;
ArchiveName.clear();
Expand All @@ -1086,7 +1085,9 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
}
dumpSymbolNamesFromObject(Obj, false, ArchiveName,
ArchitectureName);
} else if (!I->getAsArchive(A)) {
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
for (Archive::child_iterator AI = A->child_begin(),
AE = A->child_end();
AI != AE; ++AI) {
Expand Down Expand Up @@ -1133,13 +1134,14 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
I != E; ++I) {
if (HostArchName == I->getArchTypeName()) {
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
std::unique_ptr<Archive> A;
std::string ArchiveName;
ArchiveName.clear();
if (ObjOrErr) {
ObjectFile &Obj = *ObjOrErr.get();
dumpSymbolNamesFromObject(Obj, false);
} else if (!I->getAsArchive(A)) {
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
for (Archive::child_iterator AI = A->child_begin(),
AE = A->child_end();
AI != AE; ++AI) {
Expand Down Expand Up @@ -1170,7 +1172,6 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
E = UB->end_objects();
I != E; ++I) {
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
std::unique_ptr<Archive> A;
std::string ArchiveName;
std::string ArchitectureName;
ArchiveName.clear();
Expand All @@ -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<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
AI != AE; ++AI) {
ErrorOr<std::unique_ptr<Binary>> ChildOrErr =
Expand Down
14 changes: 8 additions & 6 deletions tools/llvm-objdump/MachODump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,16 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
ArchFound = true;
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
I->getAsObjectFile();
std::unique_ptr<Archive> A;
std::string ArchitectureName = "";
if (ArchFlags.size() > 1)
ArchitectureName = I->getArchTypeName();
if (ObjOrErr) {
ObjectFile &O = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName);
} else if (!I->getAsArchive(A)) {
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
outs() << "Archive : " << Filename;
if (!ArchitectureName.empty())
outs() << " (architecture " << ArchitectureName << ")";
Expand Down Expand Up @@ -384,14 +385,15 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
I != E; ++I) {
if (HostArchName == I->getArchTypeName()) {
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
std::unique_ptr<Archive> A;
std::string ArchiveName;
ArchiveName.clear();
if (ObjOrErr) {
ObjectFile &O = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
DisassembleInputMachO2(Filename, MachOOF);
} else if (!I->getAsArchive(A)) {
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
outs() << "Archive : " << Filename << "\n";
for (Archive::child_iterator AI = A->child_begin(),
AE = A->child_end();
Expand All @@ -415,15 +417,15 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
E = UB->end_objects();
I != E; ++I) {
ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
std::unique_ptr<Archive> A;
std::string ArchitectureName = "";
if (moreThanOneArch)
ArchitectureName = I->getArchTypeName();
if (ObjOrErr) {
ObjectFile &Obj = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName);
} else if (!I->getAsArchive(A)) {
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
outs() << "Archive : " << Filename;
if (!ArchitectureName.empty())
outs() << " (architecture " << ArchitectureName << ")";
Expand Down
15 changes: 9 additions & 6 deletions tools/llvm-size/llvm-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ static void PrintFileSectionSizes(StringRef file) {
if (ArchFlags[i] == I->getArchTypeName()) {
ArchFound = true;
ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
std::unique_ptr<Archive> UA;
if (UO) {
if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Expand All @@ -503,7 +502,9 @@ static void PrintFileSectionSizes(StringRef file) {
outs() << "\n";
}
}
} else if (!I->getAsArchive(UA)) {
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) {
std::unique_ptr<Archive> &UA = *AOrErr;
// This is an archive. Iterate over each member and display its
// sizes.
for (object::Archive::child_iterator i = UA->child_begin(),
Expand Down Expand Up @@ -560,7 +561,6 @@ static void PrintFileSectionSizes(StringRef file) {
I != E; ++I) {
if (HostArchName == I->getArchTypeName()) {
ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
std::unique_ptr<Archive> UA;
if (UO) {
if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Expand All @@ -579,7 +579,9 @@ static void PrintFileSectionSizes(StringRef file) {
outs() << "\n";
}
}
} else if (!I->getAsArchive(UA)) {
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) {
std::unique_ptr<Archive> &UA = *AOrErr;
// This is an archive. Iterate over each member and display its
// sizes.
for (object::Archive::child_iterator i = UA->child_begin(),
Expand Down Expand Up @@ -623,7 +625,6 @@ static void PrintFileSectionSizes(StringRef file) {
E = UB->end_objects();
I != E; ++I) {
ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
std::unique_ptr<Archive> UA;
if (UO) {
if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
Expand All @@ -643,7 +644,9 @@ static void PrintFileSectionSizes(StringRef file) {
outs() << "\n";
}
}
} else if (!I->getAsArchive(UA)) {
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) {
std::unique_ptr<Archive> &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();
Expand Down

0 comments on commit b2a5bf2

Please sign in to comment.