blob: 74e341267accda4b921ed8e3ebb1ff2b663d888a [file] [log] [blame]
Zachary Turner2d11c202015-02-27 09:15:59 +00001//===- LinePrinter.h ------------------------------------------ *- C++ --*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner2d11c202015-02-27 09:15:59 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H
10#define LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H
11
Zachary Turner63055452017-06-15 22:24:24 +000012#include "llvm/ADT/ArrayRef.h"
Zachary Turner98162462015-03-01 06:59:57 +000013#include "llvm/ADT/StringRef.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000014#include "llvm/ADT/Twine.h"
Zachary Turner0b36c3e2017-06-23 18:52:13 +000015#include "llvm/Support/BinaryStreamRef.h"
Zachary Turner63055452017-06-15 22:24:24 +000016#include "llvm/Support/FormatVariadic.h"
Zachary Turnerda504b72017-06-15 20:55:51 +000017#include "llvm/Support/Regex.h"
Zachary Turner63055452017-06-15 22:24:24 +000018#include "llvm/Support/raw_ostream.h"
Zachary Turnerf5abda22015-03-01 06:49:49 +000019
20#include <list>
Zachary Turner2d11c202015-02-27 09:15:59 +000021
22namespace llvm {
Zachary Turner0b36c3e2017-06-23 18:52:13 +000023class BinaryStreamReader;
24namespace msf {
25class MSFStreamLayout;
26} // namespace msf
Zachary Turnerec28fc32016-05-04 20:32:13 +000027namespace pdb {
Zachary Turner2d11c202015-02-27 09:15:59 +000028
Zachary Turner4dc4f012017-04-13 21:11:00 +000029class ClassLayout;
Zachary Turner0b36c3e2017-06-23 18:52:13 +000030class PDBFile;
Zachary Turner4dc4f012017-04-13 21:11:00 +000031
Zachary Turner2d11c202015-02-27 09:15:59 +000032class LinePrinter {
33 friend class WithColor;
34
35public:
Adrian McCarthy1aa207d2017-03-23 15:28:15 +000036 LinePrinter(int Indent, bool UseColor, raw_ostream &Stream);
Zachary Turner2d11c202015-02-27 09:15:59 +000037
Zachary Turner63055452017-06-15 22:24:24 +000038 void Indent(uint32_t Amount = 0);
39 void Unindent(uint32_t Amount = 0);
Zachary Turner2d11c202015-02-27 09:15:59 +000040 void NewLine();
41
Zachary Turner63055452017-06-15 22:24:24 +000042 void printLine(const Twine &T);
43 void print(const Twine &T);
44 template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) {
45 printLine(formatv(Fmt, std::forward<Ts>(Items)...));
46 }
47 template <typename... Ts> void format(const char *Fmt, Ts &&... Items) {
48 print(formatv(Fmt, std::forward<Ts>(Items)...));
49 }
50
51 void formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
52 uint32_t StartOffset);
Zachary Turner99402032017-06-22 20:58:11 +000053 void formatBinary(StringRef Label, ArrayRef<uint8_t> Data, uint64_t BaseAddr,
54 uint32_t StartOffset);
Zachary Turner63055452017-06-15 22:24:24 +000055
Zachary Turner0b36c3e2017-06-23 18:52:13 +000056 void formatMsfStreamData(StringRef Label, PDBFile &File, uint32_t StreamIdx,
57 StringRef StreamPurpose, uint32_t Offset,
58 uint32_t Size);
59 void formatMsfStreamData(StringRef Label, PDBFile &File,
60 const msf::MSFStreamLayout &Stream,
61 BinarySubstreamRef Substream);
Zachary Turnerc3d8eec2017-08-02 22:25:52 +000062 void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream);
Zachary Turner0b36c3e2017-06-23 18:52:13 +000063
Adrian McCarthy1aa207d2017-03-23 15:28:15 +000064 bool hasColor() const { return UseColor; }
Zachary Turner2d11c202015-02-27 09:15:59 +000065 raw_ostream &getStream() { return OS; }
Zachary Turner7797c722015-03-02 04:39:56 +000066 int getIndentLevel() const { return CurrentIndent; }
Zachary Turner2d11c202015-02-27 09:15:59 +000067
Zachary Turner4dc4f012017-04-13 21:11:00 +000068 bool IsClassExcluded(const ClassLayout &Class);
69 bool IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size);
Zachary Turnerf5abda22015-03-01 06:49:49 +000070 bool IsSymbolExcluded(llvm::StringRef SymbolName);
71 bool IsCompilandExcluded(llvm::StringRef CompilandName);
72
Zachary Turner2d11c202015-02-27 09:15:59 +000073private:
Zachary Turner7797c722015-03-02 04:39:56 +000074 template <typename Iter>
75 void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) {
76 List.clear();
77 for (; Begin != End; ++Begin)
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +000078 List.emplace_back(StringRef(*Begin));
Zachary Turner7797c722015-03-02 04:39:56 +000079 }
80
Zachary Turner2d11c202015-02-27 09:15:59 +000081 raw_ostream &OS;
82 int IndentSpaces;
83 int CurrentIndent;
Adrian McCarthy1aa207d2017-03-23 15:28:15 +000084 bool UseColor;
Zachary Turnerf5abda22015-03-01 06:49:49 +000085
Zachary Turner4dddcc62015-09-29 19:49:06 +000086 std::list<Regex> ExcludeCompilandFilters;
87 std::list<Regex> ExcludeTypeFilters;
88 std::list<Regex> ExcludeSymbolFilters;
89
90 std::list<Regex> IncludeCompilandFilters;
91 std::list<Regex> IncludeTypeFilters;
92 std::list<Regex> IncludeSymbolFilters;
Zachary Turner2d11c202015-02-27 09:15:59 +000093};
94
Zachary Turnerabb17cc2017-09-01 20:06:56 +000095struct PrintScope {
96 explicit PrintScope(LinePrinter &P, uint32_t IndentLevel)
97 : P(P), IndentLevel(IndentLevel) {}
98 explicit PrintScope(const PrintScope &Other, uint32_t LabelWidth)
99 : P(Other.P), IndentLevel(Other.IndentLevel), LabelWidth(LabelWidth) {}
100
101 LinePrinter &P;
102 uint32_t IndentLevel;
103 uint32_t LabelWidth = 0;
104};
105
106inline Optional<PrintScope> withLabelWidth(const Optional<PrintScope> &Scope,
107 uint32_t W) {
108 if (!Scope)
109 return None;
110 return PrintScope{*Scope, W};
111}
112
Zachary Turner63055452017-06-15 22:24:24 +0000113struct AutoIndent {
114 explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000115 : L(&L), Amount(Amount) {
Zachary Turner63055452017-06-15 22:24:24 +0000116 L.Indent(Amount);
117 }
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000118 explicit AutoIndent(const Optional<PrintScope> &Scope) {
119 if (Scope.hasValue()) {
120 L = &Scope->P;
121 Amount = Scope->IndentLevel;
122 }
123 }
124 ~AutoIndent() {
125 if (L)
126 L->Unindent(Amount);
127 }
Zachary Turner63055452017-06-15 22:24:24 +0000128
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000129 LinePrinter *L = nullptr;
Zachary Turner63055452017-06-15 22:24:24 +0000130 uint32_t Amount = 0;
131};
132
Zachary Turner2d11c202015-02-27 09:15:59 +0000133template <class T>
134inline raw_ostream &operator<<(LinePrinter &Printer, const T &Item) {
135 Printer.getStream() << Item;
136 return Printer.getStream();
137}
138
139enum class PDB_ColorItem {
140 None,
141 Address,
142 Type,
Zachary Turner0c990bbe2017-04-10 19:33:29 +0000143 Comment,
144 Padding,
Zachary Turner2d11c202015-02-27 09:15:59 +0000145 Keyword,
146 Offset,
147 Identifier,
148 Path,
149 SectionHeader,
150 LiteralValue,
Zachary Turner7797c722015-03-02 04:39:56 +0000151 Register,
Zachary Turner2d11c202015-02-27 09:15:59 +0000152};
153
154class WithColor {
155public:
156 WithColor(LinePrinter &P, PDB_ColorItem C);
157 ~WithColor();
158
159 raw_ostream &get() { return OS; }
160
161private:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000162 void applyColor(PDB_ColorItem C);
Zachary Turner2d11c202015-02-27 09:15:59 +0000163 raw_ostream &OS;
Adrian McCarthy5fcfc2c2017-03-29 17:11:27 +0000164 bool UseColor;
Zachary Turner2d11c202015-02-27 09:15:59 +0000165};
166}
Zachary Turnerec28fc32016-05-04 20:32:13 +0000167}
Zachary Turner2d11c202015-02-27 09:15:59 +0000168
169#endif