]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/llvm-pdbdump/PrettyClassDefinitionDumper.cpp
Vendor import of llvm release_40 branch r292009:
[FreeBSD/FreeBSD.git] / tools / llvm-pdbdump / PrettyClassDefinitionDumper.cpp
1 //===- PrettyClassDefinitionDumper.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 "PrettyClassDefinitionDumper.h"
11
12 #include "LinePrinter.h"
13 #include "PrettyEnumDumper.h"
14 #include "PrettyFunctionDumper.h"
15 #include "PrettyTypedefDumper.h"
16 #include "PrettyVariableDumper.h"
17 #include "llvm-pdbdump.h"
18
19 #include "llvm/DebugInfo/PDB/IPDBSession.h"
20 #include "llvm/DebugInfo/PDB/PDBExtras.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
28 #include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h"
29 #include "llvm/Support/Format.h"
30
31 using namespace llvm;
32 using namespace llvm::pdb;
33
34 ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
35     : PDBSymDumper(true), Printer(P) {}
36
37 void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
38   std::string Name = Class.getName();
39   WithColor(Printer, PDB_ColorItem::Keyword).get() << Class.getUdtKind() << " ";
40   WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
41
42   auto Bases = Class.findAllChildren<PDBSymbolTypeBaseClass>();
43   if (Bases->getChildCount() > 0) {
44     Printer.Indent();
45     Printer.NewLine();
46     Printer << ":";
47     uint32_t BaseIndex = 0;
48     while (auto Base = Bases->getNext()) {
49       Printer << " ";
50       WithColor(Printer, PDB_ColorItem::Keyword).get() << Base->getAccess();
51       if (Base->isVirtualBaseClass())
52         WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
53       WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base->getName();
54       if (++BaseIndex < Bases->getChildCount()) {
55         Printer.NewLine();
56         Printer << ",";
57       }
58     }
59     Printer.Unindent();
60   }
61
62   Printer << " {";
63   auto Children = Class.findAllChildren();
64   if (Children->getChildCount() == 0) {
65     Printer << "}";
66     return;
67   }
68
69   // Try to dump symbols organized by member access level.  Public members
70   // first, then protected, then private.  This might be slow, so it's worth
71   // reconsidering the value of this if performance of large PDBs is a problem.
72   // NOTE: Access level of nested types is not recorded in the PDB, so we have
73   // a special case for them.
74   SymbolGroupByAccess Groups;
75   Groups.insert(std::make_pair(0, SymbolGroup()));
76   Groups.insert(std::make_pair((int)PDB_MemberAccess::Private, SymbolGroup()));
77   Groups.insert(
78       std::make_pair((int)PDB_MemberAccess::Protected, SymbolGroup()));
79   Groups.insert(std::make_pair((int)PDB_MemberAccess::Public, SymbolGroup()));
80
81   while (auto Child = Children->getNext()) {
82     PDB_MemberAccess Access = Child->getRawSymbol().getAccess();
83     if (isa<PDBSymbolTypeBaseClass>(*Child))
84       continue;
85
86     auto &AccessGroup = Groups.find((int)Access)->second;
87
88     if (auto Func = dyn_cast<PDBSymbolFunc>(Child.get())) {
89       if (Func->isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
90         continue;
91       if (Func->getLength() == 0 && !Func->isPureVirtual() &&
92           !Func->isIntroVirtualFunction())
93         continue;
94       Child.release();
95       AccessGroup.Functions.push_back(std::unique_ptr<PDBSymbolFunc>(Func));
96     } else if (auto Data = dyn_cast<PDBSymbolData>(Child.get())) {
97       Child.release();
98       AccessGroup.Data.push_back(std::unique_ptr<PDBSymbolData>(Data));
99     } else {
100       AccessGroup.Unknown.push_back(std::move(Child));
101     }
102   }
103
104   int Count = 0;
105   Count += dumpAccessGroup((PDB_MemberAccess)0, Groups[0]);
106   Count += dumpAccessGroup(PDB_MemberAccess::Public,
107                            Groups[(int)PDB_MemberAccess::Public]);
108   Count += dumpAccessGroup(PDB_MemberAccess::Protected,
109                            Groups[(int)PDB_MemberAccess::Protected]);
110   Count += dumpAccessGroup(PDB_MemberAccess::Private,
111                            Groups[(int)PDB_MemberAccess::Private]);
112   if (Count > 0)
113     Printer.NewLine();
114   Printer << "}";
115 }
116
117 int ClassDefinitionDumper::dumpAccessGroup(PDB_MemberAccess Access,
118                                            const SymbolGroup &Group) {
119   if (Group.Functions.empty() && Group.Data.empty() && Group.Unknown.empty())
120     return 0;
121
122   int Count = 0;
123   if (Access == PDB_MemberAccess::Private) {
124     Printer.NewLine();
125     WithColor(Printer, PDB_ColorItem::Keyword).get() << "private";
126     Printer << ":";
127   } else if (Access == PDB_MemberAccess::Protected) {
128     Printer.NewLine();
129     WithColor(Printer, PDB_ColorItem::Keyword).get() << "protected";
130     Printer << ":";
131   } else if (Access == PDB_MemberAccess::Public) {
132     Printer.NewLine();
133     WithColor(Printer, PDB_ColorItem::Keyword).get() << "public";
134     Printer << ":";
135   }
136   Printer.Indent();
137   for (auto iter = Group.Functions.begin(), end = Group.Functions.end();
138        iter != end; ++iter) {
139     ++Count;
140     (*iter)->dump(*this);
141   }
142   for (auto iter = Group.Data.begin(), end = Group.Data.end(); iter != end;
143        ++iter) {
144     ++Count;
145     (*iter)->dump(*this);
146   }
147   for (auto iter = Group.Unknown.begin(), end = Group.Unknown.end();
148        iter != end; ++iter) {
149     ++Count;
150     (*iter)->dump(*this);
151   }
152   Printer.Unindent();
153   return Count;
154 }
155
156 void ClassDefinitionDumper::dump(const PDBSymbolTypeBaseClass &Symbol) {}
157
158 void ClassDefinitionDumper::dump(const PDBSymbolData &Symbol) {
159   VariableDumper Dumper(Printer);
160   Dumper.start(Symbol);
161 }
162
163 void ClassDefinitionDumper::dump(const PDBSymbolFunc &Symbol) {
164   if (Printer.IsSymbolExcluded(Symbol.getName()))
165     return;
166
167   Printer.NewLine();
168   FunctionDumper Dumper(Printer);
169   Dumper.start(Symbol, FunctionDumper::PointerType::None);
170 }
171
172 void ClassDefinitionDumper::dump(const PDBSymbolTypeVTable &Symbol) {}
173
174 void ClassDefinitionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
175   if (Printer.IsTypeExcluded(Symbol.getName()))
176     return;
177
178   Printer.NewLine();
179   EnumDumper Dumper(Printer);
180   Dumper.start(Symbol);
181 }
182
183 void ClassDefinitionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
184   if (Printer.IsTypeExcluded(Symbol.getName()))
185     return;
186
187   Printer.NewLine();
188   TypedefDumper Dumper(Printer);
189   Dumper.start(Symbol);
190 }
191
192 void ClassDefinitionDumper::dump(const PDBSymbolTypeUDT &Symbol) {}