[llvm] r357855 - [llvm-objdump] Simplify Expected<T> handling with unwrapOrError
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 7 01:19:55 PDT 2019
Author: maskray
Date: Sun Apr 7 01:19:55 2019
New Revision: 357855
URL: http://llvm.org/viewvc/llvm-project?rev=357855&view=rev
Log:
[llvm-objdump] Simplify Expected<T> handling with unwrapOrError
Modified:
llvm/trunk/tools/llvm-objdump/ELFDump.cpp
llvm/trunk/tools/llvm-objdump/MachODump.cpp
llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
llvm/trunk/tools/llvm-objdump/llvm-objdump.h
Modified: llvm/trunk/tools/llvm-objdump/ELFDump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/ELFDump.cpp?rev=357855&r1=357854&r2=357855&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/ELFDump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/ELFDump.cpp Sun Apr 7 01:19:55 2019
@@ -163,16 +163,10 @@ uint64_t llvm::getELFSectionLMA(const ob
template <class ELFT>
void printDynamicSection(const ELFFile<ELFT> *Elf, StringRef Filename) {
- auto ProgramHeaderOrError = Elf->program_headers();
- if (!ProgramHeaderOrError)
- report_error(Filename, ProgramHeaderOrError.takeError());
-
- auto DynamicEntriesOrError = Elf->dynamicEntries();
- if (!DynamicEntriesOrError)
- report_error(Filename, DynamicEntriesOrError.takeError());
-
+ ArrayRef<typename ELFT::Dyn> DynamicEntries =
+ unwrapOrError(Elf->dynamicEntries(), Filename);
outs() << "Dynamic Section:\n";
- for (const auto &Dyn : *DynamicEntriesOrError) {
+ for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
if (Dyn.d_tag == ELF::DT_NULL)
continue;
@@ -331,34 +325,23 @@ void printSymbolVersionDefinition(const
template <class ELFT>
void printSymbolVersionInfo(const ELFFile<ELFT> *Elf, StringRef FileName) {
- typedef typename ELFT::Shdr Elf_Shdr;
-
- auto SectionsOrError = Elf->sections();
- if (!SectionsOrError)
- report_error(FileName, SectionsOrError.takeError());
-
- for (const Elf_Shdr &Shdr : *SectionsOrError) {
+ ArrayRef<typename ELFT::Shdr> Sections =
+ unwrapOrError(Elf->sections(), FileName);
+ for (const typename ELFT::Shdr &Shdr : Sections) {
if (Shdr.sh_type != ELF::SHT_GNU_verneed &&
Shdr.sh_type != ELF::SHT_GNU_verdef)
continue;
- auto ContentsOrError = Elf->getSectionContents(&Shdr);
- if (!ContentsOrError)
- report_error(FileName, ContentsOrError.takeError());
-
- auto StrTabSecOrError = Elf->getSection(Shdr.sh_link);
- if (!StrTabSecOrError)
- report_error(FileName, StrTabSecOrError.takeError());
-
- auto StrTabOrError = Elf->getStringTable(*StrTabSecOrError);
- if (!StrTabOrError)
- report_error(FileName, StrTabOrError.takeError());
+ ArrayRef<uint8_t> Contents =
+ unwrapOrError(Elf->getSectionContents(&Shdr), FileName);
+ const typename ELFT::Shdr *StrTabSec =
+ unwrapOrError(Elf->getSection(Shdr.sh_link), FileName);
+ StringRef StrTab = unwrapOrError(Elf->getStringTable(StrTabSec), FileName);
if (Shdr.sh_type == ELF::SHT_GNU_verneed)
- printSymbolVersionDependency<ELFT>(*ContentsOrError, *StrTabOrError);
+ printSymbolVersionDependency<ELFT>(Contents, StrTab);
else
- printSymbolVersionDefinition<ELFT>(Shdr, *ContentsOrError,
- *StrTabOrError);
+ printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab);
}
}
Modified: llvm/trunk/tools/llvm-objdump/MachODump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/MachODump.cpp?rev=357855&r1=357854&r2=357855&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/MachODump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/MachODump.cpp Sun Apr 7 01:19:55 2019
@@ -179,11 +179,11 @@ struct SymbolSorter {
bool operator()(const SymbolRef &A, const SymbolRef &B) {
Expected<SymbolRef::Type> ATypeOrErr = A.getType();
if (!ATypeOrErr)
- report_error(A.getObject()->getFileName(), ATypeOrErr.takeError());
+ report_error(ATypeOrErr.takeError(), A.getObject()->getFileName());
SymbolRef::Type AType = *ATypeOrErr;
Expected<SymbolRef::Type> BTypeOrErr = B.getType();
if (!BTypeOrErr)
- report_error(B.getObject()->getFileName(), BTypeOrErr.takeError());
+ report_error(BTypeOrErr.takeError(), B.getObject()->getFileName());
SymbolRef::Type BType = *BTypeOrErr;
uint64_t AAddr = (AType != SymbolRef::ST_Function) ? 0 : A.getValue();
uint64_t BAddr = (BType != SymbolRef::ST_Function) ? 0 : B.getValue();
@@ -307,11 +307,10 @@ static void getSectionsAndSymbols(MachOO
std::vector<SymbolRef> &Symbols,
SmallVectorImpl<uint64_t> &FoundFns,
uint64_t &BaseSegmentAddress) {
+ const StringRef FileName = MachOObj->getFileName();
for (const SymbolRef &Symbol : MachOObj->symbols()) {
- Expected<StringRef> SymName = Symbol.getName();
- if (!SymName)
- report_error(MachOObj->getFileName(), SymName.takeError());
- if (!SymName->startswith("ltmp"))
+ StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
+ if (!SymName.startswith("ltmp"))
Symbols.push_back(Symbol);
}
@@ -348,19 +347,15 @@ static void printRelocationTargetName(co
// generating pretty output, scan through the symbol table looking for a
// symbol that aligns with that address. If we find one, print it.
// Otherwise, we just print the hex address of the target.
+ const StringRef FileName = O->getFileName();
if (O->isRelocationScattered(RE)) {
uint32_t Val = O->getPlainRelocationSymbolNum(RE);
for (const SymbolRef &Symbol : O->symbols()) {
- Expected<uint64_t> Addr = Symbol.getAddress();
- if (!Addr)
- report_error(O->getFileName(), Addr.takeError());
- if (*Addr != Val)
+ uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
+ if (Addr != Val)
continue;
- Expected<StringRef> Name = Symbol.getName();
- if (!Name)
- report_error(O->getFileName(), Name.takeError());
- Fmt << *Name;
+ Fmt << unwrapOrError(Symbol.getName(), FileName);
return;
}
@@ -395,10 +390,7 @@ static void printRelocationTargetName(co
if (isExtern) {
symbol_iterator SI = O->symbol_begin();
advance(SI, Val);
- Expected<StringRef> SOrErr = SI->getName();
- if (!SOrErr)
- report_error(O->getFileName(), SOrErr.takeError());
- S = *SOrErr;
+ S = unwrapOrError(SI->getName(), FileName);
} else {
section_iterator SI = O->section_begin();
// Adjust for the fact that sections are 1-indexed.
@@ -646,10 +638,7 @@ static void PrintIndirectSymbolTable(Mac
if (indirect_symbol < Symtab.nsyms) {
symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
SymbolRef Symbol = *Sym;
- Expected<StringRef> SymName = Symbol.getName();
- if (!SymName)
- report_error(O->getFileName(), SymName.takeError());
- outs() << *SymName;
+ outs() << unwrapOrError(Symbol.getName(), O->getFileName());
} else {
outs() << "?";
}
@@ -1199,18 +1188,13 @@ typedef DenseMap<uint64_t, StringRef> Sy
static void CreateSymbolAddressMap(MachOObjectFile *O,
SymbolAddressMap *AddrMap) {
// Create a map of symbol addresses to symbol names.
+ const StringRef FileName = O->getFileName();
for (const SymbolRef &Symbol : O->symbols()) {
- Expected<SymbolRef::Type> STOrErr = Symbol.getType();
- if (!STOrErr)
- report_error(O->getFileName(), STOrErr.takeError());
- SymbolRef::Type ST = *STOrErr;
+ SymbolRef::Type ST = unwrapOrError(Symbol.getType(), FileName);
if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
ST == SymbolRef::ST_Other) {
uint64_t Address = Symbol.getValue();
- Expected<StringRef> SymNameOrErr = Symbol.getName();
- if (!SymNameOrErr)
- report_error(O->getFileName(), SymNameOrErr.takeError());
- StringRef SymName = *SymNameOrErr;
+ StringRef SymName = unwrapOrError(Symbol.getName(), FileName);
if (!SymName.startswith(".objc"))
(*AddrMap)[Address] = SymName;
}
@@ -1443,10 +1427,8 @@ static void DumpLiteralPointerSection(Ma
});
if (Reloc != Relocs.end()) {
symbol_iterator RelocSym = Reloc->second;
- Expected<StringRef> SymName = RelocSym->getName();
- if (!SymName)
- report_error(O->getFileName(), SymName.takeError());
- outs() << "external relocation entry for symbol:" << *SymName << "\n";
+ StringRef SymName = unwrapOrError(RelocSym->getName(), O->getFileName());
+ outs() << "external relocation entry for symbol:" << SymName << "\n";
continue;
}
@@ -1590,10 +1572,7 @@ static void DumpInitTermPointerSection(M
});
if (Reloc != Relocs.end()) {
symbol_iterator RelocSym = Reloc->second;
- Expected<StringRef> SymName = RelocSym->getName();
- if (!SymName)
- report_error(O->getFileName(), SymName.takeError());
- outs() << " " << *SymName;
+ outs() << " " << unwrapOrError(RelocSym->getName(), O->getFileName());
} else {
SymbolName = GuessSymbolName(p, AddrMap);
if (SymbolName)
@@ -1866,8 +1845,8 @@ static void ProcessMachO(StringRef Name,
// the error message.
if (Disassemble || IndirectSymbols || !FilterSections.empty() || UnwindInfo)
if (Error Err = MachOOF->checkSymbolTable())
- report_error(ArchiveName, FileName, std::move(Err), ArchitectureName);
-
+ report_error(std::move(Err), ArchiveName, FileName, ArchitectureName);
+
if (DisassembleAll) {
for (const SectionRef &Section : MachOOF->sections()) {
StringRef SectName;
@@ -2119,10 +2098,8 @@ static void printArchiveChild(StringRef
StringRef ArchitectureName = StringRef()) {
if (print_offset)
outs() << C.getChildOffset() << "\t";
- Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
- if (!ModeOrErr)
- report_error(Filename, C, ModeOrErr.takeError(), ArchitectureName);
- sys::fs::perms Mode = ModeOrErr.get();
+ sys::fs::perms Mode =
+ unwrapOrError(C.getAccessMode(), Filename, C, ArchitectureName);
if (verbose) {
// FIXME: this first dash, "-", is for (Mode & S_IFMT) == S_IFREG.
// But there is nothing in sys::fs::perms for S_IFMT or S_IFREG.
@@ -2140,20 +2117,11 @@ static void printArchiveChild(StringRef
outs() << format("0%o ", Mode);
}
- Expected<unsigned> UIDOrErr = C.getUID();
- if (!UIDOrErr)
- report_error(Filename, C, UIDOrErr.takeError(), ArchitectureName);
- unsigned UID = UIDOrErr.get();
- outs() << format("%3d/", UID);
- Expected<unsigned> GIDOrErr = C.getGID();
- if (!GIDOrErr)
- report_error(Filename, C, GIDOrErr.takeError(), ArchitectureName);
- unsigned GID = GIDOrErr.get();
- outs() << format("%-3d ", GID);
- Expected<uint64_t> Size = C.getRawSize();
- if (!Size)
- report_error(Filename, C, Size.takeError(), ArchitectureName);
- outs() << format("%5" PRId64, Size.get()) << " ";
+ outs() << format(
+ "%3d/%-3d %5" PRId64 " ",
+ unwrapOrError(C.getUID(), Filename, C, ArchitectureName),
+ unwrapOrError(C.getGID(), Filename, C, ArchitectureName),
+ unwrapOrError(C.getRawSize(), Filename, C, ArchitectureName));
StringRef RawLastModified = C.getRawLastModified();
if (verbose) {
@@ -2176,21 +2144,15 @@ static void printArchiveChild(StringRef
Expected<StringRef> NameOrErr = C.getName();
if (!NameOrErr) {
consumeError(NameOrErr.takeError());
- Expected<StringRef> NameOrErr = C.getRawName();
- if (!NameOrErr)
- report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
- StringRef RawName = NameOrErr.get();
- outs() << RawName << "\n";
+ outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
+ << "\n";
} else {
StringRef Name = NameOrErr.get();
outs() << Name << "\n";
}
} else {
- Expected<StringRef> NameOrErr = C.getRawName();
- if (!NameOrErr)
- report_error(Filename, C, NameOrErr.takeError(), ArchitectureName);
- StringRef RawName = NameOrErr.get();
- outs() << RawName << "\n";
+ outs() << unwrapOrError(C.getRawName(), Filename, C, ArchitectureName)
+ << "\n";
}
}
@@ -2198,12 +2160,11 @@ static void printArchiveHeaders(StringRe
bool print_offset,
StringRef ArchitectureName = StringRef()) {
Error Err = Error::success();
- ;
for (const auto &C : A->children(Err, false))
printArchiveChild(Filename, C, verbose, print_offset, ArchitectureName);
if (Err)
- report_error(StringRef(), Filename, std::move(Err), ArchitectureName);
+ report_error(std::move(Err), StringRef(), Filename, ArchitectureName);
}
static bool ValidateArchFlags() {
@@ -2234,8 +2195,8 @@ void llvm::parseInputMachO(StringRef Fil
// Attempt to open the binary.
Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(Filename);
if (!BinaryOrErr) {
- if (auto E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
- report_error(Filename, std::move(E));
+ if (Error E = isNotObjectErrorInvalidFileType(BinaryOrErr.takeError()))
+ report_error(std::move(E), Filename);
else
outs() << Filename << ": is not an object file\n";
return;
@@ -2251,8 +2212,8 @@ void llvm::parseInputMachO(StringRef Fil
for (auto &C : A->children(Err)) {
Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
if (!ChildOrErr) {
- if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
- report_error(Filename, C, std::move(E));
+ if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
+ report_error(std::move(E), Filename, C);
continue;
}
if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
@@ -2262,7 +2223,7 @@ void llvm::parseInputMachO(StringRef Fil
}
}
if (Err)
- report_error(Filename, std::move(Err));
+ report_error(std::move(Err), Filename);
return;
}
if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Bin)) {
@@ -2312,13 +2273,12 @@ void llvm::parseInputMachO(MachOUniversa
ObjectFile &O = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
ProcessMachO(Filename, MachOOF, "", ArchitectureName);
- } else if (auto E = isNotObjectErrorInvalidFileType(
- ObjOrErr.takeError())) {
- report_error(Filename, StringRef(), std::move(E),
- ArchitectureName);
+ } else if (Error E = isNotObjectErrorInvalidFileType(
+ ObjOrErr.takeError())) {
+ report_error(std::move(E), Filename, StringRef(), ArchitectureName);
continue;
} else if (Expected<std::unique_ptr<Archive>> AOrErr =
- I->getAsArchive()) {
+ I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
outs() << "Archive : " << Filename;
if (!ArchitectureName.empty())
@@ -2331,8 +2291,8 @@ void llvm::parseInputMachO(MachOUniversa
for (auto &C : A->children(Err)) {
Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
if (!ChildOrErr) {
- if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
- report_error(Filename, C, std::move(E), ArchitectureName);
+ if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
+ report_error(std::move(E), Filename, C, ArchitectureName);
continue;
}
if (MachOObjectFile *O =
@@ -2340,7 +2300,7 @@ void llvm::parseInputMachO(MachOUniversa
ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
}
if (Err)
- report_error(Filename, std::move(Err));
+ report_error(std::move(Err), Filename);
} else {
consumeError(AOrErr.takeError());
error("Mach-O universal file: " + Filename + " for " +
@@ -2373,11 +2333,11 @@ void llvm::parseInputMachO(MachOUniversa
ObjectFile &O = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
ProcessMachO(Filename, MachOOF);
- } else if (auto E = isNotObjectErrorInvalidFileType(
- ObjOrErr.takeError())) {
- report_error(Filename, std::move(E));
+ } else if (Error E =
+ isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
+ report_error(std::move(E), Filename);
} else if (Expected<std::unique_ptr<Archive>> AOrErr =
- I->getAsArchive()) {
+ I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
outs() << "Archive : " << Filename << "\n";
if (ArchiveHeaders)
@@ -2387,8 +2347,9 @@ void llvm::parseInputMachO(MachOUniversa
for (auto &C : A->children(Err)) {
Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
if (!ChildOrErr) {
- if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
- report_error(Filename, C, std::move(E));
+ if (Error E =
+ isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
+ report_error(std::move(E), Filename, C);
continue;
}
if (MachOObjectFile *O =
@@ -2396,7 +2357,7 @@ void llvm::parseInputMachO(MachOUniversa
ProcessMachO(Filename, O, O->getFileName());
}
if (Err)
- report_error(Filename, std::move(Err));
+ report_error(std::move(Err), Filename);
} else {
consumeError(AOrErr.takeError());
error("Mach-O universal file: " + Filename + " for architecture " +
@@ -2421,11 +2382,10 @@ void llvm::parseInputMachO(MachOUniversa
ObjectFile &Obj = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
ProcessMachO(Filename, MachOOF, "", ArchitectureName);
- } else if (auto E = isNotObjectErrorInvalidFileType(
- ObjOrErr.takeError())) {
- report_error(StringRef(), Filename, std::move(E), ArchitectureName);
- } else if (Expected<std::unique_ptr<Archive>> AOrErr =
- I->getAsArchive()) {
+ } else if (Error E =
+ isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
+ report_error(std::move(E), StringRef(), Filename, ArchitectureName);
+ } else if (Expected<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr;
outs() << "Archive : " << Filename;
if (!ArchitectureName.empty())
@@ -2438,8 +2398,8 @@ void llvm::parseInputMachO(MachOUniversa
for (auto &C : A->children(Err)) {
Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
if (!ChildOrErr) {
- if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
- report_error(Filename, C, std::move(E), ArchitectureName);
+ if (Error E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
+ report_error(std::move(E), Filename, C, ArchitectureName);
continue;
}
if (MachOObjectFile *O =
@@ -2450,7 +2410,7 @@ void llvm::parseInputMachO(MachOUniversa
}
}
if (Err)
- report_error(Filename, std::move(Err));
+ report_error(std::move(Err), Filename);
} else {
consumeError(AOrErr.takeError());
error("Mach-O universal file: " + Filename + " for architecture " +
@@ -2565,12 +2525,9 @@ static int SymbolizerGetOpInfo(void *Dis
}
}
if (reloc_found && isExtern) {
- Expected<StringRef> SymName = Symbol.getName();
- if (!SymName)
- report_error(info->O->getFileName(), SymName.takeError());
- const char *name = SymName->data();
op_info->AddSymbol.Present = 1;
- op_info->AddSymbol.Name = name;
+ op_info->AddSymbol.Name =
+ unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
// For i386 extern relocation entries the value in the instruction is
// the offset from the symbol, and value is already set in op_info->Value.
return 1;
@@ -2629,10 +2586,8 @@ static int SymbolizerGetOpInfo(void *Dis
// is the offset from the external symbol.
if (info->O->getAnyRelocationPCRel(RE))
op_info->Value -= Pc + Offset + Size;
- Expected<StringRef> SymName = Symbol.getName();
- if (!SymName)
- report_error(info->O->getFileName(), SymName.takeError());
- const char *name = SymName->data();
+ const char *name =
+ unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
op_info->AddSymbol.Present = 1;
op_info->AddSymbol.Name = name;
return 1;
@@ -2669,10 +2624,8 @@ static int SymbolizerGetOpInfo(void *Dis
// is the offset from the external symbol.
if (info->O->getAnyRelocationPCRel(RE))
op_info->Value -= Pc + Offset + Size;
- Expected<StringRef> SymName = Symbol.getName();
- if (!SymName)
- report_error(info->O->getFileName(), SymName.takeError());
- const char *name = SymName->data();
+ const char *name =
+ unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
unsigned Type = info->O->getAnyRelocationType(RE);
if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
DataRefImpl RelNext = Rel;
@@ -2686,10 +2639,7 @@ static int SymbolizerGetOpInfo(void *Dis
op_info->SubtractSymbol.Name = name;
symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
Symbol = *RelocSymNext;
- Expected<StringRef> SymNameNext = Symbol.getName();
- if (!SymNameNext)
- report_error(info->O->getFileName(), SymNameNext.takeError());
- name = SymNameNext->data();
+ name = unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
}
}
// TODO: add the VariantKinds to op_info->VariantKind for relocation types
@@ -2758,10 +2708,8 @@ static int SymbolizerGetOpInfo(void *Dis
}
if (isExtern) {
- Expected<StringRef> SymName = Symbol.getName();
- if (!SymName)
- report_error(info->O->getFileName(), SymName.takeError());
- const char *name = SymName->data();
+ const char *name =
+ unwrapOrError(Symbol.getName(), info->O->getFileName()).data();
op_info->AddSymbol.Present = 1;
op_info->AddSymbol.Name = name;
switch (r_type) {
@@ -2877,10 +2825,9 @@ static int SymbolizerGetOpInfo(void *Dis
// NOTE: Scattered relocations don't exist on arm64.
if (!info->O->getPlainRelocationExternal(RE))
return 0;
- Expected<StringRef> SymName = Reloc->getSymbol()->getName();
- if (!SymName)
- report_error(info->O->getFileName(), SymName.takeError());
- const char *name = SymName->data();
+ const char *name =
+ unwrapOrError(Reloc->getSymbol()->getName(), info->O->getFileName())
+ .data();
op_info->AddSymbol.Present = 1;
op_info->AddSymbol.Name = name;
@@ -3006,12 +2953,8 @@ static const char *GuessIndirectSymbol(u
info->O->getIndirectSymbolTableEntry(Dysymtab, index);
if (indirect_symbol < Symtab.nsyms) {
symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
- SymbolRef Symbol = *Sym;
- Expected<StringRef> SymName = Symbol.getName();
- if (!SymName)
- report_error(info->O->getFileName(), SymName.takeError());
- const char *name = SymName->data();
- return name;
+ return unwrapOrError(Sym->getName(), info->O->getFileName())
+ .data();
}
}
}
@@ -3041,12 +2984,8 @@ static const char *GuessIndirectSymbol(u
info->O->getIndirectSymbolTableEntry(Dysymtab, index);
if (indirect_symbol < Symtab.nsyms) {
symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
- SymbolRef Symbol = *Sym;
- Expected<StringRef> SymName = Symbol.getName();
- if (!SymName)
- report_error(info->O->getFileName(), SymName.takeError());
- const char *name = SymName->data();
- return name;
+ return unwrapOrError(Sym->getName(), info->O->getFileName())
+ .data();
}
}
}
@@ -3272,10 +3211,7 @@ static const char *get_symbol_64(uint32_
const char *SymbolName = nullptr;
if (reloc_found && isExtern) {
n_value = Symbol.getValue();
- Expected<StringRef> NameOrError = Symbol.getName();
- if (!NameOrError)
- report_error(info->O->getFileName(), NameOrError.takeError());
- StringRef Name = *NameOrError;
+ StringRef Name = unwrapOrError(Symbol.getName(), info->O->getFileName());
if (!Name.empty()) {
SymbolName = Name.data();
return SymbolName;
@@ -7238,19 +7174,14 @@ static void DisassembleMachO(StringRef F
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
MemoryBuffer::getFileOrSTDIN(DSYMFile);
if (std::error_code EC = BufOrErr.getError()) {
- report_error(DSYMFile, errorCodeToError(EC));
- return;
- }
-
- Expected<std::unique_ptr<MachOObjectFile>> DbgObjCheck =
- ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef());
-
- if (Error E = DbgObjCheck.takeError()) {
- report_error(DSYMFile, std::move(E));
+ report_error(errorCodeToError(EC), DSYMFile);
return;
}
- DbgObj = DbgObjCheck.get().release();
+ std::unique_ptr<MachOObjectFile> DbgObjCheck = unwrapOrError(
+ ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef()),
+ DSYMFile.getValue());
+ DbgObj = DbgObjCheck.release();
// We need to keep the file alive, because we're replacing DbgObj with it.
DSYMBuf = std::move(BufOrErr.get());
}
@@ -7285,17 +7216,13 @@ static void DisassembleMachO(StringRef F
SymbolAddressMap AddrMap;
bool DisSymNameFound = false;
for (const SymbolRef &Symbol : MachOOF->symbols()) {
- Expected<SymbolRef::Type> STOrErr = Symbol.getType();
- if (!STOrErr)
- report_error(MachOOF->getFileName(), STOrErr.takeError());
- SymbolRef::Type ST = *STOrErr;
+ SymbolRef::Type ST =
+ unwrapOrError(Symbol.getType(), MachOOF->getFileName());
if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
ST == SymbolRef::ST_Other) {
uint64_t Address = Symbol.getValue();
- Expected<StringRef> SymNameOrErr = Symbol.getName();
- if (!SymNameOrErr)
- report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
- StringRef SymName = *SymNameOrErr;
+ StringRef SymName =
+ unwrapOrError(Symbol.getName(), MachOOF->getFileName());
AddrMap[Address] = SymName;
if (!DisSymName.empty() && DisSymName == SymName)
DisSymNameFound = true;
@@ -7332,15 +7259,10 @@ static void DisassembleMachO(StringRef F
// Disassemble symbol by symbol.
for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
- Expected<StringRef> SymNameOrErr = Symbols[SymIdx].getName();
- if (!SymNameOrErr)
- report_error(MachOOF->getFileName(), SymNameOrErr.takeError());
- StringRef SymName = *SymNameOrErr;
-
- Expected<SymbolRef::Type> STOrErr = Symbols[SymIdx].getType();
- if (!STOrErr)
- report_error(MachOOF->getFileName(), STOrErr.takeError());
- SymbolRef::Type ST = *STOrErr;
+ StringRef SymName =
+ unwrapOrError(Symbols[SymIdx].getName(), MachOOF->getFileName());
+ SymbolRef::Type ST =
+ unwrapOrError(Symbols[SymIdx].getType(), MachOOF->getFileName());
if (ST != SymbolRef::ST_Function && ST != SymbolRef::ST_Data)
continue;
@@ -7393,10 +7315,8 @@ static void DisassembleMachO(StringRef F
uint64_t NextSym = 0;
uint64_t NextSymIdx = SymIdx + 1;
while (Symbols.size() > NextSymIdx) {
- Expected<SymbolRef::Type> STOrErr = Symbols[NextSymIdx].getType();
- if (!STOrErr)
- report_error(MachOOF->getFileName(), STOrErr.takeError());
- SymbolRef::Type NextSymType = *STOrErr;
+ SymbolRef::Type NextSymType = unwrapOrError(
+ Symbols[NextSymIdx].getType(), MachOOF->getFileName());
if (NextSymType == SymbolRef::ST_Function) {
containsNextSym =
Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
@@ -7671,10 +7591,7 @@ static void findUnwindRelocNameAddend(co
const RelocationRef &Reloc, uint64_t Addr,
StringRef &Name, uint64_t &Addend) {
if (Reloc.getSymbol() != Obj->symbol_end()) {
- Expected<StringRef> NameOrErr = Reloc.getSymbol()->getName();
- if (!NameOrErr)
- report_error(Obj->getFileName(), NameOrErr.takeError());
- Name = *NameOrErr;
+ Name = unwrapOrError(Reloc.getSymbol()->getName(), Obj->getFileName());
Addend = Addr;
return;
}
@@ -7696,16 +7613,11 @@ static void findUnwindRelocNameAddend(co
// Go back one so that SymbolAddress <= Addr.
--Sym;
- auto SectOrErr = Sym->second.getSection();
- if (!SectOrErr)
- report_error(Obj->getFileName(), SectOrErr.takeError());
- section_iterator SymSection = *SectOrErr;
+ section_iterator SymSection =
+ unwrapOrError(Sym->second.getSection(), Obj->getFileName());
if (RelocSection == *SymSection) {
// There's a valid symbol in the same section before this reference.
- Expected<StringRef> NameOrErr = Sym->second.getName();
- if (!NameOrErr)
- report_error(Obj->getFileName(), NameOrErr.takeError());
- Name = *NameOrErr;
+ Name = unwrapOrError(Sym->second.getName(), Obj->getFileName());
Addend = Addr - Sym->first;
return;
}
@@ -10145,7 +10057,7 @@ void llvm::printMachOExportsTrie(const o
outs() << "\n";
}
if (Err)
- report_error(Obj->getFileName(), std::move(Err));
+ report_error(std::move(Err), Obj->getFileName());
}
//===----------------------------------------------------------------------===//
@@ -10166,7 +10078,7 @@ void llvm::printMachORebaseTable(object:
Address, Entry.typeName().str().c_str());
}
if (Err)
- report_error(Obj->getFileName(), std::move(Err));
+ report_error(std::move(Err), Obj->getFileName());
}
static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
@@ -10218,7 +10130,7 @@ void llvm::printMachOBindTable(object::M
<< Entry.symbolName() << Attr << "\n";
}
if (Err)
- report_error(Obj->getFileName(), std::move(Err));
+ report_error(std::move(Err), Obj->getFileName());
}
//===----------------------------------------------------------------------===//
@@ -10243,7 +10155,7 @@ void llvm::printMachOLazyBindTable(objec
<< Entry.symbolName() << "\n";
}
if (Err)
- report_error(Obj->getFileName(), std::move(Err));
+ report_error(std::move(Err), Obj->getFileName());
}
//===----------------------------------------------------------------------===//
@@ -10275,7 +10187,7 @@ void llvm::printMachOWeakBindTable(objec
<< "\n";
}
if (Err)
- report_error(Obj->getFileName(), std::move(Err));
+ report_error(std::move(Err), Obj->getFileName());
}
// get_dyld_bind_info_symbolname() is used for disassembly and passed an
@@ -10294,7 +10206,7 @@ static const char *get_dyld_bind_info_sy
(*info->bindtable)[Address] = name;
}
if (Err)
- report_error(info->O->getFileName(), std::move(Err));
+ report_error(std::move(Err), info->O->getFileName());
}
auto name = info->bindtable->lookup(ReferenceValue);
return !name.empty() ? name.data() : nullptr;
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=357855&r1=357854&r2=357855&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Sun Apr 7 01:19:55 2019
@@ -356,8 +356,7 @@ LLVM_ATTRIBUTE_NORETURN void llvm::repor
exit(1);
}
-LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
- llvm::Error E) {
+LLVM_ATTRIBUTE_NORETURN void llvm::report_error(Error E, StringRef File) {
assert(E);
std::string Buf;
raw_string_ostream OS(Buf);
@@ -367,9 +366,8 @@ LLVM_ATTRIBUTE_NORETURN void llvm::repor
exit(1);
}
-LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
+LLVM_ATTRIBUTE_NORETURN void llvm::report_error(Error E, StringRef ArchiveName,
StringRef FileName,
- llvm::Error E,
StringRef ArchitectureName) {
assert(E);
WithColor::error(errs(), ToolName);
@@ -387,9 +385,8 @@ LLVM_ATTRIBUTE_NORETURN void llvm::repor
exit(1);
}
-LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
+LLVM_ATTRIBUTE_NORETURN void llvm::report_error(Error E, StringRef ArchiveName,
const object::Archive::Child &C,
- llvm::Error E,
StringRef ArchitectureName) {
Expected<StringRef> NameOrErr = C.getName();
// TODO: if we have a error getting the name then it would be nice to print
@@ -397,9 +394,9 @@ LLVM_ATTRIBUTE_NORETURN void llvm::repor
// archive instead of "???" as the name.
if (!NameOrErr) {
consumeError(NameOrErr.takeError());
- llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName);
+ llvm::report_error(std::move(E), ArchiveName, "???", ArchitectureName);
} else
- llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E),
+ llvm::report_error(std::move(E), ArchiveName, NameOrErr.get(),
ArchitectureName);
}
@@ -813,24 +810,17 @@ addDynamicElfSymbols(const ELFObjectFile
if (SymbolType != ELF::STT_FUNC || Symbol.getSize() == 0)
continue;
- Expected<uint64_t> AddressOrErr = Symbol.getAddress();
- if (!AddressOrErr)
- report_error(Obj->getFileName(), AddressOrErr.takeError());
-
- Expected<StringRef> Name = Symbol.getName();
- if (!Name)
- report_error(Obj->getFileName(), Name.takeError());
- if (Name->empty())
+ uint64_t Address = unwrapOrError(Symbol.getAddress(), Obj->getFileName());
+ StringRef Name = unwrapOrError(Symbol.getName(), Obj->getFileName());
+ if (Name.empty())
continue;
- Expected<section_iterator> SectionOrErr = Symbol.getSection();
- if (!SectionOrErr)
- report_error(Obj->getFileName(), SectionOrErr.takeError());
- section_iterator SecI = *SectionOrErr;
+ section_iterator SecI =
+ unwrapOrError(Symbol.getSection(), Obj->getFileName());
if (SecI == Obj->section_end())
continue;
- AllSymbols[*SecI].emplace_back(*AddressOrErr, *Name, SymbolType);
+ AllSymbols[*SecI].emplace_back(Address, Name, SymbolType);
}
}
@@ -868,14 +858,10 @@ static void addPltEntries(const ObjectFi
SymbolRef Symbol(PltEntry.first, ElfObj);
uint8_t SymbolType = getElfSymbolType(Obj, Symbol);
- Expected<StringRef> NameOrErr = Symbol.getName();
- if (!NameOrErr)
- report_error(Obj->getFileName(), NameOrErr.takeError());
- if (NameOrErr->empty())
- continue;
- StringRef Name = Saver.save((*NameOrErr + "@plt").str());
-
- AllSymbols[*Plt].emplace_back(PltEntry.second, Name, SymbolType);
+ StringRef Name = unwrapOrError(Symbol.getName(), Obj->getFileName());
+ if (!Name.empty())
+ AllSymbols[*Plt].emplace_back(
+ PltEntry.second, Saver.save((Name + "@plt").str()), SymbolType);
}
}
}
@@ -941,21 +927,13 @@ static void disassembleObject(const Targ
// pretty print the symbols while disassembling.
std::map<SectionRef, SectionSymbolsTy> AllSymbols;
SectionSymbolsTy AbsoluteSymbols;
+ const StringRef FileName = Obj->getFileName();
for (const SymbolRef &Symbol : Obj->symbols()) {
- Expected<uint64_t> AddressOrErr = Symbol.getAddress();
- if (!AddressOrErr)
- report_error(Obj->getFileName(), AddressOrErr.takeError());
- uint64_t Address = *AddressOrErr;
-
- Expected<StringRef> Name = Symbol.getName();
- if (!Name)
- report_error(Obj->getFileName(), Name.takeError());
- if (Name->empty())
- continue;
+ uint64_t Address = unwrapOrError(Symbol.getAddress(), FileName);
- Expected<section_iterator> SectionOrErr = Symbol.getSection();
- if (!SectionOrErr)
- report_error(Obj->getFileName(), SectionOrErr.takeError());
+ StringRef Name = unwrapOrError(Symbol.getName(), FileName);
+ if (Name.empty())
+ continue;
uint8_t SymbolType = ELF::STT_NOTYPE;
if (Obj->isELF()) {
@@ -964,11 +942,11 @@ static void disassembleObject(const Targ
continue;
}
- section_iterator SecI = *SectionOrErr;
+ section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
if (SecI != Obj->section_end())
- AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
+ AllSymbols[*SecI].emplace_back(Address, Name, SymbolType);
else
- AbsoluteSymbols.emplace_back(Address, *Name, SymbolType);
+ AbsoluteSymbols.emplace_back(Address, Name, SymbolType);
}
if (AllSymbols.empty() && Obj->isELF())
addDynamicElfSymbols(Obj, AllSymbols);
@@ -1629,6 +1607,7 @@ void llvm::printSymbolTable(const Object
return;
}
+ const StringRef FileName = O->getFileName();
for (auto I = O->symbol_begin(), E = O->symbol_end(); I != E; ++I) {
// Skip printing the special zero symbol when dumping an ELF file.
// This makes the output consistent with the GNU objdump.
@@ -1636,34 +1615,21 @@ void llvm::printSymbolTable(const Object
continue;
const SymbolRef &Symbol = *I;
- Expected<uint64_t> AddressOrError = Symbol.getAddress();
- if (!AddressOrError)
- report_error(ArchiveName, O->getFileName(), AddressOrError.takeError(),
- ArchitectureName);
- uint64_t Address = *AddressOrError;
+ uint64_t Address = unwrapOrError(Symbol.getAddress(), ArchiveName, FileName,
+ ArchitectureName);
if ((Address < StartAddress) || (Address > StopAddress))
continue;
- Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
- if (!TypeOrError)
- report_error(ArchiveName, O->getFileName(), TypeOrError.takeError(),
- ArchitectureName);
- SymbolRef::Type Type = *TypeOrError;
+ SymbolRef::Type Type = unwrapOrError(Symbol.getType(), ArchiveName,
+ FileName, ArchitectureName);
uint32_t Flags = Symbol.getFlags();
- Expected<section_iterator> SectionOrErr = Symbol.getSection();
- if (!SectionOrErr)
- report_error(ArchiveName, O->getFileName(), SectionOrErr.takeError(),
- ArchitectureName);
- section_iterator Section = *SectionOrErr;
+ section_iterator Section = unwrapOrError(Symbol.getSection(), ArchiveName,
+ FileName, ArchitectureName);
StringRef Name;
- if (Type == SymbolRef::ST_Debug && Section != O->section_end()) {
+ if (Type == SymbolRef::ST_Debug && Section != O->section_end())
Section->getName(Name);
- } else {
- Expected<StringRef> NameOrErr = Symbol.getName();
- if (!NameOrErr)
- report_error(ArchiveName, O->getFileName(), NameOrErr.takeError(),
- ArchitectureName);
- Name = *NameOrErr;
- }
+ else
+ Name = unwrapOrError(Symbol.getName(), ArchiveName, FileName,
+ ArchitectureName);
bool Global = Flags & SymbolRef::SF_Global;
bool Weak = Flags & SymbolRef::SF_Weak;
@@ -1896,12 +1862,9 @@ static void printFileHeaders(const Objec
Triple::ArchType AT = O->getArch();
outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
- Expected<uint64_t> StartAddrOrErr = O->getStartAddress();
- if (!StartAddrOrErr)
- report_error(O->getFileName(), StartAddrOrErr.takeError());
+ uint64_t Address = unwrapOrError(O->getStartAddress(), O->getFileName());
StringRef Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
- uint64_t Address = StartAddrOrErr.get();
outs() << "start address: "
<< "0x" << format(Fmt.data(), Address) << "\n\n";
}
@@ -1926,22 +1889,9 @@ static void printArchiveChild(StringRef
outs() << " ";
- Expected<unsigned> UIDOrErr = C.getUID();
- if (!UIDOrErr)
- report_error(Filename, UIDOrErr.takeError());
- unsigned UID = UIDOrErr.get();
- outs() << format("%d/", UID);
-
- Expected<unsigned> GIDOrErr = C.getGID();
- if (!GIDOrErr)
- report_error(Filename, GIDOrErr.takeError());
- unsigned GID = GIDOrErr.get();
- outs() << format("%-d ", GID);
-
- Expected<uint64_t> Size = C.getRawSize();
- if (!Size)
- report_error(Filename, Size.takeError());
- outs() << format("%6" PRId64, Size.get()) << " ";
+ outs() << format("%d/%d %6" PRId64 " ", unwrapOrError(C.getUID(), Filename),
+ unwrapOrError(C.getGID(), Filename),
+ unwrapOrError(C.getRawSize(), Filename));
StringRef RawLastModified = C.getRawLastModified();
unsigned Seconds;
@@ -1960,10 +1910,7 @@ static void printArchiveChild(StringRef
Expected<StringRef> NameOrErr = C.getName();
if (!NameOrErr) {
consumeError(NameOrErr.takeError());
- Expected<StringRef> RawNameOrErr = C.getRawName();
- if (!RawNameOrErr)
- report_error(Filename, NameOrErr.takeError());
- Name = RawNameOrErr.get();
+ Name = unwrapOrError(C.getRawName(), Filename);
} else {
Name = NameOrErr.get();
}
@@ -2050,7 +1997,7 @@ static void dumpArchive(const Archive *A
Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
if (!ChildOrErr) {
if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
- report_error(A->getFileName(), C, std::move(E));
+ report_error(std::move(E), A->getFileName(), C);
continue;
}
if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
@@ -2061,7 +2008,7 @@ static void dumpArchive(const Archive *A
report_error(A->getFileName(), object_error::invalid_file_type);
}
if (Err)
- report_error(A->getFileName(), std::move(Err));
+ report_error(std::move(Err), A->getFileName());
}
/// Open file and figure out how to dump it.
@@ -2075,10 +2022,8 @@ static void dumpInput(StringRef file) {
}
// Attempt to open the binary.
- Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
- if (!BinaryOrErr)
- report_error(file, BinaryOrErr.takeError());
- Binary &Binary = *BinaryOrErr.get().getBinary();
+ OwningBinary<Binary> OBinary = unwrapOrError(createBinary(file), file);
+ Binary &Binary = *OBinary.getBinary();
if (Archive *A = dyn_cast<Archive>(&Binary))
dumpArchive(A);
Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.h?rev=357855&r1=357854&r2=357855&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.h (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.h Sun Apr 7 01:19:55 2019
@@ -176,17 +176,20 @@ void warn(StringRef Message);
LLVM_ATTRIBUTE_NORETURN void error(Twine Message);
LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message);
LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
-LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, llvm::Error E);
-LLVM_ATTRIBUTE_NORETURN void report_error(StringRef FileName,
- StringRef ArchiveName,
- llvm::Error E,
- StringRef ArchitectureName
- = StringRef());
-LLVM_ATTRIBUTE_NORETURN void report_error(StringRef ArchiveName,
- const object::Archive::Child &C,
- llvm::Error E,
- StringRef ArchitectureName
- = StringRef());
+LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef File);
+LLVM_ATTRIBUTE_NORETURN void
+report_error(Error E, StringRef FileName, StringRef ArchiveName,
+ StringRef ArchitectureName = StringRef());
+LLVM_ATTRIBUTE_NORETURN void
+report_error(Error E, StringRef ArchiveName, const object::Archive::Child &C,
+ StringRef ArchitectureName = StringRef());
+
+template <typename T, typename... Ts>
+T unwrapOrError(Expected<T> EO, Ts &&... Args) {
+ if (EO)
+ return std::move(*EO);
+ report_error(EO.takeError(), std::forward<Ts>(Args)...);
+}
} // end namespace llvm
More information about the llvm-commits
mailing list