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