blob: 76a0d23bf87a4c7cc7a3263a0d382449cab2a6c9 [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 Turner4dc4f012017-04-13 21:11:00 +000094void VariableDumper::start(const PDBSymbolTypeVTable &Var, uint32_t Offset) {
Zachary Turnerc883a8c2017-04-12 23:18:21 +000095 Printer.NewLine();
Zachary Turner4dc4f012017-04-13 21:11:00 +000096 Printer << "vfptr ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +000097 auto VTableType = cast<PDBSymbolTypePointer>(Var.getType());
98 uint32_t PointerSize = VTableType->getLength();
99
100 WithColor(Printer, PDB_ColorItem::Offset).get()
Zachary Turner4dc4f012017-04-13 21:11:00 +0000101 << "+" << format_hex(Offset + Var.getOffset(), 4)
102 << " [sizeof=" << PointerSize << "] ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000103}
104
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000105void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
106 auto ElementType = Symbol.getElementType();
107 assert(ElementType);
108 if (!ElementType)
109 return;
110 ElementType->dump(*this);
111}
112
113void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
114 auto ElementType = Symbol.getElementType();
115 assert(ElementType);
116 if (!ElementType)
117 return;
118 Printer << '[' << Symbol.getCount() << ']';
119 ElementType->dumpRight(*this);
120}
121
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000122void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000123 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000124 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +0000125}
126
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000127void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000128 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000129}
130
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000131void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
132 auto ReturnType = Symbol.getReturnType();
133 ReturnType->dump(*this);
134 Printer << " ";
135
136 uint32_t ClassParentId = Symbol.getClassParentId();
137 auto ClassParent =
138 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
139 ClassParentId);
140
141 if (ClassParent) {
142 WithColor(Printer, PDB_ColorItem::Identifier).get()
143 << ClassParent->getName();
144 Printer << "::";
145 }
146}
147
148void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
149 Printer << "(";
150 if (auto Arguments = Symbol.getArguments()) {
151 uint32_t Index = 0;
152 while (auto Arg = Arguments->getNext()) {
153 Arg->dump(*this);
154 if (++Index < Arguments->getChildCount())
155 Printer << ", ";
156 }
157 }
158 Printer << ")";
159
160 if (Symbol.isConstType())
161 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
162 if (Symbol.isVolatileType())
163 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
164}
Zachary Turner29c69102015-02-23 05:58:34 +0000165
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000166void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +0000167 auto PointeeType = Symbol.getPointeeType();
168 if (!PointeeType)
169 return;
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000170 PointeeType->dump(*this);
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000171 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000172 // A hack to get the calling convention in the right spot.
173 Printer << " (";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000174 PDB_CallingConv CC = FuncSig->getCallingConvention();
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000175 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000176 } else if (isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000177 Printer << " (";
Zachary Turner29c69102015-02-23 05:58:34 +0000178 }
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000179 Printer << (Symbol.isReference() ? "&" : "*");
180 if (Symbol.isConstType())
181 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
182 if (Symbol.isVolatileType())
183 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
184}
185
186void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
187 auto PointeeType = Symbol.getPointeeType();
188 assert(PointeeType);
189 if (!PointeeType)
190 return;
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000191 if (isa<PDBSymbolTypeFunctionSig>(PointeeType) ||
192 isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000193 Printer << ")";
194 }
195 PointeeType->dumpRight(*this);
Zachary Turner29c69102015-02-23 05:58:34 +0000196}
197
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000198void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000199 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
200 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000201}
202
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000203void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000204 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000205}
206
207void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000208 StringRef Name) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000209 Type.dump(*this);
210 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
211 Type.dumpRight(*this);
Zachary Turnerd270d222015-02-26 23:49:23 +0000212}