]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbutil/LinePrinter.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r305575, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-pdbutil / 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-pdbutil.h"
13
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/DebugInfo/PDB/UDTLayout.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/Regex.h"
18
19 #include <algorithm>
20
21 using namespace llvm;
22 using namespace llvm::pdb;
23
24 namespace {
25 bool IsItemExcluded(llvm::StringRef Item,
26                     std::list<llvm::Regex> &IncludeFilters,
27                     std::list<llvm::Regex> &ExcludeFilters) {
28   if (Item.empty())
29     return false;
30
31   auto match_pred = [Item](llvm::Regex &R) { return R.match(Item); };
32
33   // Include takes priority over exclude.  If the user specified include
34   // filters, and none of them include this item, them item is gone.
35   if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
36     return true;
37
38   if (any_of(ExcludeFilters, match_pred))
39     return true;
40
41   return false;
42 }
43 }
44
45 using namespace llvm;
46
47 LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream)
48     : OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor) {
49   SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(),
50              opts::pretty::ExcludeTypes.end());
51   SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(),
52              opts::pretty::ExcludeSymbols.end());
53   SetFilters(ExcludeCompilandFilters, opts::pretty::ExcludeCompilands.begin(),
54              opts::pretty::ExcludeCompilands.end());
55
56   SetFilters(IncludeTypeFilters, opts::pretty::IncludeTypes.begin(),
57              opts::pretty::IncludeTypes.end());
58   SetFilters(IncludeSymbolFilters, opts::pretty::IncludeSymbols.begin(),
59              opts::pretty::IncludeSymbols.end());
60   SetFilters(IncludeCompilandFilters, opts::pretty::IncludeCompilands.begin(),
61              opts::pretty::IncludeCompilands.end());
62 }
63
64 void LinePrinter::Indent(uint32_t Amount) {
65   if (Amount == 0)
66     Amount = IndentSpaces;
67   CurrentIndent += Amount;
68 }
69
70 void LinePrinter::Unindent(uint32_t Amount) {
71   if (Amount == 0)
72     Amount = IndentSpaces;
73   CurrentIndent = std::max<int>(0, CurrentIndent - Amount);
74 }
75
76 void LinePrinter::NewLine() {
77   OS << "\n";
78   OS.indent(CurrentIndent);
79 }
80
81 void LinePrinter::print(const Twine &T) { OS << T; }
82
83 void LinePrinter::printLine(const Twine &T) {
84   NewLine();
85   OS << T;
86 }
87
88 bool LinePrinter::IsClassExcluded(const ClassLayout &Class) {
89   if (IsTypeExcluded(Class.getName(), Class.getSize()))
90     return true;
91   if (Class.deepPaddingSize() < opts::pretty::PaddingThreshold)
92     return true;
93   return false;
94 }
95
96 void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
97                                uint32_t StartOffset) {
98   NewLine();
99   OS << Label << " (";
100   if (!Data.empty()) {
101     OS << "\n";
102     OS << format_bytes_with_ascii(Data, StartOffset, 32, 4,
103                                   CurrentIndent + IndentSpaces, true);
104     NewLine();
105   }
106   OS << ")";
107 }
108
109 bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size) {
110   if (IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters))
111     return true;
112   if (Size < opts::pretty::SizeThreshold)
113     return true;
114   return false;
115 }
116
117 bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
118   return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
119 }
120
121 bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
122   return IsItemExcluded(CompilandName, IncludeCompilandFilters,
123                         ExcludeCompilandFilters);
124 }
125
126 WithColor::WithColor(LinePrinter &P, PDB_ColorItem C)
127     : OS(P.OS), UseColor(P.hasColor()) {
128   if (UseColor)
129     applyColor(C);
130 }
131
132 WithColor::~WithColor() {
133   if (UseColor)
134     OS.resetColor();
135 }
136
137 void WithColor::applyColor(PDB_ColorItem C) {
138   switch (C) {
139   case PDB_ColorItem::None:
140     OS.resetColor();
141     return;
142   case PDB_ColorItem::Comment:
143     OS.changeColor(raw_ostream::GREEN, false);
144     return;
145   case PDB_ColorItem::Address:
146     OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
147     return;
148   case PDB_ColorItem::Keyword:
149     OS.changeColor(raw_ostream::MAGENTA, true);
150     return;
151   case PDB_ColorItem::Register:
152   case PDB_ColorItem::Offset:
153     OS.changeColor(raw_ostream::YELLOW, false);
154     return;
155   case PDB_ColorItem::Type:
156     OS.changeColor(raw_ostream::CYAN, true);
157     return;
158   case PDB_ColorItem::Identifier:
159     OS.changeColor(raw_ostream::CYAN, false);
160     return;
161   case PDB_ColorItem::Path:
162     OS.changeColor(raw_ostream::CYAN, false);
163     return;
164   case PDB_ColorItem::Padding:
165   case PDB_ColorItem::SectionHeader:
166     OS.changeColor(raw_ostream::RED, true);
167     return;
168   case PDB_ColorItem::LiteralValue:
169     OS.changeColor(raw_ostream::GREEN, true);
170     return;
171   }
172 }