• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "LinePrinter.h"
12 #include "PrettyClassLayoutGraphicalDumper.h"
13 #include "llvm-pdbutil.h"
14 
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"
20 
21 #include "llvm/Support/Format.h"
22 
23 using namespace llvm;
24 using namespace llvm::pdb;
25 
ClassDefinitionDumper(LinePrinter & P)26 ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
27     : PDBSymDumper(true), Printer(P) {}
28 
start(const PDBSymbolTypeUDT & Class)29 void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
30   assert(opts::pretty::ClassFormat !=
31          opts::pretty::ClassDefinitionFormat::None);
32 
33   ClassLayout Layout(Class);
34   start(Layout);
35 }
36 
start(const ClassLayout & Layout)37 void ClassDefinitionDumper::start(const ClassLayout &Layout) {
38   prettyPrintClassIntro(Layout);
39 
40   PrettyClassLayoutGraphicalDumper Dumper(Printer, 1, 0);
41   DumpedAnything |= Dumper.start(Layout);
42 
43   prettyPrintClassOutro(Layout);
44 }
45 
prettyPrintClassIntro(const ClassLayout & Layout)46 void ClassDefinitionDumper::prettyPrintClassIntro(const ClassLayout &Layout) {
47   DumpedAnything = false;
48   Printer.NewLine();
49 
50   uint32_t Size = Layout.getSize();
51   const PDBSymbolTypeUDT &Class = Layout.getClass();
52 
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 ";
59 
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
63                                                    << "]";
64   uint32_t BaseCount = Layout.bases().size();
65   if (BaseCount > 0) {
66     Printer.Indent();
67     char NextSeparator = ':';
68     for (auto BC : Layout.bases()) {
69       const auto &Base = BC->getBase();
70       if (Base.isIndirectVirtualBaseClass())
71         continue;
72 
73       Printer.NewLine();
74       Printer << NextSeparator << " ";
75       WithColor(Printer, PDB_ColorItem::Keyword).get() << Base.getAccess();
76       if (BC->isVirtualBase())
77         WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
78 
79       WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base.getName();
80       NextSeparator = ',';
81     }
82 
83     Printer.Unindent();
84   }
85 
86   Printer << " {";
87   Printer.Indent();
88 }
89 
prettyPrintClassOutro(const ClassLayout & Layout)90 void ClassDefinitionDumper::prettyPrintClassOutro(const ClassLayout &Layout) {
91   Printer.Unindent();
92   if (DumpedAnything)
93     Printer.NewLine();
94   Printer << "}";
95   Printer.NewLine();
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)";
104     Printer.NewLine();
105     APFloat Pct2(100.0 * (double)Layout.immediatePadding() /
106                  (double)Layout.getSize());
107     PctStr.clear();
108     Pct2.toString(PctStr, 4);
109     WithColor(Printer, PDB_ColorItem::Padding).get()
110         << "Immediate padding " << Layout.immediatePadding() << " bytes ("
111         << PctStr << "% of class size)";
112     Printer.NewLine();
113   }
114 }
115