blob: 836ede41054ec8b8e74f9d816346727fde91275f [file] [log] [blame]
Zachary Turnereb6ab042017-01-11 00:35:43 +00001//===- PrettyFunctionDumper.cpp --------------------------------- *- C++ *-===//
Zachary Turner395adf92015-02-22 22:03:38 +00002//
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
Zachary Turnereb6ab042017-01-11 00:35:43 +000010#include "PrettyFunctionDumper.h"
Zachary Turner756b8232015-02-27 09:15:59 +000011#include "LinePrinter.h"
Zachary Turnereb6ab042017-01-11 00:35:43 +000012#include "PrettyBuiltinDumper.h"
Zachary Turner395adf92015-02-22 22:03:38 +000013
14#include "llvm/DebugInfo/PDB/IPDBSession.h"
Zachary Turnerc95df942016-05-04 20:32:13 +000015#include "llvm/DebugInfo/PDB/PDBExtras.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000016#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner395adf92015-02-22 22:03:38 +000017#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000018#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
19#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Zachary Turner395adf92015-02-22 22:03:38 +000020#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
Zachary Turner395adf92015-02-22 22:03:38 +000021#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"
Zachary Turner92d755e2015-02-23 05:58:34 +000027#include "llvm/Support/Format.h"
Zachary Turner4afda8f2017-05-14 01:13:40 +000028#include "llvm/Support/FormatVariadic.h"
Zachary Turner395adf92015-02-22 22:03:38 +000029
Zachary Turner86a84e32015-02-22 22:20:26 +000030using namespace llvm;
Reid Klecknerfb30f092016-01-13 19:32:35 +000031using namespace llvm::codeview;
Zachary Turnerc95df942016-05-04 20:32:13 +000032using namespace llvm::pdb;
Zachary Turner86a84e32015-02-22 22:20:26 +000033
Zachary Turner395adf92015-02-22 22:03:38 +000034namespace {
35template <class T>
Zachary Turner756b8232015-02-27 09:15:59 +000036void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
Zachary Turnerc95df942016-05-04 20:32:13 +000037 FunctionDumper &Dumper) {
Zachary Turner395adf92015-02-22 22:03:38 +000038 uint32_t ClassParentId = Symbol.getClassParentId();
39 auto ClassParent =
David Majnemer1f229002015-02-22 22:33:57 +000040 Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
Zachary Turner395adf92015-02-22 22:03:38 +000041 ClassParentId);
42 if (!ClassParent)
43 return;
44
Zachary Turner756b8232015-02-27 09:15:59 +000045 WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
46 Printer << "::";
Zachary Turner395adf92015-02-22 22:03:38 +000047}
48}
49
Zachary Turner756b8232015-02-27 09:15:59 +000050FunctionDumper::FunctionDumper(LinePrinter &P)
51 : PDBSymDumper(true), Printer(P) {}
Zachary Turner395adf92015-02-22 22:03:38 +000052
53void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
Zachary Turner53e4b562015-03-01 06:51:29 +000054 const char *Name, PointerType Pointer) {
Zachary Turner395adf92015-02-22 22:03:38 +000055 auto ReturnType = Symbol.getReturnType();
Zachary Turnera93560b2018-10-01 17:55:16 +000056 if (!ReturnType)
57 Printer << "<unknown-type>";
58 else
59 ReturnType->dump(*this);
Zachary Turner756b8232015-02-27 09:15:59 +000060 Printer << " ";
Zachary Turner395adf92015-02-22 22:03:38 +000061 uint32_t ClassParentId = Symbol.getClassParentId();
62 auto ClassParent =
63 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
64 ClassParentId);
65
Zachary Turnerc95df942016-05-04 20:32:13 +000066 PDB_CallingConv CC = Symbol.getCallingConvention();
Zachary Turner92d755e2015-02-23 05:58:34 +000067 bool ShouldDumpCallingConvention = true;
Reid Klecknerfb30f092016-01-13 19:32:35 +000068 if ((ClassParent && CC == CallingConvention::ThisCall) ||
69 (!ClassParent && CC == CallingConvention::NearStdCall)) {
Zachary Turner92d755e2015-02-23 05:58:34 +000070 ShouldDumpCallingConvention = false;
71 }
72
Zachary Turner395adf92015-02-22 22:03:38 +000073 if (Pointer == PointerType::None) {
Zachary Turner92d755e2015-02-23 05:58:34 +000074 if (ShouldDumpCallingConvention)
Zachary Turner756b8232015-02-27 09:15:59 +000075 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
76 if (ClassParent) {
77 Printer << "(";
78 WithColor(Printer, PDB_ColorItem::Identifier).get()
79 << ClassParent->getName();
80 Printer << "::)";
81 }
Zachary Turner395adf92015-02-22 22:03:38 +000082 } else {
Zachary Turner756b8232015-02-27 09:15:59 +000083 Printer << "(";
Zachary Turner92d755e2015-02-23 05:58:34 +000084 if (ShouldDumpCallingConvention)
Zachary Turner756b8232015-02-27 09:15:59 +000085 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
86 if (ClassParent) {
87 WithColor(Printer, PDB_ColorItem::Identifier).get()
88 << ClassParent->getName();
89 Printer << "::";
90 }
Zachary Turner395adf92015-02-22 22:03:38 +000091 if (Pointer == PointerType::Reference)
Zachary Turner756b8232015-02-27 09:15:59 +000092 Printer << "&";
Zachary Turner395adf92015-02-22 22:03:38 +000093 else
Zachary Turner756b8232015-02-27 09:15:59 +000094 Printer << "*";
Zachary Turner6aba3832015-02-26 23:49:23 +000095 if (Name)
Zachary Turner756b8232015-02-27 09:15:59 +000096 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
97 Printer << ")";
Zachary Turner395adf92015-02-22 22:03:38 +000098 }
99
Zachary Turner756b8232015-02-27 09:15:59 +0000100 Printer << "(";
Zachary Turner395adf92015-02-22 22:03:38 +0000101 if (auto ChildEnum = Symbol.getArguments()) {
102 uint32_t Index = 0;
103 while (auto Arg = ChildEnum->getNext()) {
Zachary Turner53e4b562015-03-01 06:51:29 +0000104 Arg->dump(*this);
Zachary Turner395adf92015-02-22 22:03:38 +0000105 if (++Index < ChildEnum->getChildCount())
Zachary Turner756b8232015-02-27 09:15:59 +0000106 Printer << ", ";
Zachary Turner395adf92015-02-22 22:03:38 +0000107 }
108 }
Zachary Turner756b8232015-02-27 09:15:59 +0000109 Printer << ")";
Zachary Turner395adf92015-02-22 22:03:38 +0000110
111 if (Symbol.isConstType())
Zachary Turner756b8232015-02-27 09:15:59 +0000112 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner395adf92015-02-22 22:03:38 +0000113 if (Symbol.isVolatileType())
Zachary Turner756b8232015-02-27 09:15:59 +0000114 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner395adf92015-02-22 22:03:38 +0000115}
116
Zachary Turner53e4b562015-03-01 06:51:29 +0000117void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer) {
Zachary Turner7c69a582015-05-01 20:24:26 +0000118 uint64_t FuncStart = Symbol.getVirtualAddress();
119 uint64_t FuncEnd = FuncStart + Symbol.getLength();
Zachary Turner92d755e2015-02-23 05:58:34 +0000120
Zachary Turner0c7c98a2015-03-02 04:39:56 +0000121 Printer << "func [";
122 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncStart, 10);
Zachary Turner756b8232015-02-27 09:15:59 +0000123 if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) {
Zachary Turner7c69a582015-05-01 20:24:26 +0000124 uint64_t Prologue = DebugStart->getVirtualAddress() - FuncStart;
Zachary Turner4afda8f2017-05-14 01:13:40 +0000125 WithColor(Printer, PDB_ColorItem::Offset).get()
126 << formatv("+{0,2}", Prologue);
Zachary Turner756b8232015-02-27 09:15:59 +0000127 }
Zachary Turner0c7c98a2015-03-02 04:39:56 +0000128 Printer << " - ";
129 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncEnd, 10);
Zachary Turner756b8232015-02-27 09:15:59 +0000130 if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) {
Zachary Turner7c69a582015-05-01 20:24:26 +0000131 uint64_t Epilogue = FuncEnd - DebugEnd->getVirtualAddress();
Zachary Turner4afda8f2017-05-14 01:13:40 +0000132 WithColor(Printer, PDB_ColorItem::Offset).get()
133 << formatv("-{0,2}", Epilogue);
Zachary Turner756b8232015-02-27 09:15:59 +0000134 }
Zachary Turner4afda8f2017-05-14 01:13:40 +0000135
136 WithColor(Printer, PDB_ColorItem::Comment).get()
137 << formatv(" | sizeof={0,3}", Symbol.getLength());
Zachary Turner0c7c98a2015-03-02 04:39:56 +0000138 Printer << "] (";
Zachary Turner92d755e2015-02-23 05:58:34 +0000139
Zachary Turner0c7c98a2015-03-02 04:39:56 +0000140 if (Symbol.hasFramePointer()) {
141 WithColor(Printer, PDB_ColorItem::Register).get()
142 << Symbol.getLocalBasePointerRegisterId();
143 } else {
144 WithColor(Printer, PDB_ColorItem::Register).get() << "FPO";
145 }
146 Printer << ") ";
Zachary Turner92d755e2015-02-23 05:58:34 +0000147
Zachary Turner395adf92015-02-22 22:03:38 +0000148 if (Symbol.isVirtual() || Symbol.isPureVirtual())
Zachary Turner756b8232015-02-27 09:15:59 +0000149 WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
Zachary Turner395adf92015-02-22 22:03:38 +0000150
151 auto Signature = Symbol.getSignature();
152 if (!Signature) {
Zachary Turner756b8232015-02-27 09:15:59 +0000153 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner92d755e2015-02-23 05:58:34 +0000154 if (Pointer == PointerType::Pointer)
Zachary Turner756b8232015-02-27 09:15:59 +0000155 Printer << "*";
Zachary Turner92d755e2015-02-23 05:58:34 +0000156 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner756b8232015-02-27 09:15:59 +0000157 Printer << "&";
Zachary Turner395adf92015-02-22 22:03:38 +0000158 return;
159 }
160
161 auto ReturnType = Signature->getReturnType();
Zachary Turner53e4b562015-03-01 06:51:29 +0000162 ReturnType->dump(*this);
Zachary Turner756b8232015-02-27 09:15:59 +0000163 Printer << " ";
Zachary Turner395adf92015-02-22 22:03:38 +0000164
Zachary Turner92d755e2015-02-23 05:58:34 +0000165 auto ClassParent = Symbol.getClassParent();
Reid Klecknerfb30f092016-01-13 19:32:35 +0000166 CallingConvention CC = Signature->getCallingConvention();
Zachary Turner92d755e2015-02-23 05:58:34 +0000167 if (Pointer != FunctionDumper::PointerType::None)
Zachary Turner756b8232015-02-27 09:15:59 +0000168 Printer << "(";
Zachary Turner92d755e2015-02-23 05:58:34 +0000169
Reid Klecknerfb30f092016-01-13 19:32:35 +0000170 if ((ClassParent && CC != CallingConvention::ThisCall) ||
171 (!ClassParent && CC != CallingConvention::NearStdCall)) {
Zachary Turner756b8232015-02-27 09:15:59 +0000172 WithColor(Printer, PDB_ColorItem::Keyword).get()
173 << Signature->getCallingConvention() << " ";
174 }
175 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner92d755e2015-02-23 05:58:34 +0000176 if (Pointer != FunctionDumper::PointerType::None) {
177 if (Pointer == PointerType::Pointer)
Zachary Turner756b8232015-02-27 09:15:59 +0000178 Printer << "*";
Zachary Turner92d755e2015-02-23 05:58:34 +0000179 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner756b8232015-02-27 09:15:59 +0000180 Printer << "&";
181 Printer << ")";
Zachary Turner92d755e2015-02-23 05:58:34 +0000182 }
Zachary Turner395adf92015-02-22 22:03:38 +0000183
Zachary Turner756b8232015-02-27 09:15:59 +0000184 Printer << "(";
Zachary Turner92d755e2015-02-23 05:58:34 +0000185 if (auto Arguments = Symbol.getArguments()) {
Zachary Turner395adf92015-02-22 22:03:38 +0000186 uint32_t Index = 0;
Zachary Turner92d755e2015-02-23 05:58:34 +0000187 while (auto Arg = Arguments->getNext()) {
188 auto ArgType = Arg->getType();
Zachary Turner53e4b562015-03-01 06:51:29 +0000189 ArgType->dump(*this);
Zachary Turner756b8232015-02-27 09:15:59 +0000190 WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
191 << Arg->getName();
Zachary Turner92d755e2015-02-23 05:58:34 +0000192 if (++Index < Arguments->getChildCount())
Zachary Turner756b8232015-02-27 09:15:59 +0000193 Printer << ", ";
Zachary Turner395adf92015-02-22 22:03:38 +0000194 }
Aaron Smith7e2c2992018-01-17 01:22:03 +0000195 if (Signature->isCVarArgs())
196 Printer << ", ...";
Zachary Turner395adf92015-02-22 22:03:38 +0000197 }
Zachary Turner756b8232015-02-27 09:15:59 +0000198 Printer << ")";
Zachary Turner395adf92015-02-22 22:03:38 +0000199 if (Symbol.isConstType())
Zachary Turner756b8232015-02-27 09:15:59 +0000200 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner395adf92015-02-22 22:03:38 +0000201 if (Symbol.isVolatileType())
Zachary Turner756b8232015-02-27 09:15:59 +0000202 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner395adf92015-02-22 22:03:38 +0000203 if (Symbol.isPureVirtual())
Zachary Turner756b8232015-02-27 09:15:59 +0000204 Printer << " = 0";
Zachary Turner395adf92015-02-22 22:03:38 +0000205}
206
Zachary Turner53e4b562015-03-01 06:51:29 +0000207void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol) {
Zachary Turner18fcab52017-04-10 06:14:09 +0000208 auto ElementType = Symbol.getElementType();
Zachary Turner395adf92015-02-22 22:03:38 +0000209
Zachary Turner53e4b562015-03-01 06:51:29 +0000210 ElementType->dump(*this);
Zachary Turner756b8232015-02-27 09:15:59 +0000211 Printer << "[";
212 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
213 Printer << "]";
Zachary Turner395adf92015-02-22 22:03:38 +0000214}
215
Zachary Turner53e4b562015-03-01 06:51:29 +0000216void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000217 BuiltinDumper Dumper(Printer);
Zachary Turner53e4b562015-03-01 06:51:29 +0000218 Dumper.start(Symbol);
Zachary Turner395adf92015-02-22 22:03:38 +0000219}
220
Zachary Turner53e4b562015-03-01 06:51:29 +0000221void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000222 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
223 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner395adf92015-02-22 22:03:38 +0000224}
225
Zachary Turner53e4b562015-03-01 06:51:29 +0000226void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol) {
Zachary Turner395adf92015-02-22 22:03:38 +0000227 // PDBSymbolTypeFunctionArg is just a shim over the real argument. Just drill
Zachary Turner92d755e2015-02-23 05:58:34 +0000228 // through to the real thing and dump it.
Zachary Turner395adf92015-02-22 22:03:38 +0000229 uint32_t TypeId = Symbol.getTypeId();
230 auto Type = Symbol.getSession().getSymbolById(TypeId);
Zachary Turnera93560b2018-10-01 17:55:16 +0000231 if (Type)
232 Printer << "<unknown-type>";
233 else
234 Type->dump(*this);
Zachary Turner395adf92015-02-22 22:03:38 +0000235}
236
Zachary Turner53e4b562015-03-01 06:51:29 +0000237void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000238 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
239 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner395adf92015-02-22 22:03:38 +0000240}
241
Zachary Turner53e4b562015-03-01 06:51:29 +0000242void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner18fcab52017-04-10 06:14:09 +0000243 auto PointeeType = Symbol.getPointeeType();
Zachary Turner395adf92015-02-22 22:03:38 +0000244 if (!PointeeType)
245 return;
246
Zachary Turner7724dc62017-04-12 23:18:21 +0000247 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
Zachary Turner756b8232015-02-27 09:15:59 +0000248 FunctionDumper NestedDumper(Printer);
Zachary Turner395adf92015-02-22 22:03:38 +0000249 PointerType Pointer =
250 Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
Zachary Turner53e4b562015-03-01 06:51:29 +0000251 NestedDumper.start(*FuncSig, nullptr, Pointer);
Zachary Turner395adf92015-02-22 22:03:38 +0000252 } else {
253 if (Symbol.isConstType())
Zachary Turner756b8232015-02-27 09:15:59 +0000254 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner395adf92015-02-22 22:03:38 +0000255 if (Symbol.isVolatileType())
Zachary Turner756b8232015-02-27 09:15:59 +0000256 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turner53e4b562015-03-01 06:51:29 +0000257 PointeeType->dump(*this);
258 Printer << (Symbol.isReference() ? "&" : "*");
Aaron Smithd894b742018-03-05 18:29:43 +0000259
260 if (Symbol.getRawSymbol().isRestrictedType())
261 WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict";
Zachary Turner395adf92015-02-22 22:03:38 +0000262 }
263}
264
Zachary Turner53e4b562015-03-01 06:51:29 +0000265void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000266 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner395adf92015-02-22 22:03:38 +0000267}