[llvm-pdbdump] Add regex-based filtering.
[oota-llvm.git] / tools / llvm-pdbdump / VariableDumper.cpp
1 //===- VariableDumper.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 "VariableDumper.h"
11
12 #include "BuiltinDumper.h"
13 #include "LinePrinter.h"
14 #include "llvm-pdbdump.h"
15 #include "FunctionDumper.h"
16
17 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
25
26 #include "llvm/Support/Format.h"
27
28 using namespace llvm;
29
30 VariableDumper::VariableDumper(LinePrinter &P)
31     : PDBSymDumper(true), Printer(P) {}
32
33 void VariableDumper::start(const PDBSymbolData &Var, raw_ostream &OS,
34                            int Indent) {
35   if (Printer.IsSymbolExcluded(Var.getName()))
36     return;
37
38   Printer.NewLine();
39   Printer << "data ";
40
41   auto VarType = Var.getType();
42
43   switch (auto LocType = Var.getLocationType()) {
44   case PDB_LocType::Static:
45     WithColor(Printer, PDB_ColorItem::Address).get()
46         << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
47     WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
48     dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
49     break;
50   case PDB_LocType::Constant:
51     WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
52     dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
53     Printer << "[";
54     WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
55     Printer << "]";
56     break;
57   case PDB_LocType::ThisRel:
58     WithColor(Printer, PDB_ColorItem::Offset).get()
59         << "+" << format_hex(Var.getOffset(), 4) << " ";
60     dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
61     break;
62   default:
63     OS << "unknown(" << LocType << ") ";
64     WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
65     break;
66   }
67 }
68
69 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
70                           int Indent) {
71   BuiltinDumper Dumper(Printer);
72   Dumper.start(Symbol, OS);
73 }
74
75 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
76                           int Indent) {
77   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
78 }
79
80 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
81                           raw_ostream &OS, int Indent) {}
82
83 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
84                           int Indent) {
85   auto PointeeType = Symbol.getPointeeType();
86   if (!PointeeType)
87     return;
88
89   if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
90     FunctionDumper NestedDumper(Printer);
91     FunctionDumper::PointerType Pointer =
92         Symbol.isReference() ? FunctionDumper::PointerType::Reference
93                              : FunctionDumper::PointerType::Pointer;
94     NestedDumper.start(*Func, Pointer, OS, Indent);
95   } else {
96     if (Symbol.isConstType())
97       WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
98     if (Symbol.isVolatileType())
99       WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
100     PointeeType->dump(OS, Indent, *this);
101     Printer << (Symbol.isReference() ? "&" : "*");
102   }
103 }
104
105 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
106                           int Indent) {
107   WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
108   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
109 }
110
111 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
112                           int Indent) {
113   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
114 }
115
116 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
117                                            StringRef Name, raw_ostream &OS) {
118   if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
119     std::string IndexSpec;
120     raw_string_ostream IndexStream(IndexSpec);
121     std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
122     while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
123       IndexStream << "[";
124       IndexStream << NestedArray->getCount();
125       IndexStream << "]";
126       ElementType = NestedArray->getElementType();
127     }
128     IndexStream << "[" << ArrayType->getCount() << "]";
129     ElementType->dump(OS, 0, *this);
130     WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
131     Printer << IndexStream.str();
132   } else {
133     if (!tryDumpFunctionPointer(Type, Name, OS)) {
134       Type.dump(OS, 0, *this);
135       WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
136     }
137   }
138 }
139
140 bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
141                                             StringRef Name, raw_ostream &OS) {
142   // Function pointers come across as pointers to function signatures.  But the
143   // signature carries no name, so we have to handle this case separately.
144   if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
145     auto PointeeType = PointerType->getPointeeType();
146     if (auto *FunctionSig =
147             dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
148       FunctionDumper Dumper(Printer);
149       FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
150       if (PointerType->isReference())
151         PT = FunctionDumper::PointerType::Reference;
152       std::string NameStr(Name.begin(), Name.end());
153       Dumper.start(*FunctionSig, NameStr.c_str(), PT, OS);
154       return true;
155     }
156   }
157   return false;
158 }