1 //===- PrettyFunctionDumper.cpp --------------------------------- *- C++ *-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PrettyFunctionDumper.h"
11 #include "LinePrinter.h"
12 #include "PrettyBuiltinDumper.h"
14 #include "llvm/DebugInfo/PDB/IPDBSession.h"
15 #include "llvm/DebugInfo/PDB/PDBExtras.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 #include "llvm/Support/FormatVariadic.h"
31 using namespace llvm::codeview;
32 using namespace llvm::pdb;
36 void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
37 FunctionDumper &Dumper) {
38 uint32_t ClassParentId = Symbol.getClassParentId();
40 Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
45 WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
50 FunctionDumper::FunctionDumper(LinePrinter &P)
51 : PDBSymDumper(true), Printer(P) {}
53 void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
54 const char *Name, PointerType Pointer) {
55 auto ReturnType = Symbol.getReturnType();
56 ReturnType->dump(*this);
58 uint32_t ClassParentId = Symbol.getClassParentId();
60 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
63 PDB_CallingConv CC = Symbol.getCallingConvention();
64 bool ShouldDumpCallingConvention = true;
65 if ((ClassParent && CC == CallingConvention::ThisCall) ||
66 (!ClassParent && CC == CallingConvention::NearStdCall)) {
67 ShouldDumpCallingConvention = false;
70 if (Pointer == PointerType::None) {
71 if (ShouldDumpCallingConvention)
72 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
75 WithColor(Printer, PDB_ColorItem::Identifier).get()
76 << ClassParent->getName();
81 if (ShouldDumpCallingConvention)
82 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
84 WithColor(Printer, PDB_ColorItem::Identifier).get()
85 << ClassParent->getName();
88 if (Pointer == PointerType::Reference)
93 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
98 if (auto ChildEnum = Symbol.getArguments()) {
100 while (auto Arg = ChildEnum->getNext()) {
102 if (++Index < ChildEnum->getChildCount())
108 if (Symbol.isConstType())
109 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
110 if (Symbol.isVolatileType())
111 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
114 void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer) {
115 uint64_t FuncStart = Symbol.getVirtualAddress();
116 uint64_t FuncEnd = FuncStart + Symbol.getLength();
119 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncStart, 10);
120 if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) {
121 uint64_t Prologue = DebugStart->getVirtualAddress() - FuncStart;
122 WithColor(Printer, PDB_ColorItem::Offset).get()
123 << formatv("+{0,2}", Prologue);
126 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncEnd, 10);
127 if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) {
128 uint64_t Epilogue = FuncEnd - DebugEnd->getVirtualAddress();
129 WithColor(Printer, PDB_ColorItem::Offset).get()
130 << formatv("-{0,2}", Epilogue);
133 WithColor(Printer, PDB_ColorItem::Comment).get()
134 << formatv(" | sizeof={0,3}", Symbol.getLength());
137 if (Symbol.hasFramePointer()) {
138 WithColor(Printer, PDB_ColorItem::Register).get()
139 << Symbol.getLocalBasePointerRegisterId();
141 WithColor(Printer, PDB_ColorItem::Register).get() << "FPO";
145 if (Symbol.isVirtual() || Symbol.isPureVirtual())
146 WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
148 auto Signature = Symbol.getSignature();
150 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
151 if (Pointer == PointerType::Pointer)
153 else if (Pointer == FunctionDumper::PointerType::Reference)
158 auto ReturnType = Signature->getReturnType();
159 ReturnType->dump(*this);
162 auto ClassParent = Symbol.getClassParent();
163 CallingConvention CC = Signature->getCallingConvention();
164 if (Pointer != FunctionDumper::PointerType::None)
167 if ((ClassParent && CC != CallingConvention::ThisCall) ||
168 (!ClassParent && CC != CallingConvention::NearStdCall)) {
169 WithColor(Printer, PDB_ColorItem::Keyword).get()
170 << Signature->getCallingConvention() << " ";
172 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
173 if (Pointer != FunctionDumper::PointerType::None) {
174 if (Pointer == PointerType::Pointer)
176 else if (Pointer == FunctionDumper::PointerType::Reference)
182 if (auto Arguments = Symbol.getArguments()) {
184 while (auto Arg = Arguments->getNext()) {
185 auto ArgType = Arg->getType();
186 ArgType->dump(*this);
187 WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
189 if (++Index < Arguments->getChildCount())
194 if (Symbol.isConstType())
195 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
196 if (Symbol.isVolatileType())
197 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
198 if (Symbol.isPureVirtual())
202 void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol) {
203 auto ElementType = Symbol.getElementType();
205 ElementType->dump(*this);
207 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
211 void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
212 BuiltinDumper Dumper(Printer);
213 Dumper.start(Symbol);
216 void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
217 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
218 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
221 void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol) {
222 // PDBSymbolTypeFunctionArg is just a shim over the real argument. Just drill
223 // through to the real thing and dump it.
224 uint32_t TypeId = Symbol.getTypeId();
225 auto Type = Symbol.getSession().getSymbolById(TypeId);
231 void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
232 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
233 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
236 void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol) {
237 auto PointeeType = Symbol.getPointeeType();
241 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
242 FunctionDumper NestedDumper(Printer);
243 PointerType Pointer =
244 Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
245 NestedDumper.start(*FuncSig, nullptr, Pointer);
247 if (Symbol.isConstType())
248 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
249 if (Symbol.isVolatileType())
250 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
251 PointeeType->dump(*this);
252 Printer << (Symbol.isReference() ? "&" : "*");
256 void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol) {
257 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();