1 //===- LinePrinter.h ------------------------------------------ *- C++ --*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H
11 #define LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H
12
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/BinaryStreamRef.h"
17 #include "llvm/Support/FormatVariadic.h"
18 #include "llvm/Support/Regex.h"
19 #include "llvm/Support/raw_ostream.h"
20
21 #include <list>
22
23 namespace llvm {
24 class BinaryStreamReader;
25 namespace msf {
26 class MSFStreamLayout;
27 } // namespace msf
28 namespace pdb {
29
30 class ClassLayout;
31 class PDBFile;
32
33 class LinePrinter {
34 friend class WithColor;
35
36 public:
37 LinePrinter(int Indent, bool UseColor, raw_ostream &Stream);
38
39 void Indent(uint32_t Amount = 0);
40 void Unindent(uint32_t Amount = 0);
41 void NewLine();
42
43 void printLine(const Twine &T);
44 void print(const Twine &T);
formatLine(const char * Fmt,Ts &&...Items)45 template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) {
46 printLine(formatv(Fmt, std::forward<Ts>(Items)...));
47 }
format(const char * Fmt,Ts &&...Items)48 template <typename... Ts> void format(const char *Fmt, Ts &&... Items) {
49 print(formatv(Fmt, std::forward<Ts>(Items)...));
50 }
51
52 void formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
53 uint32_t StartOffset);
54 void formatBinary(StringRef Label, ArrayRef<uint8_t> Data, uint64_t BaseAddr,
55 uint32_t StartOffset);
56
57 void formatMsfStreamData(StringRef Label, PDBFile &File, uint32_t StreamIdx,
58 StringRef StreamPurpose, uint32_t Offset,
59 uint32_t Size);
60 void formatMsfStreamData(StringRef Label, PDBFile &File,
61 const msf::MSFStreamLayout &Stream,
62 BinarySubstreamRef Substream);
63 void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream);
64
hasColor()65 bool hasColor() const { return UseColor; }
getStream()66 raw_ostream &getStream() { return OS; }
getIndentLevel()67 int getIndentLevel() const { return CurrentIndent; }
68
69 bool IsClassExcluded(const ClassLayout &Class);
70 bool IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size);
71 bool IsSymbolExcluded(llvm::StringRef SymbolName);
72 bool IsCompilandExcluded(llvm::StringRef CompilandName);
73
74 private:
75 template <typename Iter>
SetFilters(std::list<Regex> & List,Iter Begin,Iter End)76 void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) {
77 List.clear();
78 for (; Begin != End; ++Begin)
79 List.emplace_back(StringRef(*Begin));
80 }
81
82 raw_ostream &OS;
83 int IndentSpaces;
84 int CurrentIndent;
85 bool UseColor;
86
87 std::list<Regex> ExcludeCompilandFilters;
88 std::list<Regex> ExcludeTypeFilters;
89 std::list<Regex> ExcludeSymbolFilters;
90
91 std::list<Regex> IncludeCompilandFilters;
92 std::list<Regex> IncludeTypeFilters;
93 std::list<Regex> IncludeSymbolFilters;
94 };
95
96 struct PrintScope {
PrintScopePrintScope97 explicit PrintScope(LinePrinter &P, uint32_t IndentLevel)
98 : P(P), IndentLevel(IndentLevel) {}
PrintScopePrintScope99 explicit PrintScope(const PrintScope &Other, uint32_t LabelWidth)
100 : P(Other.P), IndentLevel(Other.IndentLevel), LabelWidth(LabelWidth) {}
101
102 LinePrinter &P;
103 uint32_t IndentLevel;
104 uint32_t LabelWidth = 0;
105 };
106
withLabelWidth(const Optional<PrintScope> & Scope,uint32_t W)107 inline Optional<PrintScope> withLabelWidth(const Optional<PrintScope> &Scope,
108 uint32_t W) {
109 if (!Scope)
110 return None;
111 return PrintScope{*Scope, W};
112 }
113
114 struct AutoIndent {
115 explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
116 : L(&L), Amount(Amount) {
117 L.Indent(Amount);
118 }
AutoIndentAutoIndent119 explicit AutoIndent(const Optional<PrintScope> &Scope) {
120 if (Scope.hasValue()) {
121 L = &Scope->P;
122 Amount = Scope->IndentLevel;
123 }
124 }
~AutoIndentAutoIndent125 ~AutoIndent() {
126 if (L)
127 L->Unindent(Amount);
128 }
129
130 LinePrinter *L = nullptr;
131 uint32_t Amount = 0;
132 };
133
134 template <class T>
135 inline raw_ostream &operator<<(LinePrinter &Printer, const T &Item) {
136 Printer.getStream() << Item;
137 return Printer.getStream();
138 }
139
140 enum class PDB_ColorItem {
141 None,
142 Address,
143 Type,
144 Comment,
145 Padding,
146 Keyword,
147 Offset,
148 Identifier,
149 Path,
150 SectionHeader,
151 LiteralValue,
152 Register,
153 };
154
155 class WithColor {
156 public:
157 WithColor(LinePrinter &P, PDB_ColorItem C);
158 ~WithColor();
159
get()160 raw_ostream &get() { return OS; }
161
162 private:
163 void applyColor(PDB_ColorItem C);
164 raw_ostream &OS;
165 bool UseColor;
166 };
167 }
168 }
169
170 #endif
171