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/PDBSymbolTypeEnum.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
27 #include "llvm/Support/Format.h"
31 VariableDumper::VariableDumper(LinePrinter &P)
32 : PDBSymDumper(true), Printer(P) {}
34 void VariableDumper::start(const PDBSymbolData &Var) {
35 if (Var.isCompilerGenerated() && opts::ExcludeCompilerGenerated)
37 if (Printer.IsSymbolExcluded(Var.getName()))
40 auto VarType = Var.getType();
42 switch (auto LocType = Var.getLocationType()) {
43 case PDB_LocType::Static:
46 WithColor(Printer, PDB_ColorItem::Address).get()
47 << format_hex(Var.getVirtualAddress(), 10);
49 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
50 dumpSymbolTypeAndName(*VarType, Var.getName());
52 case PDB_LocType::Constant:
53 if (isa<PDBSymbolTypeEnum>(*VarType))
57 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
58 dumpSymbolTypeAndName(*VarType, Var.getName());
60 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
62 case PDB_LocType::ThisRel:
65 WithColor(Printer, PDB_ColorItem::Offset).get()
66 << "+" << format_hex(Var.getOffset(), 4) << " ";
67 dumpSymbolTypeAndName(*VarType, Var.getName());
69 case PDB_LocType::BitField:
72 WithColor(Printer, PDB_ColorItem::Offset).get()
73 << "+" << format_hex(Var.getOffset(), 4) << " ";
74 dumpSymbolTypeAndName(*VarType, Var.getName());
76 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
81 Printer << "unknown(" << LocType << ") ";
82 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
87 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
88 BuiltinDumper Dumper(Printer);
92 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
93 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
96 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
98 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
99 auto PointeeType = Symbol.getPointeeType();
103 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
104 FunctionDumper NestedDumper(Printer);
105 FunctionDumper::PointerType Pointer =
106 Symbol.isReference() ? FunctionDumper::PointerType::Reference
107 : FunctionDumper::PointerType::Pointer;
108 NestedDumper.start(*Func, Pointer);
110 if (Symbol.isConstType())
111 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
112 if (Symbol.isVolatileType())
113 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
114 PointeeType->dump(*this);
115 Printer << (Symbol.isReference() ? "&" : "*");
119 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
120 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
121 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
124 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
125 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
128 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
130 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
131 std::string IndexSpec;
132 raw_string_ostream IndexStream(IndexSpec);
133 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
134 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
136 IndexStream << NestedArray->getCount();
138 ElementType = NestedArray->getElementType();
140 IndexStream << "[" << ArrayType->getCount() << "]";
141 ElementType->dump(*this);
142 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
143 Printer << IndexStream.str();
145 if (!tryDumpFunctionPointer(Type, Name)) {
147 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
152 bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
154 // Function pointers come across as pointers to function signatures. But the
155 // signature carries no name, so we have to handle this case separately.
156 if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
157 auto PointeeType = PointerType->getPointeeType();
158 if (auto *FunctionSig =
159 dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
160 FunctionDumper Dumper(Printer);
161 FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
162 if (PointerType->isReference())
163 PT = FunctionDumper::PointerType::Reference;
164 std::string NameStr(Name.begin(), Name.end());
165 Dumper.start(*FunctionSig, NameStr.c_str(), PT);