LinePrinter.h 4.3 KB

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