blob: d6fcaea3edadbce778d1c5331c781b1c12346084 [file] [log] [blame]
Zachary Turner29c69102015-02-23 05:58:34 +00001//===- 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"
Zachary Turner2d11c202015-02-27 09:15:59 +000012#include "LinePrinter.h"
Zachary Turner29c69102015-02-23 05:58:34 +000013#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
29using namespace llvm;
30
Zachary Turner2d11c202015-02-27 09:15:59 +000031ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
32 : PDBSymDumper(true), Printer(P) {}
Zachary Turner29c69102015-02-23 05:58:34 +000033
34void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class,
35 raw_ostream &OS, int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +000036 std::string Name = Class.getName();
37 WithColor(Printer, PDB_ColorItem::Keyword).get() << "class ";
38 WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
39 Printer << " {";
Zachary Turner29c69102015-02-23 05:58:34 +000040 auto Children = Class.findAllChildren();
41 if (Children->getChildCount() == 0) {
42 OS << "}";
43 return;
44 }
45
46 // Try to dump symbols organized by member access level. Public members
47 // first, then protected, then private. This might be slow, so it's worth
48 // reconsidering the value of this if performance of large PDBs is a problem.
49 // NOTE: Access level of nested types is not recorded in the PDB, so we have
50 // a special case for them.
51 SymbolGroupByAccess Groups;
Zachary Turnerd8edf212015-02-23 06:13:27 +000052 Groups.insert(std::make_pair(0, SymbolGroup()));
53 Groups.insert(std::make_pair((int)PDB_MemberAccess::Private, SymbolGroup()));
54 Groups.insert(
55 std::make_pair((int)PDB_MemberAccess::Protected, SymbolGroup()));
56 Groups.insert(std::make_pair((int)PDB_MemberAccess::Public, SymbolGroup()));
Zachary Turner29c69102015-02-23 05:58:34 +000057
58 while (auto Child = Children->getNext()) {
59 PDB_MemberAccess Access = Child->getRawSymbol().getAccess();
60 if (isa<PDBSymbolTypeBaseClass>(*Child))
61 continue;
62
Zachary Turnerd8edf212015-02-23 06:13:27 +000063 auto &AccessGroup = Groups.find((int)Access)->second;
Zachary Turner29c69102015-02-23 05:58:34 +000064
65 if (auto Func = dyn_cast<PDBSymbolFunc>(Child.get())) {
66 if (Func->isCompilerGenerated())
67 continue;
68 if (Func->getLength() == 0 && !Func->isPureVirtual())
69 continue;
70 Child.release();
71 AccessGroup.Functions.push_back(std::unique_ptr<PDBSymbolFunc>(Func));
72 } else if (auto Data = dyn_cast<PDBSymbolData>(Child.get())) {
73 Child.release();
74 AccessGroup.Data.push_back(std::unique_ptr<PDBSymbolData>(Data));
75 } else {
76 AccessGroup.Unknown.push_back(std::move(Child));
77 }
78 }
79
80 int Count = 0;
Zachary Turnerd8edf212015-02-23 06:13:27 +000081 Count += dumpAccessGroup((PDB_MemberAccess)0, Groups[0], OS, Indent);
Zachary Turner29c69102015-02-23 05:58:34 +000082 Count += dumpAccessGroup(PDB_MemberAccess::Public,
Zachary Turnerd8edf212015-02-23 06:13:27 +000083 Groups[(int)PDB_MemberAccess::Public], OS, Indent);
84 Count +=
85 dumpAccessGroup(PDB_MemberAccess::Protected,
86 Groups[(int)PDB_MemberAccess::Protected], OS, Indent);
Zachary Turner29c69102015-02-23 05:58:34 +000087 Count += dumpAccessGroup(PDB_MemberAccess::Private,
Zachary Turnerd8edf212015-02-23 06:13:27 +000088 Groups[(int)PDB_MemberAccess::Private], OS, Indent);
Zachary Turner29c69102015-02-23 05:58:34 +000089 if (Count > 0)
Zachary Turner2d11c202015-02-27 09:15:59 +000090 Printer.NewLine();
Zachary Turner29c69102015-02-23 05:58:34 +000091 OS << "}";
Zachary Turner29c69102015-02-23 05:58:34 +000092}
93
94int ClassDefinitionDumper::dumpAccessGroup(PDB_MemberAccess Access,
95 const SymbolGroup &Group,
96 raw_ostream &OS, int Indent) {
97 if (Group.Functions.empty() && Group.Data.empty() && Group.Unknown.empty())
98 return 0;
99
100 int Count = 0;
Zachary Turner2d11c202015-02-27 09:15:59 +0000101 if (Access == PDB_MemberAccess::Private) {
102 Printer.NewLine();
103 WithColor(Printer, PDB_ColorItem::Keyword).get() << "private";
104 Printer << ":";
105 } else if (Access == PDB_MemberAccess::Protected) {
106 Printer.NewLine();
107 WithColor(Printer, PDB_ColorItem::Keyword).get() << "protected";
108 Printer << ":";
109 } else if (Access == PDB_MemberAccess::Public) {
110 Printer.NewLine();
111 WithColor(Printer, PDB_ColorItem::Keyword).get() << "public";
112 Printer << ":";
113 }
114 Printer.Indent();
Zachary Turner29c69102015-02-23 05:58:34 +0000115 for (auto iter = Group.Functions.begin(), end = Group.Functions.end();
116 iter != end; ++iter) {
117 ++Count;
118 (*iter)->dump(OS, Indent + 2, *this);
119 }
120 for (auto iter = Group.Data.begin(), end = Group.Data.end(); iter != end;
121 ++iter) {
122 ++Count;
123 (*iter)->dump(OS, Indent + 2, *this);
124 }
125 for (auto iter = Group.Unknown.begin(), end = Group.Unknown.end();
126 iter != end; ++iter) {
127 ++Count;
128 (*iter)->dump(OS, Indent + 2, *this);
129 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000130 Printer.Unindent();
Zachary Turner29c69102015-02-23 05:58:34 +0000131 return Count;
132}
133
134void ClassDefinitionDumper::dump(const PDBSymbolTypeBaseClass &Symbol,
135 raw_ostream &OS, int Indent) {}
136
137void ClassDefinitionDumper::dump(const PDBSymbolData &Symbol, raw_ostream &OS,
138 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000139 VariableDumper Dumper(Printer);
Zachary Turner29c69102015-02-23 05:58:34 +0000140 Dumper.start(Symbol, OS, Indent);
141}
142
143void ClassDefinitionDumper::dump(const PDBSymbolFunc &Symbol, raw_ostream &OS,
144 int Indent) {
Zachary Turnerf5abda22015-03-01 06:49:49 +0000145 if (Printer.IsSymbolExcluded(Symbol.getName()))
146 return;
147
Zachary Turner2d11c202015-02-27 09:15:59 +0000148 Printer.NewLine();
149 FunctionDumper Dumper(Printer);
Zachary Turner29c69102015-02-23 05:58:34 +0000150 Dumper.start(Symbol, FunctionDumper::PointerType::None, OS, Indent);
151}
152
153void ClassDefinitionDumper::dump(const PDBSymbolTypeVTable &Symbol,
154 raw_ostream &OS, int Indent) {}
155
156void ClassDefinitionDumper::dump(const PDBSymbolTypeEnum &Symbol,
157 raw_ostream &OS, int Indent) {
Zachary Turnerf5abda22015-03-01 06:49:49 +0000158 if (Printer.IsTypeExcluded(Symbol.getName()))
159 return;
160
Zachary Turner2d11c202015-02-27 09:15:59 +0000161 Printer.NewLine();
162 WithColor(Printer, PDB_ColorItem::Keyword).get() << "enum ";
163 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000164}
165
166void ClassDefinitionDumper::dump(const PDBSymbolTypeTypedef &Symbol,
167 raw_ostream &OS, int Indent) {
Zachary Turnerf5abda22015-03-01 06:49:49 +0000168 if (Printer.IsTypeExcluded(Symbol.getName()))
169 return;
170
Zachary Turner2d11c202015-02-27 09:15:59 +0000171 Printer.NewLine();
172 TypedefDumper Dumper(Printer);
Zachary Turner29c69102015-02-23 05:58:34 +0000173 Dumper.start(Symbol, OS, Indent);
Zachary Turner29c69102015-02-23 05:58:34 +0000174}
175
176void ClassDefinitionDumper::dump(const PDBSymbolTypeUDT &Symbol,
177 raw_ostream &OS, int Indent) {}