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/ADT/STLExtras.h"
15#include "llvm/Support/Regex.h"
16
17#include <algorithm>
18
19using namespace llvm;
20using namespace llvm::pdb;
21
22namespace {
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(IncludeFilters, match_pred))
34    return true;
35
36  if (any_of(ExcludeFilters, match_pred))
37    return true;
38
39  return false;
40}
41}
42
43using namespace llvm;
44
45LinePrinter::LinePrinter(int Indent, llvm::raw_ostream &Stream)
46    : OS(Stream), IndentSpaces(Indent), CurrentIndent(0) {
47  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());
53
54  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());
60}
61
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
73bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName) {
74  return IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters);
75}
76
77bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
78  return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
79}
80
81bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
82  return IsItemExcluded(CompilandName, IncludeCompilandFilters,
83                        ExcludeCompilandFilters);
84}
85
86WithColor::WithColor(LinePrinter &P, PDB_ColorItem C) : OS(P.OS) {
87  applyColor(C);
88}
89
90WithColor::~WithColor() { OS.resetColor(); }
91
92void 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}
124