blob: a43727f02b5e5aa98579c7f1c3a886b035c4eb5c [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 Turner4dddcc62015-09-29 19:49:06 +000019namespace {
Zachary Turner4dddcc62015-09-29 19:49:06 +000020bool 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.
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000030 if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
Zachary Turner4dddcc62015-09-29 19:49:06 +000031 return true;
32
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000033 if (any_of(ExcludeFilters, match_pred))
Zachary Turner4dddcc62015-09-29 19:49:06 +000034 return true;
35
36 return false;
37}
38}
39
Zachary Turner2d11c202015-02-27 09:15:59 +000040using namespace llvm;
41
42LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream)
Zachary Turner7797c722015-03-02 04:39:56 +000043 : OS(Stream), IndentSpaces(Indent), CurrentIndent(0) {
Zachary Turner4dddcc62015-09-29 19:49:06 +000044 SetFilters(ExcludeTypeFilters, opts::ExcludeTypes.begin(),
45 opts::ExcludeTypes.end());
46 SetFilters(ExcludeSymbolFilters, opts::ExcludeSymbols.begin(),
Zachary Turner7797c722015-03-02 04:39:56 +000047 opts::ExcludeSymbols.end());
Zachary Turner4dddcc62015-09-29 19:49:06 +000048 SetFilters(ExcludeCompilandFilters, opts::ExcludeCompilands.begin(),
Zachary Turner7797c722015-03-02 04:39:56 +000049 opts::ExcludeCompilands.end());
Zachary Turner4dddcc62015-09-29 19:49:06 +000050
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());
Zachary Turner7797c722015-03-02 04:39:56 +000057}
Zachary Turner2d11c202015-02-27 09:15:59 +000058
59void LinePrinter::Indent() { CurrentIndent += IndentSpaces; }
60
61void LinePrinter::Unindent() {
62 CurrentIndent = std::max(0, CurrentIndent - IndentSpaces);
63}
64
65void LinePrinter::NewLine() {
66 OS << "\n";
67 OS.indent(CurrentIndent);
68}
69
Zachary Turnerf5abda22015-03-01 06:49:49 +000070bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName) {
Zachary Turner4dddcc62015-09-29 19:49:06 +000071 return IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters);
Zachary Turnerf5abda22015-03-01 06:49:49 +000072}
73
74bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
Zachary Turner4dddcc62015-09-29 19:49:06 +000075 return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
Zachary Turnerf5abda22015-03-01 06:49:49 +000076}
77
78bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
Zachary Turner4dddcc62015-09-29 19:49:06 +000079 return IsItemExcluded(CompilandName, IncludeCompilandFilters,
80 ExcludeCompilandFilters);
Zachary Turnerf5abda22015-03-01 06:49:49 +000081}
82
Zachary Turner2d11c202015-02-27 09:15:59 +000083WithColor::WithColor(LinePrinter &P, PDB_ColorItem C) : OS(P.OS) {
Rui Ueyamafa05aac2015-11-03 01:04:44 +000084 applyColor(C);
Zachary Turner2d11c202015-02-27 09:15:59 +000085}
86
87WithColor::~WithColor() { OS.resetColor(); }
88
Rui Ueyamafa05aac2015-11-03 01:04:44 +000089void WithColor::applyColor(PDB_ColorItem C) {
Zachary Turner2d11c202015-02-27 09:15:59 +000090 switch (C) {
Rui Ueyamafa05aac2015-11-03 01:04:44 +000091 case PDB_ColorItem::None:
92 OS.resetColor();
93 return;
Zachary Turner2d11c202015-02-27 09:15:59 +000094 case PDB_ColorItem::Address:
Rui Ueyamafa05aac2015-11-03 01:04:44 +000095 OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
Zachary Turner2d11c202015-02-27 09:15:59 +000096 return;
97 case PDB_ColorItem::Keyword:
Rui Ueyamafa05aac2015-11-03 01:04:44 +000098 OS.changeColor(raw_ostream::MAGENTA, true);
Zachary Turner2d11c202015-02-27 09:15:59 +000099 return;
Zachary Turner7797c722015-03-02 04:39:56 +0000100 case PDB_ColorItem::Register:
Zachary Turner2d11c202015-02-27 09:15:59 +0000101 case PDB_ColorItem::Offset:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000102 OS.changeColor(raw_ostream::YELLOW, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000103 return;
104 case PDB_ColorItem::Type:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000105 OS.changeColor(raw_ostream::CYAN, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000106 return;
107 case PDB_ColorItem::Identifier:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000108 OS.changeColor(raw_ostream::CYAN, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000109 return;
110 case PDB_ColorItem::Path:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000111 OS.changeColor(raw_ostream::CYAN, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000112 return;
113 case PDB_ColorItem::SectionHeader:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000114 OS.changeColor(raw_ostream::RED, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000115 return;
116 case PDB_ColorItem::LiteralValue:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000117 OS.changeColor(raw_ostream::GREEN, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000118 return;
119 }
120}