Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | aac6cc3 | 2015-03-01 06:59:57 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 35abb61 | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 16 | #include "llvm/Support/BinaryStreamRef.h" |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FormatVariadic.h" |
Zachary Turner | 48370ee | 2017-06-15 20:55:51 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Regex.h" |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Zachary Turner | 9149139 | 2015-03-01 06:49:49 +0000 | [diff] [blame] | 20 | |
| 21 | #include <list> |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
Zachary Turner | 35abb61 | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 24 | class BinaryStreamReader; |
| 25 | namespace msf { |
| 26 | class MSFStreamLayout; |
| 27 | } // namespace msf |
Zachary Turner | c95df94 | 2016-05-04 20:32:13 +0000 | [diff] [blame] | 28 | namespace pdb { |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 29 | |
Zachary Turner | 1068334 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 30 | class ClassLayout; |
Zachary Turner | 35abb61 | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 31 | class PDBFile; |
Zachary Turner | 1068334 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 32 | |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 33 | class LinePrinter { |
| 34 | friend class WithColor; |
| 35 | |
| 36 | public: |
Adrian McCarthy | 99f73dd | 2017-03-23 15:28:15 +0000 | [diff] [blame] | 37 | LinePrinter(int Indent, bool UseColor, raw_ostream &Stream); |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 38 | |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 39 | void Indent(uint32_t Amount = 0); |
| 40 | void Unindent(uint32_t Amount = 0); |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 41 | void NewLine(); |
| 42 | |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 43 | 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 Turner | 08bb54f | 2017-06-22 20:58:11 +0000 | [diff] [blame] | 54 | void formatBinary(StringRef Label, ArrayRef<uint8_t> Data, uint64_t BaseAddr, |
| 55 | uint32_t StartOffset); |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 56 | |
Zachary Turner | 35abb61 | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 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); |
Zachary Turner | 34173de | 2017-08-02 22:25:52 +0000 | [diff] [blame] | 63 | void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream); |
Zachary Turner | 35abb61 | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 64 | |
Adrian McCarthy | 99f73dd | 2017-03-23 15:28:15 +0000 | [diff] [blame] | 65 | bool hasColor() const { return UseColor; } |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 66 | raw_ostream &getStream() { return OS; } |
Zachary Turner | 0c7c98a | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 67 | int getIndentLevel() const { return CurrentIndent; } |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 68 | |
Zachary Turner | 1068334 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 69 | bool IsClassExcluded(const ClassLayout &Class); |
| 70 | bool IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size); |
Zachary Turner | 9149139 | 2015-03-01 06:49:49 +0000 | [diff] [blame] | 71 | bool IsSymbolExcluded(llvm::StringRef SymbolName); |
| 72 | bool IsCompilandExcluded(llvm::StringRef CompilandName); |
| 73 | |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 74 | private: |
Zachary Turner | 0c7c98a | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 75 | template <typename Iter> |
| 76 | void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) { |
| 77 | List.clear(); |
| 78 | for (; Begin != End; ++Begin) |
Benjamin Kramer | 9589ff8 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 79 | List.emplace_back(StringRef(*Begin)); |
Zachary Turner | 0c7c98a | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 82 | raw_ostream &OS; |
| 83 | int IndentSpaces; |
| 84 | int CurrentIndent; |
Adrian McCarthy | 99f73dd | 2017-03-23 15:28:15 +0000 | [diff] [blame] | 85 | bool UseColor; |
Zachary Turner | 9149139 | 2015-03-01 06:49:49 +0000 | [diff] [blame] | 86 | |
Zachary Turner | c9736fb | 2015-09-29 19:49:06 +0000 | [diff] [blame] | 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; |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Zachary Turner | 6ef51e8 | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 96 | struct 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 | |
| 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 | |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 114 | struct AutoIndent { |
| 115 | explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0) |
Zachary Turner | 6ef51e8 | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 116 | : L(&L), Amount(Amount) { |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 117 | L.Indent(Amount); |
| 118 | } |
Zachary Turner | 6ef51e8 | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 119 | 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 Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 129 | |
Zachary Turner | 6ef51e8 | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 130 | LinePrinter *L = nullptr; |
Zachary Turner | 7e5d31e | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 131 | uint32_t Amount = 0; |
| 132 | }; |
| 133 | |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 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, |
Zachary Turner | e98c913 | 2017-04-10 19:33:29 +0000 | [diff] [blame] | 144 | Comment, |
| 145 | Padding, |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 146 | Keyword, |
| 147 | Offset, |
| 148 | Identifier, |
| 149 | Path, |
| 150 | SectionHeader, |
| 151 | LiteralValue, |
Zachary Turner | 0c7c98a | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 152 | Register, |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | class WithColor { |
| 156 | public: |
| 157 | WithColor(LinePrinter &P, PDB_ColorItem C); |
| 158 | ~WithColor(); |
| 159 | |
| 160 | raw_ostream &get() { return OS; } |
| 161 | |
| 162 | private: |
Rui Ueyama | 55861d0 | 2015-11-03 01:04:44 +0000 | [diff] [blame] | 163 | void applyColor(PDB_ColorItem C); |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 164 | raw_ostream &OS; |
Adrian McCarthy | ebe3d55 | 2017-03-29 17:11:27 +0000 | [diff] [blame] | 165 | bool UseColor; |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 166 | }; |
| 167 | } |
Zachary Turner | c95df94 | 2016-05-04 20:32:13 +0000 | [diff] [blame] | 168 | } |
Zachary Turner | 756b823 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 169 | |
| 170 | #endif |