1 //===- LinePrinter.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "LinePrinter.h"
12 #include "llvm-pdbutil.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/DebugInfo/PDB/UDTLayout.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/Regex.h"
22 using namespace llvm::pdb;
25 bool IsItemExcluded(llvm::StringRef Item,
26 std::list<llvm::Regex> &IncludeFilters,
27 std::list<llvm::Regex> &ExcludeFilters) {
31 auto match_pred = [Item](llvm::Regex &R) { return R.match(Item); };
33 // Include takes priority over exclude. If the user specified include
34 // filters, and none of them include this item, them item is gone.
35 if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
38 if (any_of(ExcludeFilters, match_pred))
47 LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream)
48 : OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor) {
49 SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(),
50 opts::pretty::ExcludeTypes.end());
51 SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(),
52 opts::pretty::ExcludeSymbols.end());
53 SetFilters(ExcludeCompilandFilters, opts::pretty::ExcludeCompilands.begin(),
54 opts::pretty::ExcludeCompilands.end());
56 SetFilters(IncludeTypeFilters, opts::pretty::IncludeTypes.begin(),
57 opts::pretty::IncludeTypes.end());
58 SetFilters(IncludeSymbolFilters, opts::pretty::IncludeSymbols.begin(),
59 opts::pretty::IncludeSymbols.end());
60 SetFilters(IncludeCompilandFilters, opts::pretty::IncludeCompilands.begin(),
61 opts::pretty::IncludeCompilands.end());
64 void LinePrinter::Indent(uint32_t Amount) {
66 Amount = IndentSpaces;
67 CurrentIndent += Amount;
70 void LinePrinter::Unindent(uint32_t Amount) {
72 Amount = IndentSpaces;
73 CurrentIndent = std::max<int>(0, CurrentIndent - Amount);
76 void LinePrinter::NewLine() {
78 OS.indent(CurrentIndent);
81 void LinePrinter::print(const Twine &T) { OS << T; }
83 void LinePrinter::printLine(const Twine &T) {
88 bool LinePrinter::IsClassExcluded(const ClassLayout &Class) {
89 if (IsTypeExcluded(Class.getName(), Class.getSize()))
91 if (Class.deepPaddingSize() < opts::pretty::PaddingThreshold)
96 void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
97 uint32_t StartOffset) {
102 OS << format_bytes_with_ascii(Data, StartOffset, 32, 4,
103 CurrentIndent + IndentSpaces, true);
109 bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size) {
110 if (IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters))
112 if (Size < opts::pretty::SizeThreshold)
117 bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
118 return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
121 bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
122 return IsItemExcluded(CompilandName, IncludeCompilandFilters,
123 ExcludeCompilandFilters);
126 WithColor::WithColor(LinePrinter &P, PDB_ColorItem C)
127 : OS(P.OS), UseColor(P.hasColor()) {
132 WithColor::~WithColor() {
137 void WithColor::applyColor(PDB_ColorItem C) {
139 case PDB_ColorItem::None:
142 case PDB_ColorItem::Comment:
143 OS.changeColor(raw_ostream::GREEN, false);
145 case PDB_ColorItem::Address:
146 OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
148 case PDB_ColorItem::Keyword:
149 OS.changeColor(raw_ostream::MAGENTA, true);
151 case PDB_ColorItem::Register:
152 case PDB_ColorItem::Offset:
153 OS.changeColor(raw_ostream::YELLOW, false);
155 case PDB_ColorItem::Type:
156 OS.changeColor(raw_ostream::CYAN, true);
158 case PDB_ColorItem::Identifier:
159 OS.changeColor(raw_ostream::CYAN, false);
161 case PDB_ColorItem::Path:
162 OS.changeColor(raw_ostream::CYAN, false);
164 case PDB_ColorItem::Padding:
165 case PDB_ColorItem::SectionHeader:
166 OS.changeColor(raw_ostream::RED, true);
168 case PDB_ColorItem::LiteralValue:
169 OS.changeColor(raw_ostream::GREEN, true);