blob: d3768104d884b341b57f1c0e1fccdb660f9a9671 [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
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000014#include "llvm/ADT/STLExtras.h"
Zachary Turnerf5abda22015-03-01 06:49:49 +000015#include "llvm/Support/Regex.h"
16
Zachary Turner2d11c202015-02-27 09:15:59 +000017#include <algorithm>
18
Zachary Turnerec28fc32016-05-04 20:32:13 +000019using namespace llvm;
20using namespace llvm::pdb;
21
Zachary Turner4dddcc62015-09-29 19:49:06 +000022namespace {
Zachary Turner4dddcc62015-09-29 19:49:06 +000023bool 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.
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000033 if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
Zachary Turner4dddcc62015-09-29 19:49:06 +000034 return true;
35
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000036 if (any_of(ExcludeFilters, match_pred))
Zachary Turner4dddcc62015-09-29 19:49:06 +000037 return true;
38
39 return false;
40}
41}
42
Zachary Turner2d11c202015-02-27 09:15:59 +000043using namespace llvm;
44
Adrian McCarthy1aa207d2017-03-23 15:28:15 +000045LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream)
46 : OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +000047 SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(),
48 opts::pretty::ExcludeTypes.end());
49 SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(),
50 opts::pretty::ExcludeSymbols.end());
51 SetFilters(ExcludeCompilandFilters, opts::pretty::ExcludeCompilands.begin(),
52 opts::pretty::ExcludeCompilands.end());
Zachary Turner4dddcc62015-09-29 19:49:06 +000053
Zachary Turnera30bd1a2016-06-30 17:42:48 +000054 SetFilters(IncludeTypeFilters, opts::pretty::IncludeTypes.begin(),
55 opts::pretty::IncludeTypes.end());
56 SetFilters(IncludeSymbolFilters, opts::pretty::IncludeSymbols.begin(),
57 opts::pretty::IncludeSymbols.end());
58 SetFilters(IncludeCompilandFilters, opts::pretty::IncludeCompilands.begin(),
59 opts::pretty::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
Adrian McCarthy5fcfc2c2017-03-29 17:11:27 +000086WithColor::WithColor(LinePrinter &P, PDB_ColorItem C)
87 : OS(P.OS), UseColor(P.hasColor()) {
88 if (UseColor)
Adrian McCarthy1aa207d2017-03-23 15:28:15 +000089 applyColor(C);
Zachary Turner2d11c202015-02-27 09:15:59 +000090}
91
Adrian McCarthy5fcfc2c2017-03-29 17:11:27 +000092WithColor::~WithColor() {
93 if (UseColor)
94 OS.resetColor();
95}
Zachary Turner2d11c202015-02-27 09:15:59 +000096
Rui Ueyamafa05aac2015-11-03 01:04:44 +000097void WithColor::applyColor(PDB_ColorItem C) {
Zachary Turner2d11c202015-02-27 09:15:59 +000098 switch (C) {
Rui Ueyamafa05aac2015-11-03 01:04:44 +000099 case PDB_ColorItem::None:
100 OS.resetColor();
101 return;
Zachary Turner2d11c202015-02-27 09:15:59 +0000102 case PDB_ColorItem::Address:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000103 OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000104 return;
105 case PDB_ColorItem::Keyword:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000106 OS.changeColor(raw_ostream::MAGENTA, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000107 return;
Zachary Turner7797c722015-03-02 04:39:56 +0000108 case PDB_ColorItem::Register:
Zachary Turner2d11c202015-02-27 09:15:59 +0000109 case PDB_ColorItem::Offset:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000110 OS.changeColor(raw_ostream::YELLOW, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000111 return;
112 case PDB_ColorItem::Type:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000113 OS.changeColor(raw_ostream::CYAN, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000114 return;
115 case PDB_ColorItem::Identifier:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000116 OS.changeColor(raw_ostream::CYAN, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000117 return;
118 case PDB_ColorItem::Path:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000119 OS.changeColor(raw_ostream::CYAN, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000120 return;
121 case PDB_ColorItem::SectionHeader:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000122 OS.changeColor(raw_ostream::RED, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000123 return;
124 case PDB_ColorItem::LiteralValue:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000125 OS.changeColor(raw_ostream::GREEN, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000126 return;
127 }
128}