clang-format lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
[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   applyColor(C);
88 }
89
90 WithColor::~WithColor() { OS.resetColor(); }
91
92 void WithColor::applyColor(PDB_ColorItem C) {
93   switch (C) {
94   case PDB_ColorItem::None:
95     OS.resetColor();
96     return;
97   case PDB_ColorItem::Address:
98     OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
99     return;
100   case PDB_ColorItem::Keyword:
101     OS.changeColor(raw_ostream::MAGENTA, true);
102     return;
103   case PDB_ColorItem::Register:
104   case PDB_ColorItem::Offset:
105     OS.changeColor(raw_ostream::YELLOW, false);
106     return;
107   case PDB_ColorItem::Type:
108     OS.changeColor(raw_ostream::CYAN, true);
109     return;
110   case PDB_ColorItem::Identifier:
111     OS.changeColor(raw_ostream::CYAN, false);
112     return;
113   case PDB_ColorItem::Path:
114     OS.changeColor(raw_ostream::CYAN, false);
115     return;
116   case PDB_ColorItem::SectionHeader:
117     OS.changeColor(raw_ostream::RED, true);
118     return;
119   case PDB_ColorItem::LiteralValue:
120     OS.changeColor(raw_ostream::GREEN, true);
121     return;
122   }
123 }