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 "llvm-pdbdump.h"
13 #include "FunctionDumper.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
16 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
24 #include "llvm/Support/Format.h"
28 VariableDumper::VariableDumper() : PDBSymDumper(true) {}
30 void VariableDumper::start(const PDBSymbolData &Var, raw_ostream &OS,
32 OS << newline(Indent);
35 auto VarType = Var.getType();
37 switch (auto LocType = Var.getLocationType()) {
38 case PDB_LocType::Static:
39 OS << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
41 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
43 case PDB_LocType::Constant:
45 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
46 OS << "[" << Var.getValue() << "]";
48 case PDB_LocType::ThisRel:
49 OS << "+" << format_hex(Var.getOffset(), 4) << " ";
50 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
54 OS << "unknown(" << LocType << ") " << Var.getName();
58 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
60 OS << Symbol.getBuiltinType();
63 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
65 OS << Symbol.getName();
68 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
69 raw_ostream &OS, int Indent) {}
71 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
73 auto PointeeType = Symbol.getPointeeType();
77 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
78 FunctionDumper NestedDumper;
79 FunctionDumper::PointerType Pointer =
80 Symbol.isReference() ? FunctionDumper::PointerType::Reference
81 : FunctionDumper::PointerType::Pointer;
82 NestedDumper.start(*Func, Pointer, OS, Indent);
84 if (Symbol.isConstType())
86 if (Symbol.isVolatileType())
88 PointeeType->dump(OS, Indent, *this);
89 OS << (Symbol.isReference() ? "&" : "*");
93 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
95 OS << "typedef " << Symbol.getName();
98 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
100 OS << Symbol.getName();
103 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
104 StringRef Name, raw_ostream &OS) {
105 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
106 std::string IndexSpec;
107 raw_string_ostream IndexStream(IndexSpec);
108 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
109 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
110 IndexStream << "[" << NestedArray->getCount() << "]";
111 ElementType = NestedArray->getElementType();
113 IndexStream << "[" << ArrayType->getCount() << "]";
114 ElementType->dump(OS, 0, *this);
115 OS << " " << Name << IndexStream.str();
117 Type.dump(OS, 0, *this);