[llvm-pdbdump] Fix builders again.
[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 "llvm-pdbdump.h"
13 #include "TypedefDumper.h"
14 #include "VariableDumper.h"
15
16 #include "llvm/DebugInfo/PDB/IPDBSession.h"
17 #include "llvm/DebugInfo/PDB/PDBExtras.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h"
26 #include "llvm/Support/Format.h"
27
28 using namespace llvm;
29
30 ClassDefinitionDumper::ClassDefinitionDumper() : PDBSymDumper(true) {}
31
32 void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class,
33                                   raw_ostream &OS, int Indent) {
34   OS << "class " << Class.getName() << " {";
35   auto Children = Class.findAllChildren();
36   if (Children->getChildCount() == 0) {
37     OS << "}";
38     return;
39   }
40
41   // Try to dump symbols organized by member access level.  Public members
42   // first, then protected, then private.  This might be slow, so it's worth
43   // reconsidering the value of this if performance of large PDBs is a problem.
44   // NOTE: Access level of nested types is not recorded in the PDB, so we have
45   // a special case for them.
46   SymbolGroupByAccess Groups;
47   Groups.insert(std::make_pair(0, SymbolGroup()));
48   Groups.insert(std::make_pair((int)PDB_MemberAccess::Private, SymbolGroup()));
49   Groups.insert(
50       std::make_pair((int)PDB_MemberAccess::Protected, SymbolGroup()));
51   Groups.insert(std::make_pair((int)PDB_MemberAccess::Public, SymbolGroup()));
52
53   while (auto Child = Children->getNext()) {
54     PDB_MemberAccess Access = Child->getRawSymbol().getAccess();
55     if (isa<PDBSymbolTypeBaseClass>(*Child))
56       continue;
57
58     SymbolGroup *InsertGroup = nullptr;
59     auto &AccessGroup = Groups.find((int)Access)->second;
60
61     if (auto Func = dyn_cast<PDBSymbolFunc>(Child.get())) {
62       if (Func->isCompilerGenerated())
63         continue;
64       if (Func->getLength() == 0 && !Func->isPureVirtual())
65         continue;
66       Child.release();
67       AccessGroup.Functions.push_back(std::unique_ptr<PDBSymbolFunc>(Func));
68     } else if (auto Data = dyn_cast<PDBSymbolData>(Child.get())) {
69       Child.release();
70       AccessGroup.Data.push_back(std::unique_ptr<PDBSymbolData>(Data));
71     } else {
72       AccessGroup.Unknown.push_back(std::move(Child));
73     }
74   }
75
76   int Count = 0;
77   Count += dumpAccessGroup((PDB_MemberAccess)0, Groups[0], OS, Indent);
78   Count += dumpAccessGroup(PDB_MemberAccess::Public,
79                            Groups[(int)PDB_MemberAccess::Public], OS, Indent);
80   Count +=
81       dumpAccessGroup(PDB_MemberAccess::Protected,
82                       Groups[(int)PDB_MemberAccess::Protected], OS, Indent);
83   Count += dumpAccessGroup(PDB_MemberAccess::Private,
84                            Groups[(int)PDB_MemberAccess::Private], OS, Indent);
85
86   if (Count > 0)
87     OS << newline(Indent);
88   OS << "}";
89 }
90
91 int ClassDefinitionDumper::dumpAccessGroup(PDB_MemberAccess Access,
92                                            const SymbolGroup &Group,
93                                            raw_ostream &OS, int Indent) {
94   if (Group.Functions.empty() && Group.Data.empty() && Group.Unknown.empty())
95     return 0;
96
97   int Count = 0;
98   if (Access == PDB_MemberAccess::Private)
99     OS << newline(Indent) << "private:";
100   else if (Access == PDB_MemberAccess::Protected)
101     OS << newline(Indent) << "protected:";
102   else if (Access == PDB_MemberAccess::Public)
103     OS << newline(Indent) << "public:";
104   for (auto iter = Group.Functions.begin(), end = Group.Functions.end();
105        iter != end; ++iter) {
106     ++Count;
107     (*iter)->dump(OS, Indent + 2, *this);
108   }
109   for (auto iter = Group.Data.begin(), end = Group.Data.end(); iter != end;
110        ++iter) {
111     ++Count;
112     (*iter)->dump(OS, Indent + 2, *this);
113   }
114   for (auto iter = Group.Unknown.begin(), end = Group.Unknown.end();
115        iter != end; ++iter) {
116     ++Count;
117     (*iter)->dump(OS, Indent + 2, *this);
118   }
119   return Count;
120 }
121
122 void ClassDefinitionDumper::dump(const PDBSymbolTypeBaseClass &Symbol,
123                                  raw_ostream &OS, int Indent) {}
124
125 void ClassDefinitionDumper::dump(const PDBSymbolData &Symbol, raw_ostream &OS,
126                                  int Indent) {
127   VariableDumper Dumper;
128   Dumper.start(Symbol, OS, Indent);
129 }
130
131 void ClassDefinitionDumper::dump(const PDBSymbolFunc &Symbol, raw_ostream &OS,
132                                  int Indent) {
133   FunctionDumper Dumper;
134   Dumper.start(Symbol, FunctionDumper::PointerType::None, OS, Indent);
135 }
136
137 void ClassDefinitionDumper::dump(const PDBSymbolTypeVTable &Symbol,
138                                  raw_ostream &OS, int Indent) {}
139
140 void ClassDefinitionDumper::dump(const PDBSymbolTypeEnum &Symbol,
141                                  raw_ostream &OS, int Indent) {
142   OS << newline(Indent) << "enum " << Symbol.getName();
143 }
144
145 void ClassDefinitionDumper::dump(const PDBSymbolTypeTypedef &Symbol,
146                                  raw_ostream &OS, int Indent) {
147   OS << newline(Indent);
148   TypedefDumper Dumper;
149   Dumper.start(Symbol, OS, Indent);
150 }
151
152 void ClassDefinitionDumper::dump(const PDBSymbolTypeUDT &Symbol,
153                                  raw_ostream &OS, int Indent) {}