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-pdbdump.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"
27 #include "llvm/Support/Format.h"
30 using namespace llvm::pdb;
32 VariableDumper::VariableDumper(LinePrinter &P)
33 : PDBSymDumper(true), Printer(P) {}
35 void VariableDumper::start(const PDBSymbolData &Var) {
36 if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
38 if (Printer.IsSymbolExcluded(Var.getName()))
41 auto VarType = Var.getType();
43 switch (auto LocType = Var.getLocationType()) {
44 case PDB_LocType::Static:
47 WithColor(Printer, PDB_ColorItem::Address).get()
48 << format_hex(Var.getVirtualAddress(), 10);
50 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
51 dumpSymbolTypeAndName(*VarType, Var.getName());
53 case PDB_LocType::Constant:
54 if (isa<PDBSymbolTypeEnum>(*VarType))
58 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
59 dumpSymbolTypeAndName(*VarType, Var.getName());
61 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
63 case PDB_LocType::ThisRel:
66 WithColor(Printer, PDB_ColorItem::Offset).get()
67 << "+" << format_hex(Var.getOffset(), 4) << " ";
68 dumpSymbolTypeAndName(*VarType, Var.getName());
70 case PDB_LocType::BitField:
73 WithColor(Printer, PDB_ColorItem::Offset).get()
74 << "+" << format_hex(Var.getOffset(), 4) << " ";
75 dumpSymbolTypeAndName(*VarType, Var.getName());
77 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
82 Printer << "unknown(" << LocType << ") ";
83 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
88 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
89 BuiltinDumper Dumper(Printer);
93 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
94 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
97 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
99 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
100 auto PointeeType = Symbol.getPointeeType();
104 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
105 FunctionDumper NestedDumper(Printer);
106 FunctionDumper::PointerType Pointer =
107 Symbol.isReference() ? FunctionDumper::PointerType::Reference
108 : FunctionDumper::PointerType::Pointer;
109 NestedDumper.start(*Func, Pointer);
111 if (Symbol.isConstType())
112 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
113 if (Symbol.isVolatileType())
114 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
115 PointeeType->dump(*this);
116 Printer << (Symbol.isReference() ? "&" : "*");
120 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
121 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
122 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
125 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
126 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
129 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
131 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
132 std::string IndexSpec;
133 raw_string_ostream IndexStream(IndexSpec);
134 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
135 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
137 IndexStream << NestedArray->getCount();
139 ElementType = NestedArray->getElementType();
141 IndexStream << "[" << ArrayType->getCount() << "]";
142 ElementType->dump(*this);
143 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
144 Printer << IndexStream.str();
146 if (!tryDumpFunctionPointer(Type, Name)) {
148 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
153 bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
155 // Function pointers come across as pointers to function signatures. But the
156 // signature carries no name, so we have to handle this case separately.
157 if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
158 auto PointeeType = PointerType->getPointeeType();
159 if (auto *FunctionSig =
160 dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
161 FunctionDumper Dumper(Printer);
162 FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
163 if (PointerType->isReference())
164 PT = FunctionDumper::PointerType::Reference;
165 std::string NameStr(Name.begin(), Name.end());
166 Dumper.start(*FunctionSig, NameStr.c_str(), PT);