1 //===- PrettyTypeDumper.cpp - PDBSymDumper type dumper *------------ C++ *-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PrettyTypeDumper.h"
12 #include "LinePrinter.h"
13 #include "PrettyBuiltinDumper.h"
14 #include "PrettyClassDefinitionDumper.h"
15 #include "PrettyEnumDumper.h"
16 #include "PrettyTypedefDumper.h"
17 #include "llvm-pdbutil.h"
19 #include "llvm/DebugInfo/PDB/IPDBSession.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
25 #include "llvm/DebugInfo/PDB/UDTLayout.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/FormatVariadic.h"
30 using namespace llvm::pdb;
32 using LayoutPtr = std::unique_ptr<ClassLayout>;
34 typedef bool (*CompareFunc)(const LayoutPtr &S1, const LayoutPtr &S2);
36 static bool CompareNames(const LayoutPtr &S1, const LayoutPtr &S2) {
37 return S1->getName() < S2->getName();
40 static bool CompareSizes(const LayoutPtr &S1, const LayoutPtr &S2) {
41 return S1->getSize() < S2->getSize();
44 static bool ComparePadding(const LayoutPtr &S1, const LayoutPtr &S2) {
45 return S1->deepPaddingSize() < S2->deepPaddingSize();
48 static bool ComparePaddingPct(const LayoutPtr &S1, const LayoutPtr &S2) {
49 double Pct1 = (double)S1->deepPaddingSize() / (double)S1->getSize();
50 double Pct2 = (double)S2->deepPaddingSize() / (double)S2->getSize();
54 static bool ComparePaddingImmediate(const LayoutPtr &S1, const LayoutPtr &S2) {
55 return S1->immediatePadding() < S2->immediatePadding();
58 static bool ComparePaddingPctImmediate(const LayoutPtr &S1,
59 const LayoutPtr &S2) {
60 double Pct1 = (double)S1->immediatePadding() / (double)S1->getSize();
61 double Pct2 = (double)S2->immediatePadding() / (double)S2->getSize();
65 static CompareFunc getComparisonFunc(opts::pretty::ClassSortMode Mode) {
67 case opts::pretty::ClassSortMode::Name:
69 case opts::pretty::ClassSortMode::Size:
71 case opts::pretty::ClassSortMode::Padding:
72 return ComparePadding;
73 case opts::pretty::ClassSortMode::PaddingPct:
74 return ComparePaddingPct;
75 case opts::pretty::ClassSortMode::PaddingImmediate:
76 return ComparePaddingImmediate;
77 case opts::pretty::ClassSortMode::PaddingPctImmediate:
78 return ComparePaddingPctImmediate;
84 template <typename Enumerator>
85 static std::vector<std::unique_ptr<ClassLayout>>
86 filterAndSortClassDefs(LinePrinter &Printer, Enumerator &E,
87 uint32_t UnfilteredCount) {
88 std::vector<std::unique_ptr<ClassLayout>> Filtered;
90 Filtered.reserve(UnfilteredCount);
91 CompareFunc Comp = getComparisonFunc(opts::pretty::ClassOrder);
93 if (UnfilteredCount > 10000) {
94 errs() << formatv("Filtering and sorting {0} types", UnfilteredCount);
97 uint32_t Examined = 0;
98 uint32_t Discarded = 0;
99 while (auto Class = E.getNext()) {
101 if (Examined % 10000 == 0) {
102 errs() << formatv("Examined {0}/{1} items. {2} items discarded\n",
103 Examined, UnfilteredCount, Discarded);
107 if (Class->getUnmodifiedTypeId() != 0) {
112 if (Printer.IsTypeExcluded(Class->getName(), Class->getLength())) {
117 auto Layout = llvm::make_unique<ClassLayout>(std::move(Class));
118 if (Layout->deepPaddingSize() < opts::pretty::PaddingThreshold) {
122 if (Layout->immediatePadding() < opts::pretty::ImmediatePaddingThreshold) {
127 Filtered.push_back(std::move(Layout));
131 std::sort(Filtered.begin(), Filtered.end(), Comp);
135 TypeDumper::TypeDumper(LinePrinter &P) : PDBSymDumper(true), Printer(P) {}
137 void TypeDumper::start(const PDBSymbolExe &Exe) {
138 auto Children = Exe.findAllChildren();
139 if (opts::pretty::Enums) {
140 if (auto Enums = Exe.findAllChildren<PDBSymbolTypeEnum>()) {
142 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Enums";
143 Printer << ": (" << Enums->getChildCount() << " items)";
145 while (auto Enum = Enums->getNext())
151 if (opts::pretty::Typedefs) {
152 if (auto Typedefs = Exe.findAllChildren<PDBSymbolTypeTypedef>()) {
154 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Typedefs";
155 Printer << ": (" << Typedefs->getChildCount() << " items)";
157 while (auto Typedef = Typedefs->getNext())
158 Typedef->dump(*this);
163 if (opts::pretty::Classes) {
164 if (auto Classes = Exe.findAllChildren<PDBSymbolTypeUDT>()) {
165 uint32_t All = Classes->getChildCount();
168 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Classes";
170 bool Precompute = false;
172 (opts::pretty::ClassOrder != opts::pretty::ClassSortMode::None);
174 // If we're using no sort mode, then we can start getting immediate output
175 // from the tool by just filtering as we go, rather than processing
176 // everything up front so that we can sort it. This makes the tool more
177 // responsive. So only precompute the filtered/sorted set of classes if
178 // necessary due to the specified options.
179 std::vector<LayoutPtr> Filtered;
180 uint32_t Shown = All;
182 Filtered = filterAndSortClassDefs(Printer, *Classes, All);
184 Shown = Filtered.size();
187 Printer << ": (Showing " << Shown << " items";
189 Printer << ", " << (All - Shown) << " filtered";
193 // If we pre-computed, iterate the filtered/sorted list, otherwise iterate
194 // the DIA enumerator and filter on the fly.
196 for (auto &Class : Filtered)
197 dumpClassLayout(*Class);
199 while (auto Class = Classes->getNext()) {
200 if (Class->getUnmodifiedTypeId() != 0)
203 if (Printer.IsTypeExcluded(Class->getName(), Class->getLength()))
206 auto Layout = llvm::make_unique<ClassLayout>(std::move(Class));
207 if (Layout->deepPaddingSize() < opts::pretty::PaddingThreshold)
210 dumpClassLayout(*Layout);
219 void TypeDumper::dump(const PDBSymbolTypeEnum &Symbol) {
220 assert(opts::pretty::Enums);
222 if (Printer.IsTypeExcluded(Symbol.getName(), Symbol.getLength()))
224 // Dump member enums when dumping their class definition.
225 if (nullptr != Symbol.getClassParent())
229 EnumDumper Dumper(Printer);
230 Dumper.start(Symbol);
233 void TypeDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
234 assert(opts::pretty::Typedefs);
236 if (Printer.IsTypeExcluded(Symbol.getName(), Symbol.getLength()))
240 TypedefDumper Dumper(Printer);
241 Dumper.start(Symbol);
244 void TypeDumper::dumpClassLayout(const ClassLayout &Class) {
245 assert(opts::pretty::Classes);
247 if (opts::pretty::ClassFormat == opts::pretty::ClassDefinitionFormat::None) {
249 WithColor(Printer, PDB_ColorItem::Keyword).get() << "class ";
250 WithColor(Printer, PDB_ColorItem::Identifier).get() << Class.getName();
252 ClassDefinitionDumper Dumper(Printer);