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