blob: 81ef26167a2666c2518d844509f04cdda2199ce4 [file] [log] [blame]
Zachary Turnera9054dd2017-01-11 00:35:43 +00001//===- PrettyClassDefinitionDumper.cpp --------------------------*- C++ -*-===//
Zachary Turner29c69102015-02-23 05:58:34 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turnera9054dd2017-01-11 00:35:43 +000010#include "PrettyClassDefinitionDumper.h"
11
Zachary Turner2d11c202015-02-27 09:15:59 +000012#include "LinePrinter.h"
Zachary Turner9e7dda32017-04-12 23:18:51 +000013#include "PrettyClassLayoutGraphicalDumper.h"
14#include "PrettyClassLayoutTextDumper.h"
Zachary Turner29c69102015-02-23 05:58:34 +000015#include "llvm-pdbdump.h"
Zachary Turner29c69102015-02-23 05:58:34 +000016
Zachary Turner0c990bbe2017-04-10 19:33:29 +000017#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/SmallString.h"
Zachary Turner29c69102015-02-23 05:58:34 +000019#include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
Zachary Turner29c69102015-02-23 05:58:34 +000020#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
Zachary Turnerc883a8c2017-04-12 23:18:21 +000021#include "llvm/DebugInfo/PDB/UDTLayout.h"
22
Zachary Turner29c69102015-02-23 05:58:34 +000023#include "llvm/Support/Format.h"
24
25using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000026using namespace llvm::pdb;
Zachary Turner29c69102015-02-23 05:58:34 +000027
Zachary Turner2d11c202015-02-27 09:15:59 +000028ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
29 : PDBSymDumper(true), Printer(P) {}
Zachary Turner29c69102015-02-23 05:58:34 +000030
Zachary Turnerb52d08d2015-03-01 06:51:29 +000031void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
Zachary Turner10169b62017-04-06 23:43:39 +000032 assert(opts::pretty::ClassFormat !=
33 opts::pretty::ClassDefinitionFormat::None);
34
Zachary Turner9e7dda32017-04-12 23:18:51 +000035 ClassLayout Layout(Class);
Zachary Turner4dc4f012017-04-13 21:11:00 +000036 start(Layout);
37}
Zachary Turnerc883a8c2017-04-12 23:18:21 +000038
Zachary Turner4dc4f012017-04-13 21:11:00 +000039void ClassDefinitionDumper::start(const ClassLayout &Layout) {
Zachary Turner9e7dda32017-04-12 23:18:51 +000040 prettyPrintClassIntro(Layout);
41
42 switch (opts::pretty::ClassFormat) {
43 case opts::pretty::ClassDefinitionFormat::Graphical: {
Zachary Turner4dc4f012017-04-13 21:11:00 +000044 PrettyClassLayoutGraphicalDumper Dumper(Printer, 0);
Zachary Turner9e7dda32017-04-12 23:18:51 +000045 DumpedAnything = Dumper.start(Layout);
46 break;
47 }
48 case opts::pretty::ClassDefinitionFormat::Standard:
49 case opts::pretty::ClassDefinitionFormat::Layout: {
50 PrettyClassLayoutTextDumper Dumper(Printer);
51 DumpedAnything |= Dumper.start(Layout);
52 break;
53 }
54 default:
55 llvm_unreachable("Unreachable!");
56 }
57
58 prettyPrintClassOutro(Layout);
59}
60
61void ClassDefinitionDumper::prettyPrintClassIntro(const ClassLayout &Layout) {
62 DumpedAnything = false;
Zachary Turner0c990bbe2017-04-10 19:33:29 +000063 Printer.NewLine();
Zachary Turner0c990bbe2017-04-10 19:33:29 +000064
Zachary Turner16901642017-04-24 17:47:24 +000065 uint32_t Size = Layout.getSize();
Zachary Turner9e7dda32017-04-12 23:18:51 +000066 const PDBSymbolTypeUDT &Class = Layout.getClass();
67
Zachary Turner7797c722015-03-02 04:39:56 +000068 WithColor(Printer, PDB_ColorItem::Keyword).get() << Class.getUdtKind() << " ";
Zachary Turner2d11c202015-02-27 09:15:59 +000069 WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
Zachary Turnerc883a8c2017-04-12 23:18:21 +000070 WithColor(Printer, PDB_ColorItem::Comment).get() << " [sizeof = " << Size
71 << "]";
Zachary Turner4dc4f012017-04-13 21:11:00 +000072 uint32_t BaseCount = Layout.bases().size();
Zachary Turner16901642017-04-24 17:47:24 +000073 if (BaseCount > 0) {
Zachary Turner7797c722015-03-02 04:39:56 +000074 Printer.Indent();
Zachary Turner16901642017-04-24 17:47:24 +000075 char NextSeparator = ':';
Zachary Turner4dc4f012017-04-13 21:11:00 +000076 for (auto BC : Layout.bases()) {
Zachary Turner9e7dda32017-04-12 23:18:51 +000077 const auto &Base = BC->getBase();
Zachary Turner16901642017-04-24 17:47:24 +000078 if (Base.isIndirectVirtualBaseClass())
79 continue;
80
81 Printer.NewLine();
82 Printer << NextSeparator << " ";
83 WithColor(Printer, PDB_ColorItem::Keyword).get() << Base.getAccess();
84 if (BC->isVirtualBase())
85 WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
86
87 WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base.getName();
88 NextSeparator = ',';
Zachary Turner4dc4f012017-04-13 21:11:00 +000089 }
90
Zachary Turner7797c722015-03-02 04:39:56 +000091 Printer.Unindent();
92 }
93
Zachary Turner2d11c202015-02-27 09:15:59 +000094 Printer << " {";
Zachary Turner10169b62017-04-06 23:43:39 +000095 Printer.Indent();
Zachary Turner9e7dda32017-04-12 23:18:51 +000096}
Zachary Turner29c69102015-02-23 05:58:34 +000097
Zachary Turner9e7dda32017-04-12 23:18:51 +000098void ClassDefinitionDumper::prettyPrintClassOutro(const ClassLayout &Layout) {
Zachary Turner10169b62017-04-06 23:43:39 +000099 Printer.Unindent();
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000100 if (DumpedAnything)
Zachary Turner2d11c202015-02-27 09:15:59 +0000101 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000102 Printer << "}";
Zachary Turner0c990bbe2017-04-10 19:33:29 +0000103 Printer.NewLine();
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000104 if (Layout.deepPaddingSize() > 0) {
Zachary Turner9e7dda32017-04-12 23:18:51 +0000105 APFloat Pct(100.0 * (double)Layout.deepPaddingSize() /
Zachary Turner16901642017-04-24 17:47:24 +0000106 (double)Layout.getSize());
Zachary Turner0c990bbe2017-04-10 19:33:29 +0000107 SmallString<8> PctStr;
108 Pct.toString(PctStr, 4);
109 WithColor(Printer, PDB_ColorItem::Padding).get()
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000110 << "Total padding " << Layout.deepPaddingSize() << " bytes (" << PctStr
Zachary Turner0c990bbe2017-04-10 19:33:29 +0000111 << "% of class size)";
112 Printer.NewLine();
113 }
Zachary Turner29c69102015-02-23 05:58:34 +0000114}