1 //===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "PrettyVariableDumper.h"
11 #include "LinePrinter.h"
12 #include "PrettyBuiltinDumper.h"
13 #include "PrettyFunctionDumper.h"
14 #include "llvm-pdbutil.h"
16 #include "llvm/DebugInfo/PDB/IPDBSession.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
26 #include "llvm/DebugInfo/PDB/PDBTypes.h"
28 #include "llvm/Support/Format.h"
31 using namespace llvm::codeview;
32 using namespace llvm::pdb;
34 VariableDumper::VariableDumper(LinePrinter &P)
35 : PDBSymDumper(true), Printer(P) {}
37 void VariableDumper::start(const PDBSymbolData &Var, uint32_t Offset) {
38 if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
40 if (Printer.IsSymbolExcluded(Var.getName()))
43 auto VarType = Var.getType();
45 uint64_t Length = VarType->getRawSymbol().getLength();
47 switch (auto LocType = Var.getLocationType()) {
48 case PDB_LocType::Static:
51 WithColor(Printer, PDB_ColorItem::Address).get()
52 << format_hex(Var.getVirtualAddress(), 10);
53 Printer << ", sizeof=" << Length << "] ";
54 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
55 dumpSymbolTypeAndName(*VarType, Var.getName());
57 case PDB_LocType::Constant:
58 if (isa<PDBSymbolTypeEnum>(*VarType))
61 Printer << "data [sizeof=" << Length << "] ";
62 dumpSymbolTypeAndName(*VarType, Var.getName());
64 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
66 case PDB_LocType::ThisRel:
69 WithColor(Printer, PDB_ColorItem::Offset).get()
70 << "+" << format_hex(Offset + Var.getOffset(), 4)
71 << " [sizeof=" << Length << "] ";
72 dumpSymbolTypeAndName(*VarType, Var.getName());
74 case PDB_LocType::BitField:
77 WithColor(Printer, PDB_ColorItem::Offset).get()
78 << "+" << format_hex(Offset + Var.getOffset(), 4)
79 << " [sizeof=" << Length << "] ";
80 dumpSymbolTypeAndName(*VarType, Var.getName());
82 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
86 Printer << "data [sizeof=" << Length << "] ";
87 Printer << "unknown(" << LocType << ") ";
88 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
93 void VariableDumper::startVbptr(uint32_t Offset, uint32_t Size) {
97 WithColor(Printer, PDB_ColorItem::Offset).get()
98 << "+" << format_hex(Offset, 4) << " [sizeof=" << Size << "] ";
101 void VariableDumper::start(const PDBSymbolTypeVTable &Var, uint32_t Offset) {
104 auto VTableType = cast<PDBSymbolTypePointer>(Var.getType());
105 uint32_t PointerSize = VTableType->getLength();
107 WithColor(Printer, PDB_ColorItem::Offset).get()
108 << "+" << format_hex(Offset + Var.getOffset(), 4)
109 << " [sizeof=" << PointerSize << "] ";
112 void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
113 auto ElementType = Symbol.getElementType();
117 ElementType->dump(*this);
120 void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
121 auto ElementType = Symbol.getElementType();
125 Printer << '[' << Symbol.getCount() << ']';
126 ElementType->dumpRight(*this);
129 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
130 BuiltinDumper Dumper(Printer);
131 Dumper.start(Symbol);
134 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
135 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
138 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
139 auto ReturnType = Symbol.getReturnType();
140 ReturnType->dump(*this);
143 uint32_t ClassParentId = Symbol.getClassParentId();
145 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
149 WithColor(Printer, PDB_ColorItem::Identifier).get()
150 << ClassParent->getName();
155 void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
157 if (auto Arguments = Symbol.getArguments()) {
159 while (auto Arg = Arguments->getNext()) {
161 if (++Index < Arguments->getChildCount())
167 if (Symbol.isConstType())
168 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
169 if (Symbol.isVolatileType())
170 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
172 if (Symbol.getRawSymbol().isRestrictedType())
173 WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict";
176 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
177 auto PointeeType = Symbol.getPointeeType();
180 PointeeType->dump(*this);
181 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
182 // A hack to get the calling convention in the right spot.
184 PDB_CallingConv CC = FuncSig->getCallingConvention();
185 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
186 } else if (isa<PDBSymbolTypeArray>(PointeeType)) {
189 Printer << (Symbol.isReference() ? "&" : "*");
190 if (Symbol.isConstType())
191 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
192 if (Symbol.isVolatileType())
193 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
195 if (Symbol.getRawSymbol().isRestrictedType())
196 WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict ";
199 void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
200 auto PointeeType = Symbol.getPointeeType();
204 if (isa<PDBSymbolTypeFunctionSig>(PointeeType) ||
205 isa<PDBSymbolTypeArray>(PointeeType)) {
208 PointeeType->dumpRight(*this);
211 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
212 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
213 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
216 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
217 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
220 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
223 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
224 Type.dumpRight(*this);