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, raw_ostream &OS,
35 if (Printer.IsSymbolExcluded(Var.getName()))
41 auto VarType = Var.getType();
43 switch (auto LocType = Var.getLocationType()) {
44 case PDB_LocType::Static:
45 WithColor(Printer, PDB_ColorItem::Address).get()
46 << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
47 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
48 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
50 case PDB_LocType::Constant:
51 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
52 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
54 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
57 case PDB_LocType::ThisRel:
58 WithColor(Printer, PDB_ColorItem::Offset).get()
59 << "+" << format_hex(Var.getOffset(), 4) << " ";
60 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
63 OS << "unknown(" << LocType << ") ";
64 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
69 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
71 BuiltinDumper Dumper(Printer);
72 Dumper.start(Symbol, OS);
75 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
77 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
80 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
81 raw_ostream &OS, int Indent) {}
83 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
85 auto PointeeType = Symbol.getPointeeType();
89 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
90 FunctionDumper NestedDumper(Printer);
91 FunctionDumper::PointerType Pointer =
92 Symbol.isReference() ? FunctionDumper::PointerType::Reference
93 : FunctionDumper::PointerType::Pointer;
94 NestedDumper.start(*Func, Pointer, OS, Indent);
96 if (Symbol.isConstType())
97 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
98 if (Symbol.isVolatileType())
99 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
100 PointeeType->dump(OS, Indent, *this);
101 Printer << (Symbol.isReference() ? "&" : "*");
105 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
107 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
108 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
111 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
113 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
116 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
117 StringRef Name, raw_ostream &OS) {
118 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
119 std::string IndexSpec;
120 raw_string_ostream IndexStream(IndexSpec);
121 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
122 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
124 IndexStream << NestedArray->getCount();
126 ElementType = NestedArray->getElementType();
128 IndexStream << "[" << ArrayType->getCount() << "]";
129 ElementType->dump(OS, 0, *this);
130 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
131 Printer << IndexStream.str();
133 if (!tryDumpFunctionPointer(Type, Name, OS)) {
134 Type.dump(OS, 0, *this);
135 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
140 bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
141 StringRef Name, raw_ostream &OS) {
142 // Function pointers come across as pointers to function signatures. But the
143 // signature carries no name, so we have to handle this case separately.
144 if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
145 auto PointeeType = PointerType->getPointeeType();
146 if (auto *FunctionSig =
147 dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
148 FunctionDumper Dumper(Printer);
149 FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
150 if (PointerType->isReference())
151 PT = FunctionDumper::PointerType::Reference;
152 std::string NameStr(Name.begin(), Name.end());
153 Dumper.start(*FunctionSig, NameStr.c_str(), PT, OS);