[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / tools / llvm-pdbutil / LinePrinter.h
blobb6bb77280fd5c696d3ad29af7ff168d89e85afd3
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H
10 #define LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/Support/BinaryStreamRef.h"
16 #include "llvm/Support/FormatVariadic.h"
17 #include "llvm/Support/Regex.h"
18 #include "llvm/Support/raw_ostream.h"
20 #include <list>
22 namespace llvm {
23 namespace msf {
24 class MSFStreamLayout;
25 } // namespace msf
26 namespace pdb {
28 class ClassLayout;
29 class PDBFile;
31 class LinePrinter {
32 friend class WithColor;
34 public:
35 LinePrinter(int Indent, bool UseColor, raw_ostream &Stream);
37 void Indent(uint32_t Amount = 0);
38 void Unindent(uint32_t Amount = 0);
39 void NewLine();
41 void printLine(const Twine &T);
42 void print(const Twine &T);
43 template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) {
44 printLine(formatv(Fmt, std::forward<Ts>(Items)...));
46 template <typename... Ts> void format(const char *Fmt, Ts &&... Items) {
47 print(formatv(Fmt, std::forward<Ts>(Items)...));
50 void formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
51 uint64_t StartOffset);
52 void formatBinary(StringRef Label, ArrayRef<uint8_t> Data, uint64_t BaseAddr,
53 uint64_t StartOffset);
55 void formatMsfStreamData(StringRef Label, PDBFile &File, uint32_t StreamIdx,
56 StringRef StreamPurpose, uint64_t Offset,
57 uint64_t Size);
58 void formatMsfStreamData(StringRef Label, PDBFile &File,
59 const msf::MSFStreamLayout &Stream,
60 BinarySubstreamRef Substream);
61 void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream);
63 bool hasColor() const { return UseColor; }
64 raw_ostream &getStream() { return OS; }
65 int getIndentLevel() const { return CurrentIndent; }
67 bool IsClassExcluded(const ClassLayout &Class);
68 bool IsTypeExcluded(llvm::StringRef TypeName, uint64_t Size);
69 bool IsSymbolExcluded(llvm::StringRef SymbolName);
70 bool IsCompilandExcluded(llvm::StringRef CompilandName);
72 private:
73 template <typename Iter>
74 void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) {
75 List.clear();
76 for (; Begin != End; ++Begin)
77 List.emplace_back(StringRef(*Begin));
80 raw_ostream &OS;
81 int IndentSpaces;
82 int CurrentIndent;
83 bool UseColor;
85 std::list<Regex> ExcludeCompilandFilters;
86 std::list<Regex> ExcludeTypeFilters;
87 std::list<Regex> ExcludeSymbolFilters;
89 std::list<Regex> IncludeCompilandFilters;
90 std::list<Regex> IncludeTypeFilters;
91 std::list<Regex> IncludeSymbolFilters;
94 struct PrintScope {
95 explicit PrintScope(LinePrinter &P, uint32_t IndentLevel)
96 : P(P), IndentLevel(IndentLevel) {}
97 explicit PrintScope(const PrintScope &Other, uint32_t LabelWidth)
98 : P(Other.P), IndentLevel(Other.IndentLevel), LabelWidth(LabelWidth) {}
100 LinePrinter &P;
101 uint32_t IndentLevel;
102 uint32_t LabelWidth = 0;
105 inline Optional<PrintScope> withLabelWidth(const Optional<PrintScope> &Scope,
106 uint32_t W) {
107 if (!Scope)
108 return None;
109 return PrintScope{*Scope, W};
112 struct AutoIndent {
113 explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
114 : L(&L), Amount(Amount) {
115 L.Indent(Amount);
117 explicit AutoIndent(const Optional<PrintScope> &Scope) {
118 if (Scope.hasValue()) {
119 L = &Scope->P;
120 Amount = Scope->IndentLevel;
123 ~AutoIndent() {
124 if (L)
125 L->Unindent(Amount);
128 LinePrinter *L = nullptr;
129 uint32_t Amount = 0;
132 template <class T>
133 inline raw_ostream &operator<<(LinePrinter &Printer, const T &Item) {
134 return Printer.getStream() << Item;
137 enum class PDB_ColorItem {
138 None,
139 Address,
140 Type,
141 Comment,
142 Padding,
143 Keyword,
144 Offset,
145 Identifier,
146 Path,
147 SectionHeader,
148 LiteralValue,
149 Register,
152 class WithColor {
153 public:
154 WithColor(LinePrinter &P, PDB_ColorItem C);
155 ~WithColor();
157 raw_ostream &get() { return OS; }
159 private:
160 void applyColor(PDB_ColorItem C);
161 raw_ostream &OS;
162 bool UseColor;
167 #endif