1 //===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PrettyVariableDumper.h"
12 #include "LinePrinter.h"
13 #include "PrettyBuiltinDumper.h"
14 #include "PrettyFunctionDumper.h"
15 #include "llvm-pdbutil.h"
17 #include "llvm/DebugInfo/PDB/IPDBSession.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
27 #include "llvm/DebugInfo/PDB/PDBTypes.h"
29 #include "llvm/Support/Format.h"
32 using namespace llvm::codeview
;
33 using namespace llvm::pdb
;
35 VariableDumper::VariableDumper(LinePrinter
&P
)
36 : PDBSymDumper(true), Printer(P
) {}
38 void VariableDumper::start(const PDBSymbolData
&Var
, uint32_t Offset
) {
39 if (Var
.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated
)
41 if (Printer
.IsSymbolExcluded(Var
.getName()))
44 auto VarType
= Var
.getType();
46 uint64_t Length
= VarType
->getRawSymbol().getLength();
48 switch (auto LocType
= Var
.getLocationType()) {
49 case PDB_LocType::Static
:
52 WithColor(Printer
, PDB_ColorItem::Address
).get()
53 << format_hex(Var
.getVirtualAddress(), 10);
54 Printer
<< ", sizeof=" << Length
<< "] ";
55 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << "static ";
56 dumpSymbolTypeAndName(*VarType
, Var
.getName());
58 case PDB_LocType::Constant
:
59 if (isa
<PDBSymbolTypeEnum
>(*VarType
))
62 Printer
<< "data [sizeof=" << Length
<< "] ";
63 dumpSymbolTypeAndName(*VarType
, Var
.getName());
65 WithColor(Printer
, PDB_ColorItem::LiteralValue
).get() << Var
.getValue();
67 case PDB_LocType::ThisRel
:
70 WithColor(Printer
, PDB_ColorItem::Offset
).get()
71 << "+" << format_hex(Offset
+ Var
.getOffset(), 4)
72 << " [sizeof=" << Length
<< "] ";
73 dumpSymbolTypeAndName(*VarType
, Var
.getName());
75 case PDB_LocType::BitField
:
78 WithColor(Printer
, PDB_ColorItem::Offset
).get()
79 << "+" << format_hex(Offset
+ Var
.getOffset(), 4)
80 << " [sizeof=" << Length
<< "] ";
81 dumpSymbolTypeAndName(*VarType
, Var
.getName());
83 WithColor(Printer
, PDB_ColorItem::LiteralValue
).get() << Var
.getLength();
87 Printer
<< "data [sizeof=" << Length
<< "] ";
88 Printer
<< "unknown(" << LocType
<< ") ";
89 WithColor(Printer
, PDB_ColorItem::Identifier
).get() << Var
.getName();
94 void VariableDumper::startVbptr(uint32_t Offset
, uint32_t Size
) {
98 WithColor(Printer
, PDB_ColorItem::Offset
).get()
99 << "+" << format_hex(Offset
, 4) << " [sizeof=" << Size
<< "] ";
102 void VariableDumper::start(const PDBSymbolTypeVTable
&Var
, uint32_t Offset
) {
105 auto VTableType
= cast
<PDBSymbolTypePointer
>(Var
.getType());
106 uint32_t PointerSize
= VTableType
->getLength();
108 WithColor(Printer
, PDB_ColorItem::Offset
).get()
109 << "+" << format_hex(Offset
+ Var
.getOffset(), 4)
110 << " [sizeof=" << PointerSize
<< "] ";
113 void VariableDumper::dump(const PDBSymbolTypeArray
&Symbol
) {
114 auto ElementType
= Symbol
.getElementType();
118 ElementType
->dump(*this);
121 void VariableDumper::dumpRight(const PDBSymbolTypeArray
&Symbol
) {
122 auto ElementType
= Symbol
.getElementType();
126 Printer
<< '[' << Symbol
.getCount() << ']';
127 ElementType
->dumpRight(*this);
130 void VariableDumper::dump(const PDBSymbolTypeBuiltin
&Symbol
) {
131 BuiltinDumper
Dumper(Printer
);
132 Dumper
.start(Symbol
);
135 void VariableDumper::dump(const PDBSymbolTypeEnum
&Symbol
) {
136 WithColor(Printer
, PDB_ColorItem::Type
).get() << Symbol
.getName();
139 void VariableDumper::dump(const PDBSymbolTypeFunctionSig
&Symbol
) {
140 auto ReturnType
= Symbol
.getReturnType();
141 ReturnType
->dump(*this);
144 uint32_t ClassParentId
= Symbol
.getClassParentId();
146 Symbol
.getSession().getConcreteSymbolById
<PDBSymbolTypeUDT
>(
150 WithColor(Printer
, PDB_ColorItem::Identifier
).get()
151 << ClassParent
->getName();
156 void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig
&Symbol
) {
158 if (auto Arguments
= Symbol
.getArguments()) {
160 while (auto Arg
= Arguments
->getNext()) {
162 if (++Index
< Arguments
->getChildCount())
168 if (Symbol
.isConstType())
169 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << " const";
170 if (Symbol
.isVolatileType())
171 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << " volatile";
174 void VariableDumper::dump(const PDBSymbolTypePointer
&Symbol
) {
175 auto PointeeType
= Symbol
.getPointeeType();
178 PointeeType
->dump(*this);
179 if (auto FuncSig
= unique_dyn_cast
<PDBSymbolTypeFunctionSig
>(PointeeType
)) {
180 // A hack to get the calling convention in the right spot.
182 PDB_CallingConv CC
= FuncSig
->getCallingConvention();
183 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << CC
<< " ";
184 } else if (isa
<PDBSymbolTypeArray
>(PointeeType
)) {
187 Printer
<< (Symbol
.isReference() ? "&" : "*");
188 if (Symbol
.isConstType())
189 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << " const ";
190 if (Symbol
.isVolatileType())
191 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << " volatile ";
194 void VariableDumper::dumpRight(const PDBSymbolTypePointer
&Symbol
) {
195 auto PointeeType
= Symbol
.getPointeeType();
199 if (isa
<PDBSymbolTypeFunctionSig
>(PointeeType
) ||
200 isa
<PDBSymbolTypeArray
>(PointeeType
)) {
203 PointeeType
->dumpRight(*this);
206 void VariableDumper::dump(const PDBSymbolTypeTypedef
&Symbol
) {
207 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << "typedef ";
208 WithColor(Printer
, PDB_ColorItem::Type
).get() << Symbol
.getName();
211 void VariableDumper::dump(const PDBSymbolTypeUDT
&Symbol
) {
212 WithColor(Printer
, PDB_ColorItem::Type
).get() << Symbol
.getName();
215 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol
&Type
,
218 WithColor(Printer
, PDB_ColorItem::Identifier
).get() << " " << Name
;
219 Type
.dumpRight(*this);