1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
//===- PrettyClassDefinitionDumper.cpp --------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "PrettyClassDefinitionDumper.h"
#include "LinePrinter.h"
#include "PrettyEnumDumper.h"
#include "PrettyFunctionDumper.h"
#include "PrettyTypedefDumper.h"
#include "PrettyVariableDumper.h"
#include "llvm-pdbdump.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/DebugInfo/PDB/IPDBSession.h"
#include "llvm/DebugInfo/PDB/PDBExtras.h"
#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Format.h"
using namespace llvm;
using namespace llvm::pdb;
ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
: PDBSymDumper(true), Printer(P) {}
static void analyzePadding(const PDBSymbolTypeUDT &Class, BitVector &Padding,
uint32_t &FirstFieldOffset) {
Padding.resize(Class.getLength(), true);
auto Children = Class.findAllChildren<PDBSymbolData>();
bool IsFirst = true;
FirstFieldOffset = Class.getLength();
while (auto Data = Children->getNext()) {
// Ignore data members which are not relative to this. Usually these are
// static data members or constexpr and occupy no space. We also need to
// handle BitFields since the PDB doesn't consider them ThisRel, but they
// still occupy space in the record layout.
auto LocType = Data->getLocationType();
if (LocType != PDB_LocType::ThisRel && LocType != PDB_LocType::BitField)
continue;
uint64_t Start = Data->getOffset();
if (IsFirst) {
FirstFieldOffset = Start;
IsFirst = false;
}
auto VarType = Data->getType();
uint64_t Size = VarType->getRawSymbol().getLength();
Padding.reset(Start, Start + Size);
}
// Unmark anything that comes before the first field so it doesn't get
// counted as padding. In reality this is going to be vptrs or base class
// members, but we don't correctly handle that yet.
// FIXME: Handle it.
Padding.reset(0, FirstFieldOffset);
}
void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
assert(opts::pretty::ClassFormat !=
opts::pretty::ClassDefinitionFormat::None);
uint32_t Size = Class.getLength();
uint32_t FirstFieldOffset = 0;
BitVector Padding;
analyzePadding(Class, Padding, FirstFieldOffset);
if (opts::pretty::OnlyPaddingClasses && (Padding.count() == 0))
return;
Printer.NewLine();
WithColor(Printer, PDB_ColorItem::Comment).get() << "// sizeof = " << Size;
Printer.NewLine();
WithColor(Printer, PDB_ColorItem::Keyword).get() << Class.getUdtKind() << " ";
WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
auto Bases = Class.findAllChildren<PDBSymbolTypeBaseClass>();
if (Bases->getChildCount() > 0) {
Printer.Indent();
Printer.NewLine();
Printer << ":";
uint32_t BaseIndex = 0;
while (auto Base = Bases->getNext()) {
Printer << " ";
WithColor(Printer, PDB_ColorItem::Keyword).get() << Base->getAccess();
if (Base->isVirtualBaseClass())
WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base->getName();
if (++BaseIndex < Bases->getChildCount()) {
Printer.NewLine();
Printer << ",";
}
}
Printer.Unindent();
}
Printer << " {";
auto Children = Class.findAllChildren();
Printer.Indent();
int DumpedCount = 0;
int NextPaddingByte = Padding.find_first();
while (auto Child = Children->getNext()) {
if (auto Data = llvm::dyn_cast<PDBSymbolData>(Child.get())) {
if (Data->getDataKind() == PDB_DataKind::Member && NextPaddingByte >= 0) {
// If there are padding bytes remaining, see if this field is the first
// to cross a padding boundary, and print a padding field indicator if
// so.
int Off = Data->getOffset();
if (Off > NextPaddingByte) {
uint32_t Amount = Off - NextPaddingByte;
Printer.NewLine();
WithColor(Printer, PDB_ColorItem::Padding).get()
<< "<padding> (" << Amount << " bytes)";
assert(Padding.find_next_unset(NextPaddingByte) == Off);
NextPaddingByte = Padding.find_next(Off);
}
}
}
if (auto Func = Child->cast<PDBSymbolFunc>()) {
if (Func->isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
continue;
if (Func->getLength() == 0 && !Func->isPureVirtual() &&
!Func->isIntroVirtualFunction())
continue;
}
++DumpedCount;
Child->dump(*this);
}
if (NextPaddingByte >= 0) {
uint32_t Amount = Size - NextPaddingByte;
Printer.NewLine();
WithColor(Printer, PDB_ColorItem::Padding).get() << "<padding> (" << Amount
<< " bytes)";
}
Printer.Unindent();
if (DumpedCount > 0)
Printer.NewLine();
Printer << "}";
Printer.NewLine();
if (Padding.count() > 0) {
APFloat Pct(100.0 * (double)Padding.count() /
(double)(Size - FirstFieldOffset));
SmallString<8> PctStr;
Pct.toString(PctStr, 4);
WithColor(Printer, PDB_ColorItem::Padding).get()
<< "Total padding " << Padding.count() << " bytes (" << PctStr
<< "% of class size)";
Printer.NewLine();
}
}
void ClassDefinitionDumper::dump(const PDBSymbolTypeBaseClass &Symbol) {}
void ClassDefinitionDumper::dump(const PDBSymbolData &Symbol) {
VariableDumper Dumper(Printer);
Dumper.start(Symbol);
}
void ClassDefinitionDumper::dump(const PDBSymbolFunc &Symbol) {
if (Printer.IsSymbolExcluded(Symbol.getName()))
return;
Printer.NewLine();
FunctionDumper Dumper(Printer);
Dumper.start(Symbol, FunctionDumper::PointerType::None);
}
void ClassDefinitionDumper::dump(const PDBSymbolTypeVTable &Symbol) {}
void ClassDefinitionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
if (Printer.IsTypeExcluded(Symbol.getName()))
return;
Printer.NewLine();
EnumDumper Dumper(Printer);
Dumper.start(Symbol);
}
void ClassDefinitionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
if (Printer.IsTypeExcluded(Symbol.getName()))
return;
Printer.NewLine();
TypedefDumper Dumper(Printer);
Dumper.start(Symbol);
}
void ClassDefinitionDumper::dump(const PDBSymbolTypeUDT &Symbol) {}
|