xref: /aosp_15_r20/external/llvm/tools/llvm-pdbdump/LinePrinter.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- LinePrinter.cpp ------------------------------------------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "LinePrinter.h"
11*9880d681SAndroid Build Coastguard Worker 
12*9880d681SAndroid Build Coastguard Worker #include "llvm-pdbdump.h"
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Regex.h"
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include <algorithm>
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker using namespace llvm;
20*9880d681SAndroid Build Coastguard Worker using namespace llvm::pdb;
21*9880d681SAndroid Build Coastguard Worker 
22*9880d681SAndroid Build Coastguard Worker namespace {
IsItemExcluded(llvm::StringRef Item,std::list<llvm::Regex> & IncludeFilters,std::list<llvm::Regex> & ExcludeFilters)23*9880d681SAndroid Build Coastguard Worker bool IsItemExcluded(llvm::StringRef Item,
24*9880d681SAndroid Build Coastguard Worker                     std::list<llvm::Regex> &IncludeFilters,
25*9880d681SAndroid Build Coastguard Worker                     std::list<llvm::Regex> &ExcludeFilters) {
26*9880d681SAndroid Build Coastguard Worker   if (Item.empty())
27*9880d681SAndroid Build Coastguard Worker     return false;
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker   auto match_pred = [Item](llvm::Regex &R) { return R.match(Item); };
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker   // Include takes priority over exclude.  If the user specified include
32*9880d681SAndroid Build Coastguard Worker   // filters, and none of them include this item, them item is gone.
33*9880d681SAndroid Build Coastguard Worker   if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
34*9880d681SAndroid Build Coastguard Worker     return true;
35*9880d681SAndroid Build Coastguard Worker 
36*9880d681SAndroid Build Coastguard Worker   if (any_of(ExcludeFilters, match_pred))
37*9880d681SAndroid Build Coastguard Worker     return true;
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker   return false;
40*9880d681SAndroid Build Coastguard Worker }
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker 
43*9880d681SAndroid Build Coastguard Worker using namespace llvm;
44*9880d681SAndroid Build Coastguard Worker 
LinePrinter(int Indent,llvm::raw_ostream & Stream)45*9880d681SAndroid Build Coastguard Worker LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream)
46*9880d681SAndroid Build Coastguard Worker     : OS(Stream), IndentSpaces(Indent), CurrentIndent(0) {
47*9880d681SAndroid Build Coastguard Worker   SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(),
48*9880d681SAndroid Build Coastguard Worker              opts::pretty::ExcludeTypes.end());
49*9880d681SAndroid Build Coastguard Worker   SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(),
50*9880d681SAndroid Build Coastguard Worker              opts::pretty::ExcludeSymbols.end());
51*9880d681SAndroid Build Coastguard Worker   SetFilters(ExcludeCompilandFilters, opts::pretty::ExcludeCompilands.begin(),
52*9880d681SAndroid Build Coastguard Worker              opts::pretty::ExcludeCompilands.end());
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker   SetFilters(IncludeTypeFilters, opts::pretty::IncludeTypes.begin(),
55*9880d681SAndroid Build Coastguard Worker              opts::pretty::IncludeTypes.end());
56*9880d681SAndroid Build Coastguard Worker   SetFilters(IncludeSymbolFilters, opts::pretty::IncludeSymbols.begin(),
57*9880d681SAndroid Build Coastguard Worker              opts::pretty::IncludeSymbols.end());
58*9880d681SAndroid Build Coastguard Worker   SetFilters(IncludeCompilandFilters, opts::pretty::IncludeCompilands.begin(),
59*9880d681SAndroid Build Coastguard Worker              opts::pretty::IncludeCompilands.end());
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker 
Indent()62*9880d681SAndroid Build Coastguard Worker void LinePrinter::Indent() { CurrentIndent += IndentSpaces; }
63*9880d681SAndroid Build Coastguard Worker 
Unindent()64*9880d681SAndroid Build Coastguard Worker void LinePrinter::Unindent() {
65*9880d681SAndroid Build Coastguard Worker   CurrentIndent = std::max(0, CurrentIndent - IndentSpaces);
66*9880d681SAndroid Build Coastguard Worker }
67*9880d681SAndroid Build Coastguard Worker 
NewLine()68*9880d681SAndroid Build Coastguard Worker void LinePrinter::NewLine() {
69*9880d681SAndroid Build Coastguard Worker   OS << "\n";
70*9880d681SAndroid Build Coastguard Worker   OS.indent(CurrentIndent);
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker 
IsTypeExcluded(llvm::StringRef TypeName)73*9880d681SAndroid Build Coastguard Worker bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName) {
74*9880d681SAndroid Build Coastguard Worker   return IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters);
75*9880d681SAndroid Build Coastguard Worker }
76*9880d681SAndroid Build Coastguard Worker 
IsSymbolExcluded(llvm::StringRef SymbolName)77*9880d681SAndroid Build Coastguard Worker bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
78*9880d681SAndroid Build Coastguard Worker   return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker 
IsCompilandExcluded(llvm::StringRef CompilandName)81*9880d681SAndroid Build Coastguard Worker bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
82*9880d681SAndroid Build Coastguard Worker   return IsItemExcluded(CompilandName, IncludeCompilandFilters,
83*9880d681SAndroid Build Coastguard Worker                         ExcludeCompilandFilters);
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker 
WithColor(LinePrinter & P,PDB_ColorItem C)86*9880d681SAndroid Build Coastguard Worker WithColor::WithColor(LinePrinter &P, PDB_ColorItem C) : OS(P.OS) {
87*9880d681SAndroid Build Coastguard Worker   applyColor(C);
88*9880d681SAndroid Build Coastguard Worker }
89*9880d681SAndroid Build Coastguard Worker 
~WithColor()90*9880d681SAndroid Build Coastguard Worker WithColor::~WithColor() { OS.resetColor(); }
91*9880d681SAndroid Build Coastguard Worker 
applyColor(PDB_ColorItem C)92*9880d681SAndroid Build Coastguard Worker void WithColor::applyColor(PDB_ColorItem C) {
93*9880d681SAndroid Build Coastguard Worker   switch (C) {
94*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::None:
95*9880d681SAndroid Build Coastguard Worker     OS.resetColor();
96*9880d681SAndroid Build Coastguard Worker     return;
97*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::Address:
98*9880d681SAndroid Build Coastguard Worker     OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
99*9880d681SAndroid Build Coastguard Worker     return;
100*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::Keyword:
101*9880d681SAndroid Build Coastguard Worker     OS.changeColor(raw_ostream::MAGENTA, true);
102*9880d681SAndroid Build Coastguard Worker     return;
103*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::Register:
104*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::Offset:
105*9880d681SAndroid Build Coastguard Worker     OS.changeColor(raw_ostream::YELLOW, false);
106*9880d681SAndroid Build Coastguard Worker     return;
107*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::Type:
108*9880d681SAndroid Build Coastguard Worker     OS.changeColor(raw_ostream::CYAN, true);
109*9880d681SAndroid Build Coastguard Worker     return;
110*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::Identifier:
111*9880d681SAndroid Build Coastguard Worker     OS.changeColor(raw_ostream::CYAN, false);
112*9880d681SAndroid Build Coastguard Worker     return;
113*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::Path:
114*9880d681SAndroid Build Coastguard Worker     OS.changeColor(raw_ostream::CYAN, false);
115*9880d681SAndroid Build Coastguard Worker     return;
116*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::SectionHeader:
117*9880d681SAndroid Build Coastguard Worker     OS.changeColor(raw_ostream::RED, true);
118*9880d681SAndroid Build Coastguard Worker     return;
119*9880d681SAndroid Build Coastguard Worker   case PDB_ColorItem::LiteralValue:
120*9880d681SAndroid Build Coastguard Worker     OS.changeColor(raw_ostream::GREEN, true);
121*9880d681SAndroid Build Coastguard Worker     return;
122*9880d681SAndroid Build Coastguard Worker   }
123*9880d681SAndroid Build Coastguard Worker }
124