From 88bb163f816d00d2805b6a0d97ea0f4200b84553 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Tue, 3 May 2016 00:28:04 +0000 Subject: [PATCH] Move llvm-readobj/StreamWriter to Support. We wish to re-use this from llvm-pdbdump, and it provides a nice way to print structured data in scoped format that could prove useful for many other dumping tools as well. Moving to support and changing name to ScopedPrinter to better reflect its purpose. llvm-svn: 268342 --- .../llvm/Support/ScopedPrinter.h} | 107 +++++++++------------ llvm/lib/Support/CMakeLists.txt | 1 + .../Support/ScopedPrinter.cpp} | 9 +- llvm/tools/llvm-readobj/ARMAttributeParser.cpp | 2 +- llvm/tools/llvm-readobj/ARMAttributeParser.h | 6 +- llvm/tools/llvm-readobj/ARMEHABIPrinter.h | 10 +- llvm/tools/llvm-readobj/ARMWinEHPrinter.h | 6 +- llvm/tools/llvm-readobj/CMakeLists.txt | 1 - llvm/tools/llvm-readobj/COFFDumper.cpp | 14 +-- llvm/tools/llvm-readobj/ELFDumper.cpp | 34 +++---- llvm/tools/llvm-readobj/MachODumper.cpp | 11 +-- llvm/tools/llvm-readobj/ObjDumper.cpp | 6 +- llvm/tools/llvm-readobj/ObjDumper.h | 12 +-- llvm/tools/llvm-readobj/Win64EHDumper.h | 6 +- llvm/tools/llvm-readobj/llvm-readobj.cpp | 7 +- 15 files changed, 106 insertions(+), 126 deletions(-) rename llvm/{tools/llvm-readobj/StreamWriter.h => include/llvm/Support/ScopedPrinter.h} (74%) rename llvm/{tools/llvm-readobj/StreamWriter.cpp => lib/Support/ScopedPrinter.cpp} (86%) diff --git a/llvm/tools/llvm-readobj/StreamWriter.h b/llvm/include/llvm/Support/ScopedPrinter.h similarity index 74% rename from llvm/tools/llvm-readobj/StreamWriter.h rename to llvm/include/llvm/Support/ScopedPrinter.h index 9b0e5a6..c29d532 100644 --- a/llvm/tools/llvm-readobj/StreamWriter.h +++ b/llvm/include/llvm/Support/ScopedPrinter.h @@ -1,4 +1,4 @@ -//===-- StreamWriter.h ----------------------------------------------------===// +//===-- ScopedPrinter.h ---------------------------------------------------===// // // The LLVM Compiler Infrastructure // @@ -7,8 +7,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_TOOLS_LLVM_READOBJ_STREAMWRITER_H -#define LLVM_TOOLS_LLVM_READOBJ_STREAMWRITER_H +#ifndef LLVM_SUPPORT_SCOPEDPRINTER_H +#define LLVM_SUPPORT_SCOPEDPRINTER_H #include "llvm/ADT/APSInt.h" #include "llvm/ADT/ArrayRef.h" @@ -23,8 +23,7 @@ using namespace llvm; namespace llvm { -template -struct EnumEntry { +template struct EnumEntry { StringRef Name; // While Name suffices in most of the cases, in certain cases // GNU style and LLVM style of ELFDumper do not @@ -44,21 +43,22 @@ struct HexNumber { // 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(Value)) { } - HexNumber(signed char Value) : Value(static_cast(Value)) { } - HexNumber(signed short Value) : Value(static_cast(Value)) { } - HexNumber(signed int Value) : Value(static_cast(Value)) { } - HexNumber(signed long Value) : Value(static_cast(Value)) { } - HexNumber(signed long long Value) : Value(static_cast(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) { } + HexNumber(char Value) : Value(static_cast(Value)) {} + HexNumber(signed char Value) : Value(static_cast(Value)) {} + HexNumber(signed short Value) : Value(static_cast(Value)) {} + HexNumber(signed int Value) : Value(static_cast(Value)) {} + HexNumber(signed long Value) : Value(static_cast(Value)) {} + HexNumber(signed long long Value) + : Value(static_cast(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); +raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value); const std::string to_hexString(uint64_t Value, bool UpperCase = true); template const std::string to_string(const T &Value) { @@ -68,20 +68,13 @@ template const std::string to_string(const T &Value) { return stream.str(); } -class StreamWriter { +class ScopedPrinter { public: - StreamWriter(raw_ostream &OS) - : OS(OS) - , IndentLevel(0) { - } + ScopedPrinter(raw_ostream &OS) : OS(OS), IndentLevel(0) {} - void flush() { - OS.flush(); - } + void flush() { OS.flush(); } - void indent(int Levels = 1) { - IndentLevel += Levels; - } + void indent(int Levels = 1) { IndentLevel += Levels; } void unindent(int Levels = 1) { IndentLevel = std::max(0, IndentLevel - Levels); @@ -92,14 +85,11 @@ public: OS << " "; } - template - HexNumber hex(T Value) { - return HexNumber(Value); - } + template HexNumber hex(T Value) { return HexNumber(Value); } - template + template void printEnum(StringRef Label, T Value, - ArrayRef > EnumValues) { + ArrayRef> EnumValues) { StringRef Name; bool Found = false; for (const auto &EnumItem : EnumValues) { @@ -138,7 +128,7 @@ public: EnumMask = EnumMask3; bool IsEnum = (Flag.Value & EnumMask) != 0; if ((!IsEnum && (Value & Flag.Value) == Flag.Value) || - (IsEnum && (Value & EnumMask) == Flag.Value)) { + (IsEnum && (Value & EnumMask) == Flag.Value)) { SetFlags.push_back(Flag); } } @@ -152,8 +142,7 @@ public: startLine() << "]\n"; } - template - void printFlags(StringRef Label, T Value) { + template void printFlags(StringRef Label, T Value) { startLine() << Label << " [ (" << hex(Value) << ")\n"; uint64_t Flag = 1; uint64_t Curr = Value; @@ -206,8 +195,7 @@ public: startLine() << Label << ": " << (Value ? "Yes" : "No") << '\n'; } - template - void printList(StringRef Label, const T &List) { + template void printList(StringRef Label, const T &List) { startLine() << Label << ": ["; bool Comma = false; for (const auto &Item : List) { @@ -219,8 +207,7 @@ public: OS << "]\n"; } - template - void printHexList(StringRef Label, const T &List) { + template void printHexList(StringRef Label, const T &List) { startLine() << Label << ": ["; bool Comma = false; for (const auto &Item : List) { @@ -232,13 +219,11 @@ public: OS << "]\n"; } - template - void printHex(StringRef Label, T Value) { + template void printHex(StringRef Label, T Value) { startLine() << Label << ": " << hex(Value) << "\n"; } - template - void printHex(StringRef Label, StringRef Str, T Value) { + template void printHex(StringRef Label, StringRef Str, T Value) { startLine() << Label << ": " << Str << " (" << hex(Value) << ")\n"; } @@ -255,7 +240,7 @@ public: startLine() << Label << ": " << Value << "\n"; } - template + template void printNumber(StringRef Label, StringRef Str, T Value) { startLine() << Label << ": " << Str << " (" << Value << ")\n"; } @@ -265,7 +250,7 @@ public: } void printBinary(StringRef Label, StringRef Str, ArrayRef Value) { - auto V = makeArrayRef(reinterpret_cast(Value.data()), + auto V = makeArrayRef(reinterpret_cast(Value.data()), Value.size()); printBinaryImpl(Label, Str, V, false); } @@ -275,35 +260,33 @@ public: } void printBinary(StringRef Label, ArrayRef Value) { - auto V = makeArrayRef(reinterpret_cast(Value.data()), + auto V = makeArrayRef(reinterpret_cast(Value.data()), Value.size()); printBinaryImpl(Label, StringRef(), V, false); } void printBinary(StringRef Label, StringRef Value) { - auto V = makeArrayRef(reinterpret_cast(Value.data()), + auto V = makeArrayRef(reinterpret_cast(Value.data()), Value.size()); printBinaryImpl(Label, StringRef(), V, false); } void printBinaryBlock(StringRef Label, StringRef Value) { - auto V = makeArrayRef(reinterpret_cast(Value.data()), + auto V = makeArrayRef(reinterpret_cast(Value.data()), Value.size()); printBinaryImpl(Label, StringRef(), V, true); } - raw_ostream& startLine() { + raw_ostream &startLine() { printIndent(); return OS; } - raw_ostream& getOStream() { - return OS; - } + raw_ostream &getOStream() { return OS; } private: - template - static bool flagName(const EnumEntry& lhs, const EnumEntry& rhs) { + template + static bool flagName(const EnumEntry &lhs, const EnumEntry &rhs) { return lhs.Name < rhs.Name; } @@ -316,13 +299,13 @@ private: template <> inline void -StreamWriter::printHex(StringRef Label, - support::ulittle16_t Value) { +ScopedPrinter::printHex(StringRef Label, + support::ulittle16_t Value) { startLine() << Label << ": " << hex(Value) << "\n"; } struct DictScope { - DictScope(StreamWriter& W, StringRef N) : W(W) { + DictScope(ScopedPrinter &W, StringRef N) : W(W) { W.startLine() << N << " {\n"; W.indent(); } @@ -332,11 +315,11 @@ struct DictScope { W.startLine() << "}\n"; } - StreamWriter& W; + ScopedPrinter &W; }; struct ListScope { - ListScope(StreamWriter& W, StringRef N) : W(W) { + ListScope(ScopedPrinter &W, StringRef N) : W(W) { W.startLine() << N << " [\n"; W.indent(); } @@ -346,7 +329,7 @@ struct ListScope { W.startLine() << "]\n"; } - StreamWriter& W; + ScopedPrinter &W; }; } // namespace llvm diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index 0de1972..3d718e6 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -76,6 +76,7 @@ add_llvm_library(LLVMSupport RandomNumberGenerator.cpp Regex.cpp ScaledNumber.cpp + ScopedPrinter.cpp SHA1.cpp SmallPtrSet.cpp SmallVector.cpp diff --git a/llvm/tools/llvm-readobj/StreamWriter.cpp b/llvm/lib/Support/ScopedPrinter.cpp similarity index 86% rename from llvm/tools/llvm-readobj/StreamWriter.cpp rename to llvm/lib/Support/ScopedPrinter.cpp index 391c126..0225f01 100644 --- a/llvm/tools/llvm-readobj/StreamWriter.cpp +++ b/llvm/lib/Support/ScopedPrinter.cpp @@ -1,4 +1,5 @@ -#include "StreamWriter.h" +#include "llvm/Support/ScopedPrinter.h" + #include "llvm/ADT/StringExtras.h" #include "llvm/Support/Format.h" #include @@ -7,7 +8,7 @@ using namespace llvm::support; namespace llvm { -raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value) { +raw_ostream &operator<<(raw_ostream &OS, const HexNumber &Value) { OS << "0x" << to_hexString(Value.Value); return OS; } @@ -19,8 +20,8 @@ const std::string to_hexString(uint64_t Value, bool UpperCase) { return stream.str(); } -void StreamWriter::printBinaryImpl(StringRef Label, StringRef Str, - ArrayRef Data, bool Block) { +void ScopedPrinter::printBinaryImpl(StringRef Label, StringRef Str, + ArrayRef Data, bool Block) { if (Data.size() > 16) Block = true; diff --git a/llvm/tools/llvm-readobj/ARMAttributeParser.cpp b/llvm/tools/llvm-readobj/ARMAttributeParser.cpp index 76de144..877dd71 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 a582856..6936b70 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 &IndexList); void ParseSubsection(const uint8_t *Data, uint32_t Length); public: - ARMAttributeParser(StreamWriter &SW) : SW(SW) {} + ARMAttributeParser(ScopedPrinter &SW) : SW(SW) {} void Parse(ArrayRef Section); }; diff --git a/llvm/tools/llvm-readobj/ARMEHABIPrinter.h b/llvm/tools/llvm-readobj/ARMEHABIPrinter.h index 5845eb0..59c9b71 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::Elf_Rel Elf_Rel; typedef typename object::ELFFile::Elf_Word Elf_Word; - StreamWriter &SW; + ScopedPrinter &SW; const object::ELFFile *ELF; const Elf_Shdr *Symtab; ArrayRef 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 *ELF, + PrinterContext(ScopedPrinter &SW, const object::ELFFile *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 274ef11..95f5217 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 a06b678..e167874 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 a782d2a..48e8178 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 &Result) { const COFFObjectFile *COFFObj = dyn_cast(Obj); if (!COFFObj) diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index 4905b43..31bf569 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 class ELFDumper : public ObjDumper { public: - ELFDumper(const ELFFile *Obj, StreamWriter &Writer); + ELFDumper(const ELFFile *Obj, ScopedPrinter &Writer); void printFileHeaders() override; void printSections() override; @@ -300,7 +300,7 @@ template class GNUStyle : public DumpStyle { formatted_raw_ostream OS; public: TYPEDEF_ELF_TYPES(ELFT) - GNUStyle(StreamWriter &W, ELFDumper *Dumper) + GNUStyle(ScopedPrinter &W, ELFDumper *Dumper) : DumpStyle(Dumper), OS(W.getOStream()) {} void printFileHeaders(const ELFO *Obj) override; void printGroupSections(const ELFFile *Obj) override; @@ -353,7 +353,7 @@ private: template class LLVMStyle : public DumpStyle { public: TYPEDEF_ELF_TYPES(ELFT) - LLVMStyle(StreamWriter &W, ELFDumper *Dumper) + LLVMStyle(ScopedPrinter &W, ELFDumper *Dumper) : DumpStyle(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 static std::error_code createELFDumper(const ELFFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr &Result) { Result.reset(new ELFDumper(Obj, Writer)); return readobj_error::success; } std::error_code createELFDumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr &Result) { // Little-endian 32-bit if (const ELF32LEObjectFile *ELFObj = dyn_cast(Obj)) @@ -491,12 +491,10 @@ template void ELFDumper::LoadVersionMap() const { LoadVersionNeeds(dot_gnu_version_r_sec); } - template -static void printVersionSymbolSection(ELFDumper *Dumper, - const ELFO *Obj, +static void printVersionSymbolSection(ELFDumper *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 static void printVersionDefinitionSection(ELFDumper *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 ElfMips16SymOtherFlags[] = { }; template -ELFDumper::ELFDumper(const ELFFile *Obj, StreamWriter &Writer) +ELFDumper::ELFDumper(const ELFFile *Obj, ScopedPrinter &Writer) : ObjDumper(Writer), Obj(Obj) { SmallVector LoadSegments; @@ -1795,7 +1793,7 @@ public: typedef typename ELFO::Elf_Rela Elf_Rela; MipsGOTParser(ELFDumper *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 *Dumper; const ELFO *Obj; - StreamWriter &W; + ScopedPrinter &W; llvm::Optional DtPltGot; llvm::Optional DtLocalGotNum; llvm::Optional DtGotSym; @@ -1828,7 +1826,7 @@ private: template MipsGOTParser::MipsGOTParser(ELFDumper *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 cc57211..3773df2 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 &Result) { const MachOObjectFile *MachOObj = dyn_cast(Obj); if (!MachOObj) diff --git a/llvm/tools/llvm-readobj/ObjDumper.cpp b/llvm/tools/llvm-readobj/ObjDumper.cpp index 91803de..2a0a90e 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 2a63341..e34789e 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 &Result); std::error_code createELFDumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr &Result); std::error_code createMachODumper(const object::ObjectFile *Obj, - StreamWriter &Writer, + ScopedPrinter &Writer, std::unique_ptr &Result); void dumpCOFFImportFile(const object::COFFImportFile *File); diff --git a/llvm/tools/llvm-readobj/Win64EHDumper.h b/llvm/tools/llvm-readobj/Win64EHDumper.h index a80df9c4..772f68b 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 58241ab..fe075a5 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 &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 Dumper; if (std::error_code EC = createDumper(Obj, Writer, Dumper)) reportError(Obj->getFileName(), EC); -- 2.7.4