1 //===- PrettyClassDefinitionDumper.cpp --------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "PrettyClassDefinitionDumper.h"
11 #include "LinePrinter.h"
12 #include "PrettyClassLayoutGraphicalDumper.h"
13 #include "llvm-pdbutil.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
19 #include "llvm/DebugInfo/PDB/UDTLayout.h"
21 #include "llvm/Support/Format.h"
24 using namespace llvm::pdb
;
26 ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter
&P
)
27 : PDBSymDumper(true), Printer(P
) {}
29 void ClassDefinitionDumper::start(const PDBSymbolTypeUDT
&Class
) {
30 assert(opts::pretty::ClassFormat
!=
31 opts::pretty::ClassDefinitionFormat::None
);
33 ClassLayout
Layout(Class
);
37 void ClassDefinitionDumper::start(const ClassLayout
&Layout
) {
38 prettyPrintClassIntro(Layout
);
40 PrettyClassLayoutGraphicalDumper
Dumper(Printer
, 1, 0);
41 DumpedAnything
|= Dumper
.start(Layout
);
43 prettyPrintClassOutro(Layout
);
46 void ClassDefinitionDumper::prettyPrintClassIntro(const ClassLayout
&Layout
) {
47 DumpedAnything
= false;
50 uint32_t Size
= Layout
.getSize();
51 const PDBSymbolTypeUDT
&Class
= Layout
.getClass();
53 if (Layout
.getClass().isConstType())
54 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << "const ";
55 if (Layout
.getClass().isVolatileType())
56 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << "volatile ";
57 if (Layout
.getClass().isUnalignedType())
58 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << "unaligned ";
60 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << Class
.getUdtKind() << " ";
61 WithColor(Printer
, PDB_ColorItem::Type
).get() << Class
.getName();
62 WithColor(Printer
, PDB_ColorItem::Comment
).get() << " [sizeof = " << Size
64 uint32_t BaseCount
= Layout
.bases().size();
67 char NextSeparator
= ':';
68 for (auto BC
: Layout
.bases()) {
69 const auto &Base
= BC
->getBase();
70 if (Base
.isIndirectVirtualBaseClass())
74 Printer
<< NextSeparator
<< " ";
75 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << Base
.getAccess();
76 if (BC
->isVirtualBase())
77 WithColor(Printer
, PDB_ColorItem::Keyword
).get() << " virtual";
79 WithColor(Printer
, PDB_ColorItem::Type
).get() << " " << Base
.getName();
90 void ClassDefinitionDumper::prettyPrintClassOutro(const ClassLayout
&Layout
) {
96 if (Layout
.deepPaddingSize() > 0) {
97 APFloat
Pct(100.0 * (double)Layout
.deepPaddingSize() /
98 (double)Layout
.getSize());
99 SmallString
<8> PctStr
;
100 Pct
.toString(PctStr
, 4);
101 WithColor(Printer
, PDB_ColorItem::Padding
).get()
102 << "Total padding " << Layout
.deepPaddingSize() << " bytes (" << PctStr
103 << "% of class size)";
105 APFloat
Pct2(100.0 * (double)Layout
.immediatePadding() /
106 (double)Layout
.getSize());
108 Pct2
.toString(PctStr
, 4);
109 WithColor(Printer
, PDB_ColorItem::Padding
).get()
110 << "Immediate padding " << Layout
.immediatePadding() << " bytes ("
111 << PctStr
<< "% of class size)";