blob: ec72d16fef9aff925ce42f24a8f8facfdf9b18e7 [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
Zachary Turnerd270d222015-02-26 23:49:23 +000012#include "BuiltinDumper.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000013#include "LinePrinter.h"
Zachary Turner29c69102015-02-23 05:58:34 +000014#include "llvm-pdbdump.h"
15#include "FunctionDumper.h"
16
17#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
18#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
19#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
Zachary Turnerd270d222015-02-26 23:49:23 +000020#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
Zachary Turner29c69102015-02-23 05:58:34 +000021#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"
25
26#include "llvm/Support/Format.h"
27
28using namespace llvm;
29
Zachary Turner2d11c202015-02-27 09:15:59 +000030VariableDumper::VariableDumper(LinePrinter &P)
31 : PDBSymDumper(true), Printer(P) {}
Zachary Turner29c69102015-02-23 05:58:34 +000032
33void VariableDumper::start(const PDBSymbolData &Var, raw_ostream &OS,
34 int Indent) {
Zachary Turnerf5abda22015-03-01 06:49:49 +000035 if (Printer.IsSymbolExcluded(Var.getName()))
36 return;
37
Zachary Turner2d11c202015-02-27 09:15:59 +000038 Printer.NewLine();
39 Printer << "data ";
Zachary Turner29c69102015-02-23 05:58:34 +000040
41 auto VarType = Var.getType();
42
43 switch (auto LocType = Var.getLocationType()) {
44 case PDB_LocType::Static:
Zachary Turner2d11c202015-02-27 09:15:59 +000045 WithColor(Printer, PDB_ColorItem::Address).get()
46 << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
47 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
Zachary Turner29c69102015-02-23 05:58:34 +000048 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
49 break;
50 case PDB_LocType::Constant:
Zachary Turner2d11c202015-02-27 09:15:59 +000051 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner29c69102015-02-23 05:58:34 +000052 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
Zachary Turner2d11c202015-02-27 09:15:59 +000053 Printer << "[";
54 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
55 Printer << "]";
Zachary Turner29c69102015-02-23 05:58:34 +000056 break;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000057 case PDB_LocType::ThisRel:
Zachary Turner2d11c202015-02-27 09:15:59 +000058 WithColor(Printer, PDB_ColorItem::Offset).get()
59 << "+" << format_hex(Var.getOffset(), 4) << " ";
Zachary Turner29c69102015-02-23 05:58:34 +000060 dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
61 break;
Zachary Turner29c69102015-02-23 05:58:34 +000062 default:
Zachary Turner2d11c202015-02-27 09:15:59 +000063 OS << "unknown(" << LocType << ") ";
64 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000065 break;
Zachary Turner29c69102015-02-23 05:58:34 +000066 }
67}
68
69void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
70 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +000071 BuiltinDumper Dumper(Printer);
Zachary Turnerd270d222015-02-26 23:49:23 +000072 Dumper.start(Symbol, OS);
Zachary Turner29c69102015-02-23 05:58:34 +000073}
74
75void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
76 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +000077 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000078}
79
80void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
81 raw_ostream &OS, int Indent) {}
82
83void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
84 int Indent) {
Zachary Turner29c69102015-02-23 05:58:34 +000085 auto PointeeType = Symbol.getPointeeType();
86 if (!PointeeType)
87 return;
88
89 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +000090 FunctionDumper NestedDumper(Printer);
Zachary Turner29c69102015-02-23 05:58:34 +000091 FunctionDumper::PointerType Pointer =
92 Symbol.isReference() ? FunctionDumper::PointerType::Reference
93 : FunctionDumper::PointerType::Pointer;
94 NestedDumper.start(*Func, Pointer, OS, Indent);
95 } else {
96 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +000097 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner29c69102015-02-23 05:58:34 +000098 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +000099 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turner29c69102015-02-23 05:58:34 +0000100 PointeeType->dump(OS, Indent, *this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000101 Printer << (Symbol.isReference() ? "&" : "*");
Zachary Turner29c69102015-02-23 05:58:34 +0000102 }
103}
104
105void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
106 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000107 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
108 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000109}
110
111void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
112 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000113 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000114}
115
116void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
117 StringRef Name, raw_ostream &OS) {
118 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
Zachary Turner29c69102015-02-23 05:58:34 +0000119 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())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000123 IndexStream << "[";
124 IndexStream << NestedArray->getCount();
125 IndexStream << "]";
Zachary Turner29c69102015-02-23 05:58:34 +0000126 ElementType = NestedArray->getElementType();
127 }
128 IndexStream << "[" << ArrayType->getCount() << "]";
129 ElementType->dump(OS, 0, *this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000130 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
131 Printer << IndexStream.str();
Zachary Turner29c69102015-02-23 05:58:34 +0000132 } else {
Zachary Turnerd270d222015-02-26 23:49:23 +0000133 if (!tryDumpFunctionPointer(Type, Name, OS)) {
134 Type.dump(OS, 0, *this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000135 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
Zachary Turnerd270d222015-02-26 23:49:23 +0000136 }
Zachary Turner29c69102015-02-23 05:58:34 +0000137 }
138}
Zachary Turnerd270d222015-02-26 23:49:23 +0000139
140bool 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())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000148 FunctionDumper Dumper(Printer);
Zachary Turnerd270d222015-02-26 23:49:23 +0000149 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);
154 return true;
155 }
156 }
157 return false;
158}