Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 1 | //===- LinePrinter.h ------------------------------------------ *- C++ --*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H |
| 10 | #define LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H |
| 11 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | 9816246 | 2015-03-01 06:59:57 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 0b36c3e | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 15 | #include "llvm/Support/BinaryStreamRef.h" |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 16 | #include "llvm/Support/FormatVariadic.h" |
Zachary Turner | da504b7 | 2017-06-15 20:55:51 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Regex.h" |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Zachary Turner | f5abda2 | 2015-03-01 06:49:49 +0000 | [diff] [blame] | 19 | |
| 20 | #include <list> |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
Zachary Turner | 0b36c3e | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 23 | class BinaryStreamReader; |
| 24 | namespace msf { |
| 25 | class MSFStreamLayout; |
| 26 | } // namespace msf |
Zachary Turner | ec28fc3 | 2016-05-04 20:32:13 +0000 | [diff] [blame] | 27 | namespace pdb { |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 28 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 29 | class ClassLayout; |
Zachary Turner | 0b36c3e | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 30 | class PDBFile; |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 31 | |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 32 | class LinePrinter { |
| 33 | friend class WithColor; |
| 34 | |
| 35 | public: |
Adrian McCarthy | 1aa207d | 2017-03-23 15:28:15 +0000 | [diff] [blame] | 36 | LinePrinter(int Indent, bool UseColor, raw_ostream &Stream); |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 37 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 38 | void Indent(uint32_t Amount = 0); |
| 39 | void Unindent(uint32_t Amount = 0); |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 40 | void NewLine(); |
| 41 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 42 | 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 Turner | 9940203 | 2017-06-22 20:58:11 +0000 | [diff] [blame] | 53 | void formatBinary(StringRef Label, ArrayRef<uint8_t> Data, uint64_t BaseAddr, |
| 54 | uint32_t StartOffset); |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 55 | |
Zachary Turner | 0b36c3e | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 56 | 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 Turner | c3d8eec | 2017-08-02 22:25:52 +0000 | [diff] [blame] | 62 | void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream); |
Zachary Turner | 0b36c3e | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 63 | |
Adrian McCarthy | 1aa207d | 2017-03-23 15:28:15 +0000 | [diff] [blame] | 64 | bool hasColor() const { return UseColor; } |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 65 | raw_ostream &getStream() { return OS; } |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 66 | int getIndentLevel() const { return CurrentIndent; } |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 67 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 68 | bool IsClassExcluded(const ClassLayout &Class); |
| 69 | bool IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size); |
Zachary Turner | f5abda2 | 2015-03-01 06:49:49 +0000 | [diff] [blame] | 70 | bool IsSymbolExcluded(llvm::StringRef SymbolName); |
| 71 | bool IsCompilandExcluded(llvm::StringRef CompilandName); |
| 72 | |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 73 | private: |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 74 | template <typename Iter> |
| 75 | void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) { |
| 76 | List.clear(); |
| 77 | for (; Begin != End; ++Begin) |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 78 | List.emplace_back(StringRef(*Begin)); |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 81 | raw_ostream &OS; |
| 82 | int IndentSpaces; |
| 83 | int CurrentIndent; |
Adrian McCarthy | 1aa207d | 2017-03-23 15:28:15 +0000 | [diff] [blame] | 84 | bool UseColor; |
Zachary Turner | f5abda2 | 2015-03-01 06:49:49 +0000 | [diff] [blame] | 85 | |
Zachary Turner | 4dddcc6 | 2015-09-29 19:49:06 +0000 | [diff] [blame] | 86 | 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 Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 95 | struct 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 | |
| 106 | inline 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 Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 113 | struct AutoIndent { |
| 114 | explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0) |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 115 | : L(&L), Amount(Amount) { |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 116 | L.Indent(Amount); |
| 117 | } |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 118 | 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 Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 128 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 129 | LinePrinter *L = nullptr; |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 130 | uint32_t Amount = 0; |
| 131 | }; |
| 132 | |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 133 | template <class T> |
| 134 | inline raw_ostream &operator<<(LinePrinter &Printer, const T &Item) { |
| 135 | Printer.getStream() << Item; |
| 136 | return Printer.getStream(); |
| 137 | } |
| 138 | |
| 139 | enum class PDB_ColorItem { |
| 140 | None, |
| 141 | Address, |
| 142 | Type, |
Zachary Turner | 0c990bbe | 2017-04-10 19:33:29 +0000 | [diff] [blame] | 143 | Comment, |
| 144 | Padding, |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 145 | Keyword, |
| 146 | Offset, |
| 147 | Identifier, |
| 148 | Path, |
| 149 | SectionHeader, |
| 150 | LiteralValue, |
Zachary Turner | 7797c72 | 2015-03-02 04:39:56 +0000 | [diff] [blame] | 151 | Register, |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | class WithColor { |
| 155 | public: |
| 156 | WithColor(LinePrinter &P, PDB_ColorItem C); |
| 157 | ~WithColor(); |
| 158 | |
| 159 | raw_ostream &get() { return OS; } |
| 160 | |
| 161 | private: |
Rui Ueyama | fa05aac | 2015-11-03 01:04:44 +0000 | [diff] [blame] | 162 | void applyColor(PDB_ColorItem C); |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 163 | raw_ostream &OS; |
Adrian McCarthy | 5fcfc2c | 2017-03-29 17:11:27 +0000 | [diff] [blame] | 164 | bool UseColor; |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 165 | }; |
| 166 | } |
Zachary Turner | ec28fc3 | 2016-05-04 20:32:13 +0000 | [diff] [blame] | 167 | } |
Zachary Turner | 2d11c20 | 2015-02-27 09:15:59 +0000 | [diff] [blame] | 168 | |
| 169 | #endif |