LinePrinter.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //===- LinePrinter.h ------------------------------------------ *- C++ --*-===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H
  9. #define LLVM_TOOLS_LLVMPDBDUMP_LINEPRINTER_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/ADT/Twine.h"
  13. #include "llvm/Support/BinaryStreamRef.h"
  14. #include "llvm/Support/FormatVariadic.h"
  15. #include "llvm/Support/Regex.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <list>
  18. namespace llvm {
  19. class BinaryStreamReader;
  20. namespace msf {
  21. class MSFStreamLayout;
  22. } // namespace msf
  23. namespace pdb {
  24. class ClassLayout;
  25. class PDBFile;
  26. class LinePrinter {
  27. friend class WithColor;
  28. public:
  29. LinePrinter(int Indent, bool UseColor, raw_ostream &Stream);
  30. void Indent(uint32_t Amount = 0);
  31. void Unindent(uint32_t Amount = 0);
  32. void NewLine();
  33. void printLine(const Twine &T);
  34. void print(const Twine &T);
  35. template <typename... Ts> void formatLine(const char *Fmt, Ts &&... Items) {
  36. printLine(formatv(Fmt, std::forward<Ts>(Items)...));
  37. }
  38. template <typename... Ts> void format(const char *Fmt, Ts &&... Items) {
  39. print(formatv(Fmt, std::forward<Ts>(Items)...));
  40. }
  41. void formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
  42. uint32_t StartOffset);
  43. void formatBinary(StringRef Label, ArrayRef<uint8_t> Data, uint64_t BaseAddr,
  44. uint32_t StartOffset);
  45. void formatMsfStreamData(StringRef Label, PDBFile &File, uint32_t StreamIdx,
  46. StringRef StreamPurpose, uint32_t Offset,
  47. uint32_t Size);
  48. void formatMsfStreamData(StringRef Label, PDBFile &File,
  49. const msf::MSFStreamLayout &Stream,
  50. BinarySubstreamRef Substream);
  51. void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream);
  52. bool hasColor() const { return UseColor; }
  53. raw_ostream &getStream() { return OS; }
  54. int getIndentLevel() const { return CurrentIndent; }
  55. bool IsClassExcluded(const ClassLayout &Class);
  56. bool IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size);
  57. bool IsSymbolExcluded(llvm::StringRef SymbolName);
  58. bool IsCompilandExcluded(llvm::StringRef CompilandName);
  59. private:
  60. template <typename Iter>
  61. void SetFilters(std::list<Regex> &List, Iter Begin, Iter End) {
  62. List.clear();
  63. for (; Begin != End; ++Begin)
  64. List.emplace_back(StringRef(*Begin));
  65. }
  66. raw_ostream &OS;
  67. int IndentSpaces;
  68. int CurrentIndent;
  69. bool UseColor;
  70. std::list<Regex> ExcludeCompilandFilters;
  71. std::list<Regex> ExcludeTypeFilters;
  72. std::list<Regex> ExcludeSymbolFilters;
  73. std::list<Regex> IncludeCompilandFilters;
  74. std::list<Regex> IncludeTypeFilters;
  75. std::list<Regex> IncludeSymbolFilters;
  76. };
  77. struct PrintScope {
  78. explicit PrintScope(LinePrinter &P, uint32_t IndentLevel)
  79. : P(P), IndentLevel(IndentLevel) {}
  80. explicit PrintScope(const PrintScope &Other, uint32_t LabelWidth)
  81. : P(Other.P), IndentLevel(Other.IndentLevel), LabelWidth(LabelWidth) {}
  82. LinePrinter &P;
  83. uint32_t IndentLevel;
  84. uint32_t LabelWidth = 0;
  85. };
  86. inline Optional<PrintScope> withLabelWidth(const Optional<PrintScope> &Scope,
  87. uint32_t W) {
  88. if (!Scope)
  89. return None;
  90. return PrintScope{*Scope, W};
  91. }
  92. struct AutoIndent {
  93. explicit AutoIndent(LinePrinter &L, uint32_t Amount = 0)
  94. : L(&L), Amount(Amount) {
  95. L.Indent(Amount);
  96. }
  97. explicit AutoIndent(const Optional<PrintScope> &Scope) {
  98. if (Scope.hasValue()) {
  99. L = &Scope->P;
  100. Amount = Scope->IndentLevel;
  101. }
  102. }
  103. ~AutoIndent() {
  104. if (L)
  105. L->Unindent(Amount);
  106. }
  107. LinePrinter *L = nullptr;
  108. uint32_t Amount = 0;
  109. };
  110. template <class T>
  111. inline raw_ostream &operator<<(LinePrinter &Printer, const T &Item) {
  112. return Printer.getStream() << Item;
  113. }
  114. enum class PDB_ColorItem {
  115. None,
  116. Address,
  117. Type,
  118. Comment,
  119. Padding,
  120. Keyword,
  121. Offset,
  122. Identifier,
  123. Path,
  124. SectionHeader,
  125. LiteralValue,
  126. Register,
  127. };
  128. class WithColor {
  129. public:
  130. WithColor(LinePrinter &P, PDB_ColorItem C);
  131. ~WithColor();
  132. raw_ostream &get() { return OS; }
  133. private:
  134. void applyColor(PDB_ColorItem C);
  135. raw_ostream &OS;
  136. bool UseColor;
  137. };
  138. }
  139. }
  140. #endif