Prevent assertion with "llc -debug" and anonymous symbols.
[oota-llvm.git] / tools / llvm-pdbdump / LinePrinter.cpp
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/Support/Regex.h"
15
16 #include <algorithm>
17
18 namespace {
19 template <class T, class Pred> bool any_of_range(T &&R, Pred P) {
20   return std::any_of(R.begin(), R.end(), P);
21 }
22
23 bool IsItemExcluded(llvm::StringRef Item,
24                     std::list<llvm::Regex> &IncludeFilters,
25                     std::list<llvm::Regex> &ExcludeFilters) {
26   if (Item.empty())
27     return false;
28
29   auto match_pred = [Item](llvm::Regex &R) { return R.match(Item); };
30
31   // Include takes priority over exclude.  If the user specified include
32   // filters, and none of them include this item, them item is gone.
33   if (!IncludeFilters.empty() && !any_of_range(IncludeFilters, match_pred))
34     return true;
35
36   if (any_of_range(ExcludeFilters, match_pred))
37     return true;
38
39   return false;
40 }
41 }
42
43 using namespace llvm;
44
45 LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream)
46     : OS(Stream), IndentSpaces(Indent), CurrentIndent(0) {
47   SetFilters(ExcludeTypeFilters, opts::ExcludeTypes.begin(),
48              opts::ExcludeTypes.end());
49   SetFilters(ExcludeSymbolFilters, opts::ExcludeSymbols.begin(),
50              opts::ExcludeSymbols.end());
51   SetFilters(ExcludeCompilandFilters, opts::ExcludeCompilands.begin(),
52              opts::ExcludeCompilands.end());
53
54   SetFilters(IncludeTypeFilters, opts::IncludeTypes.begin(),
55              opts::IncludeTypes.end());
56   SetFilters(IncludeSymbolFilters, opts::IncludeSymbols.begin(),
57              opts::IncludeSymbols.end());
58   SetFilters(IncludeCompilandFilters, opts::IncludeCompilands.begin(),
59              opts::IncludeCompilands.end());
60 }
61
62 void LinePrinter::Indent() { CurrentIndent += IndentSpaces; }
63
64 void LinePrinter::Unindent() {
65   CurrentIndent = std::max(0, CurrentIndent - IndentSpaces);
66 }
67
68 void LinePrinter::NewLine() {
69   OS << "\n";
70   OS.indent(CurrentIndent);
71 }
72
73 bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName) {
74   return IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters);
75 }
76
77 bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
78   return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
79 }
80
81 bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
82   return IsItemExcluded(CompilandName, IncludeCompilandFilters,
83                         ExcludeCompilandFilters);
84 }
85
86 WithColor::WithColor(LinePrinter &P, PDB_ColorItem C) : OS(P.OS) {
87   if (C == PDB_ColorItem::None)
88     OS.resetColor();
89   else {
90     raw_ostream::Colors Color;
91     bool Bold;
92     translateColor(C, Color, Bold);
93     OS.changeColor(Color, Bold);
94   }
95 }
96
97 WithColor::~WithColor() { OS.resetColor(); }
98
99 void WithColor::translateColor(PDB_ColorItem C, raw_ostream::Colors &Color,
100                                bool &Bold) const {
101   switch (C) {
102   case PDB_ColorItem::Address:
103     Color = raw_ostream::YELLOW;
104     Bold = true;
105     return;
106   case PDB_ColorItem::Keyword:
107     Color = raw_ostream::MAGENTA;
108     Bold = true;
109     return;
110   case PDB_ColorItem::Register:
111   case PDB_ColorItem::Offset:
112     Color = raw_ostream::YELLOW;
113     Bold = false;
114     return;
115   case PDB_ColorItem::Type:
116     Color = raw_ostream::CYAN;
117     Bold = true;
118     return;
119   case PDB_ColorItem::Identifier:
120     Color = raw_ostream::CYAN;
121     Bold = false;
122     return;
123   case PDB_ColorItem::Path:
124     Color = raw_ostream::CYAN;
125     Bold = false;
126     return;
127   case PDB_ColorItem::SectionHeader:
128     Color = raw_ostream::RED;
129     Bold = true;
130     return;
131   case PDB_ColorItem::LiteralValue:
132     Color = raw_ostream::GREEN;
133     Bold = true;
134   default:
135     return;
136   }
137 }