[PATCH] D83208: [llvm-readobj] - Refactor ELFDumper<ELFT>::getStaticSymbolName.
George Rimar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 6 12:53:40 PDT 2020
grimar created this revision.
grimar added reviewers: jhenderson, MaskRay.
Herald added subscribers: rupprecht, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.
This is a followup for D83129 <https://reviews.llvm.org/D83129>.
It is possible to make `getStaticSymbolName` report warnings inside
and return the "<?>" on a error. This allows to encapsulate errors handling
and slightly simplifies the logic in callers code.
https://reviews.llvm.org/D83208
Files:
llvm/test/tools/llvm-readobj/ELF/addrsig.test
llvm/tools/llvm-readobj/ELFDumper.cpp
Index: llvm/tools/llvm-readobj/ELFDumper.cpp
===================================================================
--- llvm/tools/llvm-readobj/ELFDumper.cpp
+++ llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -343,7 +343,7 @@
const Elf_Sym *FirstSym) const;
Expected<StringRef> getSymbolSectionName(const Elf_Sym *Symbol,
unsigned SectionIndex) const;
- Expected<std::string> getStaticSymbolName(uint32_t Index) const;
+ std::string getStaticSymbolName(uint32_t Index) const;
StringRef getDynamicString(uint64_t Value) const;
Expected<StringRef> getSymbolVersionByIndex(uint32_t VersionSymbolIndex,
bool &IsDefault) const;
@@ -1131,21 +1131,27 @@
}
template <typename ELFT>
-Expected<std::string>
-ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
+std::string ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
+ auto ReportWarn = [&](Error E) -> std::string {
+ this->reportUniqueWarning(
+ createError("unable to read the name of symbol with index " +
+ Twine(Index) + ": " + toString(std::move(E))));
+ return "<?>";
+ };
+
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Expected<const typename ELFT::Sym *> SymOrErr =
Obj->getSymbol(DotSymtabSec, Index);
if (!SymOrErr)
- return SymOrErr.takeError();
+ return ReportWarn(SymOrErr.takeError());
Expected<StringRef> StrTabOrErr = Obj->getStringTableForSymtab(*DotSymtabSec);
if (!StrTabOrErr)
- return StrTabOrErr.takeError();
+ return ReportWarn(StrTabOrErr.takeError());
Expected<StringRef> NameOrErr = (*SymOrErr)->getName(*StrTabOrErr);
if (!NameOrErr)
- return NameOrErr.takeError();
+ return ReportWarn(NameOrErr.takeError());
return maybeDemangle(*NameOrErr);
}
@@ -6555,21 +6561,12 @@
return;
}
- auto GetSymName = [&](uint32_t Index) -> std::string {
- if (Expected<std::string> NameOrErr =
- this->dumper()->getStaticSymbolName(Index))
- return *NameOrErr;
- else
- this->reportUniqueWarning(
- createError("unable to read the name of symbol with index " +
- Twine(Index) + ": " + toString(NameOrErr.takeError())));
- return "<?>";
- };
-
for (const Elf_CGProfile &CGPE : *CGProfileOrErr) {
DictScope D(W, "CGProfileEntry");
- W.printNumber("From", GetSymName(CGPE.cgp_from), CGPE.cgp_from);
- W.printNumber("To", GetSymName(CGPE.cgp_to), CGPE.cgp_to);
+ W.printNumber("From", this->dumper()->getStaticSymbolName(CGPE.cgp_from),
+ CGPE.cgp_from);
+ W.printNumber("To", this->dumper()->getStaticSymbolName(CGPE.cgp_to),
+ CGPE.cgp_to);
W.printNumber("Weight", CGPE.cgp_weight);
}
}
Index: llvm/test/tools/llvm-readobj/ELF/addrsig.test
===================================================================
--- llvm/test/tools/llvm-readobj/ELF/addrsig.test
+++ llvm/test/tools/llvm-readobj/ELF/addrsig.test
@@ -58,7 +58,7 @@
# INVALID-INDEX: Addrsig [
# INVALID-INDEX-NEXT: Sym: foo (1)
-# INVALID-INDEX-NEXT: warning: '[[FILE]]': unable to get symbol from section [index 2]: invalid symbol index (255)
+# INVALID-INDEX-NEXT: warning: '[[FILE]]': unable to read the name of symbol with index 255: unable to get symbol from section [index 2]: invalid symbol index (255)
# INVALID-INDEX-NEXT: Sym: <?> (255)
# INVALID-INDEX-NEXT: Sym: bar (2)
# INVALID-INDEX-NEXT: ]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83208.275644.patch
Type: text/x-patch
Size: 3532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200706/ceca67ad/attachment-0001.bin>
More information about the llvm-commits
mailing list