blob: 913cfee6622313578e067470a16ce7e084488df4 [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;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000048 case PDB_LocType::ThisRel:
Zachary Turner29c69102015-02-23 05:58:34 +000049 OS << "+" << format_hex(Var.getOffset(), 4) << " ";
Zachary Turner29c69102015-02-23 05:58:34 +000050 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
51 break;
Zachary Turner29c69102015-02-23 05:58:34 +000052 default:
53 break;
54 OS << "unknown(" << LocType << ") " << Var.getName();
55 }
56}
57
58void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
59 int Indent) {
60 OS << Symbol.getBuiltinType();
61}
62
63void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
64 int Indent) {
65 OS << Symbol.getName();
66}
67
68void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
69 raw_ostream &OS, int Indent) {}
70
71void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
72 int Indent) {
Zachary Turner29c69102015-02-23 05:58:34 +000073 auto PointeeType = Symbol.getPointeeType();
74 if (!PointeeType)
75 return;
76
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);
83 } else {
84 if (Symbol.isConstType())
85 OS << "const ";
86 if (Symbol.isVolatileType())
87 OS << "volatile ";
88 PointeeType->dump(OS, Indent, *this);
89 OS << (Symbol.isReference() ? "&" : "*");
90 }
91}
92
93void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
94 int Indent) {
95 OS << "typedef " << Symbol.getName();
96}
97
98void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
99 int Indent) {
100 OS << Symbol.getName();
101}
102
103void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
104 StringRef Name, raw_ostream &OS) {
105 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
Zachary Turner29c69102015-02-23 05:58:34 +0000106 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();
112 }
113 IndexStream << "[" << ArrayType->getCount() << "]";
114 ElementType->dump(OS, 0, *this);
115 OS << " " << Name << IndexStream.str();
116 } else {
117 Type.dump(OS, 0, *this);
118 OS << " " << Name;
119 }
120}