blob: a46e1b0c4e9ca7071f1efdbbf5f21cebc565d866 [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
Zachary Turnerb52d08d2015-03-01 06:51:29 +000033void VariableDumper::start(const PDBSymbolData &Var) {
Zachary Turner7797c722015-03-02 04:39:56 +000034 if (Var.isCompilerGenerated() && opts::ExcludeCompilerGenerated)
35 return;
Zachary Turnerf5abda22015-03-01 06:49:49 +000036 if (Printer.IsSymbolExcluded(Var.getName()))
37 return;
38
Zachary Turner2d11c202015-02-27 09:15:59 +000039 Printer.NewLine();
40 Printer << "data ";
Zachary Turner29c69102015-02-23 05:58:34 +000041
42 auto VarType = Var.getType();
43
44 switch (auto LocType = Var.getLocationType()) {
45 case PDB_LocType::Static:
Zachary Turner7797c722015-03-02 04:39:56 +000046 Printer << "[";
Zachary Turner2d11c202015-02-27 09:15:59 +000047 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turner7797c722015-03-02 04:39:56 +000048 << format_hex(Var.getRelativeVirtualAddress(), 10);
49 Printer << "] ";
Zachary Turner2d11c202015-02-27 09:15:59 +000050 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000051 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000052 break;
53 case PDB_LocType::Constant:
Zachary Turner2d11c202015-02-27 09:15:59 +000054 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000055 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner7797c722015-03-02 04:39:56 +000056 Printer << " = ";
Zachary Turner2d11c202015-02-27 09:15:59 +000057 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
Zachary Turner29c69102015-02-23 05:58:34 +000058 break;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000059 case PDB_LocType::ThisRel:
Zachary Turner2d11c202015-02-27 09:15:59 +000060 WithColor(Printer, PDB_ColorItem::Offset).get()
61 << "+" << format_hex(Var.getOffset(), 4) << " ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000062 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000063 break;
Zachary Turner7797c722015-03-02 04:39:56 +000064 case PDB_LocType::BitField:
65 WithColor(Printer, PDB_ColorItem::Offset).get()
66 << "+" << format_hex(Var.getOffset(), 4) << " ";
67 dumpSymbolTypeAndName(*VarType, Var.getName());
68 Printer << " : ";
69 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
70 break;
Zachary Turner29c69102015-02-23 05:58:34 +000071 default:
Zachary Turnerb52d08d2015-03-01 06:51:29 +000072 Printer << "unknown(" << LocType << ") ";
Zachary Turner2d11c202015-02-27 09:15:59 +000073 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000074 break;
Zachary Turner29c69102015-02-23 05:58:34 +000075 }
76}
77
Zachary Turnerb52d08d2015-03-01 06:51:29 +000078void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +000079 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +000080 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +000081}
82
Zachary Turnerb52d08d2015-03-01 06:51:29 +000083void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +000084 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000085}
86
Zachary Turnerb52d08d2015-03-01 06:51:29 +000087void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
Zachary Turner29c69102015-02-23 05:58:34 +000088
Zachary Turnerb52d08d2015-03-01 06:51:29 +000089void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +000090 auto PointeeType = Symbol.getPointeeType();
91 if (!PointeeType)
92 return;
93
94 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +000095 FunctionDumper NestedDumper(Printer);
Zachary Turner29c69102015-02-23 05:58:34 +000096 FunctionDumper::PointerType Pointer =
97 Symbol.isReference() ? FunctionDumper::PointerType::Reference
98 : FunctionDumper::PointerType::Pointer;
Zachary Turnerb52d08d2015-03-01 06:51:29 +000099 NestedDumper.start(*Func, Pointer);
Zachary Turner29c69102015-02-23 05:58:34 +0000100 } else {
101 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000102 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner29c69102015-02-23 05:58:34 +0000103 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000104 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000105 PointeeType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000106 Printer << (Symbol.isReference() ? "&" : "*");
Zachary Turner29c69102015-02-23 05:58:34 +0000107 }
108}
109
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000110void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000111 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
112 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000113}
114
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000115void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000116 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000117}
118
119void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000120 StringRef Name) {
Zachary Turner29c69102015-02-23 05:58:34 +0000121 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
Zachary Turner29c69102015-02-23 05:58:34 +0000122 std::string IndexSpec;
123 raw_string_ostream IndexStream(IndexSpec);
124 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
125 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000126 IndexStream << "[";
127 IndexStream << NestedArray->getCount();
128 IndexStream << "]";
Zachary Turner29c69102015-02-23 05:58:34 +0000129 ElementType = NestedArray->getElementType();
130 }
131 IndexStream << "[" << ArrayType->getCount() << "]";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000132 ElementType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000133 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
134 Printer << IndexStream.str();
Zachary Turner29c69102015-02-23 05:58:34 +0000135 } else {
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000136 if (!tryDumpFunctionPointer(Type, Name)) {
137 Type.dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000138 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
Zachary Turnerd270d222015-02-26 23:49:23 +0000139 }
Zachary Turner29c69102015-02-23 05:58:34 +0000140 }
141}
Zachary Turnerd270d222015-02-26 23:49:23 +0000142
143bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000144 StringRef Name) {
Zachary Turnerd270d222015-02-26 23:49:23 +0000145 // Function pointers come across as pointers to function signatures. But the
146 // signature carries no name, so we have to handle this case separately.
147 if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
148 auto PointeeType = PointerType->getPointeeType();
149 if (auto *FunctionSig =
150 dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000151 FunctionDumper Dumper(Printer);
Zachary Turnerd270d222015-02-26 23:49:23 +0000152 FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
153 if (PointerType->isReference())
154 PT = FunctionDumper::PointerType::Reference;
155 std::string NameStr(Name.begin(), Name.end());
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000156 Dumper.start(*FunctionSig, NameStr.c_str(), PT);
Zachary Turnerd270d222015-02-26 23:49:23 +0000157 return true;
158 }
159 }
160 return false;
161}