blob: 9f0f5d8c06836c5cccd04c2a46f5bc4a1ee08068 [file] [log] [blame]
Zachary Turner2d11c202015-02-27 09:15:59 +00001//===- 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
Zachary Turner7797c722015-03-02 04:39:56 +000012#include "llvm-pdbdump.h"
13
Zachary Turnerf5abda22015-03-01 06:49:49 +000014#include "llvm/Support/Regex.h"
15
Zachary Turner2d11c202015-02-27 09:15:59 +000016#include <algorithm>
17
Zachary Turner4dddcc62015-09-29 19:49:06 +000018namespace {
19template <class T, class Pred> bool any_of_range(T &&R, Pred P) {
20 return std::any_of(R.begin(), R.end(), P);
21}
22
23bool 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
Zachary Turner2d11c202015-02-27 09:15:59 +000043using namespace llvm;
44
45LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream)
Zachary Turner7797c722015-03-02 04:39:56 +000046 : OS(Stream), IndentSpaces(Indent), CurrentIndent(0) {
Zachary Turner4dddcc62015-09-29 19:49:06 +000047 SetFilters(ExcludeTypeFilters, opts::ExcludeTypes.begin(),
48 opts::ExcludeTypes.end());
49 SetFilters(ExcludeSymbolFilters, opts::ExcludeSymbols.begin(),
Zachary Turner7797c722015-03-02 04:39:56 +000050 opts::ExcludeSymbols.end());
Zachary Turner4dddcc62015-09-29 19:49:06 +000051 SetFilters(ExcludeCompilandFilters, opts::ExcludeCompilands.begin(),
Zachary Turner7797c722015-03-02 04:39:56 +000052 opts::ExcludeCompilands.end());
Zachary Turner4dddcc62015-09-29 19:49:06 +000053
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());
Zachary Turner7797c722015-03-02 04:39:56 +000060}
Zachary Turner2d11c202015-02-27 09:15:59 +000061
62void LinePrinter::Indent() { CurrentIndent += IndentSpaces; }
63
64void LinePrinter::Unindent() {
65 CurrentIndent = std::max(0, CurrentIndent - IndentSpaces);
66}
67
68void LinePrinter::NewLine() {
69 OS << "\n";
70 OS.indent(CurrentIndent);
71}
72
Zachary Turnerf5abda22015-03-01 06:49:49 +000073bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName) {
Zachary Turner4dddcc62015-09-29 19:49:06 +000074 return IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters);
Zachary Turnerf5abda22015-03-01 06:49:49 +000075}
76
77bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
Zachary Turner4dddcc62015-09-29 19:49:06 +000078 return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
Zachary Turnerf5abda22015-03-01 06:49:49 +000079}
80
81bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
Zachary Turner4dddcc62015-09-29 19:49:06 +000082 return IsItemExcluded(CompilandName, IncludeCompilandFilters,
83 ExcludeCompilandFilters);
Zachary Turnerf5abda22015-03-01 06:49:49 +000084}
85
Zachary Turner2d11c202015-02-27 09:15:59 +000086WithColor::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
97WithColor::~WithColor() { OS.resetColor(); }
98
99void 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;
Zachary Turner7797c722015-03-02 04:39:56 +0000110 case PDB_ColorItem::Register:
Zachary Turner2d11c202015-02-27 09:15:59 +0000111 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}