xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-pdbutil/PrettyClassDefinitionDumper.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- PrettyClassDefinitionDumper.cpp --------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "PrettyClassDefinitionDumper.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "PrettyClassLayoutGraphicalDumper.h"
120b57cec5SDimitry Andric #include "llvm-pdbutil.h"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h"
150b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
1681ad6265SDimitry Andric #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
170b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
1881ad6265SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
190b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
200b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/UDTLayout.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #include "llvm/Support/Format.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric using namespace llvm::pdb;
260b57cec5SDimitry Andric 
ClassDefinitionDumper(LinePrinter & P)270b57cec5SDimitry Andric ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
280b57cec5SDimitry Andric     : PDBSymDumper(true), Printer(P) {}
290b57cec5SDimitry Andric 
start(const PDBSymbolTypeUDT & Class)300b57cec5SDimitry Andric void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
310b57cec5SDimitry Andric   assert(opts::pretty::ClassFormat !=
320b57cec5SDimitry Andric          opts::pretty::ClassDefinitionFormat::None);
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric   ClassLayout Layout(Class);
350b57cec5SDimitry Andric   start(Layout);
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
start(const ClassLayout & Layout)380b57cec5SDimitry Andric void ClassDefinitionDumper::start(const ClassLayout &Layout) {
390b57cec5SDimitry Andric   prettyPrintClassIntro(Layout);
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   PrettyClassLayoutGraphicalDumper Dumper(Printer, 1, 0);
420b57cec5SDimitry Andric   DumpedAnything |= Dumper.start(Layout);
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric   prettyPrintClassOutro(Layout);
450b57cec5SDimitry Andric }
460b57cec5SDimitry Andric 
prettyPrintClassIntro(const ClassLayout & Layout)470b57cec5SDimitry Andric void ClassDefinitionDumper::prettyPrintClassIntro(const ClassLayout &Layout) {
480b57cec5SDimitry Andric   DumpedAnything = false;
490b57cec5SDimitry Andric   Printer.NewLine();
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   uint32_t Size = Layout.getSize();
520b57cec5SDimitry Andric   const PDBSymbolTypeUDT &Class = Layout.getClass();
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   if (Layout.getClass().isConstType())
550b57cec5SDimitry Andric     WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
560b57cec5SDimitry Andric   if (Layout.getClass().isVolatileType())
570b57cec5SDimitry Andric     WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
580b57cec5SDimitry Andric   if (Layout.getClass().isUnalignedType())
590b57cec5SDimitry Andric     WithColor(Printer, PDB_ColorItem::Keyword).get() << "unaligned ";
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   WithColor(Printer, PDB_ColorItem::Keyword).get() << Class.getUdtKind() << " ";
620b57cec5SDimitry Andric   WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
630b57cec5SDimitry Andric   WithColor(Printer, PDB_ColorItem::Comment).get() << " [sizeof = " << Size
640b57cec5SDimitry Andric                                                    << "]";
650b57cec5SDimitry Andric   uint32_t BaseCount = Layout.bases().size();
660b57cec5SDimitry Andric   if (BaseCount > 0) {
670b57cec5SDimitry Andric     Printer.Indent();
680b57cec5SDimitry Andric     char NextSeparator = ':';
69*bdd1243dSDimitry Andric     for (auto *BC : Layout.bases()) {
700b57cec5SDimitry Andric       const auto &Base = BC->getBase();
710b57cec5SDimitry Andric       if (Base.isIndirectVirtualBaseClass())
720b57cec5SDimitry Andric         continue;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric       Printer.NewLine();
750b57cec5SDimitry Andric       Printer << NextSeparator << " ";
760b57cec5SDimitry Andric       WithColor(Printer, PDB_ColorItem::Keyword).get() << Base.getAccess();
770b57cec5SDimitry Andric       if (BC->isVirtualBase())
780b57cec5SDimitry Andric         WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric       WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base.getName();
810b57cec5SDimitry Andric       NextSeparator = ',';
820b57cec5SDimitry Andric     }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric     Printer.Unindent();
850b57cec5SDimitry Andric   }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   Printer << " {";
880b57cec5SDimitry Andric   Printer.Indent();
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
prettyPrintClassOutro(const ClassLayout & Layout)910b57cec5SDimitry Andric void ClassDefinitionDumper::prettyPrintClassOutro(const ClassLayout &Layout) {
920b57cec5SDimitry Andric   Printer.Unindent();
930b57cec5SDimitry Andric   if (DumpedAnything)
940b57cec5SDimitry Andric     Printer.NewLine();
950b57cec5SDimitry Andric   Printer << "}";
960b57cec5SDimitry Andric   Printer.NewLine();
970b57cec5SDimitry Andric   if (Layout.deepPaddingSize() > 0) {
980b57cec5SDimitry Andric     APFloat Pct(100.0 * (double)Layout.deepPaddingSize() /
990b57cec5SDimitry Andric                 (double)Layout.getSize());
1000b57cec5SDimitry Andric     SmallString<8> PctStr;
1010b57cec5SDimitry Andric     Pct.toString(PctStr, 4);
1020b57cec5SDimitry Andric     WithColor(Printer, PDB_ColorItem::Padding).get()
1030b57cec5SDimitry Andric         << "Total padding " << Layout.deepPaddingSize() << " bytes (" << PctStr
1040b57cec5SDimitry Andric         << "% of class size)";
1050b57cec5SDimitry Andric     Printer.NewLine();
1060b57cec5SDimitry Andric     APFloat Pct2(100.0 * (double)Layout.immediatePadding() /
1070b57cec5SDimitry Andric                  (double)Layout.getSize());
1080b57cec5SDimitry Andric     PctStr.clear();
1090b57cec5SDimitry Andric     Pct2.toString(PctStr, 4);
1100b57cec5SDimitry Andric     WithColor(Printer, PDB_ColorItem::Padding).get()
1110b57cec5SDimitry Andric         << "Immediate padding " << Layout.immediatePadding() << " bytes ("
1120b57cec5SDimitry Andric         << PctStr << "% of class size)";
1130b57cec5SDimitry Andric     Printer.NewLine();
1140b57cec5SDimitry Andric   }
1150b57cec5SDimitry Andric }
116