blob: 31348ef1e1799c64df95c5f23f97ca41f2731407 [file] [log] [blame]
Zachary Turner29c69102015-02-23 05:58:34 +00001//===- VariableDumper.cpp - -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "VariableDumper.h"
11
12#include "llvm-pdbdump.h"
13#include "FunctionDumper.h"
14
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"
23
24#include "llvm/Support/Format.h"
25
26using namespace llvm;
27
28VariableDumper::VariableDumper() : PDBSymDumper(true) {}
29
30void VariableDumper::start(const PDBSymbolData &Var, raw_ostream &OS,
31 int Indent) {
32 OS << newline(Indent);
33 OS << "data ";
34
35 auto VarType = Var.getType();
36
37 switch (auto LocType = Var.getLocationType()) {
38 case PDB_LocType::Static:
39 OS << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
40 OS << "static ";
41 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
42 break;
43 case PDB_LocType::Constant:
44 OS << "const ";
45 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
46 OS << "[" << Var.getValue() << "]";
47 break;
48 case PDB_LocType::ThisRel: {
49 int Offset = Var.getOffset();
50 OS << "+" << format_hex(Var.getOffset(), 4) << " ";
Zachary Turner29c69102015-02-23 05:58:34 +000051 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
52 break;
53 }
54 default:
55 break;
56 OS << "unknown(" << LocType << ") " << Var.getName();
57 }
58}
59
60void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
61 int Indent) {
62 OS << Symbol.getBuiltinType();
63}
64
65void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
66 int Indent) {
67 OS << Symbol.getName();
68}
69
70void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
71 raw_ostream &OS, int Indent) {}
72
73void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
74 int Indent) {
75 uint32_t PointeeId = Symbol.getTypeId();
76 auto PointeeType = Symbol.getPointeeType();
77 if (!PointeeType)
78 return;
79
80 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
81 FunctionDumper NestedDumper;
82 FunctionDumper::PointerType Pointer =
83 Symbol.isReference() ? FunctionDumper::PointerType::Reference
84 : FunctionDumper::PointerType::Pointer;
85 NestedDumper.start(*Func, Pointer, OS, Indent);
86 } else {
87 if (Symbol.isConstType())
88 OS << "const ";
89 if (Symbol.isVolatileType())
90 OS << "volatile ";
91 PointeeType->dump(OS, Indent, *this);
92 OS << (Symbol.isReference() ? "&" : "*");
93 }
94}
95
96void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
97 int Indent) {
98 OS << "typedef " << Symbol.getName();
99}
100
101void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
102 int Indent) {
103 OS << Symbol.getName();
104}
105
106void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
107 StringRef Name, raw_ostream &OS) {
108 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
109 bool Done = false;
110 std::string IndexSpec;
111 raw_string_ostream IndexStream(IndexSpec);
112 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
113 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
114 IndexStream << "[" << NestedArray->getCount() << "]";
115 ElementType = NestedArray->getElementType();
116 }
117 IndexStream << "[" << ArrayType->getCount() << "]";
118 ElementType->dump(OS, 0, *this);
119 OS << " " << Name << IndexStream.str();
120 } else {
121 Type.dump(OS, 0, *this);
122 OS << " " << Name;
123 }
124}