LinePrinter.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LinePrinter.h ------------------------------------------ *- C++ --*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_PDB_NATIVE_LINEPRINTER_H
  14. #define LLVM_DEBUGINFO_PDB_NATIVE_LINEPRINTER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/DebugInfo/PDB/Native/FormatUtil.h"
  19. #include "llvm/Support/BinaryStreamRef.h"
  20. #include "llvm/Support/FormatVariadic.h"
  21. #include "llvm/Support/Regex.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <list>
  24. // Container for filter options to control which elements will be printed.
  25. struct FilterOptions {
  26. std::list<std::string> ExcludeTypes;
  27. std::list<std::string> ExcludeSymbols;
  28. std::list<std::string> ExcludeCompilands;
  29. std::list<std::string> IncludeTypes;
  30. std::list<std::string> IncludeSymbols;
  31. std::list<std::string> IncludeCompilands;
  32. uint32_t PaddingThreshold;
  33. uint32_t SizeThreshold;
  34. std::optional<uint32_t> DumpModi;
  35. std::optional<uint32_t> ParentRecurseDepth;
  36. std::optional<uint32_t> ChildrenRecurseDepth;
  37. std::optional<uint32_t> SymbolOffset;
  38. bool JustMyCode;
  39. };
  40. namespace llvm {
  41. namespace msf {
  42. class MSFStreamLayout;
  43. } // namespace msf
  44. namespace pdb {
  45. class ClassLayout;
  46. class PDBFile;
  47. class SymbolGroup;
  48. class LinePrinter {
  49. friend class WithColor;
  50. public:
  51. LinePrinter(int Indent, bool UseColor, raw_ostream &Stream,
  52. const FilterOptions &Filters);
  53. void Indent(uint32_t Amount = 0);
  54. void Unindent(uint32_t Amount = 0);
  55. void NewLine();
  56. void printLine(const Twine &T);
  57. void print(const Twine &T);
  58. template <typename... Ts> void formatLine(const char *Fmt, Ts &&...Items) {
  59. printLine(formatv(Fmt, std::forward<Ts>(Items)...));
  60. }
  61. template <typename... Ts> void format(const char *Fmt, Ts &&...Items) {
  62. print(formatv(Fmt, std::forward<Ts>(Items)...));
  63. }
  64. void formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
  65. uint64_t StartOffset);
  66. void formatBinary(StringRef Label, ArrayRef<uint8_t> Data, uint64_t BaseAddr,
  67. uint64_t StartOffset);
  68. void formatMsfStreamData(StringRef Label, PDBFile &File, uint32_t StreamIdx,
  69. StringRef StreamPurpose, uint64_t Offset,
  70. uint64_t Size);
  71. void formatMsfStreamData(StringRef Label, PDBFile &File,
  72. const msf::MSFStreamLayout &Stream,
  73. BinarySubstreamRef Substream);
  74. void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream);
  75. bool hasColor() const { return UseColor; }
  76. raw_ostream &getStream() { return OS; }
  77. int getIndentLevel() const { return CurrentIndent; }
  78. bool IsClassExcluded(const ClassLayout &Class);
  79. bool IsTypeExcluded(llvm::StringRef TypeName, uint64_t Size);
  80. bool IsSymbolExcluded(llvm::StringRef SymbolName);
  81. bool IsCompilandExcluded(llvm::StringRef CompilandName);
  82. const FilterOptions &getFilters() const { return Filters; }
  83. private:
  84. template <typename Iter>
  85. void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) {
  86. List.clear();
  87. for (; Begin != End; ++Begin)
  88. List.emplace_back(StringRef(*Begin));
  89. }
  90. raw_ostream &OS;
  91. int IndentSpaces;
  92. int CurrentIndent;
  93. bool UseColor;
  94. const FilterOptions &Filters;
  95. std::list<Regex> ExcludeCompilandFilters;
  96. std::list<Regex> ExcludeTypeFilters;
  97. std::list<Regex> ExcludeSymbolFilters;
  98. std::list<Regex> IncludeCompilandFilters;
  99. std::list<Regex> IncludeTypeFilters;
  100. std::list<Regex> IncludeSymbolFilters;
  101. };
  102. struct PrintScope {
  103. explicit PrintScope(LinePrinter &P, uint32_t IndentLevel)
  104. : P(P), IndentLevel(IndentLevel) {}
  105. explicit PrintScope(const PrintScope &Other, uint32_t LabelWidth)
  106. : P(Other.P), IndentLevel(Other.IndentLevel), LabelWidth(LabelWidth) {}
  107. LinePrinter &P;
  108. uint32_t IndentLevel;
  109. uint32_t LabelWidth = 0;
  110. };
  111. inline PrintScope withLabelWidth(const PrintScope &Scope, uint32_t W) {
  112. return PrintScope{Scope, W};
  113. }
  114. struct AutoIndent {
  115. explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
  116. : L(&L), Amount(Amount) {
  117. L.Indent(Amount);
  118. }
  119. explicit AutoIndent(const PrintScope &Scope) {
  120. L = &Scope.P;
  121. Amount = Scope.IndentLevel;
  122. }
  123. ~AutoIndent() {
  124. if (L)
  125. L->Unindent(Amount);
  126. }
  127. LinePrinter *L = nullptr;
  128. uint32_t Amount = 0;
  129. };
  130. template <class T>
  131. inline raw_ostream &operator<<(LinePrinter &Printer, const T &Item) {
  132. return Printer.getStream() << Item;
  133. }
  134. enum class PDB_ColorItem {
  135. None,
  136. Address,
  137. Type,
  138. Comment,
  139. Padding,
  140. Keyword,
  141. Offset,
  142. Identifier,
  143. Path,
  144. SectionHeader,
  145. LiteralValue,
  146. Register,
  147. };
  148. class WithColor {
  149. public:
  150. WithColor(LinePrinter &P, PDB_ColorItem C);
  151. ~WithColor();
  152. raw_ostream &get() { return OS; }
  153. private:
  154. void applyColor(PDB_ColorItem C);
  155. raw_ostream &OS;
  156. bool UseColor;
  157. };
  158. } // namespace pdb
  159. } // namespace llvm
  160. #endif
  161. #ifdef __GNUC__
  162. #pragma GCC diagnostic pop
  163. #endif