blob: c565fcf0dd8aa3580ef2fca835e4f1a07ab68586 [file] [log] [blame]
Zachary Turnera9054dd2017-01-11 00:35:43 +00001//===- PrettyVariableDumper.cpp ---------------------------------*- C++ -*-===//
Zachary Turner29c69102015-02-23 05:58:34 +00002//
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
Zachary Turnera9054dd2017-01-11 00:35:43 +000010#include "PrettyVariableDumper.h"
Zachary Turner29c69102015-02-23 05:58:34 +000011
Zachary Turner2d11c202015-02-27 09:15:59 +000012#include "LinePrinter.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000013#include "PrettyBuiltinDumper.h"
14#include "PrettyFunctionDumper.h"
Zachary Turner29c69102015-02-23 05:58:34 +000015#include "llvm-pdbdump.h"
Zachary Turner29c69102015-02-23 05:58:34 +000016
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 Turnera9054dd2017-01-11 00:35:43 +000021#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Zachary Turnerd270d222015-02-26 23:49:23 +000022#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
Zachary Turner29c69102015-02-23 05:58:34 +000023#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
24#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
Zachary Turner29c69102015-02-23 05:58:34 +000025#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
26
27#include "llvm/Support/Format.h"
28
29using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000030using namespace llvm::pdb;
Zachary Turner29c69102015-02-23 05:58:34 +000031
Zachary Turner2d11c202015-02-27 09:15:59 +000032VariableDumper::VariableDumper(LinePrinter &P)
33 : PDBSymDumper(true), Printer(P) {}
Zachary Turner29c69102015-02-23 05:58:34 +000034
Zachary Turnerb52d08d2015-03-01 06:51:29 +000035void VariableDumper::start(const PDBSymbolData &Var) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +000036 if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
Zachary Turner7797c722015-03-02 04:39:56 +000037 return;
Zachary Turnerf5abda22015-03-01 06:49:49 +000038 if (Printer.IsSymbolExcluded(Var.getName()))
39 return;
40
Zachary Turner29c69102015-02-23 05:58:34 +000041 auto VarType = Var.getType();
42
43 switch (auto LocType = Var.getLocationType()) {
44 case PDB_LocType::Static:
Zachary Turner65323652015-03-04 06:09:53 +000045 Printer.NewLine();
46 Printer << "data [";
Zachary Turner2d11c202015-02-27 09:15:59 +000047 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turnere5cb2692015-05-01 20:24:26 +000048 << format_hex(Var.getVirtualAddress(), 10);
Zachary Turner7797c722015-03-02 04:39:56 +000049 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 Turner65323652015-03-04 06:09:53 +000054 if (isa<PDBSymbolTypeEnum>(*VarType))
55 break;
56 Printer.NewLine();
57 Printer << "data ";
Zachary Turner2d11c202015-02-27 09:15:59 +000058 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000059 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner7797c722015-03-02 04:39:56 +000060 Printer << " = ";
Zachary Turner2d11c202015-02-27 09:15:59 +000061 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
Zachary Turner29c69102015-02-23 05:58:34 +000062 break;
Benjamin Kramerd0be1702015-02-23 11:33:54 +000063 case PDB_LocType::ThisRel:
Zachary Turner65323652015-03-04 06:09:53 +000064 Printer.NewLine();
65 Printer << "data ";
Zachary Turner2d11c202015-02-27 09:15:59 +000066 WithColor(Printer, PDB_ColorItem::Offset).get()
67 << "+" << format_hex(Var.getOffset(), 4) << " ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000068 dumpSymbolTypeAndName(*VarType, Var.getName());
Zachary Turner29c69102015-02-23 05:58:34 +000069 break;
Zachary Turner7797c722015-03-02 04:39:56 +000070 case PDB_LocType::BitField:
Zachary Turner65323652015-03-04 06:09:53 +000071 Printer.NewLine();
72 Printer << "data ";
Zachary Turner7797c722015-03-02 04:39:56 +000073 WithColor(Printer, PDB_ColorItem::Offset).get()
74 << "+" << format_hex(Var.getOffset(), 4) << " ";
75 dumpSymbolTypeAndName(*VarType, Var.getName());
76 Printer << " : ";
77 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
78 break;
Zachary Turner29c69102015-02-23 05:58:34 +000079 default:
Zachary Turner65323652015-03-04 06:09:53 +000080 Printer.NewLine();
81 Printer << "data ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +000082 Printer << "unknown(" << LocType << ") ";
Zachary Turner2d11c202015-02-27 09:15:59 +000083 WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000084 break;
Zachary Turner29c69102015-02-23 05:58:34 +000085 }
86}
87
Zachary Turnerb52d08d2015-03-01 06:51:29 +000088void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +000089 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +000090 Dumper.start(Symbol);
Zachary Turner29c69102015-02-23 05:58:34 +000091}
92
Zachary Turnerb52d08d2015-03-01 06:51:29 +000093void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +000094 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +000095}
96
Zachary Turnerb52d08d2015-03-01 06:51:29 +000097void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
Zachary Turner29c69102015-02-23 05:58:34 +000098
Zachary Turnerb52d08d2015-03-01 06:51:29 +000099void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner29c69102015-02-23 05:58:34 +0000100 auto PointeeType = Symbol.getPointeeType();
101 if (!PointeeType)
102 return;
103
Zachary Turner1b1a70f2017-04-10 06:14:09 +0000104 if (auto Func = PointeeType->cast<PDBSymbolFunc>()) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000105 FunctionDumper NestedDumper(Printer);
Zachary Turner29c69102015-02-23 05:58:34 +0000106 FunctionDumper::PointerType Pointer =
107 Symbol.isReference() ? FunctionDumper::PointerType::Reference
108 : FunctionDumper::PointerType::Pointer;
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000109 NestedDumper.start(*Func, Pointer);
Zachary Turner29c69102015-02-23 05:58:34 +0000110 } else {
111 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000112 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner29c69102015-02-23 05:58:34 +0000113 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000114 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000115 PointeeType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000116 Printer << (Symbol.isReference() ? "&" : "*");
Zachary Turner29c69102015-02-23 05:58:34 +0000117 }
118}
119
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000120void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000121 WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
122 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000123}
124
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000125void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000126 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000127}
128
129void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000130 StringRef Name) {
Zachary Turner1b1a70f2017-04-10 06:14:09 +0000131 if (auto *ArrayType = Type.cast<PDBSymbolTypeArray>()) {
Zachary Turner29c69102015-02-23 05:58:34 +0000132 std::string IndexSpec;
133 raw_string_ostream IndexStream(IndexSpec);
134 std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
Zachary Turner1b1a70f2017-04-10 06:14:09 +0000135 while (auto NestedArray = ElementType->cast<PDBSymbolTypeArray>()) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000136 IndexStream << "[";
137 IndexStream << NestedArray->getCount();
138 IndexStream << "]";
Zachary Turner29c69102015-02-23 05:58:34 +0000139 ElementType = NestedArray->getElementType();
140 }
141 IndexStream << "[" << ArrayType->getCount() << "]";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000142 ElementType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000143 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
144 Printer << IndexStream.str();
Zachary Turner29c69102015-02-23 05:58:34 +0000145 } else {
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000146 if (!tryDumpFunctionPointer(Type, Name)) {
147 Type.dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000148 WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
Zachary Turnerd270d222015-02-26 23:49:23 +0000149 }
Zachary Turner29c69102015-02-23 05:58:34 +0000150 }
151}
Zachary Turnerd270d222015-02-26 23:49:23 +0000152
153bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000154 StringRef Name) {
Zachary Turnerd270d222015-02-26 23:49:23 +0000155 // Function pointers come across as pointers to function signatures. But the
156 // signature carries no name, so we have to handle this case separately.
Zachary Turner1b1a70f2017-04-10 06:14:09 +0000157 if (auto *PointerType = Type.cast<PDBSymbolTypePointer>()) {
Zachary Turnerd270d222015-02-26 23:49:23 +0000158 auto PointeeType = PointerType->getPointeeType();
Zachary Turner1b1a70f2017-04-10 06:14:09 +0000159 if (auto *FunctionSig = PointeeType->cast<PDBSymbolTypeFunctionSig>()) {
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}