blob: ddac8cf0da4a89c846a394a6f72ad4a2764947e5 [file] [log] [blame]
Zachary Turnereb6ab042017-01-11 00:35:43 +00001//===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===//
Zachary Turner92d755e2015-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 Turnereb6ab042017-01-11 00:35:43 +000010#include "PrettyVariableDumper.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000011
Zachary Turner756b8232015-02-27 09:15:59 +000012#include "LinePrinter.h"
Zachary Turnereb6ab042017-01-11 00:35:43 +000013#include "PrettyBuiltinDumper.h"
14#include "PrettyFunctionDumper.h"
Zachary Turnercfb13562017-06-09 20:46:17 +000015#include "llvm-pdbutil.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000016
Adrian McCarthy95d726a2017-04-10 16:43:09 +000017#include "llvm/DebugInfo/PDB/IPDBSession.h"
Zachary Turner92d755e2015-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 Turnerb9c28bc2015-03-04 06:09:53 +000021#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Zachary Turnereb6ab042017-01-11 00:35:43 +000022#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Zachary Turner6aba3832015-02-26 23:49:23 +000023#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000024#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
25#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000026#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
Adrian McCarthy95d726a2017-04-10 16:43:09 +000027#include "llvm/DebugInfo/PDB/PDBTypes.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000028
29#include "llvm/Support/Format.h"
30
31using namespace llvm;
Adrian McCarthy95d726a2017-04-10 16:43:09 +000032using namespace llvm::codeview;
Zachary Turnerc95df942016-05-04 20:32:13 +000033using namespace llvm::pdb;
Zachary Turner92d755e2015-02-23 05:58:34 +000034
Zachary Turner756b8232015-02-27 09:15:59 +000035VariableDumper::VariableDumper(LinePrinter &P)
36 : PDBSymDumper(true), Printer(P) {}
Zachary Turner92d755e2015-02-23 05:58:34 +000037
Zachary Turner10683342017-04-13 21:11:00 +000038void VariableDumper::start(const PDBSymbolData &Var, uint32_t Offset) {
Zachary Turner3a4681d2016-06-30 17:42:48 +000039 if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
Zachary Turner0c7c98a2015-03-02 04:39:56 +000040 return;
Zachary Turner91491392015-03-01 06:49:49 +000041 if (Printer.IsSymbolExcluded(Var.getName()))
42 return;
43
Zachary Turner92d755e2015-02-23 05:58:34 +000044 auto VarType = Var.getType();
45
Zachary Turnere98c9132017-04-10 19:33:29 +000046 uint64_t Length = VarType->getRawSymbol().getLength();
47
Zachary Turner92d755e2015-02-23 05:58:34 +000048 switch (auto LocType = Var.getLocationType()) {
49 case PDB_LocType::Static:
Zachary Turnerb9c28bc2015-03-04 06:09:53 +000050 Printer.NewLine();
51 Printer << "data [";
Zachary Turner756b8232015-02-27 09:15:59 +000052 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turner7c69a582015-05-01 20:24:26 +000053 << format_hex(Var.getVirtualAddress(), 10);
Zachary Turnere98c9132017-04-10 19:33:29 +000054 Printer << ", sizeof=" << Length << "] ";
Zachary Turner756b8232015-02-27 09:15:59 +000055 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
Zachary Turner53e4b562015-03-01 06:51:29 +000056 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner92d755e2015-02-23 05:58:34 +000057 break;
58 case PDB_LocType::Constant:
Zachary Turnerb9c28bc2015-03-04 06:09:53 +000059 if (isa<PDBSymbolTypeEnum>(*VarType))
60 break;
61 Printer.NewLine();
Zachary Turnere98c9132017-04-10 19:33:29 +000062 Printer << "data [sizeof=" << Length << "] ";
Zachary Turner53e4b562015-03-01 06:51:29 +000063 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner0c7c98a2015-03-02 04:39:56 +000064 Printer << " = ";
Zachary Turner756b8232015-02-27 09:15:59 +000065 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
Zachary Turner92d755e2015-02-23 05:58:34 +000066 break;
Benjamin Kramer84bcaea2015-02-23 11:33:54 +000067 case PDB_LocType::ThisRel:
Zachary Turnerb9c28bc2015-03-04 06:09:53 +000068 Printer.NewLine();
69 Printer << "data ";
Zachary Turner756b8232015-02-27 09:15:59 +000070 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner10683342017-04-13 21:11:00 +000071 << "+" << format_hex(Offset + Var.getOffset(), 4)
72 << " [sizeof=" << Length << "] ";
Zachary Turner53e4b562015-03-01 06:51:29 +000073 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner92d755e2015-02-23 05:58:34 +000074 break;
Zachary Turner0c7c98a2015-03-02 04:39:56 +000075 case PDB_LocType::BitField:
Zachary Turnerb9c28bc2015-03-04 06:09:53 +000076 Printer.NewLine();
77 Printer << "data ";
Zachary Turner0c7c98a2015-03-02 04:39:56 +000078 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner10683342017-04-13 21:11:00 +000079 << "+" << format_hex(Offset + Var.getOffset(), 4)
80 << " [sizeof=" << Length << "] ";
Zachary Turner0c7c98a2015-03-02 04:39:56 +000081 dumpSymbolTypeAndName(*VarType, Var.getName());
82 Printer << " : ";
83 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
84 break;
Zachary Turner92d755e2015-02-23 05:58:34 +000085 default:
Zachary Turnerb9c28bc2015-03-04 06:09:53 +000086 Printer.NewLine();
Zachary Turnere98c9132017-04-10 19:33:29 +000087 Printer << "data [sizeof=" << Length << "] ";
Zachary Turner53e4b562015-03-01 06:51:29 +000088 Printer << "unknown(" << LocType << ") ";
Zachary Turner756b8232015-02-27 09:15:59 +000089 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner92d755e2015-02-23 05:58:34 +000090 break;
Zachary Turner92d755e2015-02-23 05:58:34 +000091 }
92}
93
Zachary Turner0e8b7072017-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 Turner10683342017-04-13 21:11:00 +0000102void VariableDumper::start(const PDBSymbolTypeVTable &Var, uint32_t Offset) {
Zachary Turner7724dc62017-04-12 23:18:21 +0000103 Printer.NewLine();
Zachary Turner10683342017-04-13 21:11:00 +0000104 Printer << "vfptr ";
Zachary Turner7724dc62017-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 Turner10683342017-04-13 21:11:00 +0000109 << "+" << format_hex(Offset + Var.getOffset(), 4)
110 << " [sizeof=" << PointerSize << "] ";
Zachary Turner7724dc62017-04-12 23:18:21 +0000111}
112
Adrian McCarthy95d726a2017-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 Turner53e4b562015-03-01 06:51:29 +0000130void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000131 BuiltinDumper Dumper(Printer);
Zachary Turner53e4b562015-03-01 06:51:29 +0000132 Dumper.start(Symbol);
Zachary Turner92d755e2015-02-23 05:58:34 +0000133}
134
Zachary Turner53e4b562015-03-01 06:51:29 +0000135void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000136 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner92d755e2015-02-23 05:58:34 +0000137}
138
Adrian McCarthy95d726a2017-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";
Aaron Smithd894b742018-03-05 18:29:43 +0000172
173 if (Symbol.getRawSymbol().isRestrictedType())
174 WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict";
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000175}
Zachary Turner92d755e2015-02-23 05:58:34 +0000176
Zachary Turner53e4b562015-03-01 06:51:29 +0000177void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner92d755e2015-02-23 05:58:34 +0000178 auto PointeeType = Symbol.getPointeeType();
179 if (!PointeeType)
180 return;
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000181 PointeeType->dump(*this);
Zachary Turner7724dc62017-04-12 23:18:21 +0000182 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000183 // A hack to get the calling convention in the right spot.
184 Printer << " (";
Zachary Turner7724dc62017-04-12 23:18:21 +0000185 PDB_CallingConv CC = FuncSig->getCallingConvention();
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000186 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
Zachary Turner7724dc62017-04-12 23:18:21 +0000187 } else if (isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000188 Printer << " (";
Zachary Turner92d755e2015-02-23 05:58:34 +0000189 }
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000190 Printer << (Symbol.isReference() ? "&" : "*");
191 if (Symbol.isConstType())
192 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
193 if (Symbol.isVolatileType())
194 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
Aaron Smithd894b742018-03-05 18:29:43 +0000195
196 if (Symbol.getRawSymbol().isRestrictedType())
197 WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict ";
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000198}
199
200void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
201 auto PointeeType = Symbol.getPointeeType();
202 assert(PointeeType);
203 if (!PointeeType)
204 return;
Zachary Turner7724dc62017-04-12 23:18:21 +0000205 if (isa<PDBSymbolTypeFunctionSig>(PointeeType) ||
206 isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000207 Printer << ")";
208 }
209 PointeeType->dumpRight(*this);
Zachary Turner92d755e2015-02-23 05:58:34 +0000210}
211
Zachary Turner53e4b562015-03-01 06:51:29 +0000212void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000213 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
214 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner92d755e2015-02-23 05:58:34 +0000215}
216
Zachary Turner53e4b562015-03-01 06:51:29 +0000217void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000218 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner92d755e2015-02-23 05:58:34 +0000219}
220
221void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turner53e4b562015-03-01 06:51:29 +0000222 StringRef Name) {
Adrian McCarthy95d726a2017-04-10 16:43:09 +0000223 Type.dump(*this);
224 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
225 Type.dumpRight(*this);
Zachary Turner6aba3832015-02-26 23:49:23 +0000226}