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 llvm::sort(Filtered.begin(), Filtered.end(), Comp);
135 TypeDumper::TypeDumper(LinePrinter &P) : PDBSymDumper(true), Printer(P) {}
137 void TypeDumper::start(const PDBSymbolExe &Exe) {
138 if (opts::pretty::Enums) {
139 if (auto Enums = Exe.findAllChildren<PDBSymbolTypeEnum>()) {
141 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Enums";
142 Printer << ": (" << Enums->getChildCount() << " items)";
144 while (auto Enum = Enums->getNext())
150 if (opts::pretty::Typedefs) {
151 if (auto Typedefs = Exe.findAllChildren<PDBSymbolTypeTypedef>()) {
153 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Typedefs";
154 Printer << ": (" << Typedefs->getChildCount() << " items)";
156 while (auto Typedef = Typedefs->getNext())
157 Typedef->dump(*this);
162 if (opts::pretty::Classes) {
163 if (auto Classes = Exe.findAllChildren<PDBSymbolTypeUDT>()) {
164 uint32_t All = Classes->getChildCount();
167 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Classes";
169 bool Precompute = false;
171 (opts::pretty::ClassOrder != opts::pretty::ClassSortMode::None);
173 // If we're using no sort mode, then we can start getting immediate output
174 // from the tool by just filtering as we go, rather than processing
175 // everything up front so that we can sort it. This makes the tool more
176 // responsive. So only precompute the filtered/sorted set of classes if
177 // necessary due to the specified options.
178 std::vector<LayoutPtr> Filtered;
179 uint32_t Shown = All;
181 Filtered = filterAndSortClassDefs(Printer, *Classes, All);
183 Shown = Filtered.size();
186 Printer << ": (Showing " << Shown << " items";
188 Printer << ", " << (All - Shown) << " filtered";
192 // If we pre-computed, iterate the filtered/sorted list, otherwise iterate
193 // the DIA enumerator and filter on the fly.
195 for (auto &Class : Filtered)
196 dumpClassLayout(*Class);
198 while (auto Class = Classes->getNext()) {
199 if (Class->getUnmodifiedTypeId() != 0)
202 if (Printer.IsTypeExcluded(Class->getName(), Class->getLength()))
205 auto Layout = llvm::make_unique<ClassLayout>(std::move(Class));
206 if (Layout->deepPaddingSize() < opts::pretty::PaddingThreshold)
209 dumpClassLayout(*Layout);
218 void TypeDumper::dump(const PDBSymbolTypeEnum &Symbol) {
219 assert(opts::pretty::Enums);
221 if (Printer.IsTypeExcluded(Symbol.getName(), Symbol.getLength()))
223 // Dump member enums when dumping their class definition.
224 if (nullptr != Symbol.getClassParent())
228 EnumDumper Dumper(Printer);
229 Dumper.start(Symbol);
232 void TypeDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
233 assert(opts::pretty::Typedefs);
235 if (Printer.IsTypeExcluded(Symbol.getName(), Symbol.getLength()))
239 TypedefDumper Dumper(Printer);
240 Dumper.start(Symbol);
243 void TypeDumper::dumpClassLayout(const ClassLayout &Class) {
244 assert(opts::pretty::Classes);
246 if (opts::pretty::ClassFormat == opts::pretty::ClassDefinitionFormat::None) {
248 WithColor(Printer, PDB_ColorItem::Keyword).get() << "class ";
249 WithColor(Printer, PDB_ColorItem::Identifier).get() << Class.getName();
251 ClassDefinitionDumper Dumper(Printer);