[llvm] 62c74d4 - [NFC] move GNUELFDumper::printEnum() into a common header for reuse.
via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 2 22:34:33 PST 2021
Author: Esme-Yi
Date: 2021-12-03T06:33:56Z
New Revision: 62c74d496bb40a1845329cece0a38d9267db43c6
URL: https://github.com/llvm/llvm-project/commit/62c74d496bb40a1845329cece0a38d9267db43c6
DIFF: https://github.com/llvm/llvm-project/commit/62c74d496bb40a1845329cece0a38d9267db43c6.diff
LOG: [NFC] move GNUELFDumper::printEnum() into a common header for reuse.
Summary:
This is a NFC patch moving the GNUELFDumper<ELFT>::printEnum()
function from ELFDumper into ScopedPrinter.h for reuse.
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D114840
Added:
Modified:
llvm/include/llvm/Support/ScopedPrinter.h
llvm/tools/llvm-readobj/ELFDumper.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/ScopedPrinter.h b/llvm/include/llvm/Support/ScopedPrinter.h
index 0dfe1245f7d6e..e0509c90a39b3 100644
--- a/llvm/include/llvm/Support/ScopedPrinter.h
+++ b/llvm/include/llvm/Support/ScopedPrinter.h
@@ -67,6 +67,14 @@ template <class T> std::string to_string(const T &Value) {
return stream.str();
}
+template <typename T, typename TEnum>
+std::string enumToString(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
+ for (const EnumEntry<TEnum> &EnumItem : EnumValues)
+ if (EnumItem.Value == Value)
+ return std::string(EnumItem.AltName);
+ return to_hexString(Value, false);
+}
+
class ScopedPrinter {
public:
ScopedPrinter(raw_ostream &OS) : OS(OS), IndentLevel(0) {}
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index 9dd777dd98e7b..431b20f77a589 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -587,14 +587,6 @@ template <typename ELFT> class GNUELFDumper : public ELFDumper<ELFT> {
Field(unsigned Col) : Column(Col) {}
};
- template <typename T, typename TEnum>
- std::string printEnum(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) const {
- for (const EnumEntry<TEnum> &EnumItem : EnumValues)
- if (EnumItem.Value == Value)
- return std::string(EnumItem.AltName);
- return to_hexString(Value, false);
- }
-
template <typename T, typename TEnum>
std::string printFlags(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues,
TEnum EnumMask1 = {}, TEnum EnumMask2 = {},
@@ -3241,9 +3233,9 @@ template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
for (int i = 0; i < ELF::EI_NIDENT; i++)
OS << format(" %02x", static_cast<int>(e.e_ident[i]));
OS << "\n";
- Str = printEnum(e.e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
+ Str = enumToString(e.e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass));
printFields(OS, "Class:", Str);
- Str = printEnum(e.e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
+ Str = enumToString(e.e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding));
printFields(OS, "Data:", Str);
OS.PadToColumn(2u);
OS << "Version:";
@@ -3252,7 +3244,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
if (e.e_version == ELF::EV_CURRENT)
OS << " (current)";
OS << "\n";
- Str = printEnum(e.e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
+ Str = enumToString(e.e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI));
printFields(OS, "OS/ABI:", Str);
printFields(OS,
"ABI Version:", std::to_string(e.e_ident[ELF::EI_ABIVERSION]));
@@ -3269,7 +3261,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
}
printFields(OS, "Type:", Str);
- Str = printEnum(e.e_machine, makeArrayRef(ElfMachineType));
+ Str = enumToString(e.e_machine, makeArrayRef(ElfMachineType));
printFields(OS, "Machine:", Str);
Str = "0x" + to_hexString(e.e_version);
printFields(OS, "Version:", Str);
@@ -3759,14 +3751,14 @@ void GNUELFDumper<ELFT>::printSymbol(const Elf_Sym &Symbol, unsigned SymIndex,
unsigned char SymbolType = Symbol.getType();
if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
- Fields[3].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
+ Fields[3].Str = enumToString(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
else
- Fields[3].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
+ Fields[3].Str = enumToString(SymbolType, makeArrayRef(ElfSymbolTypes));
Fields[4].Str =
- printEnum(Symbol.getBinding(), makeArrayRef(ElfSymbolBindings));
+ enumToString(Symbol.getBinding(), makeArrayRef(ElfSymbolBindings));
Fields[5].Str =
- printEnum(Symbol.getVisibility(), makeArrayRef(ElfSymbolVisibilities));
+ enumToString(Symbol.getVisibility(), makeArrayRef(ElfSymbolVisibilities));
if (Symbol.st_other & ~0x3) {
if (this->Obj.getHeader().e_machine == ELF::EM_AARCH64) {
@@ -3822,14 +3814,14 @@ void GNUELFDumper<ELFT>::printHashedSymbol(const Elf_Sym *Symbol,
unsigned char SymbolType = Symbol->getType();
if (this->Obj.getHeader().e_machine == ELF::EM_AMDGPU &&
SymbolType >= ELF::STT_LOOS && SymbolType < ELF::STT_HIOS)
- Fields[4].Str = printEnum(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
+ Fields[4].Str = enumToString(SymbolType, makeArrayRef(AMDGPUSymbolTypes));
else
- Fields[4].Str = printEnum(SymbolType, makeArrayRef(ElfSymbolTypes));
+ Fields[4].Str = enumToString(SymbolType, makeArrayRef(ElfSymbolTypes));
Fields[5].Str =
- printEnum(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
- Fields[6].Str =
- printEnum(Symbol->getVisibility(), makeArrayRef(ElfSymbolVisibilities));
+ enumToString(Symbol->getBinding(), makeArrayRef(ElfSymbolBindings));
+ Fields[6].Str = enumToString(Symbol->getVisibility(),
+ makeArrayRef(ElfSymbolVisibilities));
Fields[7].Str = getSymbolSectionNdx(*Symbol, SymIndex, ShndxTable);
Fields[8].Str =
this->getFullSymbolName(*Symbol, SymIndex, ShndxTable, StrTable, true);
@@ -4192,7 +4184,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printProgramHeaders() {
Field Fields[8] = {2, 17, 26, 37 + Bias,
48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
OS << "\nElf file type is "
- << printEnum(Header.e_type, makeArrayRef(ElfObjectFileType)) << "\n"
+ << enumToString(Header.e_type, makeArrayRef(ElfObjectFileType)) << "\n"
<< "Entry point " << format_hex(Header.e_entry, 3) << "\n"
<< "There are " << Header.e_phnum << " program headers,"
<< " starting at offset " << Header.e_phoff << "\n\n"
@@ -6180,7 +6172,7 @@ void GNUELFDumper<ELFT>::printMipsGOT(const MipsGOTParser<ELFT> &Parser) {
OS.PadToColumn(31 + 2 * Bias);
OS << to_string(format_hex_no_prefix(Sym.st_value, 8 + Bias));
OS.PadToColumn(40 + 3 * Bias);
- OS << printEnum(Sym.getType(), makeArrayRef(ElfSymbolTypes));
+ OS << enumToString(Sym.getType(), makeArrayRef(ElfSymbolTypes));
OS.PadToColumn(48 + 3 * Bias);
OS << getSymbolSectionNdx(Sym, &Sym - this->dynamic_symbols().begin(),
ShndxTable);
@@ -6234,7 +6226,7 @@ void GNUELFDumper<ELFT>::printMipsPLT(const MipsGOTParser<ELFT> &Parser) {
OS.PadToColumn(20 + 2 * Bias);
OS << to_string(format_hex_no_prefix(Sym.st_value, 8 + Bias));
OS.PadToColumn(29 + 3 * Bias);
- OS << printEnum(Sym.getType(), makeArrayRef(ElfSymbolTypes));
+ OS << enumToString(Sym.getType(), makeArrayRef(ElfSymbolTypes));
OS.PadToColumn(37 + 3 * Bias);
OS << getSymbolSectionNdx(Sym, &Sym - this->dynamic_symbols().begin(),
ShndxTable);
@@ -6281,10 +6273,10 @@ template <class ELFT> void GNUELFDumper<ELFT>::printMipsABIFlags() {
OS << "GPR size: " << getMipsRegisterSize(Flags->gpr_size) << "\n";
OS << "CPR1 size: " << getMipsRegisterSize(Flags->cpr1_size) << "\n";
OS << "CPR2 size: " << getMipsRegisterSize(Flags->cpr2_size) << "\n";
- OS << "FP ABI: " << printEnum(Flags->fp_abi, makeArrayRef(ElfMipsFpABIType))
- << "\n";
+ OS << "FP ABI: "
+ << enumToString(Flags->fp_abi, makeArrayRef(ElfMipsFpABIType)) << "\n";
OS << "ISA Extension: "
- << printEnum(Flags->isa_ext, makeArrayRef(ElfMipsISAExtType)) << "\n";
+ << enumToString(Flags->isa_ext, makeArrayRef(ElfMipsISAExtType)) << "\n";
if (Flags->ases == 0)
OS << "ASEs: None\n";
else
More information about the llvm-commits
mailing list