blob: 6dd7cc384cc9e724d7886d6a9d5a065a4d8fe5f7 [file] [log] [blame]
Zachary Turnera9054dd2017-01-11 00:35:43 +00001//===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===//
Zachary Turner29c69102015-02-23 05:58:34 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turner29c69102015-02-23 05:58:34 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turnera9054dd2017-01-11 00:35:43 +00009#include "PrettyVariableDumper.h"
Zachary Turner29c69102015-02-23 05:58:34 +000010
Zachary Turner2d11c202015-02-27 09:15:59 +000011#include "LinePrinter.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000012#include "PrettyBuiltinDumper.h"
13#include "PrettyFunctionDumper.h"
Zachary Turnerbd336e42017-06-09 20:46:17 +000014#include "llvm-pdbutil.h"
Zachary Turner29c69102015-02-23 05:58:34 +000015
Adrian McCarthy08eb3432017-04-10 16:43:09 +000016#include "llvm/DebugInfo/PDB/IPDBSession.h"
Zachary Turner29c69102015-02-23 05:58:34 +000017#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
18#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
19#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
Zachary Turner65323652015-03-04 06:09:53 +000020#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000021#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Zachary Turnerd270d222015-02-26 23:49:23 +000022#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
Zachary Turner29c69102015-02-23 05:58:34 +000023#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
24#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
Zachary Turner29c69102015-02-23 05:58:34 +000025#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
Adrian McCarthy08eb3432017-04-10 16:43:09 +000026#include "llvm/DebugInfo/PDB/PDBTypes.h"
Zachary Turner29c69102015-02-23 05:58:34 +000027
28#include "llvm/Support/Format.h"
29
30using namespace llvm;
Adrian McCarthy08eb3432017-04-10 16:43:09 +000031using namespace llvm::codeview;
Zachary Turnerec28fc32016-05-04 20:32:13 +000032using namespace llvm::pdb;
Zachary Turner29c69102015-02-23 05:58:34 +000033
Zachary Turner2d11c202015-02-27 09:15:59 +000034VariableDumper::VariableDumper(LinePrinter &P)
35 : PDBSymDumper(true), Printer(P) {}
Zachary Turner29c69102015-02-23 05:58:34 +000036
Zachary Turner4dc4f012017-04-13 21:11:00 +000037void VariableDumper::start(const PDBSymbolData &Var, uint32_t Offset) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +000038 if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
Zachary Turner7797c722015-03-02 04:39:56 +000039 return;
Zachary Turnerf5abda22015-03-01 06:49:49 +000040 if (Printer.IsSymbolExcluded(Var.getName()))
41 return;
42
Zachary Turner29c69102015-02-23 05:58:34 +000043 auto VarType = Var.getType();
44
Zachary Turner0c990bbe2017-04-10 19:33:29 +000045 uint64_t Length = VarType->getRawSymbol().getLength();
46
Zachary Turner29c69102015-02-23 05:58:34 +000047 switch (auto LocType = Var.getLocationType()) {
48 case PDB_LocType::Static:
Zachary Turner65323652015-03-04 06:09:53 +000049 Printer.NewLine();
50 Printer << "data [";
Zachary Turner2d11c202015-02-27 09:15:59 +000051 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turnere5cb2692015-05-01 20:24:26 +000052 << format_hex(Var.getVirtualAddress(), 10);
Zachary Turner0c990bbe2017-04-10 19:33:29 +000053 Printer << ", sizeof=" << Length << "] ";
Zachary Turner2d11c202015-02-27 09:15:59 +000054 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000055 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000056 break;
57 case PDB_LocType::Constant:
Zachary Turner65323652015-03-04 06:09:53 +000058 if (isa<PDBSymbolTypeEnum>(*VarType))
59 break;
60 Printer.NewLine();
Zachary Turner0c990bbe2017-04-10 19:33:29 +000061 Printer << "data [sizeof=" << Length << "] ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000062 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner7797c722015-03-02 04:39:56 +000063 Printer << " = ";
Zachary Turner2d11c202015-02-27 09:15:59 +000064 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
Zachary Turner29c69102015-02-23 05:58:34 +000065 break;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000066 case PDB_LocType::ThisRel:
Zachary Turner65323652015-03-04 06:09:53 +000067 Printer.NewLine();
68 Printer << "data ";
Zachary Turner2d11c202015-02-27 09:15:59 +000069 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner4dc4f012017-04-13 21:11:00 +000070 << "+" << format_hex(Offset + Var.getOffset(), 4)
71 << " [sizeof=" << Length << "] ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000072 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000073 break;
Zachary Turner7797c722015-03-02 04:39:56 +000074 case PDB_LocType::BitField:
Zachary Turner65323652015-03-04 06:09:53 +000075 Printer.NewLine();
76 Printer << "data ";
Zachary Turner7797c722015-03-02 04:39:56 +000077 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner4dc4f012017-04-13 21:11:00 +000078 << "+" << format_hex(Offset + Var.getOffset(), 4)
79 << " [sizeof=" << Length << "] ";
Zachary Turner7797c722015-03-02 04:39:56 +000080 dumpSymbolTypeAndName(*VarType, Var.getName());
81 Printer << " : ";
82 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
83 break;
Zachary Turner29c69102015-02-23 05:58:34 +000084 default:
Zachary Turner65323652015-03-04 06:09:53 +000085 Printer.NewLine();
Zachary Turner0c990bbe2017-04-10 19:33:29 +000086 Printer << "data [sizeof=" << Length << "] ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000087 Printer << "unknown(" << LocType << ") ";
Zachary Turner2d11c202015-02-27 09:15:59 +000088 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000089 break;
Zachary Turner29c69102015-02-23 05:58:34 +000090 }
91}
92
Zachary Turner16901642017-04-24 17:47:24 +000093void VariableDumper::startVbptr(uint32_t Offset, uint32_t Size) {
94 Printer.NewLine();
95 Printer << "vbptr ";
96
97 WithColor(Printer, PDB_ColorItem::Offset).get()
98 << "+" << format_hex(Offset, 4) << " [sizeof=" << Size << "] ";
99}
100
Zachary Turner4dc4f012017-04-13 21:11:00 +0000101void VariableDumper::start(const PDBSymbolTypeVTable &Var, uint32_t Offset) {
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000102 Printer.NewLine();
Zachary Turner4dc4f012017-04-13 21:11:00 +0000103 Printer << "vfptr ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000104 auto VTableType = cast<PDBSymbolTypePointer>(Var.getType());
105 uint32_t PointerSize = VTableType->getLength();
106
107 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner4dc4f012017-04-13 21:11:00 +0000108 << "+" << format_hex(Offset + Var.getOffset(), 4)
109 << " [sizeof=" << PointerSize << "] ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000110}
111
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000112void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
113 auto ElementType = Symbol.getElementType();
114 assert(ElementType);
115 if (!ElementType)
116 return;
117 ElementType->dump(*this);
118}
119
120void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
121 auto ElementType = Symbol.getElementType();
122 assert(ElementType);
123 if (!ElementType)
124 return;
125 Printer << '[' << Symbol.getCount() << ']';
126 ElementType->dumpRight(*this);
127}
128
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000129void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000130 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000131 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +0000132}
133
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000134void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000135 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000136}
137
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000138void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
139 auto ReturnType = Symbol.getReturnType();
140 ReturnType->dump(*this);
141 Printer << " ";
142
143 uint32_t ClassParentId = Symbol.getClassParentId();
144 auto ClassParent =
145 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
146 ClassParentId);
147
148 if (ClassParent) {
149 WithColor(Printer, PDB_ColorItem::Identifier).get()
150 << ClassParent->getName();
151 Printer << "::";
152 }
153}
154
155void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
156 Printer << "(";
157 if (auto Arguments = Symbol.getArguments()) {
158 uint32_t Index = 0;
159 while (auto Arg = Arguments->getNext()) {
160 Arg->dump(*this);
161 if (++Index < Arguments->getChildCount())
162 Printer << ", ";
163 }
164 }
165 Printer << ")";
166
167 if (Symbol.isConstType())
168 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
169 if (Symbol.isVolatileType())
170 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Aaron Smith5ab08cf2018-03-05 18:29:43 +0000171
172 if (Symbol.getRawSymbol().isRestrictedType())
173 WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict";
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000174}
Zachary Turner29c69102015-02-23 05:58:34 +0000175
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000176void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +0000177 auto PointeeType = Symbol.getPointeeType();
178 if (!PointeeType)
179 return;
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000180 PointeeType->dump(*this);
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000181 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000182 // A hack to get the calling convention in the right spot.
183 Printer << " (";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000184 PDB_CallingConv CC = FuncSig->getCallingConvention();
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000185 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000186 } else if (isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000187 Printer << " (";
Zachary Turner29c69102015-02-23 05:58:34 +0000188 }
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000189 Printer << (Symbol.isReference() ? "&" : "*");
190 if (Symbol.isConstType())
191 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
192 if (Symbol.isVolatileType())
193 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
Aaron Smith5ab08cf2018-03-05 18:29:43 +0000194
195 if (Symbol.getRawSymbol().isRestrictedType())
196 WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict ";
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000197}
198
199void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
200 auto PointeeType = Symbol.getPointeeType();
201 assert(PointeeType);
202 if (!PointeeType)
203 return;
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000204 if (isa<PDBSymbolTypeFunctionSig>(PointeeType) ||
205 isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000206 Printer << ")";
207 }
208 PointeeType->dumpRight(*this);
Zachary Turner29c69102015-02-23 05:58:34 +0000209}
210
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000211void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000212 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
213 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000214}
215
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000216void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000217 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000218}
219
220void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000221 StringRef Name) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000222 Type.dump(*this);
223 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
224 Type.dumpRight(*this);
Zachary Turnerd270d222015-02-26 23:49:23 +0000225}