1//===- PrettyClassDefinitionDumper.cpp --------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "PrettyClassDefinitionDumper.h"
10
11#include "PrettyClassLayoutGraphicalDumper.h"
12#include "llvm-pdbutil.h"
13
14#include "llvm/ADT/APFloat.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
17#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
18#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
19#include "llvm/DebugInfo/PDB/UDTLayout.h"
20
21using namespace llvm;
22using namespace llvm::pdb;
23
24ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
25 : PDBSymDumper(true), Printer(P) {}
26
27void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
28 assert(opts::pretty::ClassFormat !=
29 opts::pretty::ClassDefinitionFormat::None);
30
31 ClassLayout Layout(Class);
32 start(Class: Layout);
33}
34
35void ClassDefinitionDumper::start(const ClassLayout &Layout) {
36 prettyPrintClassIntro(Class: Layout);
37
38 PrettyClassLayoutGraphicalDumper Dumper(Printer, 1, 0);
39 DumpedAnything |= Dumper.start(Layout);
40
41 prettyPrintClassOutro(Class: Layout);
42}
43
44void ClassDefinitionDumper::prettyPrintClassIntro(const ClassLayout &Layout) {
45 DumpedAnything = false;
46 Printer.NewLine();
47
48 uint32_t Size = Layout.getSize();
49 const PDBSymbolTypeUDT &Class = Layout.getClass();
50
51 if (Layout.getClass().isConstType())
52 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
53 if (Layout.getClass().isVolatileType())
54 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
55 if (Layout.getClass().isUnalignedType())
56 WithColor(Printer, PDB_ColorItem::Keyword).get() << "unaligned ";
57
58 WithColor(Printer, PDB_ColorItem::Keyword).get() << Class.getUdtKind() << " ";
59 WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
60 WithColor(Printer, PDB_ColorItem::Comment).get() << " [sizeof = " << Size
61 << "]";
62 uint32_t BaseCount = Layout.bases().size();
63 if (BaseCount > 0) {
64 Printer.Indent();
65 char NextSeparator = ':';
66 for (auto *BC : Layout.bases()) {
67 const auto &Base = BC->getBase();
68 if (Base.isIndirectVirtualBaseClass())
69 continue;
70
71 Printer.NewLine();
72 Printer << NextSeparator << " ";
73 WithColor(Printer, PDB_ColorItem::Keyword).get() << Base.getAccess();
74 if (BC->isVirtualBase())
75 WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
76
77 WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base.getName();
78 NextSeparator = ',';
79 }
80
81 Printer.Unindent();
82 }
83
84 Printer << " {";
85 Printer.Indent();
86}
87
88void ClassDefinitionDumper::prettyPrintClassOutro(const ClassLayout &Layout) {
89 Printer.Unindent();
90 if (DumpedAnything)
91 Printer.NewLine();
92 Printer << "}";
93 Printer.NewLine();
94 if (Layout.deepPaddingSize() > 0) {
95 APFloat Pct(100.0 * (double)Layout.deepPaddingSize() /
96 (double)Layout.getSize());
97 SmallString<8> PctStr;
98 Pct.toString(Str&: PctStr, FormatPrecision: 4);
99 WithColor(Printer, PDB_ColorItem::Padding).get()
100 << "Total padding " << Layout.deepPaddingSize() << " bytes (" << PctStr
101 << "% of class size)";
102 Printer.NewLine();
103 APFloat Pct2(100.0 * (double)Layout.immediatePadding() /
104 (double)Layout.getSize());
105 PctStr.clear();
106 Pct2.toString(Str&: PctStr, FormatPrecision: 4);
107 WithColor(Printer, PDB_ColorItem::Padding).get()
108 << "Immediate padding " << Layout.immediatePadding() << " bytes ("
109 << PctStr << "% of class size)";
110 Printer.NewLine();
111 }
112}
113