xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/PDB/Native/LinePrinter.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- LinePrinter.h ------------------------------------------ *- C++ --*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_DEBUGINFO_PDB_NATIVE_LINEPRINTER_H
10 #define LLVM_DEBUGINFO_PDB_NATIVE_LINEPRINTER_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/DebugInfo/PDB/Native/FormatUtil.h"
16 #include "llvm/Support/BinaryStreamRef.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/FormatVariadic.h"
19 #include "llvm/Support/Regex.h"
20 #include "llvm/Support/raw_ostream.h"
21 
22 #include <list>
23 
24 // Container for filter options to control which elements will be printed.
25 struct FilterOptions {
26   std::list<std::string> ExcludeTypes;
27   std::list<std::string> ExcludeSymbols;
28   std::list<std::string> ExcludeCompilands;
29   std::list<std::string> IncludeTypes;
30   std::list<std::string> IncludeSymbols;
31   std::list<std::string> IncludeCompilands;
32   uint32_t PaddingThreshold;
33   uint32_t SizeThreshold;
34   std::optional<uint32_t> DumpModi;
35   std::optional<uint32_t> ParentRecurseDepth;
36   std::optional<uint32_t> ChildrenRecurseDepth;
37   std::optional<uint32_t> SymbolOffset;
38   bool JustMyCode;
39 };
40 
41 namespace llvm {
42 namespace msf {
43 class MSFStreamLayout;
44 } // namespace msf
45 namespace pdb {
46 
47 class ClassLayout;
48 class PDBFile;
49 class SymbolGroup;
50 
51 class LinePrinter {
52   friend class WithColor;
53 
54 public:
55   LLVM_ABI LinePrinter(int Indent, bool UseColor, raw_ostream &Stream,
56                        const FilterOptions &Filters);
57 
58   LLVM_ABI void Indent(uint32_t Amount = 0);
59   LLVM_ABI void Unindent(uint32_t Amount = 0);
60   LLVM_ABI void NewLine();
61 
62   LLVM_ABI void printLine(const Twine &T);
63   LLVM_ABI void print(const Twine &T);
formatLine(const char * Fmt,Ts &&...Items)64   template <typename... Ts> void formatLine(const char *Fmt, Ts &&...Items) {
65     printLine(formatv(Fmt, std::forward<Ts>(Items)...));
66   }
format(const char * Fmt,Ts &&...Items)67   template <typename... Ts> void format(const char *Fmt, Ts &&...Items) {
68     print(formatv(Fmt, std::forward<Ts>(Items)...));
69   }
70 
71   LLVM_ABI void formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
72                              uint64_t StartOffset);
73   LLVM_ABI void formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
74                              uint64_t BaseAddr, uint64_t StartOffset);
75 
76   LLVM_ABI void formatMsfStreamData(StringRef Label, PDBFile &File,
77                                     uint32_t StreamIdx, StringRef StreamPurpose,
78                                     uint64_t Offset, uint64_t Size);
79   LLVM_ABI void formatMsfStreamData(StringRef Label, PDBFile &File,
80                                     const msf::MSFStreamLayout &Stream,
81                                     BinarySubstreamRef Substream);
82   LLVM_ABI void formatMsfStreamBlocks(PDBFile &File,
83                                       const msf::MSFStreamLayout &Stream);
84 
hasColor()85   bool hasColor() const { return UseColor; }
getStream()86   raw_ostream &getStream() { return OS; }
getIndentLevel()87   int getIndentLevel() const { return CurrentIndent; }
88 
89   LLVM_ABI bool IsClassExcluded(const ClassLayout &Class);
90   LLVM_ABI bool IsTypeExcluded(llvm::StringRef TypeName, uint64_t Size);
91   LLVM_ABI bool IsSymbolExcluded(llvm::StringRef SymbolName);
92   LLVM_ABI bool IsCompilandExcluded(llvm::StringRef CompilandName);
93 
getFilters()94   const FilterOptions &getFilters() const { return Filters; }
95 
96 private:
97   template <typename Iter>
SetFilters(std::list<Regex> & List,Iter Begin,Iter End)98   void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) {
99     List.clear();
100     for (; Begin != End; ++Begin)
101       List.emplace_back(StringRef(*Begin));
102   }
103 
104   raw_ostream &OS;
105   int IndentSpaces;
106   int CurrentIndent;
107   bool UseColor;
108   const FilterOptions &Filters;
109 
110   std::list<Regex> ExcludeCompilandFilters;
111   std::list<Regex> ExcludeTypeFilters;
112   std::list<Regex> ExcludeSymbolFilters;
113 
114   std::list<Regex> IncludeCompilandFilters;
115   std::list<Regex> IncludeTypeFilters;
116   std::list<Regex> IncludeSymbolFilters;
117 };
118 
119 struct PrintScope {
PrintScopePrintScope120   explicit PrintScope(LinePrinter &P, uint32_t IndentLevel)
121       : P(P), IndentLevel(IndentLevel) {}
PrintScopePrintScope122   explicit PrintScope(const PrintScope &Other, uint32_t LabelWidth)
123       : P(Other.P), IndentLevel(Other.IndentLevel), LabelWidth(LabelWidth) {}
124 
125   LinePrinter &P;
126   uint32_t IndentLevel;
127   uint32_t LabelWidth = 0;
128 };
129 
withLabelWidth(const PrintScope & Scope,uint32_t W)130 inline PrintScope withLabelWidth(const PrintScope &Scope, uint32_t W) {
131   return PrintScope{Scope, W};
132 }
133 
134 struct AutoIndent {
135   explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
136       : L(&L), Amount(Amount) {
137     L.Indent(Amount);
138   }
AutoIndentAutoIndent139   explicit AutoIndent(const PrintScope &Scope) {
140     L = &Scope.P;
141     Amount = Scope.IndentLevel;
142   }
~AutoIndentAutoIndent143   ~AutoIndent() {
144     if (L)
145       L->Unindent(Amount);
146   }
147 
148   LinePrinter *L = nullptr;
149   uint32_t Amount = 0;
150 };
151 
152 template <class T>
153 inline raw_ostream &operator<<(LinePrinter &Printer, const T &Item) {
154   return Printer.getStream() << Item;
155 }
156 
157 enum class PDB_ColorItem {
158   None,
159   Address,
160   Type,
161   Comment,
162   Padding,
163   Keyword,
164   Offset,
165   Identifier,
166   Path,
167   SectionHeader,
168   LiteralValue,
169   Register,
170 };
171 
172 class WithColor {
173 public:
174   LLVM_ABI WithColor(LinePrinter &P, PDB_ColorItem C);
175   LLVM_ABI ~WithColor();
176 
get()177   raw_ostream &get() { return OS; }
178 
179 private:
180   void applyColor(PDB_ColorItem C);
181   raw_ostream &OS;
182   bool UseColor;
183 };
184 } // namespace pdb
185 } // namespace llvm
186 
187 #endif
188