blob: 030610c8c58c23b0828451d613934b0014a652cb [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 Turner65323652015-03-04 06:09:53 +000020#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Zachary Turnerd270d222015-02-26 23:49:23 +000021#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
Zachary Turner29c69102015-02-23 05:58:34 +000022#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
23#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
24#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
25#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
26
27#include "llvm/Support/Format.h"
28
29using namespace llvm;
30
Zachary Turner2d11c202015-02-27 09:15:59 +000031VariableDumper::VariableDumper(LinePrinter &P)
32 : PDBSymDumper(true), Printer(P) {}
Zachary Turner29c69102015-02-23 05:58:34 +000033
Zachary Turnerb52d08d2015-03-01 06:51:29 +000034void VariableDumper::start(const PDBSymbolData &Var) {
Zachary Turner7797c722015-03-02 04:39:56 +000035 if (Var.isCompilerGenerated() && opts::ExcludeCompilerGenerated)
36 return;
Zachary Turnerf5abda22015-03-01 06:49:49 +000037 if (Printer.IsSymbolExcluded(Var.getName()))
38 return;
39
Zachary Turner29c69102015-02-23 05:58:34 +000040 auto VarType = Var.getType();
41
42 switch (auto LocType = Var.getLocationType()) {
43 case PDB_LocType::Static:
Zachary Turner65323652015-03-04 06:09:53 +000044 Printer.NewLine();
45 Printer << "data [";
Zachary Turner2d11c202015-02-27 09:15:59 +000046 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turner7797c722015-03-02 04:39:56 +000047 << format_hex(Var.getRelativeVirtualAddress(), 10);
48 Printer << "] ";
Zachary Turner2d11c202015-02-27 09:15:59 +000049 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000050 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000051 break;
52 case PDB_LocType::Constant:
Zachary Turner65323652015-03-04 06:09:53 +000053 if (isa<PDBSymbolTypeEnum>(*VarType))
54 break;
55 Printer.NewLine();
56 Printer << "data ";
Zachary Turner2d11c202015-02-27 09:15:59 +000057 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000058 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner7797c722015-03-02 04:39:56 +000059 Printer << " = ";
Zachary Turner2d11c202015-02-27 09:15:59 +000060 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
Zachary Turner29c69102015-02-23 05:58:34 +000061 break;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000062 case PDB_LocType::ThisRel:
Zachary Turner65323652015-03-04 06:09:53 +000063 Printer.NewLine();
64 Printer << "data ";
Zachary Turner2d11c202015-02-27 09:15:59 +000065 WithColor(Printer, PDB_ColorItem::Offset).get()
66 << "+" << format_hex(Var.getOffset(), 4) << " ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000067 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000068 break;
Zachary Turner7797c722015-03-02 04:39:56 +000069 case PDB_LocType::BitField:
Zachary Turner65323652015-03-04 06:09:53 +000070 Printer.NewLine();
71 Printer << "data ";
Zachary Turner7797c722015-03-02 04:39:56 +000072 WithColor(Printer, PDB_ColorItem::Offset).get()
73 << "+" << format_hex(Var.getOffset(), 4) << " ";
74 dumpSymbolTypeAndName(*VarType, Var.getName());
75 Printer << " : ";
76 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
77 break;
Zachary Turner29c69102015-02-23 05:58:34 +000078 default:
Zachary Turner65323652015-03-04 06:09:53 +000079 Printer.NewLine();
80 Printer << "data ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000081 Printer << "unknown(" << LocType << ") ";
Zachary Turner2d11c202015-02-27 09:15:59 +000082 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000083 break;
Zachary Turner29c69102015-02-23 05:58:34 +000084 }
85}
86
Zachary Turnerb52d08d2015-03-01 06:51:29 +000087void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +000088 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +000089 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +000090}
91
Zachary Turnerb52d08d2015-03-01 06:51:29 +000092void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +000093 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000094}
95
Zachary Turnerb52d08d2015-03-01 06:51:29 +000096void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
Zachary Turner29c69102015-02-23 05:58:34 +000097
Zachary Turnerb52d08d2015-03-01 06:51:29 +000098void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +000099 auto PointeeType = Symbol.getPointeeType();
100 if (!PointeeType)
101 return;
102
103 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000104 FunctionDumper NestedDumper(Printer);
Zachary Turner29c69102015-02-23 05:58:34 +0000105 FunctionDumper::PointerType Pointer =
106 Symbol.isReference() ? FunctionDumper::PointerType::Reference
107 : FunctionDumper::PointerType::Pointer;
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000108 NestedDumper.start(*Func, Pointer);
Zachary Turner29c69102015-02-23 05:58:34 +0000109 } else {
110 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000111 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner29c69102015-02-23 05:58:34 +0000112 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000113 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000114 PointeeType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000115 Printer << (Symbol.isReference() ? "&" : "*");
Zachary Turner29c69102015-02-23 05:58:34 +0000116 }
117}
118
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000119void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000120 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
121 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000122}
123
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000124void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000125 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000126}
127
128void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000129 StringRef Name) {
Zachary Turner29c69102015-02-23 05:58:34 +0000130 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
Zachary Turner29c69102015-02-23 05:58:34 +0000131 std::string IndexSpec;
132 raw_string_ostream IndexStream(IndexSpec);
133 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
134 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000135 IndexStream << "[";
136 IndexStream << NestedArray->getCount();
137 IndexStream << "]";
Zachary Turner29c69102015-02-23 05:58:34 +0000138 ElementType = NestedArray->getElementType();
139 }
140 IndexStream << "[" << ArrayType->getCount() << "]";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000141 ElementType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000142 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
143 Printer << IndexStream.str();
Zachary Turner29c69102015-02-23 05:58:34 +0000144 } else {
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000145 if (!tryDumpFunctionPointer(Type, Name)) {
146 Type.dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000147 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
Zachary Turnerd270d222015-02-26 23:49:23 +0000148 }
Zachary Turner29c69102015-02-23 05:58:34 +0000149 }
150}
Zachary Turnerd270d222015-02-26 23:49:23 +0000151
152bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000153 StringRef Name) {
Zachary Turnerd270d222015-02-26 23:49:23 +0000154 // Function pointers come across as pointers to function signatures. But the
155 // signature carries no name, so we have to handle this case separately.
156 if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
157 auto PointeeType = PointerType->getPointeeType();
158 if (auto *FunctionSig =
159 dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000160 FunctionDumper Dumper(Printer);
Zachary Turnerd270d222015-02-26 23:49:23 +0000161 FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
162 if (PointerType->isReference())
163 PT = FunctionDumper::PointerType::Reference;
164 std::string NameStr(Name.begin(), Name.end());
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000165 Dumper.start(*FunctionSig, NameStr.c_str(), PT);
Zachary Turnerd270d222015-02-26 23:49:23 +0000166 return true;
167 }
168 }
169 return false;
170}