diff options
Diffstat (limited to 'llvm/tools')
| -rw-r--r-- | llvm/tools/llvm-readobj/ARMAttributeParser.cpp | 2 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/ARMAttributeParser.h | 6 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/ARMEHABIPrinter.h | 10 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/ARMWinEHPrinter.h | 6 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/COFFDumper.cpp | 14 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/ELFDumper.cpp | 34 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/MachODumper.cpp | 11 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/ObjDumper.cpp | 6 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/ObjDumper.h | 12 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/StreamWriter.cpp | 71 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/StreamWriter.h | 354 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/Win64EHDumper.h | 6 | ||||
| -rw-r--r-- | llvm/tools/llvm-readobj/llvm-readobj.cpp | 7 |
14 files changed, 55 insertions, 485 deletions
diff --git a/llvm/tools/llvm-readobj/ARMAttributeParser.cpp b/llvm/tools/llvm-readobj/ARMAttributeParser.cpp index 76de1440d09..877dd71c907 100644 --- a/llvm/tools/llvm-readobj/ARMAttributeParser.cpp +++ b/llvm/tools/llvm-readobj/ARMAttributeParser.cpp @@ -8,10 +8,10 @@ //===----------------------------------------------------------------------===// #include "ARMAttributeParser.h" -#include "StreamWriter.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/LEB128.h" +#include "llvm/Support/ScopedPrinter.h" using namespace llvm; using namespace llvm::ARMBuildAttrs; diff --git a/llvm/tools/llvm-readobj/ARMAttributeParser.h b/llvm/tools/llvm-readobj/ARMAttributeParser.h index a58285688f2..6936b70ca12 100644 --- a/llvm/tools/llvm-readobj/ARMAttributeParser.h +++ b/llvm/tools/llvm-readobj/ARMAttributeParser.h @@ -10,14 +10,14 @@ #ifndef LLVM_TOOLS_LLVM_READOBJ_ARMATTRIBUTEPARSER_H #define LLVM_TOOLS_LLVM_READOBJ_ARMATTRIBUTEPARSER_H -#include "StreamWriter.h" #include "llvm/Support/ARMBuildAttributes.h" +#include "llvm/Support/ScopedPrinter.h" namespace llvm { class StringRef; class ARMAttributeParser { - StreamWriter &SW; + ScopedPrinter &SW; struct DisplayHandler { ARMBuildAttrs::AttrType Attribute; @@ -115,7 +115,7 @@ class ARMAttributeParser { SmallVectorImpl<uint8_t> &IndexList); void ParseSubsection(const uint8_t *Data, uint32_t Length); public: - ARMAttributeParser(StreamWriter &SW) : SW(SW) {} + ARMAttributeParser(ScopedPrinter &SW) : SW(SW) {} void Parse(ArrayRef<uint8_t> Section); }; diff --git a/llvm/tools/llvm-readobj/ARMEHABIPrinter.h b/llvm/tools/llvm-readobj/ARMEHABIPrinter.h index 5845eb02d11..59c9b713d85 100644 --- a/llvm/tools/llvm-readobj/ARMEHABIPrinter.h +++ b/llvm/tools/llvm-readobj/ARMEHABIPrinter.h @@ -11,7 +11,6 @@ #define LLVM_TOOLS_LLVM_READOBJ_ARMEHABIPRINTER_H #include "Error.h" -#include "StreamWriter.h" #include "llvm-readobj.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Object/ELF.h" @@ -20,6 +19,7 @@ #include "llvm/Support/Debug.h" #include "llvm/Support/Endian.h" #include "llvm/Support/Format.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/type_traits.h" namespace llvm { @@ -27,7 +27,7 @@ namespace ARM { namespace EHABI { class OpcodeDecoder { - StreamWriter &SW; + ScopedPrinter &SW; raw_ostream &OS; struct RingEntry { @@ -64,7 +64,7 @@ class OpcodeDecoder { void PrintRegisters(uint32_t Mask, StringRef Prefix); public: - OpcodeDecoder(StreamWriter &SW) : SW(SW), OS(SW.getOStream()) {} + OpcodeDecoder(ScopedPrinter &SW) : SW(SW), OS(SW.getOStream()) {} void Decode(const uint8_t *Opcodes, off_t Offset, size_t Length); }; @@ -311,7 +311,7 @@ class PrinterContext { typedef typename object::ELFFile<ET>::Elf_Rel Elf_Rel; typedef typename object::ELFFile<ET>::Elf_Word Elf_Word; - StreamWriter &SW; + ScopedPrinter &SW; const object::ELFFile<ET> *ELF; const Elf_Shdr *Symtab; ArrayRef<Elf_Word> ShndxTable; @@ -335,7 +335,7 @@ class PrinterContext { void PrintOpcodes(const uint8_t *Entry, size_t Length, off_t Offset) const; public: - PrinterContext(StreamWriter &SW, const object::ELFFile<ET> *ELF, + PrinterContext(ScopedPrinter &SW, const object::ELFFile<ET> *ELF, const Elf_Shdr *Symtab) : SW(SW), ELF(ELF), Symtab(Symtab) {} diff --git a/llvm/tools/llvm-readobj/ARMWinEHPrinter.h b/llvm/tools/llvm-readobj/ARMWinEHPrinter.h index 274ef114841..95f52170226 100644 --- a/llvm/tools/llvm-readobj/ARMWinEHPrinter.h +++ b/llvm/tools/llvm-readobj/ARMWinEHPrinter.h @@ -10,9 +10,9 @@ #ifndef LLVM_TOOLS_LLVM_READOBJ_ARMWINEHPRINTER_H #define LLVM_TOOLS_LLVM_READOBJ_ARMWINEHPRINTER_H -#include "StreamWriter.h" #include "llvm/Object/COFF.h" #include "llvm/Support/ErrorOr.h" +#include "llvm/Support/ScopedPrinter.h" namespace llvm { namespace ARM { @@ -22,7 +22,7 @@ class RuntimeFunction; class Decoder { static const size_t PDataEntrySize; - StreamWriter &SW; + ScopedPrinter &SW; raw_ostream &OS; struct RingEntry { @@ -107,7 +107,7 @@ class Decoder { const object::SectionRef Section); public: - Decoder(StreamWriter &SW) : SW(SW), OS(SW.getOStream()) {} + Decoder(ScopedPrinter &SW) : SW(SW), OS(SW.getOStream()) {} std::error_code dumpProcedureData(const object::COFFObjectFile &COFF); }; } diff --git a/llvm/tools/llvm-readobj/CMakeLists.txt b/llvm/tools/llvm-readobj/CMakeLists.txt index a06b678780c..e167874c1a1 100644 --- a/llvm/tools/llvm-readobj/CMakeLists.txt +++ b/llvm/tools/llvm-readobj/CMakeLists.txt @@ -13,6 +13,5 @@ add_llvm_tool(llvm-readobj llvm-readobj.cpp MachODumper.cpp ObjDumper.cpp - StreamWriter.cpp Win64EHDumper.cpp ) diff --git a/llvm/tools/llvm-readobj/COFFDumper.cpp b/llvm/tools/llvm-readobj/COFFDumper.cpp index a782d2a68c0..48e8178788b 100644 --- a/llvm/tools/llvm-readobj/COFFDumper.cpp +++ b/llvm/tools/llvm-readobj/COFFDumper.cpp @@ -12,23 +12,22 @@ /// //===----------------------------------------------------------------------===// -#include "llvm-readobj.h" #include "ARMWinEHPrinter.h" #include "CodeView.h" #include "Error.h" #include "ObjDumper.h" #include "StackMapPrinter.h" -#include "StreamWriter.h" #include "Win64EHDumper.h" +#include "llvm-readobj.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSet.h" #include "llvm/DebugInfo/CodeView/CodeView.h" #include "llvm/DebugInfo/CodeView/Line.h" +#include "llvm/DebugInfo/CodeView/SymbolRecord.h" #include "llvm/DebugInfo/CodeView/TypeIndex.h" #include "llvm/DebugInfo/CodeView/TypeRecord.h" -#include "llvm/DebugInfo/CodeView/SymbolRecord.h" #include "llvm/Object/COFF.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/COFF.h" @@ -36,6 +35,7 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/DataExtractor.h" #include "llvm/Support/Format.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/Win64EH.h" #include "llvm/Support/raw_ostream.h" @@ -54,7 +54,7 @@ namespace { class CVTypeDumper { public: - CVTypeDumper(StreamWriter &W) : W(W) {} + CVTypeDumper(ScopedPrinter &W) : W(W) {} StringRef getTypeName(TypeIndex TI); void printTypeIndex(StringRef FieldName, TypeIndex TI); @@ -65,7 +65,7 @@ private: void printCodeViewFieldList(StringRef FieldData); void printMemberAttributes(MemberAttributes Attrs); - StreamWriter &W; + ScopedPrinter &W; /// All user defined type records in .debug$T live in here. Type indices /// greater than 0x1000 are user defined. Subtract 0x1000 from the index to @@ -77,7 +77,7 @@ private: class COFFDumper : public ObjDumper { public: - COFFDumper(const llvm::object::COFFObjectFile *Obj, StreamWriter &Writer) + COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer) : ObjDumper(Writer), Obj(Obj), CVTD(Writer) {} void printFileHeaders() override; @@ -166,7 +166,7 @@ private: namespace llvm { std::error_code createCOFFDumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr<ObjDumper> &Result) { const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Obj); if (!COFFObj) diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 4905b43a453..31bf5693502 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -12,13 +12,12 @@ /// //===----------------------------------------------------------------------===// -#include "llvm-readobj.h" #include "ARMAttributeParser.h" #include "ARMEHABIPrinter.h" #include "Error.h" #include "ObjDumper.h" #include "StackMapPrinter.h" -#include "StreamWriter.h" +#include "llvm-readobj.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" @@ -26,10 +25,11 @@ #include "llvm/Support/ARMBuildAttributes.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/Format.h" +#include "llvm/Support/FormattedStream.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/MipsABIFlags.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" -#include "llvm/Support/FormattedStream.h" using namespace llvm; using namespace llvm::object; @@ -97,7 +97,7 @@ struct DynRegionInfo { template<typename ELFT> class ELFDumper : public ObjDumper { public: - ELFDumper(const ELFFile<ELFT> *Obj, StreamWriter &Writer); + ELFDumper(const ELFFile<ELFT> *Obj, ScopedPrinter &Writer); void printFileHeaders() override; void printSections() override; @@ -300,7 +300,7 @@ template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> { formatted_raw_ostream OS; public: TYPEDEF_ELF_TYPES(ELFT) - GNUStyle(StreamWriter &W, ELFDumper<ELFT> *Dumper) + GNUStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper) : DumpStyle<ELFT>(Dumper), OS(W.getOStream()) {} void printFileHeaders(const ELFO *Obj) override; void printGroupSections(const ELFFile<ELFT> *Obj) override; @@ -353,7 +353,7 @@ private: template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> { public: TYPEDEF_ELF_TYPES(ELFT) - LLVMStyle(StreamWriter &W, ELFDumper<ELFT> *Dumper) + LLVMStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper) : DumpStyle<ELFT>(Dumper), W(W) {} void printFileHeaders(const ELFO *Obj) override; @@ -372,7 +372,7 @@ private: void printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel); void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First, StringRef StrTable, bool IsDynamic) override; - StreamWriter &W; + ScopedPrinter &W; }; } // namespace @@ -381,14 +381,14 @@ namespace llvm { template <class ELFT> static std::error_code createELFDumper(const ELFFile<ELFT> *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr<ObjDumper> &Result) { Result.reset(new ELFDumper<ELFT>(Obj, Writer)); return readobj_error::success; } std::error_code createELFDumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr<ObjDumper> &Result) { // Little-endian 32-bit if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj)) @@ -491,12 +491,10 @@ template <class ELFT> void ELFDumper<ELFT>::LoadVersionMap() const { LoadVersionNeeds(dot_gnu_version_r_sec); } - template <typename ELFO, class ELFT> -static void printVersionSymbolSection(ELFDumper<ELFT> *Dumper, - const ELFO *Obj, +static void printVersionSymbolSection(ELFDumper<ELFT> *Dumper, const ELFO *Obj, const typename ELFO::Elf_Shdr *Sec, - StreamWriter &W) { + ScopedPrinter &W) { DictScope SS(W, "Version symbols"); if (!Sec) return; @@ -525,7 +523,7 @@ template <typename ELFO, class ELFT> static void printVersionDefinitionSection(ELFDumper<ELFT> *Dumper, const ELFO *Obj, const typename ELFO::Elf_Shdr *Sec, - StreamWriter &W) { + ScopedPrinter &W) { DictScope SD(W, "Version definition"); if (!Sec) return; @@ -1217,7 +1215,7 @@ static const EnumEntry<unsigned> ElfMips16SymOtherFlags[] = { }; template <typename ELFT> -ELFDumper<ELFT>::ELFDumper(const ELFFile<ELFT> *Obj, StreamWriter &Writer) +ELFDumper<ELFT>::ELFDumper(const ELFFile<ELFT> *Obj, ScopedPrinter &Writer) : ObjDumper(Writer), Obj(Obj) { SmallVector<const Elf_Phdr *, 4> LoadSegments; @@ -1795,7 +1793,7 @@ public: typedef typename ELFO::Elf_Rela Elf_Rela; MipsGOTParser(ELFDumper<ELFT> *Dumper, const ELFO *Obj, - Elf_Dyn_Range DynTable, StreamWriter &W); + Elf_Dyn_Range DynTable, ScopedPrinter &W); void parseGOT(); void parsePLT(); @@ -1803,7 +1801,7 @@ public: private: ELFDumper<ELFT> *Dumper; const ELFO *Obj; - StreamWriter &W; + ScopedPrinter &W; llvm::Optional<uint64_t> DtPltGot; llvm::Optional<uint64_t> DtLocalGotNum; llvm::Optional<uint64_t> DtGotSym; @@ -1828,7 +1826,7 @@ private: template <class ELFT> MipsGOTParser<ELFT>::MipsGOTParser(ELFDumper<ELFT> *Dumper, const ELFO *Obj, - Elf_Dyn_Range DynTable, StreamWriter &W) + Elf_Dyn_Range DynTable, ScopedPrinter &W) : Dumper(Dumper), Obj(Obj), W(W) { for (const auto &Entry : DynTable) { switch (Entry.getTag()) { diff --git a/llvm/tools/llvm-readobj/MachODumper.cpp b/llvm/tools/llvm-readobj/MachODumper.cpp index cc57211b280..3773df25057 100644 --- a/llvm/tools/llvm-readobj/MachODumper.cpp +++ b/llvm/tools/llvm-readobj/MachODumper.cpp @@ -11,15 +11,15 @@ // //===----------------------------------------------------------------------===// -#include "llvm-readobj.h" #include "Error.h" #include "ObjDumper.h" #include "StackMapPrinter.h" -#include "StreamWriter.h" +#include "llvm-readobj.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Object/MachO.h" #include "llvm/Support/Casting.h" +#include "llvm/Support/ScopedPrinter.h" using namespace llvm; using namespace object; @@ -28,9 +28,8 @@ namespace { class MachODumper : public ObjDumper { public: - MachODumper(const MachOObjectFile *Obj, StreamWriter& Writer) - : ObjDumper(Writer) - , Obj(Obj) { } + MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer) + : ObjDumper(Writer), Obj(Obj) {} void printFileHeaders() override; void printSections() override; @@ -69,7 +68,7 @@ private: namespace llvm { std::error_code createMachODumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr<ObjDumper> &Result) { const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj); if (!MachOObj) diff --git a/llvm/tools/llvm-readobj/ObjDumper.cpp b/llvm/tools/llvm-readobj/ObjDumper.cpp index 91803de1a1d..2a0a90e5cfd 100644 --- a/llvm/tools/llvm-readobj/ObjDumper.cpp +++ b/llvm/tools/llvm-readobj/ObjDumper.cpp @@ -14,15 +14,13 @@ #include "ObjDumper.h" #include "Error.h" -#include "StreamWriter.h" #include "llvm/Object/ObjectFile.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" namespace llvm { -ObjDumper::ObjDumper(StreamWriter& Writer) - : W(Writer) { -} +ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {} ObjDumper::~ObjDumper() { } diff --git a/llvm/tools/llvm-readobj/ObjDumper.h b/llvm/tools/llvm-readobj/ObjDumper.h index 2a633411933..e34789e1bdb 100644 --- a/llvm/tools/llvm-readobj/ObjDumper.h +++ b/llvm/tools/llvm-readobj/ObjDumper.h @@ -19,11 +19,11 @@ class COFFImportFile; class ObjectFile; } -class StreamWriter; +class ScopedPrinter; class ObjDumper { public: - ObjDumper(StreamWriter& Writer); + ObjDumper(ScopedPrinter &Writer); virtual ~ObjDumper(); virtual void printFileHeaders() = 0; @@ -71,19 +71,19 @@ public: virtual void printStackMap() const = 0; protected: - StreamWriter& W; + ScopedPrinter &W; }; std::error_code createCOFFDumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr<ObjDumper> &Result); std::error_code createELFDumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr<ObjDumper> &Result); std::error_code createMachODumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr<ObjDumper> &Result); void dumpCOFFImportFile(const object::COFFImportFile *File); diff --git a/llvm/tools/llvm-readobj/StreamWriter.cpp b/llvm/tools/llvm-readobj/StreamWriter.cpp deleted file mode 100644 index 391c12691da..00000000000 --- a/llvm/tools/llvm-readobj/StreamWriter.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "StreamWriter.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/Support/Format.h" -#include <cctype> - -using namespace llvm::support; - -namespace llvm { - -raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value) { - OS << "0x" << to_hexString(Value.Value); - return OS; -} - -const std::string to_hexString(uint64_t Value, bool UpperCase) { - std::string number; - llvm::raw_string_ostream stream(number); - stream << format_hex_no_prefix(Value, 1, UpperCase); - return stream.str(); -} - -void StreamWriter::printBinaryImpl(StringRef Label, StringRef Str, - ArrayRef<uint8_t> Data, bool Block) { - if (Data.size() > 16) - Block = true; - - if (Block) { - startLine() << Label; - if (Str.size() > 0) - OS << ": " << Str; - OS << " (\n"; - for (size_t addr = 0, end = Data.size(); addr < end; addr += 16) { - startLine() << format(" %04" PRIX64 ": ", uint64_t(addr)); - // Dump line of hex. - for (size_t i = 0; i < 16; ++i) { - if (i != 0 && i % 4 == 0) - OS << ' '; - if (addr + i < end) - OS << hexdigit((Data[addr + i] >> 4) & 0xF, false) - << hexdigit(Data[addr + i] & 0xF, false); - else - OS << " "; - } - // Print ascii. - OS << " |"; - for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { - if (std::isprint(Data[addr + i] & 0xFF)) - OS << Data[addr + i]; - else - OS << "."; - } - OS << "|\n"; - } - - startLine() << ")\n"; - } else { - startLine() << Label << ":"; - if (Str.size() > 0) - OS << " " << Str; - OS << " ("; - for (size_t i = 0; i < Data.size(); ++i) { - if (i > 0) - OS << " "; - - OS << format("%02X", static_cast<int>(Data[i])); - } - OS << ")\n"; - } -} - -} // namespace llvm diff --git a/llvm/tools/llvm-readobj/StreamWriter.h b/llvm/tools/llvm-readobj/StreamWriter.h deleted file mode 100644 index 9b0e5a6d398..00000000000 --- a/llvm/tools/llvm-readobj/StreamWriter.h +++ /dev/null @@ -1,354 +0,0 @@ -//===-- StreamWriter.h ----------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_TOOLS_LLVM_READOBJ_STREAMWRITER_H -#define LLVM_TOOLS_LLVM_READOBJ_STREAMWRITER_H - -#include "llvm/ADT/APSInt.h" -#include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/DataTypes.h" -#include "llvm/Support/Endian.h" -#include "llvm/Support/raw_ostream.h" -#include <algorithm> - -using namespace llvm; - -namespace llvm { - -template<typename T> -struct EnumEntry { - StringRef Name; - // While Name suffices in most of the cases, in certain cases - // GNU style and LLVM style of ELFDumper do not - // display same string for same enum. The AltName if initialized appropriately - // will hold the string that GNU style emits. - // Example: - // "EM_X86_64" string on LLVM style for Elf_Ehdr->e_machine corresponds to - // "Advanced Micro Devices X86-64" on GNU style - StringRef AltName; - T Value; - EnumEntry(StringRef N, StringRef A, T V) : Name(N), AltName(A), Value(V) {} - EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {} -}; - -struct HexNumber { - // To avoid sign-extension we have to explicitly cast to the appropriate - // unsigned type. The overloads are here so that every type that is implicitly - // convertible to an integer (including enums and endian helpers) can be used - // without requiring type traits or call-site changes. - HexNumber(char Value) : Value(static_cast<unsigned char>(Value)) { } - HexNumber(signed char Value) : Value(static_cast<unsigned char>(Value)) { } - HexNumber(signed short Value) : Value(static_cast<unsigned short>(Value)) { } - HexNumber(signed int Value) : Value(static_cast<unsigned int>(Value)) { } - HexNumber(signed long Value) : Value(static_cast<unsigned long>(Value)) { } - HexNumber(signed long long Value) : Value(static_cast<unsigned long long>(Value)) { } - HexNumber(unsigned char Value) : Value(Value) { } - HexNumber(unsigned short Value) : Value(Value) { } - HexNumber(unsigned int Value) : Value(Value) { } - HexNumber(unsigned long Value) : Value(Value) { } - HexNumber(unsigned long long Value) : Value(Value) { } - uint64_t Value; -}; - -raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value); -const std::string to_hexString(uint64_t Value, bool UpperCase = true); - -template <class T> const std::string to_string(const T &Value) { - std::string number; - llvm::raw_string_ostream stream(number); - stream << Value; - return stream.str(); -} - -class StreamWriter { -public: - StreamWriter(raw_ostream &OS) - : OS(OS) - , IndentLevel(0) { - } - - void flush() { - OS.flush(); - } - - void indent(int Levels = 1) { - IndentLevel += Levels; - } - - void unindent(int Levels = 1) { - IndentLevel = std::max(0, IndentLevel - Levels); - } - - void printIndent() { - for (int i = 0; i < IndentLevel; ++i) - OS << " "; - } - - template<typename T> - HexNumber hex(T Value) { - return HexNumber(Value); - } - - template<typename T, typename TEnum> - void printEnum(StringRef Label, T Value, - ArrayRef<EnumEntry<TEnum> > EnumValues) { - StringRef Name; - bool Found = false; - for (const auto &EnumItem : EnumValues) { - if (EnumItem.Value == Value) { - Name = EnumItem.Name; - Found = true; - break; - } - } - - if (Found) { - startLine() << Label << ": " << Name << " (" << hex(Value) << ")\n"; - } else { - startLine() << Label << ": " << hex(Value) << "\n"; - } - } - - template <typename T, typename TFlag> - void printFlags(StringRef Label, T Value, ArrayRef<EnumEntry<TFlag>> Flags, - TFlag EnumMask1 = {}, TFlag EnumMask2 = {}, - TFlag EnumMask3 = {}) { - typedef EnumEntry<TFlag> FlagEntry; - typedef SmallVector<FlagEntry, 10> FlagVector; - FlagVector SetFlags; - - for (const auto &Flag : Flags) { - if (Flag.Value == 0) - continue; - - TFlag EnumMask{}; - if (Flag.Value & EnumMask1) - EnumMask = EnumMask1; - else if (Flag.Value & EnumMask2) - EnumMask = EnumMask2; - else if (Flag.Value & EnumMask3) - EnumMask = EnumMask3; - bool IsEnum = (Flag.Value & EnumMask) != 0; - if ((!IsEnum && (Value & Flag.Value) == Flag.Value) || - (IsEnum && (Value & EnumMask) == Flag.Value)) { - SetFlags.push_back(Flag); - } - } - - std::sort(SetFlags.begin(), SetFlags.end(), &flagName<TFlag>); - - startLine() << Label << " [ (" << hex(Value) << ")\n"; - for (const auto &Flag : SetFlags) { - startLine() << " " << Flag.Name << " (" << hex(Flag.Value) << ")\n"; - } - startLine() << "]\n"; - } - - template<typename T> - void printFlags(StringRef Label, T Value) { - startLine() << Label << " [ (" << hex(Value) << ")\n"; - uint64_t Flag = 1; - uint64_t Curr = Value; - while (Curr > 0) { - if (Curr & 1) - startLine() << " " << hex(Flag) << "\n"; - Curr >>= 1; - Flag <<= 1; - } - startLine() << "]\n"; - } - - void printNumber(StringRef Label, uint64_t Value) { - startLine() << Label << ": " << Value << "\n"; - } - - void printNumber(StringRef Label, uint32_t Value) { - startLine() << Label << ": " << Value << "\n"; - } - - void printNumber(StringRef Label, uint16_t Value) { - startLine() << Label << ": " << Value << "\n"; - } - - void printNumber(StringRef Label, uint8_t Value) { - startLine() << Label << ": " << unsigned(Value) << "\n"; - } - - void printNumber(StringRef Label, int64_t Value) { - startLine() << Label << ": " << Value << "\n"; - } - - void printNumber(StringRef Label, int32_t Value) { - startLine() << Label << ": " << Value << "\n"; - } - - void printNumber(StringRef Label, int16_t Value) { - startLine() << Label << ": " << Value << "\n"; - } - - void printNumber(StringRef Label, int8_t Value) { - startLine() << Label << ": " << int(Value) << "\n"; - } - - void printNumber(StringRef Label, APSInt Value) { - startLine() << Label << ": " << Value << "\n"; - } - - void printBoolean(StringRef Label, bool Value) { - startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n'; - } - - template <typename T> - void printList(StringRef Label, const T &List) { - startLine() << Label << ": ["; - bool Comma = false; - for (const auto &Item : List) { - if (Comma) - OS << ", "; - OS << Item; - Comma = true; - } - OS << "]\n"; - } - - template <typename T> - void printHexList(StringRef Label, const T &List) { - startLine() << Label << ": ["; - bool Comma = false; - for (const auto &Item : List) { - if (Comma) - OS << ", "; - OS << hex(Item); - Comma = true; - } - OS << "]\n"; - } - - template<typename T> - void printHex(StringRef Label, T Value) { - startLine() << Label << ": " << hex(Value) << "\n"; - } - - template<typename T> - void printHex(StringRef Label, StringRef Str, T Value) { - startLine() << Label << ": " << Str << " (" << hex(Value) << ")\n"; - } - - template <typename T> - void printSymbolOffset(StringRef Label, StringRef Symbol, T Value) { - startLine() << Label << ": " << Symbol << '+' << hex(Value) << '\n'; - } - - void printString(StringRef Label, StringRef Value) { - startLine() << Label << ": " << Value << "\n"; - } - - void printString(StringRef Label, const std::string &Value) { - startLine() << Label << ": " << Value << "\n"; - } - - template<typename T> - void printNumber(StringRef Label, StringRef Str, T Value) { - startLine() << Label << ": " << Str << " (" << Value << ")\n"; - } - - void printBinary(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value) { - printBinaryImpl(Label, Str, Value, false); - } - - void printBinary(StringRef Label, StringRef Str, ArrayRef<char> Value) { - auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()), - Value.size()); - printBinaryImpl(Label, Str, V, false); - } - - void printBinary(StringRef Label, ArrayRef<uint8_t> Value) { - printBinaryImpl(Label, StringRef(), Value, false); - } - - void printBinary(StringRef Label, ArrayRef<char> Value) { - auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()), - Value.size()); - printBinaryImpl(Label, StringRef(), V, false); - } - - void printBinary(StringRef Label, StringRef Value) { - auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()), - Value.size()); - printBinaryImpl(Label, StringRef(), V, false); - } - - void printBinaryBlock(StringRef Label, StringRef Value) { - auto V = makeArrayRef(reinterpret_cast<const uint8_t*>(Value.data()), - Value.size()); - printBinaryImpl(Label, StringRef(), V, true); - } - - raw_ostream& startLine() { - printIndent(); - return OS; - } - - raw_ostream& getOStream() { - return OS; - } - -private: - template<typename T> - static bool flagName(const EnumEntry<T>& lhs, const EnumEntry<T>& rhs) { - return lhs.Name < rhs.Name; - } - - void printBinaryImpl(StringRef Label, StringRef Str, ArrayRef<uint8_t> Value, - bool Block); - - raw_ostream &OS; - int IndentLevel; -}; - -template <> -inline void -StreamWriter::printHex<support::ulittle16_t>(StringRef Label, - support::ulittle16_t Value) { - startLine() << Label << ": " << hex(Value) << "\n"; -} - -struct DictScope { - DictScope(StreamWriter& W, StringRef N) : W(W) { - W.startLine() << N << " {\n"; - W.indent(); - } - - ~DictScope() { - W.unindent(); - W.startLine() << "}\n"; - } - - StreamWriter& W; -}; - -struct ListScope { - ListScope(StreamWriter& W, StringRef N) : W(W) { - W.startLine() << N << " [\n"; - W.indent(); - } - - ~ListScope() { - W.unindent(); - W.startLine() << "]\n"; - } - - StreamWriter& W; -}; - -} // namespace llvm - -#endif diff --git a/llvm/tools/llvm-readobj/Win64EHDumper.h b/llvm/tools/llvm-readobj/Win64EHDumper.h index a80df9c4f94..772f68bf283 100644 --- a/llvm/tools/llvm-readobj/Win64EHDumper.h +++ b/llvm/tools/llvm-readobj/Win64EHDumper.h @@ -10,7 +10,7 @@ #ifndef LLVM_TOOLS_LLVM_READOBJ_WIN64EHDUMPER_H #define LLVM_TOOLS_LLVM_READOBJ_WIN64EHDUMPER_H -#include "StreamWriter.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/Win64EH.h" namespace llvm { @@ -22,7 +22,7 @@ struct coff_section; namespace Win64EH { class Dumper { - StreamWriter &SW; + ScopedPrinter &SW; raw_ostream &OS; public: @@ -53,7 +53,7 @@ private: uint64_t SectionOffset, const RuntimeFunction &RF); public: - Dumper(StreamWriter &SW) : SW(SW), OS(SW.getOStream()) {} + Dumper(ScopedPrinter &SW) : SW(SW), OS(SW.getOStream()) {} void printData(const Context &Ctx); }; diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp index 58241ab43d6..fe075a54f96 100644 --- a/llvm/tools/llvm-readobj/llvm-readobj.cpp +++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp @@ -22,7 +22,6 @@ #include "llvm-readobj.h" #include "Error.h" #include "ObjDumper.h" -#include "StreamWriter.h" #include "llvm/Object/Archive.h" #include "llvm/Object/COFFImportFile.h" #include "llvm/Object/ELFObjectFile.h" @@ -35,6 +34,7 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/Signals.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" @@ -293,7 +293,8 @@ static bool isMipsArch(unsigned Arch) { } /// @brief Creates an format-specific object file dumper. -static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer, +static std::error_code createDumper(const ObjectFile *Obj, + ScopedPrinter &Writer, std::unique_ptr<ObjDumper> &Result) { if (!Obj) return readobj_error::unsupported_file_format; @@ -310,7 +311,7 @@ static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer, /// @brief Dumps the specified object file. static void dumpObject(const ObjectFile *Obj) { - StreamWriter Writer(outs()); + ScopedPrinter Writer(outs()); std::unique_ptr<ObjDumper> Dumper; if (std::error_code EC = createDumper(Obj, Writer, Dumper)) reportError(Obj->getFileName(), EC); |

