blob: 65f0139dfbc5d9c3e907bb9bfe175342e62aeac8 [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
Adrian McCarthy08eb3432017-04-10 16:43:09 +000094void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
95 auto ElementType = Symbol.getElementType();
96 assert(ElementType);
97 if (!ElementType)
98 return;
99 ElementType->dump(*this);
100}
101
102void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
103 auto ElementType = Symbol.getElementType();
104 assert(ElementType);
105 if (!ElementType)
106 return;
107 Printer << '[' << Symbol.getCount() << ']';
108 ElementType->dumpRight(*this);
109}
110
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000111void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000112 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000113 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +0000114}
115
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000116void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000117 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000118}
119
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000120void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
121 auto ReturnType = Symbol.getReturnType();
122 ReturnType->dump(*this);
123 Printer << " ";
124
125 uint32_t ClassParentId = Symbol.getClassParentId();
126 auto ClassParent =
127 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
128 ClassParentId);
129
130 if (ClassParent) {
131 WithColor(Printer, PDB_ColorItem::Identifier).get()
132 << ClassParent->getName();
133 Printer << "::";
134 }
135}
136
137void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
138 Printer << "(";
139 if (auto Arguments = Symbol.getArguments()) {
140 uint32_t Index = 0;
141 while (auto Arg = Arguments->getNext()) {
142 Arg->dump(*this);
143 if (++Index < Arguments->getChildCount())
144 Printer << ", ";
145 }
146 }
147 Printer << ")";
148
149 if (Symbol.isConstType())
150 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
151 if (Symbol.isVolatileType())
152 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
153}
Zachary Turner29c69102015-02-23 05:58:34 +0000154
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000155void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +0000156 auto PointeeType = Symbol.getPointeeType();
157 if (!PointeeType)
158 return;
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000159 PointeeType->dump(*this);
160 if (auto Func = PointeeType->cast<PDBSymbolTypeFunctionSig>()) {
161 // A hack to get the calling convention in the right spot.
162 Printer << " (";
163 PDB_CallingConv CC = Func->getCallingConvention();
164 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
165 } else if (isa<PDBSymbolTypeArray>(PointeeType.get())) {
166 Printer << " (";
Zachary Turner29c69102015-02-23 05:58:34 +0000167 }
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000168 Printer << (Symbol.isReference() ? "&" : "*");
169 if (Symbol.isConstType())
170 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
171 if (Symbol.isVolatileType())
172 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
173}
174
175void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
176 auto PointeeType = Symbol.getPointeeType();
177 assert(PointeeType);
178 if (!PointeeType)
179 return;
180 if (isa<PDBSymbolTypeFunctionSig>(PointeeType.get()) ||
181 isa<PDBSymbolTypeArray>(PointeeType.get())) {
182 Printer << ")";
183 }
184 PointeeType->dumpRight(*this);
Zachary Turner29c69102015-02-23 05:58:34 +0000185}
186
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000187void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000188 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
189 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000190}
191
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000192void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000193 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000194}
195
196void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000197 StringRef Name) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000198 Type.dump(*this);
199 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
200 Type.dumpRight(*this);
Zachary Turnerd270d222015-02-26 23:49:23 +0000201}