1 //===- VariableDumper.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 "VariableDumper.h"
12 #include "BuiltinDumper.h"
13 #include "LinePrinter.h"
14 #include "llvm-pdbdump.h"
15 #include "FunctionDumper.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/PDBSymbolTypeFunctionSig.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
26 #include "llvm/Support/Format.h"
30 VariableDumper::VariableDumper(LinePrinter &P)
31 : PDBSymDumper(true), Printer(P) {}
33 void VariableDumper::start(const PDBSymbolData &Var) {
34 if (Printer.IsSymbolExcluded(Var.getName()))
40 auto VarType = Var.getType();
42 switch (auto LocType = Var.getLocationType()) {
43 case PDB_LocType::Static:
44 WithColor(Printer, PDB_ColorItem::Address).get()
45 << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
46 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
47 dumpSymbolTypeAndName(*VarType, Var.getName());
49 case PDB_LocType::Constant:
50 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
51 dumpSymbolTypeAndName(*VarType, Var.getName());
53 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
56 case PDB_LocType::ThisRel:
57 WithColor(Printer, PDB_ColorItem::Offset).get()
58 << "+" << format_hex(Var.getOffset(), 4) << " ";
59 dumpSymbolTypeAndName(*VarType, Var.getName());
62 Printer << "unknown(" << LocType << ") ";
63 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
68 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
69 BuiltinDumper Dumper(Printer);
73 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
74 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
77 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
79 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
80 auto PointeeType = Symbol.getPointeeType();
84 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
85 FunctionDumper NestedDumper(Printer);
86 FunctionDumper::PointerType Pointer =
87 Symbol.isReference() ? FunctionDumper::PointerType::Reference
88 : FunctionDumper::PointerType::Pointer;
89 NestedDumper.start(*Func, Pointer);
91 if (Symbol.isConstType())
92 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
93 if (Symbol.isVolatileType())
94 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
95 PointeeType->dump(*this);
96 Printer << (Symbol.isReference() ? "&" : "*");
100 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
101 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
102 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
105 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
106 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
109 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
111 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
112 std::string IndexSpec;
113 raw_string_ostream IndexStream(IndexSpec);
114 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
115 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
117 IndexStream << NestedArray->getCount();
119 ElementType = NestedArray->getElementType();
121 IndexStream << "[" << ArrayType->getCount() << "]";
122 ElementType->dump(*this);
123 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
124 Printer << IndexStream.str();
126 if (!tryDumpFunctionPointer(Type, Name)) {
128 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
133 bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
135 // Function pointers come across as pointers to function signatures. But the
136 // signature carries no name, so we have to handle this case separately.
137 if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
138 auto PointeeType = PointerType->getPointeeType();
139 if (auto *FunctionSig =
140 dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
141 FunctionDumper Dumper(Printer);
142 FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
143 if (PointerType->isReference())
144 PT = FunctionDumper::PointerType::Reference;
145 std::string NameStr(Name.begin(), Name.end());
146 Dumper.start(*FunctionSig, NameStr.c_str(), PT);