blob: 70925f4b03d0901bb732fc368d3789c5f217be07 [file] [log] [blame]
Zachary Turnera9054dd2017-01-11 00:35:43 +00001//===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===//
Zachary Turner29c69102015-02-23 05:58:34 +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 Turnera9054dd2017-01-11 00:35:43 +000010#include "PrettyVariableDumper.h"
Zachary Turner29c69102015-02-23 05:58:34 +000011
Zachary Turner2d11c202015-02-27 09:15:59 +000012#include "LinePrinter.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000013#include "PrettyBuiltinDumper.h"
14#include "PrettyFunctionDumper.h"
Zachary Turner29c69102015-02-23 05:58:34 +000015#include "llvm-pdbdump.h"
Zachary Turner29c69102015-02-23 05:58:34 +000016
Adrian McCarthy08eb3432017-04-10 16:43:09 +000017#include "llvm/DebugInfo/PDB/IPDBSession.h"
Zachary Turner29c69102015-02-23 05:58:34 +000018#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
19#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
20#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
Zachary Turner65323652015-03-04 06:09:53 +000021#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000022#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Zachary Turnerd270d222015-02-26 23:49:23 +000023#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
Zachary Turner29c69102015-02-23 05:58:34 +000024#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
25#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
Zachary Turner29c69102015-02-23 05:58:34 +000026#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
Adrian McCarthy08eb3432017-04-10 16:43:09 +000027#include "llvm/DebugInfo/PDB/PDBTypes.h"
Zachary Turner29c69102015-02-23 05:58:34 +000028
29#include "llvm/Support/Format.h"
30
31using namespace llvm;
Adrian McCarthy08eb3432017-04-10 16:43:09 +000032using namespace llvm::codeview;
Zachary Turnerec28fc32016-05-04 20:32:13 +000033using namespace llvm::pdb;
Zachary Turner29c69102015-02-23 05:58:34 +000034
Zachary Turner2d11c202015-02-27 09:15:59 +000035VariableDumper::VariableDumper(LinePrinter &P)
36 : PDBSymDumper(true), Printer(P) {}
Zachary Turner29c69102015-02-23 05:58:34 +000037
Zachary Turner4dc4f012017-04-13 21:11:00 +000038void VariableDumper::start(const PDBSymbolData &Var, uint32_t Offset) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +000039 if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
Zachary Turner7797c722015-03-02 04:39:56 +000040 return;
Zachary Turnerf5abda22015-03-01 06:49:49 +000041 if (Printer.IsSymbolExcluded(Var.getName()))
42 return;
43
Zachary Turner29c69102015-02-23 05:58:34 +000044 auto VarType = Var.getType();
45
Zachary Turner0c990bbe2017-04-10 19:33:29 +000046 uint64_t Length = VarType->getRawSymbol().getLength();
47
Zachary Turner29c69102015-02-23 05:58:34 +000048 switch (auto LocType = Var.getLocationType()) {
49 case PDB_LocType::Static:
Zachary Turner65323652015-03-04 06:09:53 +000050 Printer.NewLine();
51 Printer << "data [";
Zachary Turner2d11c202015-02-27 09:15:59 +000052 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turnere5cb2692015-05-01 20:24:26 +000053 << format_hex(Var.getVirtualAddress(), 10);
Zachary Turner0c990bbe2017-04-10 19:33:29 +000054 Printer << ", sizeof=" << Length << "] ";
Zachary Turner2d11c202015-02-27 09:15:59 +000055 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000056 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000057 break;
58 case PDB_LocType::Constant:
Zachary Turner65323652015-03-04 06:09:53 +000059 if (isa<PDBSymbolTypeEnum>(*VarType))
60 break;
61 Printer.NewLine();
Zachary Turner0c990bbe2017-04-10 19:33:29 +000062 Printer << "data [sizeof=" << Length << "] ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000063 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner7797c722015-03-02 04:39:56 +000064 Printer << " = ";
Zachary Turner2d11c202015-02-27 09:15:59 +000065 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
Zachary Turner29c69102015-02-23 05:58:34 +000066 break;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000067 case PDB_LocType::ThisRel:
Zachary Turner65323652015-03-04 06:09:53 +000068 Printer.NewLine();
69 Printer << "data ";
Zachary Turner2d11c202015-02-27 09:15:59 +000070 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner4dc4f012017-04-13 21:11:00 +000071 << "+" << format_hex(Offset + Var.getOffset(), 4)
72 << " [sizeof=" << Length << "] ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000073 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000074 break;
Zachary Turner7797c722015-03-02 04:39:56 +000075 case PDB_LocType::BitField:
Zachary Turner65323652015-03-04 06:09:53 +000076 Printer.NewLine();
77 Printer << "data ";
Zachary Turner7797c722015-03-02 04:39:56 +000078 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner4dc4f012017-04-13 21:11:00 +000079 << "+" << format_hex(Offset + Var.getOffset(), 4)
80 << " [sizeof=" << Length << "] ";
Zachary Turner7797c722015-03-02 04:39:56 +000081 dumpSymbolTypeAndName(*VarType, Var.getName());
82 Printer << " : ";
83 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
84 break;
Zachary Turner29c69102015-02-23 05:58:34 +000085 default:
Zachary Turner65323652015-03-04 06:09:53 +000086 Printer.NewLine();
Zachary Turner0c990bbe2017-04-10 19:33:29 +000087 Printer << "data [sizeof=" << Length << "] ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000088 Printer << "unknown(" << LocType << ") ";
Zachary Turner2d11c202015-02-27 09:15:59 +000089 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000090 break;
Zachary Turner29c69102015-02-23 05:58:34 +000091 }
92}
93
Zachary Turner16901642017-04-24 17:47:24 +000094void VariableDumper::startVbptr(uint32_t Offset, uint32_t Size) {
95 Printer.NewLine();
96 Printer << "vbptr ";
97
98 WithColor(Printer, PDB_ColorItem::Offset).get()
99 << "+" << format_hex(Offset, 4) << " [sizeof=" << Size << "] ";
100}
101
Zachary Turner4dc4f012017-04-13 21:11:00 +0000102void VariableDumper::start(const PDBSymbolTypeVTable &Var, uint32_t Offset) {
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000103 Printer.NewLine();
Zachary Turner4dc4f012017-04-13 21:11:00 +0000104 Printer << "vfptr ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000105 auto VTableType = cast<PDBSymbolTypePointer>(Var.getType());
106 uint32_t PointerSize = VTableType->getLength();
107
108 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner4dc4f012017-04-13 21:11:00 +0000109 << "+" << format_hex(Offset + Var.getOffset(), 4)
110 << " [sizeof=" << PointerSize << "] ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000111}
112
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000113void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
114 auto ElementType = Symbol.getElementType();
115 assert(ElementType);
116 if (!ElementType)
117 return;
118 ElementType->dump(*this);
119}
120
121void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
122 auto ElementType = Symbol.getElementType();
123 assert(ElementType);
124 if (!ElementType)
125 return;
126 Printer << '[' << Symbol.getCount() << ']';
127 ElementType->dumpRight(*this);
128}
129
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000130void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000131 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000132 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +0000133}
134
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000135void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000136 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000137}
138
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000139void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
140 auto ReturnType = Symbol.getReturnType();
141 ReturnType->dump(*this);
142 Printer << " ";
143
144 uint32_t ClassParentId = Symbol.getClassParentId();
145 auto ClassParent =
146 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
147 ClassParentId);
148
149 if (ClassParent) {
150 WithColor(Printer, PDB_ColorItem::Identifier).get()
151 << ClassParent->getName();
152 Printer << "::";
153 }
154}
155
156void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
157 Printer << "(";
158 if (auto Arguments = Symbol.getArguments()) {
159 uint32_t Index = 0;
160 while (auto Arg = Arguments->getNext()) {
161 Arg->dump(*this);
162 if (++Index < Arguments->getChildCount())
163 Printer << ", ";
164 }
165 }
166 Printer << ")";
167
168 if (Symbol.isConstType())
169 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
170 if (Symbol.isVolatileType())
171 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
172}
Zachary Turner29c69102015-02-23 05:58:34 +0000173
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000174void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +0000175 auto PointeeType = Symbol.getPointeeType();
176 if (!PointeeType)
177 return;
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000178 PointeeType->dump(*this);
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000179 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000180 // A hack to get the calling convention in the right spot.
181 Printer << " (";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000182 PDB_CallingConv CC = FuncSig->getCallingConvention();
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000183 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000184 } else if (isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000185 Printer << " (";
Zachary Turner29c69102015-02-23 05:58:34 +0000186 }
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000187 Printer << (Symbol.isReference() ? "&" : "*");
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}
193
194void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
195 auto PointeeType = Symbol.getPointeeType();
196 assert(PointeeType);
197 if (!PointeeType)
198 return;
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000199 if (isa<PDBSymbolTypeFunctionSig>(PointeeType) ||
200 isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000201 Printer << ")";
202 }
203 PointeeType->dumpRight(*this);
Zachary Turner29c69102015-02-23 05:58:34 +0000204}
205
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000206void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000207 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
208 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000209}
210
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000211void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000212 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000213}
214
215void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000216 StringRef Name) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000217 Type.dump(*this);
218 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
219 Type.dumpRight(*this);
Zachary Turnerd270d222015-02-26 23:49:23 +0000220}