OSDN Git Service

Update aosp/master LLVM for rebase to r230699.
[android-x86/external-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     OS << "+" << format_hex(Var.getOffset(), 4) << " ";
50     dumpSymbolTypeAndName(*VarType, Var.getName(), OS);
51     break;
52   default:
53     break;
54     OS << "unknown(" << LocType << ") " << Var.getName();
55   }
56 }
57
58 void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
59                           int Indent) {
60   OS << Symbol.getBuiltinType();
61 }
62
63 void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
64                           int Indent) {
65   OS << Symbol.getName();
66 }
67
68 void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol,
69                           raw_ostream &OS, int Indent) {}
70
71 void VariableDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
72                           int Indent) {
73   auto PointeeType = Symbol.getPointeeType();
74   if (!PointeeType)
75     return;
76
77   if (auto Func = dyn_cast<PDBSymbolFunc>(PointeeType.get())) {
78     FunctionDumper NestedDumper;
79     FunctionDumper::PointerType Pointer =
80         Symbol.isReference() ? FunctionDumper::PointerType::Reference
81                              : FunctionDumper::PointerType::Pointer;
82     NestedDumper.start(*Func, Pointer, OS, Indent);
83   } else {
84     if (Symbol.isConstType())
85       OS << "const ";
86     if (Symbol.isVolatileType())
87       OS << "volatile ";
88     PointeeType->dump(OS, Indent, *this);
89     OS << (Symbol.isReference() ? "&" : "*");
90   }
91 }
92
93 void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
94                           int Indent) {
95   OS << "typedef " << Symbol.getName();
96 }
97
98 void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
99                           int Indent) {
100   OS << Symbol.getName();
101 }
102
103 void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
104                                            StringRef Name, raw_ostream &OS) {
105   if (auto *ArrayType = dyn_cast<PDBSymbolTypeArray>(&Type)) {
106     std::string IndexSpec;
107     raw_string_ostream IndexStream(IndexSpec);
108     std::unique_ptr<PDBSymbol> ElementType = ArrayType->getElementType();
109     while (auto NestedArray = dyn_cast<PDBSymbolTypeArray>(ElementType.get())) {
110       IndexStream << "[" << NestedArray->getCount() << "]";
111       ElementType = NestedArray->getElementType();
112     }
113     IndexStream << "[" << ArrayType->getCount() << "]";
114     ElementType->dump(OS, 0, *this);
115     OS << " " << Name << IndexStream.str();
116   } else {
117     Type.dump(OS, 0, *this);
118     OS << " " << Name;
119   }
120 }