blob: d520fd10099df4cc61fccdb7a8c64171c6260943 [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 Turnerf5abda22015-03-01 06:49:49 +000034 if (Printer.IsSymbolExcluded(Var.getName()))
35 return;
36
Zachary Turner2d11c202015-02-27 09:15:59 +000037 Printer.NewLine();
38 Printer << "data ";
Zachary Turner29c69102015-02-23 05:58:34 +000039
40 auto VarType = Var.getType();
41
42 switch (auto LocType = Var.getLocationType()) {
43 case PDB_LocType::Static:
Zachary Turner2d11c202015-02-27 09:15:59 +000044 WithColor(Printer, PDB_ColorItem::Address).get()
45 << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
46 WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000047 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000048 break;
49 case PDB_LocType::Constant:
Zachary Turner2d11c202015-02-27 09:15:59 +000050 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000051 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner2d11c202015-02-27 09:15:59 +000052 Printer << "[";
53 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
54 Printer << "]";
Zachary Turner29c69102015-02-23 05:58:34 +000055 break;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000056 case PDB_LocType::ThisRel:
Zachary Turner2d11c202015-02-27 09:15:59 +000057 WithColor(Printer, PDB_ColorItem::Offset).get()
58 << "+" << format_hex(Var.getOffset(), 4) << " ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000059 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000060 break;
Zachary Turner29c69102015-02-23 05:58:34 +000061 default:
Zachary Turnerb52d08d2015-03-01 06:51:29 +000062 Printer << "unknown(" << LocType << ") ";
Zachary Turner2d11c202015-02-27 09:15:59 +000063 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000064 break;
Zachary Turner29c69102015-02-23 05:58:34 +000065 }
66}
67
Zachary Turnerb52d08d2015-03-01 06:51:29 +000068void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +000069 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +000070 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +000071}
72
Zachary Turnerb52d08d2015-03-01 06:51:29 +000073void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +000074 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000075}
76
Zachary Turnerb52d08d2015-03-01 06:51:29 +000077void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
Zachary Turner29c69102015-02-23 05:58:34 +000078
Zachary Turnerb52d08d2015-03-01 06:51:29 +000079void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +000080 auto PointeeType = Symbol.getPointeeType();
81 if (!PointeeType)
82 return;
83
84 if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +000085 FunctionDumper NestedDumper(Printer);
Zachary Turner29c69102015-02-23 05:58:34 +000086 FunctionDumper::PointerType Pointer =
87 Symbol.isReference() ? FunctionDumper::PointerType::Reference
88 : FunctionDumper::PointerType::Pointer;
Zachary Turnerb52d08d2015-03-01 06:51:29 +000089 NestedDumper.start(*Func, Pointer);
Zachary Turner29c69102015-02-23 05:58:34 +000090 } else {
91 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +000092 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner29c69102015-02-23 05:58:34 +000093 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +000094 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000095 PointeeType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +000096 Printer << (Symbol.isReference() ? "&" : "*");
Zachary Turner29c69102015-02-23 05:58:34 +000097 }
98}
99
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000100void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000101 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
102 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000103}
104
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000105void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000106 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000107}
108
109void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000110 StringRef Name) {
Zachary Turner29c69102015-02-23 05:58:34 +0000111 if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
Zachary Turner29c69102015-02-23 05:58:34 +0000112 std::string IndexSpec;
113 raw_string_ostream IndexStream(IndexSpec);
114 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
115 while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000116 IndexStream << "[";
117 IndexStream << NestedArray->getCount();
118 IndexStream << "]";
Zachary Turner29c69102015-02-23 05:58:34 +0000119 ElementType = NestedArray->getElementType();
120 }
121 IndexStream << "[" << ArrayType->getCount() << "]";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000122 ElementType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000123 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
124 Printer << IndexStream.str();
Zachary Turner29c69102015-02-23 05:58:34 +0000125 } else {
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000126 if (!tryDumpFunctionPointer(Type, Name)) {
127 Type.dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000128 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
Zachary Turnerd270d222015-02-26 23:49:23 +0000129 }
Zachary Turner29c69102015-02-23 05:58:34 +0000130 }
131}
Zachary Turnerd270d222015-02-26 23:49:23 +0000132
133bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000134 StringRef Name) {
Zachary Turnerd270d222015-02-26 23:49:23 +0000135 // Function pointers come across as pointers to function signatures. But the
136 // signature carries no name, so we have to handle this case separately.
137 if (auto *PointerType = dyn_cast<PDBSymbolTypePointer>(&Type)) {
138 auto PointeeType = PointerType->getPointeeType();
139 if (auto *FunctionSig =
140 dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000141 FunctionDumper Dumper(Printer);
Zachary Turnerd270d222015-02-26 23:49:23 +0000142 FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
143 if (PointerType->isReference())
144 PT = FunctionDumper::PointerType::Reference;
145 std::string NameStr(Name.begin(), Name.end());
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000146 Dumper.start(*FunctionSig, NameStr.c_str(), PT);
Zachary Turnerd270d222015-02-26 23:49:23 +0000147 return true;
148 }
149 }
150 return false;
151}