blob: 651cb8b7649e40459f2edb12416ede722c30a497 [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"
Zachary Turnerbd336e42017-06-09 20:46:17 +000014#include "llvm-pdbutil.h"
Zachary Turner29c69102015-02-23 05:58:34 +000015
Zachary Turner0c990bbe2017-04-10 19:33:29 +000016#include "llvm/ADT/APFloat.h"
17#include "llvm/ADT/SmallString.h"
Zachary Turner29c69102015-02-23 05:58:34 +000018#include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
Zachary Turner29c69102015-02-23 05:58:34 +000019#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
Zachary Turnerc883a8c2017-04-12 23:18:21 +000020#include "llvm/DebugInfo/PDB/UDTLayout.h"
21
Zachary Turner29c69102015-02-23 05:58:34 +000022#include "llvm/Support/Format.h"
23
24using namespace llvm;
Zachary Turnerec28fc32016-05-04 20:32:13 +000025using namespace llvm::pdb;
Zachary Turner29c69102015-02-23 05:58:34 +000026
Zachary Turner2d11c202015-02-27 09:15:59 +000027ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
28 : PDBSymDumper(true), Printer(P) {}
Zachary Turner29c69102015-02-23 05:58:34 +000029
Zachary Turnerb52d08d2015-03-01 06:51:29 +000030void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
Zachary Turner10169b62017-04-06 23:43:39 +000031 assert(opts::pretty::ClassFormat !=
32 opts::pretty::ClassDefinitionFormat::None);
33
Zachary Turner9e7dda32017-04-12 23:18:51 +000034 ClassLayout Layout(Class);
Zachary Turner4dc4f012017-04-13 21:11:00 +000035 start(Layout);
36}
Zachary Turnerc883a8c2017-04-12 23:18:21 +000037
Zachary Turner4dc4f012017-04-13 21:11:00 +000038void ClassDefinitionDumper::start(const ClassLayout &Layout) {
Zachary Turner9e7dda32017-04-12 23:18:51 +000039 prettyPrintClassIntro(Layout);
40
Zachary Turnerda949c12017-04-24 17:47:52 +000041 PrettyClassLayoutGraphicalDumper Dumper(Printer, 1, 0);
42 DumpedAnything |= Dumper.start(Layout);
Zachary Turner9e7dda32017-04-12 23:18:51 +000043
44 prettyPrintClassOutro(Layout);
45}
46
47void ClassDefinitionDumper::prettyPrintClassIntro(const ClassLayout &Layout) {
48 DumpedAnything = false;
Zachary Turner0c990bbe2017-04-10 19:33:29 +000049 Printer.NewLine();
Zachary Turner0c990bbe2017-04-10 19:33:29 +000050
Zachary Turner16901642017-04-24 17:47:24 +000051 uint32_t Size = Layout.getSize();
Zachary Turner9e7dda32017-04-12 23:18:51 +000052 const PDBSymbolTypeUDT &Class = Layout.getClass();
53
Zachary Turner7797c722015-03-02 04:39:56 +000054 WithColor(Printer, PDB_ColorItem::Keyword).get() << Class.getUdtKind() << " ";
Zachary Turner2d11c202015-02-27 09:15:59 +000055 WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
Zachary Turnerc883a8c2017-04-12 23:18:21 +000056 WithColor(Printer, PDB_ColorItem::Comment).get() << " [sizeof = " << Size
57 << "]";
Zachary Turner4dc4f012017-04-13 21:11:00 +000058 uint32_t BaseCount = Layout.bases().size();
Zachary Turner16901642017-04-24 17:47:24 +000059 if (BaseCount > 0) {
Zachary Turner7797c722015-03-02 04:39:56 +000060 Printer.Indent();
Zachary Turner16901642017-04-24 17:47:24 +000061 char NextSeparator = ':';
Zachary Turner4dc4f012017-04-13 21:11:00 +000062 for (auto BC : Layout.bases()) {
Zachary Turner9e7dda32017-04-12 23:18:51 +000063 const auto &Base = BC->getBase();
Zachary Turner16901642017-04-24 17:47:24 +000064 if (Base.isIndirectVirtualBaseClass())
65 continue;
66
67 Printer.NewLine();
68 Printer << NextSeparator << " ";
69 WithColor(Printer, PDB_ColorItem::Keyword).get() << Base.getAccess();
70 if (BC->isVirtualBase())
71 WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
72
73 WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base.getName();
74 NextSeparator = ',';
Zachary Turner4dc4f012017-04-13 21:11:00 +000075 }
76
Zachary Turner7797c722015-03-02 04:39:56 +000077 Printer.Unindent();
78 }
79
Zachary Turner2d11c202015-02-27 09:15:59 +000080 Printer << " {";
Zachary Turner10169b62017-04-06 23:43:39 +000081 Printer.Indent();
Zachary Turner9e7dda32017-04-12 23:18:51 +000082}
Zachary Turner29c69102015-02-23 05:58:34 +000083
Zachary Turner9e7dda32017-04-12 23:18:51 +000084void ClassDefinitionDumper::prettyPrintClassOutro(const ClassLayout &Layout) {
Zachary Turner10169b62017-04-06 23:43:39 +000085 Printer.Unindent();
Zachary Turnerc883a8c2017-04-12 23:18:21 +000086 if (DumpedAnything)
Zachary Turner2d11c202015-02-27 09:15:59 +000087 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +000088 Printer << "}";
Zachary Turner0c990bbe2017-04-10 19:33:29 +000089 Printer.NewLine();
Zachary Turnerc883a8c2017-04-12 23:18:21 +000090 if (Layout.deepPaddingSize() > 0) {
Zachary Turner9e7dda32017-04-12 23:18:51 +000091 APFloat Pct(100.0 * (double)Layout.deepPaddingSize() /
Zachary Turner16901642017-04-24 17:47:24 +000092 (double)Layout.getSize());
Zachary Turner0c990bbe2017-04-10 19:33:29 +000093 SmallString<8> PctStr;
94 Pct.toString(PctStr, 4);
95 WithColor(Printer, PDB_ColorItem::Padding).get()
Zachary Turnerc883a8c2017-04-12 23:18:21 +000096 << "Total padding " << Layout.deepPaddingSize() << " bytes (" << PctStr
Zachary Turner0c990bbe2017-04-10 19:33:29 +000097 << "% of class size)";
98 Printer.NewLine();
Zachary Turnerda307b62017-04-25 20:22:29 +000099 APFloat Pct2(100.0 * (double)Layout.immediatePadding() /
100 (double)Layout.getSize());
101 PctStr.clear();
102 Pct2.toString(PctStr, 4);
103 WithColor(Printer, PDB_ColorItem::Padding).get()
104 << "Immediate padding " << Layout.immediatePadding() << " bytes ("
105 << PctStr << "% of class size)";
106 Printer.NewLine();
Zachary Turner0c990bbe2017-04-10 19:33:29 +0000107 }
Zachary Turner29c69102015-02-23 05:58:34 +0000108}