[llvm-pdbdump] Add an option to dump full class definitions.
[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 "llvm-pdbdump.h"
13 #include "FunctionDumper.h"
14
15 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
16 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
23
24 #include "llvm/Support/Format.h"
25
26 using namespace llvm;
27
28 VariableDumper::VariableDumper() : PDBSymDumper(true) {}
29
30 void VariableDumper::start(const PDBSymbolData &Var, raw_ostream &OS,
31                            int Indent) {
32   OS << newline(Indent);
33   OS << "data ";
34
35   auto VarType = Var.getType();
36
37   switch (auto LocType = Var.getLocationType()) {
38   case PDB_LocType::Static:
39     OS << "[" << format_hex(Var.getRelativeVirtualAddress(), 10) << "] ";
40     OS << "static ";
41     dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
42     break;
43   case PDB_LocType::Constant:
44     OS << "const ";
45     dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
46     OS << "[" << Var.getValue() << "]";
47     break;
48   case PDB_LocType::ThisRel: {
49     int Offset = Var.getOffset();
50     OS << "+" << format_hex(Var.getOffset(), 4) << " ";
51     OS.flush();
52     dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
53     break;
54   }
55   default:
56     break;
57     OS << "unknown(" << LocType << ") " << Var.getName();
58   }
59 }
60
61 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
62                           int Indent) {
63   OS << Symbol.getBuiltinType();
64 }
65
66 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
67                           int Indent) {
68   OS << Symbol.getName();
69 }
70
71 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
72                           raw_ostream &OS, int Indent) {}
73
74 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
75                           int Indent) {
76   uint32_t PointeeId = Symbol.getTypeId();
77   auto PointeeType = Symbol.getPointeeType();
78   if (!PointeeType)
79     return;
80
81   if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
82     FunctionDumper NestedDumper;
83     FunctionDumper::PointerType Pointer =
84         Symbol.isReference() ? FunctionDumper::PointerType::Reference
85                              : FunctionDumper::PointerType::Pointer;
86     NestedDumper.start(*Func, Pointer, OS, Indent);
87   } else {
88     if (Symbol.isConstType())
89       OS << "const ";
90     if (Symbol.isVolatileType())
91       OS << "volatile ";
92     PointeeType->dump(OS, Indent, *this);
93     OS << (Symbol.isReference() ? "&" : "*");
94   }
95 }
96
97 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
98                           int Indent) {
99   OS << "typedef " << Symbol.getName();
100 }
101
102 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
103                           int Indent) {
104   OS << Symbol.getName();
105 }
106
107 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
108                                            StringRef Name, raw_ostream &OS) {
109   if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
110     bool Done = false;
111     std::string IndexSpec;
112     raw_string_ostream IndexStream(IndexSpec);
113     std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
114     while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
115       IndexStream << "[" << NestedArray->getCount() << "]";
116       ElementType = NestedArray->getElementType();
117     }
118     IndexStream << "[" << ArrayType->getCount() << "]";
119     ElementType->dump(OS, 0, *this);
120     OS << " " << Name << IndexStream.str();
121   } else {
122     Type.dump(OS, 0, *this);
123     OS << " " << Name;
124   }
125 }