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 int Offset = Var.getOffset();
50 OS << "+" << format_hex(Var.getOffset(), 4) << " ";
52 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
57 OS << "unknown(" << LocType << ") " << Var.getName();
61 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
63 OS << Symbol.getBuiltinType();
66 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
68 OS << Symbol.getName();
71 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
72 raw_ostream &OS, int Indent) {}
74 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
76 uint32_t PointeeId = Symbol.getTypeId();
77 auto PointeeType = Symbol.getPointeeType();
81 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
82 FunctionDumper NestedDumper;
83 FunctionDumper::PointerType Pointer =
84 Symbol.isReference() ? FunctionDumper::PointerType::Reference
85 : FunctionDumper::PointerType::Pointer;
86 NestedDumper.start(*Func, Pointer, OS, Indent);
88 if (Symbol.isConstType())
90 if (Symbol.isVolatileType())
92 PointeeType->dump(OS, Indent, *this);
93 OS << (Symbol.isReference() ? "&" : "*");
97 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
99 OS << "typedef " << Symbol.getName();
102 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
104 OS << Symbol.getName();
107 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
108 StringRef Name, raw_ostream &OS) {
109 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
111 std::string IndexSpec;
112 raw_string_ostream IndexStream(IndexSpec);
113 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
114 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
115 IndexStream << "[" << NestedArray->getCount() << "]";
116 ElementType = NestedArray->getElementType();
118 IndexStream << "[" << ArrayType->getCount() << "]";
119 ElementType->dump(OS, 0, *this);
120 OS << " " << Name << IndexStream.str();
122 Type.dump(OS, 0, *this);