diff --git a/llvm/include/llvm/Support/ScopedPrinter.h b/llvm/include/llvm/Support/ScopedPrinter.h --- a/llvm/include/llvm/Support/ScopedPrinter.h +++ b/llvm/include/llvm/Support/ScopedPrinter.h @@ -67,6 +67,14 @@ return stream.str(); } +template +std::string enumToString(T Value, ArrayRef> EnumValues) { + for (const EnumEntry &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 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -587,14 +587,6 @@ Field(unsigned Col) : Column(Col) {} }; - template - std::string printEnum(T Value, ArrayRef> EnumValues) const { - for (const EnumEntry &EnumItem : EnumValues) - if (EnumItem.Value == Value) - return std::string(EnumItem.AltName); - return to_hexString(Value, false); - } - template std::string printFlags(T Value, ArrayRef> EnumValues, TEnum EnumMask1 = {}, TEnum EnumMask2 = {}, @@ -3241,9 +3233,9 @@ for (int i = 0; i < ELF::EI_NIDENT; i++) OS << format(" %02x", static_cast(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 @@ 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 @@ } 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 @@ 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 @@ 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 @@ 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" @@ -6167,7 +6159,7 @@ 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); @@ -6221,7 +6213,7 @@ 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); @@ -6268,10 +6260,10 @@ 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