blob: 8b85f978869ded13b134dd39e75e913d657a94db [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
46 switch (auto LocType = Var.getLocationType()) {
47 case PDB_LocType::Static:
Zachary Turner65323652015-03-04 06:09:53 +000048 Printer.NewLine();
49 Printer << "data [";
Zachary Turner2d11c202015-02-27 09:15:59 +000050 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turnere5cb2692015-05-01 20:24:26 +000051 << format_hex(Var.getVirtualAddress(), 10);
Zachary Turner7797c722015-03-02 04:39:56 +000052 Printer << "] ";
Zachary Turner2d11c202015-02-27 09:15:59 +000053 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000054 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000055 break;
56 case PDB_LocType::Constant:
Zachary Turner65323652015-03-04 06:09:53 +000057 if (isa<PDBSymbolTypeEnum>(*VarType))
58 break;
59 Printer.NewLine();
60 Printer << "data ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000061 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner7797c722015-03-02 04:39:56 +000062 Printer << " = ";
Zachary Turner2d11c202015-02-27 09:15:59 +000063 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
Zachary Turner29c69102015-02-23 05:58:34 +000064 break;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000065 case PDB_LocType::ThisRel:
Zachary Turner65323652015-03-04 06:09:53 +000066 Printer.NewLine();
67 Printer << "data ";
Zachary Turner2d11c202015-02-27 09:15:59 +000068 WithColor(Printer, PDB_ColorItem::Offset).get()
69 << "+" << format_hex(Var.getOffset(), 4) << " ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000070 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000071 break;
Zachary Turner7797c722015-03-02 04:39:56 +000072 case PDB_LocType::BitField:
Zachary Turner65323652015-03-04 06:09:53 +000073 Printer.NewLine();
74 Printer << "data ";
Zachary Turner7797c722015-03-02 04:39:56 +000075 WithColor(Printer, PDB_ColorItem::Offset).get()
76 << "+" << format_hex(Var.getOffset(), 4) << " ";
77 dumpSymbolTypeAndName(*VarType, Var.getName());
78 Printer << " : ";
79 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
80 break;
Zachary Turner29c69102015-02-23 05:58:34 +000081 default:
Zachary Turner65323652015-03-04 06:09:53 +000082 Printer.NewLine();
83 Printer << "data ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000084 Printer << "unknown(" << LocType << ") ";
Zachary Turner2d11c202015-02-27 09:15:59 +000085 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000086 break;
Zachary Turner29c69102015-02-23 05:58:34 +000087 }
88}
89
Adrian McCarthy08eb3432017-04-10 16:43:09 +000090void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
91 auto ElementType = Symbol.getElementType();
92 assert(ElementType);
93 if (!ElementType)
94 return;
95 ElementType->dump(*this);
96}
97
98void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
99 auto ElementType = Symbol.getElementType();
100 assert(ElementType);
101 if (!ElementType)
102 return;
103 Printer << '[' << Symbol.getCount() << ']';
104 ElementType->dumpRight(*this);
105}
106
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000107void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000108 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000109 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +0000110}
111
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000112void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000113 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000114}
115
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000116void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
117 auto ReturnType = Symbol.getReturnType();
118 ReturnType->dump(*this);
119 Printer << " ";
120
121 uint32_t ClassParentId = Symbol.getClassParentId();
122 auto ClassParent =
123 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
124 ClassParentId);
125
126 if (ClassParent) {
127 WithColor(Printer, PDB_ColorItem::Identifier).get()
128 << ClassParent->getName();
129 Printer << "::";
130 }
131}
132
133void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
134 Printer << "(";
135 if (auto Arguments = Symbol.getArguments()) {
136 uint32_t Index = 0;
137 while (auto Arg = Arguments->getNext()) {
138 Arg->dump(*this);
139 if (++Index < Arguments->getChildCount())
140 Printer << ", ";
141 }
142 }
143 Printer << ")";
144
145 if (Symbol.isConstType())
146 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
147 if (Symbol.isVolatileType())
148 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
149}
Zachary Turner29c69102015-02-23 05:58:34 +0000150
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000151void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +0000152 auto PointeeType = Symbol.getPointeeType();
153 if (!PointeeType)
154 return;
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000155 PointeeType->dump(*this);
156 if (auto Func = PointeeType->cast<PDBSymbolTypeFunctionSig>()) {
157 // A hack to get the calling convention in the right spot.
158 Printer << " (";
159 PDB_CallingConv CC = Func->getCallingConvention();
160 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
161 } else if (isa<PDBSymbolTypeArray>(PointeeType.get())) {
162 Printer << " (";
Zachary Turner29c69102015-02-23 05:58:34 +0000163 }
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000164 Printer << (Symbol.isReference() ? "&" : "*");
165 if (Symbol.isConstType())
166 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
167 if (Symbol.isVolatileType())
168 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
169}
170
171void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
172 auto PointeeType = Symbol.getPointeeType();
173 assert(PointeeType);
174 if (!PointeeType)
175 return;
176 if (isa<PDBSymbolTypeFunctionSig>(PointeeType.get()) ||
177 isa<PDBSymbolTypeArray>(PointeeType.get())) {
178 Printer << ")";
179 }
180 PointeeType->dumpRight(*this);
Zachary Turner29c69102015-02-23 05:58:34 +0000181}
182
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000183void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000184 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
185 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000186}
187
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000188void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000189 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000190}
191
192void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000193 StringRef Name) {
Adrian McCarthy08eb3432017-04-10 16:43:09 +0000194 Type.dump(*this);
195 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
196 Type.dumpRight(*this);
Zachary Turnerd270d222015-02-26 23:49:23 +0000197}