blob: 09bde28f516aa9888a33264fbbbf9b5c67dc3e86 [file] [log] [blame]
Zachary Turner756b8232015-02-27 09:15:59 +00001//===- 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
Zachary Turner7e5d31e2017-06-15 22:24:24 +000013#include "llvm/ADT/ArrayRef.h"
Zachary Turneraac6cc32015-03-01 06:59:57 +000014#include "llvm/ADT/StringRef.h"
Zachary Turner756b8232015-02-27 09:15:59 +000015#include "llvm/ADT/Twine.h"
Zachary Turner35abb612017-06-23 18:52:13 +000016#include "llvm/Support/BinaryStreamRef.h"
Zachary Turner7e5d31e2017-06-15 22:24:24 +000017#include "llvm/Support/FormatVariadic.h"
Zachary Turner48370ee2017-06-15 20:55:51 +000018#include "llvm/Support/Regex.h"
Zachary Turner7e5d31e2017-06-15 22:24:24 +000019#include "llvm/Support/raw_ostream.h"
Zachary Turner91491392015-03-01 06:49:49 +000020
21#include <list>
Zachary Turner756b8232015-02-27 09:15:59 +000022
23namespace llvm {
Zachary Turner35abb612017-06-23 18:52:13 +000024class BinaryStreamReader;
25namespace msf {
26class MSFStreamLayout;
27} // namespace msf
Zachary Turnerc95df942016-05-04 20:32:13 +000028namespace pdb {
Zachary Turner756b8232015-02-27 09:15:59 +000029
Zachary Turner10683342017-04-13 21:11:00 +000030class ClassLayout;
Zachary Turner35abb612017-06-23 18:52:13 +000031class PDBFile;
Zachary Turner10683342017-04-13 21:11:00 +000032
Zachary Turner756b8232015-02-27 09:15:59 +000033class LinePrinter {
34 friend class WithColor;
35
36public:
Adrian McCarthy99f73dd2017-03-23 15:28:15 +000037 LinePrinter(int Indent, bool UseColor, raw_ostream &Stream);
Zachary Turner756b8232015-02-27 09:15:59 +000038
Zachary Turner7e5d31e2017-06-15 22:24:24 +000039 void Indent(uint32_t Amount = 0);
40 void Unindent(uint32_t Amount = 0);
Zachary Turner756b8232015-02-27 09:15:59 +000041 void NewLine();
42
Zachary Turner7e5d31e2017-06-15 22:24:24 +000043 void printLine(const Twine &T);
44 void print(const Twine &T);
45 template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) {
46 printLine(formatv(Fmt, std::forward<Ts>(Items)...));
47 }
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);
Zachary Turner08bb54f2017-06-22 20:58:11 +000054 void formatBinary(StringRef Label, ArrayRef<uint8_t> Data, uint64_t BaseAddr,
55 uint32_t StartOffset);
Zachary Turner7e5d31e2017-06-15 22:24:24 +000056
Zachary Turner35abb612017-06-23 18:52:13 +000057 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);
Zachary Turner34173de2017-08-02 22:25:52 +000063 void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream);
Zachary Turner35abb612017-06-23 18:52:13 +000064
Adrian McCarthy99f73dd2017-03-23 15:28:15 +000065 bool hasColor() const { return UseColor; }
Zachary Turner756b8232015-02-27 09:15:59 +000066 raw_ostream &getStream() { return OS; }
Zachary Turner0c7c98a2015-03-02 04:39:56 +000067 int getIndentLevel() const { return CurrentIndent; }
Zachary Turner756b8232015-02-27 09:15:59 +000068
Zachary Turner10683342017-04-13 21:11:00 +000069 bool IsClassExcluded(const ClassLayout &Class);
70 bool IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size);
Zachary Turner91491392015-03-01 06:49:49 +000071 bool IsSymbolExcluded(llvm::StringRef SymbolName);
72 bool IsCompilandExcluded(llvm::StringRef CompilandName);
73
Zachary Turner756b8232015-02-27 09:15:59 +000074private:
Zachary Turner0c7c98a2015-03-02 04:39:56 +000075 template <typename Iter>
76 void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) {
77 List.clear();
78 for (; Begin != End; ++Begin)
Benjamin Kramer9589ff82015-05-29 19:43:39 +000079 List.emplace_back(StringRef(*Begin));
Zachary Turner0c7c98a2015-03-02 04:39:56 +000080 }
81
Zachary Turner756b8232015-02-27 09:15:59 +000082 raw_ostream &OS;
83 int IndentSpaces;
84 int CurrentIndent;
Adrian McCarthy99f73dd2017-03-23 15:28:15 +000085 bool UseColor;
Zachary Turner91491392015-03-01 06:49:49 +000086
Zachary Turnerc9736fb2015-09-29 19:49:06 +000087 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;
Zachary Turner756b8232015-02-27 09:15:59 +000094};
95
Zachary Turner6ef51e82017-09-01 20:06:56 +000096struct PrintScope {
97 explicit PrintScope(LinePrinter &P, uint32_t IndentLevel)
98 : P(P), IndentLevel(IndentLevel) {}
99 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
107inline Optional<PrintScope> withLabelWidth(const Optional<PrintScope> &Scope,
108 uint32_t W) {
109 if (!Scope)
110 return None;
111 return PrintScope{*Scope, W};
112}
113
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000114struct AutoIndent {
115 explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
Zachary Turner6ef51e82017-09-01 20:06:56 +0000116 : L(&L), Amount(Amount) {
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000117 L.Indent(Amount);
118 }
Zachary Turner6ef51e82017-09-01 20:06:56 +0000119 explicit AutoIndent(const Optional<PrintScope> &Scope) {
120 if (Scope.hasValue()) {
121 L = &Scope->P;
122 Amount = Scope->IndentLevel;
123 }
124 }
125 ~AutoIndent() {
126 if (L)
127 L->Unindent(Amount);
128 }
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000129
Zachary Turner6ef51e82017-09-01 20:06:56 +0000130 LinePrinter *L = nullptr;
Zachary Turner7e5d31e2017-06-15 22:24:24 +0000131 uint32_t Amount = 0;
132};
133
Zachary Turner756b8232015-02-27 09:15:59 +0000134template <class T>
135inline raw_ostream &operator<<(LinePrinter &Printer, const T &Item) {
136 Printer.getStream() << Item;
137 return Printer.getStream();
138}
139
140enum class PDB_ColorItem {
141 None,
142 Address,
143 Type,
Zachary Turnere98c9132017-04-10 19:33:29 +0000144 Comment,
145 Padding,
Zachary Turner756b8232015-02-27 09:15:59 +0000146 Keyword,
147 Offset,
148 Identifier,
149 Path,
150 SectionHeader,
151 LiteralValue,
Zachary Turner0c7c98a2015-03-02 04:39:56 +0000152 Register,
Zachary Turner756b8232015-02-27 09:15:59 +0000153};
154
155class WithColor {
156public:
157 WithColor(LinePrinter &P, PDB_ColorItem C);
158 ~WithColor();
159
160 raw_ostream &get() { return OS; }
161
162private:
Rui Ueyama55861d02015-11-03 01:04:44 +0000163 void applyColor(PDB_ColorItem C);
Zachary Turner756b8232015-02-27 09:15:59 +0000164 raw_ostream &OS;
Adrian McCarthyebe3d552017-03-29 17:11:27 +0000165 bool UseColor;
Zachary Turner756b8232015-02-27 09:15:59 +0000166};
167}
Zachary Turnerc95df942016-05-04 20:32:13 +0000168}
Zachary Turner756b8232015-02-27 09:15:59 +0000169
170#endif