[llvm-pdbdump] Add regex-based filtering.
[oota-llvm.git] / tools / llvm-pdbdump / FunctionDumper.cpp
1 //===- FunctionDumper.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 "FunctionDumper.h"
11 #include "BuiltinDumper.h"
12 #include "LinePrinter.h"
13 #include "llvm-pdbdump.h"
14
15 #include "llvm/DebugInfo/PDB/IPDBSession.h"
16 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
27 #include "llvm/Support/Format.h"
28
29 using namespace llvm;
30
31 namespace {
32 template <class T>
33 void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
34                                       llvm::FunctionDumper &Dumper) {
35   uint32_t ClassParentId = Symbol.getClassParentId();
36   auto ClassParent =
37       Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
38           ClassParentId);
39   if (!ClassParent)
40     return;
41
42   WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
43   Printer << "::";
44 }
45 }
46
47 FunctionDumper::FunctionDumper(LinePrinter &P)
48     : PDBSymDumper(true), Printer(P) {}
49
50 void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
51                            const char *Name, PointerType Pointer,
52                            raw_ostream &OS) {
53   auto ReturnType = Symbol.getReturnType();
54   ReturnType->dump(OS, 0, *this);
55   Printer << " ";
56   uint32_t ClassParentId = Symbol.getClassParentId();
57   auto ClassParent =
58       Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
59           ClassParentId);
60
61   PDB_CallingConv CC = Symbol.getCallingConvention();
62   bool ShouldDumpCallingConvention = true;
63   if ((ClassParent && CC == PDB_CallingConv::Thiscall) ||
64       (!ClassParent && CC == PDB_CallingConv::NearStdcall)) {
65     ShouldDumpCallingConvention = false;
66   }
67
68   if (Pointer == PointerType::None) {
69     if (ShouldDumpCallingConvention)
70       WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
71     if (ClassParent) {
72       Printer << "(";
73       WithColor(Printer, PDB_ColorItem::Identifier).get()
74           << ClassParent->getName();
75       Printer << "::)";
76     }
77   } else {
78     Printer << "(";
79     if (ShouldDumpCallingConvention)
80       WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
81     if (ClassParent) {
82       WithColor(Printer, PDB_ColorItem::Identifier).get()
83           << ClassParent->getName();
84       Printer << "::";
85     }
86     if (Pointer == PointerType::Reference)
87       Printer << "&";
88     else
89       Printer << "*";
90     if (Name)
91       WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
92     Printer << ")";
93   }
94
95   Printer << "(";
96   if (auto ChildEnum = Symbol.getArguments()) {
97     uint32_t Index = 0;
98     while (auto Arg = ChildEnum->getNext()) {
99       Arg->dump(OS, 0, *this);
100       if (++Index < ChildEnum->getChildCount())
101         Printer << ", ";
102     }
103   }
104   Printer << ")";
105
106   if (Symbol.isConstType())
107     WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
108   if (Symbol.isVolatileType())
109     WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
110 }
111
112 void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer,
113                            raw_ostream &OS, int Indent) {
114   uint32_t FuncStart = Symbol.getRelativeVirtualAddress();
115   uint32_t FuncEnd = FuncStart + Symbol.getLength();
116
117   Printer << "func ";
118   WithColor(Printer, PDB_ColorItem::Address).get() << "["
119                                                    << format_hex(FuncStart, 8);
120   if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) {
121     uint32_t Prologue = DebugStart->getRelativeVirtualAddress() - FuncStart;
122     WithColor(Printer, PDB_ColorItem::Offset).get() << "+" << Prologue;
123   }
124   WithColor(Printer, PDB_ColorItem::Address).get() << " - "
125                                                    << format_hex(FuncEnd, 8);
126   if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) {
127     uint32_t Epilogue = FuncEnd - DebugEnd->getRelativeVirtualAddress();
128     WithColor(Printer, PDB_ColorItem::Offset).get() << "-" << Epilogue;
129   }
130   WithColor(Printer, PDB_ColorItem::Address).get() << "] ";
131
132   if (Symbol.hasFramePointer())
133     WithColor(Printer, PDB_ColorItem::Address).get()
134         << "(" << Symbol.getLocalBasePointerRegisterId() << ")";
135   else
136     WithColor(Printer, PDB_ColorItem::Address).get() << "(FPO)";
137
138   Printer << " ";
139   if (Symbol.isVirtual() || Symbol.isPureVirtual())
140     WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
141
142   auto Signature = Symbol.getSignature();
143   if (!Signature) {
144     WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
145     if (Pointer == PointerType::Pointer)
146       Printer << "*";
147     else if (Pointer == FunctionDumper::PointerType::Reference)
148       Printer << "&";
149     return;
150   }
151
152   auto ReturnType = Signature->getReturnType();
153   ReturnType->dump(OS, 0, *this);
154   Printer << " ";
155
156   auto ClassParent = Symbol.getClassParent();
157   PDB_CallingConv CC = Signature->getCallingConvention();
158   if (Pointer != FunctionDumper::PointerType::None)
159     Printer << "(";
160
161   if ((ClassParent && CC != PDB_CallingConv::Thiscall) ||
162       (!ClassParent && CC != PDB_CallingConv::NearStdcall)) {
163     WithColor(Printer, PDB_ColorItem::Keyword).get()
164         << Signature->getCallingConvention() << " ";
165   }
166   WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
167   if (Pointer != FunctionDumper::PointerType::None) {
168     if (Pointer == PointerType::Pointer)
169       Printer << "*";
170     else if (Pointer == FunctionDumper::PointerType::Reference)
171       Printer << "&";
172     Printer << ")";
173   }
174
175   Printer << "(";
176   if (auto Arguments = Symbol.getArguments()) {
177     uint32_t Index = 0;
178     while (auto Arg = Arguments->getNext()) {
179       auto ArgType = Arg->getType();
180       ArgType->dump(OS, 0, *this);
181       WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
182                                                           << Arg->getName();
183       if (++Index < Arguments->getChildCount())
184         Printer << ", ";
185     }
186   }
187   Printer << ")";
188   if (Symbol.isConstType())
189     WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
190   if (Symbol.isVolatileType())
191     WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
192   if (Symbol.isPureVirtual())
193     Printer << " = 0";
194 }
195
196 void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol, raw_ostream &OS,
197                           int Indent) {
198   uint32_t ElementTypeId = Symbol.getTypeId();
199   auto ElementType = Symbol.getSession().getSymbolById(ElementTypeId);
200   if (!ElementType)
201     return;
202
203   ElementType->dump(OS, 0, *this);
204   Printer << "[";
205   WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
206   Printer << "]";
207 }
208
209 void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
210                           int Indent) {
211   BuiltinDumper Dumper(Printer);
212   Dumper.start(Symbol, OS);
213 }
214
215 void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
216                           int Indent) {
217   dumpClassParentWithScopeOperator(Symbol, Printer, *this);
218   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
219 }
220
221 void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol,
222                           raw_ostream &OS, int Indent) {
223   // PDBSymbolTypeFunctionArg is just a shim over the real argument.  Just drill
224   // through to the real thing and dump it.
225   uint32_t TypeId = Symbol.getTypeId();
226   auto Type = Symbol.getSession().getSymbolById(TypeId);
227   if (!Type)
228     return;
229   Type->dump(OS, 0, *this);
230 }
231
232 void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
233                           int Indent) {
234   dumpClassParentWithScopeOperator(Symbol, Printer, *this);
235   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
236 }
237
238 void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
239                           int Indent) {
240   uint32_t PointeeId = Symbol.getTypeId();
241   auto PointeeType = Symbol.getSession().getSymbolById(PointeeId);
242   if (!PointeeType)
243     return;
244
245   if (auto FuncSig = dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
246     FunctionDumper NestedDumper(Printer);
247     PointerType Pointer =
248         Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
249     NestedDumper.start(*FuncSig, nullptr, Pointer, OS);
250   } else {
251     if (Symbol.isConstType())
252       WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
253     if (Symbol.isVolatileType())
254       WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
255     PointeeType->dump(OS, Indent, *this);
256     OS << (Symbol.isReference() ? "&" : "*");
257   }
258 }
259
260 void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
261                           int Indent) {
262   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
263 }