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 (Var.isCompilerGenerated() && opts::ExcludeCompilerGenerated)
36 if (Printer.IsSymbolExcluded(Var.getName()))
42 auto VarType = Var.getType();
44 switch (auto LocType = Var.getLocationType()) {
45 case PDB_LocType::Static:
47 WithColor(Printer, PDB_ColorItem::Address).get()
48 << format_hex(Var.getRelativeVirtualAddress(), 10);
50 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
51 dumpSymbolTypeAndName(*VarType, Var.getName());
53 case PDB_LocType::Constant:
54 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
55 dumpSymbolTypeAndName(*VarType, Var.getName());
57 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
59 case PDB_LocType::ThisRel:
60 WithColor(Printer, PDB_ColorItem::Offset).get()
61 << "+" << format_hex(Var.getOffset(), 4) << " ";
62 dumpSymbolTypeAndName(*VarType, Var.getName());
64 case PDB_LocType::BitField:
65 WithColor(Printer, PDB_ColorItem::Offset).get()
66 << "+" << format_hex(Var.getOffset(), 4) << " ";
67 dumpSymbolTypeAndName(*VarType, Var.getName());
69 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
72 Printer << "unknown(" << LocType << ") ";
73 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
78 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
79 BuiltinDumper Dumper(Printer);
83 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
84 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
87 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
89 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
90 auto PointeeType = Symbol.getPointeeType();
94 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
95 FunctionDumper NestedDumper(Printer);
96 FunctionDumper::PointerType Pointer =
97 Symbol.isReference() ? FunctionDumper::PointerType::Reference
98 : FunctionDumper::PointerType::Pointer;
99 NestedDumper.start(*Func, Pointer);
101 if (Symbol.isConstType())
102 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
103 if (Symbol.isVolatileType())
104 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
105 PointeeType->dump(*this);
106 Printer << (Symbol.isReference() ? "&" : "*");
110 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
111 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
112 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
115 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
116 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
119 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
121 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
122 std::string IndexSpec;
123 raw_string_ostream IndexStream(IndexSpec);
124 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
125 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
127 IndexStream << NestedArray->getCount();
129 ElementType = NestedArray->getElementType();
131 IndexStream << "[" << ArrayType->getCount() << "]";
132 ElementType->dump(*this);
133 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
134 Printer << IndexStream.str();
136 if (!tryDumpFunctionPointer(Type, Name)) {
138 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
143 bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
145 // Function pointers come across as pointers to function signatures. But the
146 // signature carries no name, so we have to handle this case separately.
147 if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
148 auto PointeeType = PointerType->getPointeeType();
149 if (auto *FunctionSig =
150 dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
151 FunctionDumper Dumper(Printer);
152 FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
153 if (PointerType->isReference())
154 PT = FunctionDumper::PointerType::Reference;
155 std::string NameStr(Name.begin(), Name.end());
156 Dumper.start(*FunctionSig, NameStr.c_str(), PT);