[llvm-pdbdump] Many minor fixes and improvements
[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 (Var.isCompilerGenerated() && opts::ExcludeCompilerGenerated)
35     return;
36   if (Printer.IsSymbolExcluded(Var.getName()))
37     return;
38
39   Printer.NewLine();
40   Printer << "data ";
41
42   auto VarType = Var.getType();
43
44   switch (auto LocType = Var.getLocationType()) {
45   case PDB_LocType::Static:
46     Printer << "[";
47     WithColor(Printer, PDB_ColorItem::Address).get()
48         << format_hex(Var.getRelativeVirtualAddress(), 10);
49     Printer << "] ";
50     WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
51     dumpSymbolTypeAndName(*VarType, Var.getName());
52     break;
53   case PDB_LocType::Constant:
54     WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
55     dumpSymbolTypeAndName(*VarType, Var.getName());
56     Printer << " = ";
57     WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
58     break;
59   case PDB_LocType::ThisRel:
60     WithColor(Printer, PDB_ColorItem::Offset).get()
61         << "+" << format_hex(Var.getOffset(), 4) << " ";
62     dumpSymbolTypeAndName(*VarType, Var.getName());
63     break;
64   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;
71   default:
72     Printer << "unknown(" << LocType << ") ";
73     WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
74     break;
75   }
76 }
77
78 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
79   BuiltinDumper Dumper(Printer);
80   Dumper.start(Symbol);
81 }
82
83 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
84   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
85 }
86
87 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {}
88
89 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
90   auto PointeeType = Symbol.getPointeeType();
91   if (!PointeeType)
92     return;
93
94   if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
95     FunctionDumper NestedDumper(Printer);
96     FunctionDumper::PointerType Pointer =
97         Symbol.isReference() ? FunctionDumper::PointerType::Reference
98                              : FunctionDumper::PointerType::Pointer;
99     NestedDumper.start(*Func, Pointer);
100   } else {
101     if (Symbol.isConstType())
102       WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
103     if (Symbol.isVolatileType())
104       WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
105     PointeeType->dump(*this);
106     Printer << (Symbol.isReference() ? "&" : "*");
107   }
108 }
109
110 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
111   WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
112   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
113 }
114
115 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
116   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
117 }
118
119 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
120                                            StringRef Name) {
121   if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
122     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())) {
126       IndexStream << "[";
127       IndexStream << NestedArray->getCount();
128       IndexStream << "]";
129       ElementType = NestedArray->getElementType();
130     }
131     IndexStream << "[" << ArrayType->getCount() << "]";
132     ElementType->dump(*this);
133     WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
134     Printer << IndexStream.str();
135   } else {
136     if (!tryDumpFunctionPointer(Type, Name)) {
137       Type.dump(*this);
138       WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
139     }
140   }
141 }
142
143 bool VariableDumper::tryDumpFunctionPointer(const PDBSymbol &Type,
144                                             StringRef Name) {
145   // 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())) {
151       FunctionDumper Dumper(Printer);
152       FunctionDumper::PointerType PT = FunctionDumper::PointerType::Pointer;
153       if (PointerType->isReference())
154         PT = FunctionDumper::PointerType::Reference;
155       std::string NameStr(Name.begin(), Name.end());
156       Dumper.start(*FunctionSig, NameStr.c_str(), PT);
157       return true;
158     }
159   }
160   return false;
161 }