blob: ef9a9b51bd018d00dd880d9dc4d612e0604d02a9 [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 Turnerb52d08d2015-03-01 06:51:29 +000038void VariableDumper::start(const PDBSymbolData &Var) {
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 Turner0c990bbe2017-04-10 19:33:29 +000071 << "+" << format_hex(Var.getOffset(), 4) << " [sizeof=" << Length
72 << "] ";
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 Turner0c990bbe2017-04-10 19:33:29 +000079 << "+" << format_hex(Var.getOffset(), 4) << " [sizeof=" << Length
80 << "] ";
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 Turnerc883a8c2017-04-12 23:18:21 +000094void VariableDumper::start(const PDBSymbolTypeVTable &Var) {
95 Printer.NewLine();
96 Printer << "data ";
97 auto VTableType = cast<PDBSymbolTypePointer>(Var.getType());
98 uint32_t PointerSize = VTableType->getLength();
99
100 WithColor(Printer, PDB_ColorItem::Offset).get()
101 << "+" << format_hex(Var.getOffset(), 4) << " [sizeof=" << PointerSize
102 << "] ";
103
104 WithColor(Printer, PDB_ColorItem::Identifier).get() << " __vfptr";
105}
106
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000107void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
108 auto ElementType = Symbol.getElementType();
109 assert(ElementType);
110 if (!ElementType)
111 return;
112 ElementType->dump(*this);
113}
114
115void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
116 auto ElementType = Symbol.getElementType();
117 assert(ElementType);
118 if (!ElementType)
119 return;
120 Printer << '[' << Symbol.getCount() << ']';
121 ElementType->dumpRight(*this);
122}
123
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000124void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000125 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000126 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +0000127}
128
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000129void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000130 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000131}
132
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000133void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
134 auto ReturnType = Symbol.getReturnType();
135 ReturnType->dump(*this);
136 Printer << " ";
137
138 uint32_t ClassParentId = Symbol.getClassParentId();
139 auto ClassParent =
140 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
141 ClassParentId);
142
143 if (ClassParent) {
144 WithColor(Printer, PDB_ColorItem::Identifier).get()
145 << ClassParent->getName();
146 Printer << "::";
147 }
148}
149
150void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
151 Printer << "(";
152 if (auto Arguments = Symbol.getArguments()) {
153 uint32_t Index = 0;
154 while (auto Arg = Arguments->getNext()) {
155 Arg->dump(*this);
156 if (++Index < Arguments->getChildCount())
157 Printer << ", ";
158 }
159 }
160 Printer << ")";
161
162 if (Symbol.isConstType())
163 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
164 if (Symbol.isVolatileType())
165 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
166}
Zachary Turner29c69102015-02-23 05:58:34 +0000167
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000168void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +0000169 auto PointeeType = Symbol.getPointeeType();
170 if (!PointeeType)
171 return;
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000172 PointeeType->dump(*this);
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000173 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000174 // A hack to get the calling convention in the right spot.
175 Printer << " (";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000176 PDB_CallingConv CC = FuncSig->getCallingConvention();
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000177 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000178 } else if (isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000179 Printer << " (";
Zachary Turner29c69102015-02-23 05:58:34 +0000180 }
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000181 Printer << (Symbol.isReference() ? "&" : "*");
182 if (Symbol.isConstType())
183 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
184 if (Symbol.isVolatileType())
185 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
186}
187
188void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
189 auto PointeeType = Symbol.getPointeeType();
190 assert(PointeeType);
191 if (!PointeeType)
192 return;
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000193 if (isa<PDBSymbolTypeFunctionSig>(PointeeType) ||
194 isa<PDBSymbolTypeArray>(PointeeType)) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000195 Printer << ")";
196 }
197 PointeeType->dumpRight(*this);
Zachary Turner29c69102015-02-23 05:58:34 +0000198}
199
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000200void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000201 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
202 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000203}
204
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000205void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000206 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000207}
208
209void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000210 StringRef Name) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000211 Type.dump(*this);
212 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
213 Type.dumpRight(*this);
Zachary Turnerd270d222015-02-26 23:49:23 +0000214}