d520fd10099df4cc61fccdb7a8c64171c6260943
[oota-llvm.git] / tools / llvm-pdbdump / VariableDumper.cpp
1 //===- 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
12 #include "BuiltinDumper.h"
13 #include "LinePrinter.h"
14 #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"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
21 #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
28 using namespace llvm;
29
30 VariableDumper::VariableDumper(LinePrinter &P)
31     : PDBSymDumper(true), Printer(P) {}
32
33 void VariableDumper::start(const PDBSymbolData &Var) {
34   if (Printer.IsSymbolExcluded(Var.getName()))
35     return;
36
37   Printer.NewLine();
38   Printer << "data ";
39
40   auto VarType = Var.getType();
41
42   switch (auto LocType = Var.getLocationType()) {
43   case PDB_LocType::Static:
44     WithColor(Printer, PDB_ColorItem::Address).get()
45         << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
46     WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
47     dumpSymbolTypeAndName(*VarType, Var.getName());
48     break;
49   case PDB_LocType::Constant:
50     WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
51     dumpSymbolTypeAndName(*VarType, Var.getName());
52     Printer << "[";
53     WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
54     Printer << "]";
55     break;
56   case PDB_LocType::ThisRel:
57     WithColor(Printer, PDB_ColorItem::Offset).get()
58         << "+" << format_hex(Var.getOffset(), 4) << " ";
59     dumpSymbolTypeAndName(*VarType, Var.getName());
60     break;
61   default:
62     Printer << "unknown(" << LocType << ") ";
63     WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
64     break;
65   }
66 }
67
68 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
69   BuiltinDumper Dumper(Printer);
70   Dumper.start(Symbol);
71 }
72
73 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
74   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
75 }
76
77 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
78
79 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
80   auto PointeeType = Symbol.getPointeeType();
81   if (!PointeeType)
82     return;
83
84   if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
85     FunctionDumper NestedDumper(Printer);
86     FunctionDumper::PointerType Pointer =
87         Symbol.isReference() ? FunctionDumper::PointerType::Reference
88                              : FunctionDumper::PointerType::Pointer;
89     NestedDumper.start(*Func, Pointer);
90   } else {
91     if (Symbol.isConstType())
92       WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
93     if (Symbol.isVolatileType())
94       WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
95     PointeeType->dump(*this);
96     Printer << (Symbol.isReference() ? "&" : "*");
97   }
98 }
99
100 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
101   WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
102   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
103 }
104
105 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
106   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
107 }
108
109 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
110                                            StringRef Name) {
111   if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
112     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())) {
116       IndexStream << "[";
117       IndexStream << NestedArray->getCount();
118       IndexStream << "]";
119       ElementType = NestedArray->getElementType();
120     }
121     IndexStream << "[" << ArrayType->getCount() << "]";
122     ElementType->dump(*this);
123     WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
124     Printer << IndexStream.str();
125   } else {
126     if (!tryDumpFunctionPointer(Type, Name)) {
127       Type.dump(*this);
128       WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
129     }
130   }
131 }
132
133 bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
134                                             StringRef Name) {
135   // 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())) {
141       FunctionDumper Dumper(Printer);
142       FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
143       if (PointerType->isReference())
144         PT = FunctionDumper::PointerType::Reference;
145       std::string NameStr(Name.begin(), Name.end());
146       Dumper.start(*FunctionSig, NameStr.c_str(), PT);
147       return true;
148     }
149   }
150   return false;
151 }