[llvm] r211532 - Convert a few methods to use ErrorOr.
Rafael Espindola
rafael.espindola at gmail.com
Mon Jun 23 13:41:02 PDT 2014
Author: rafael
Date: Mon Jun 23 15:41:02 2014
New Revision: 211532
URL: http://llvm.org/viewvc/llvm-project?rev=211532&view=rev
Log:
Convert a few methods to use ErrorOr.
It used to be inconvenient to mix ErrorOr and UniquePtr, but with c++11
they work OK together.
Modified:
llvm/trunk/include/llvm/Object/MachOUniversal.h
llvm/trunk/lib/Object/MachOUniversal.cpp
llvm/trunk/tools/llvm-nm/llvm-nm.cpp
llvm/trunk/tools/llvm-size/llvm-size.cpp
llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp
Modified: llvm/trunk/include/llvm/Object/MachOUniversal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/MachOUniversal.h?rev=211532&r1=211531&r2=211532&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/MachOUniversal.h (original)
+++ llvm/trunk/include/llvm/Object/MachOUniversal.h Mon Jun 23 15:41:02 2014
@@ -57,7 +57,7 @@ public:
return Triple::getArchTypeName(MachOObjectFile::getArch(Header.cputype));
}
- std::error_code getAsObjectFile(std::unique_ptr<ObjectFile> &Result) const;
+ ErrorOr<std::unique_ptr<ObjectFile>> getAsObjectFile() const;
std::error_code getAsArchive(std::unique_ptr<Archive> &Result) const;
};
@@ -100,8 +100,8 @@ public:
return V->isMachOUniversalBinary();
}
- std::error_code getObjectForArch(Triple::ArchType Arch,
- std::unique_ptr<ObjectFile> &Result) const;
+ ErrorOr<std::unique_ptr<ObjectFile>>
+ getObjectForArch(Triple::ArchType Arch) const;
};
}
Modified: llvm/trunk/lib/Object/MachOUniversal.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOUniversal.cpp?rev=211532&r1=211531&r2=211532&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOUniversal.cpp (original)
+++ llvm/trunk/lib/Object/MachOUniversal.cpp Mon Jun 23 15:41:02 2014
@@ -67,19 +67,15 @@ MachOUniversalBinary::ObjectForArch::Obj
}
}
-std::error_code MachOUniversalBinary::ObjectForArch::getAsObjectFile(
- std::unique_ptr<ObjectFile> &Result) const {
+ErrorOr<std::unique_ptr<ObjectFile>>
+MachOUniversalBinary::ObjectForArch::getAsObjectFile() const {
if (Parent) {
StringRef ParentData = Parent->getData();
StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
std::string ObjectName = Parent->getFileName().str();
MemoryBuffer *ObjBuffer = MemoryBuffer::getMemBuffer(
ObjectData, ObjectName, false);
- ErrorOr<ObjectFile *> Obj = ObjectFile::createMachOObjectFile(ObjBuffer);
- if (std::error_code EC = Obj.getError())
- return EC;
- Result.reset(Obj.get());
- return object_error::success;
+ return ObjectFile::createMachOObjectFile(ObjBuffer);
}
return object_error::parse_failed;
}
@@ -145,14 +141,14 @@ static bool getCTMForArch(Triple::ArchTy
}
}
-std::error_code MachOUniversalBinary::getObjectForArch(
- Triple::ArchType Arch, std::unique_ptr<ObjectFile> &Result) const {
+ErrorOr<std::unique_ptr<ObjectFile>>
+MachOUniversalBinary::getObjectForArch(Triple::ArchType Arch) const {
MachO::CPUType CTM;
if (!getCTMForArch(Arch, CTM))
return object_error::arch_not_found;
for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
if (I->getCPUType() == static_cast<uint32_t>(CTM))
- return I->getAsObjectFile(Result);
+ return I->getAsObjectFile();
}
return object_error::arch_not_found;
}
Modified: llvm/trunk/tools/llvm-nm/llvm-nm.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-nm/llvm-nm.cpp?rev=211532&r1=211531&r2=211532&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Mon Jun 23 15:41:02 2014
@@ -773,9 +773,10 @@ static void dumpSymbolNamesFromFile(std:
for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
E = UB->end_objects();
I != E; ++I) {
- std::unique_ptr<ObjectFile> Obj;
+ ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile();
std::unique_ptr<Archive> A;
- if (!I->getAsObjectFile(Obj)) {
+ if (ObjOrErr) {
+ std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
if (moreThanOneArch)
outs() << "\n";
outs() << Obj->getFileName();
Modified: llvm/trunk/tools/llvm-size/llvm-size.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-size/llvm-size.cpp?rev=211532&r1=211531&r2=211532&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-size/llvm-size.cpp (original)
+++ llvm/trunk/tools/llvm-size/llvm-size.cpp Mon Jun 23 15:41:02 2014
@@ -466,9 +466,9 @@ static void PrintFileSectionSizes(String
for (MachOUniversalBinary::object_iterator I = UB->begin_objects(),
E = UB->end_objects();
I != E; ++I) {
- std::unique_ptr<ObjectFile> UO;
+ ErrorOr<std::unique_ptr<ObjectFile>> UO = I->getAsObjectFile();
std::unique_ptr<Archive> UA;
- if (!I->getAsObjectFile(UO)) {
+ if (UO) {
if (ObjectFile *o = dyn_cast<ObjectFile>(&*UO.get())) {
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o);
if (OutputFormat == sysv)
Modified: llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp?rev=211532&r1=211531&r2=211532&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp (original)
+++ llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp Mon Jun 23 15:41:02 2014
@@ -349,10 +349,11 @@ LLVMSymbolizer::getObjectFileFromBinary(
std::make_pair(UB, ArchName));
if (I != ObjectFileForArch.end())
return I->second;
- std::unique_ptr<ObjectFile> ParsedObj;
- if (!UB->getObjectForArch(Triple(ArchName).getArch(), ParsedObj)) {
- Res = ParsedObj.get();
- ParsedBinariesAndObjects.push_back(std::move(ParsedObj));
+ ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj =
+ UB->getObjectForArch(Triple(ArchName).getArch());
+ if (ParsedObj) {
+ Res = ParsedObj.get().get();
+ ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get()));
}
ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
} else if (Bin->isObject()) {
More information about the llvm-commits
mailing list