blob: f009f53a39325494136506c5480792ecd0b3db48 [file] [log] [blame]
Zachary Turnereb6ab042017-01-11 00:35:43 +00001//===- PrettyClassDefinitionDumper.cpp --------------------------*- C++ -*-===//
Zachary Turner92d755e2015-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 Turnereb6ab042017-01-11 00:35:43 +000010#include "PrettyClassDefinitionDumper.h"
11
Zachary Turner756b8232015-02-27 09:15:59 +000012#include "LinePrinter.h"
Zachary Turner67dcd802017-04-12 23:18:51 +000013#include "PrettyClassLayoutGraphicalDumper.h"
Zachary Turnercfb13562017-06-09 20:46:17 +000014#include "llvm-pdbutil.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000015
Zachary Turnere98c9132017-04-10 19:33:29 +000016#include "llvm/ADT/APFloat.h"
17#include "llvm/ADT/SmallString.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000018#include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
Zachary Turner92d755e2015-02-23 05:58:34 +000019#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
Zachary Turner7724dc62017-04-12 23:18:21 +000020#include "llvm/DebugInfo/PDB/UDTLayout.h"
21
Zachary Turner92d755e2015-02-23 05:58:34 +000022#include "llvm/Support/Format.h"
23
24using namespace llvm;
Zachary Turnerc95df942016-05-04 20:32:13 +000025using namespace llvm::pdb;
Zachary Turner92d755e2015-02-23 05:58:34 +000026
Zachary Turner756b8232015-02-27 09:15:59 +000027ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
28 : PDBSymDumper(true), Printer(P) {}
Zachary Turner92d755e2015-02-23 05:58:34 +000029
Zachary Turner53e4b562015-03-01 06:51:29 +000030void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
Zachary Turnerb4706102017-04-06 23:43:39 +000031 assert(opts::pretty::ClassFormat !=
32 opts::pretty::ClassDefinitionFormat::None);
33
Zachary Turner67dcd802017-04-12 23:18:51 +000034 ClassLayout Layout(Class);
Zachary Turner10683342017-04-13 21:11:00 +000035 start(Layout);
36}
Zachary Turner7724dc62017-04-12 23:18:21 +000037
Zachary Turner10683342017-04-13 21:11:00 +000038void ClassDefinitionDumper::start(const ClassLayout &Layout) {
Zachary Turner67dcd802017-04-12 23:18:51 +000039 prettyPrintClassIntro(Layout);
40
Zachary Turner41374872017-04-24 17:47:52 +000041 PrettyClassLayoutGraphicalDumper Dumper(Printer, 1, 0);
42 DumpedAnything |= Dumper.start(Layout);
Zachary Turner67dcd802017-04-12 23:18:51 +000043
44 prettyPrintClassOutro(Layout);
45}
46
47void ClassDefinitionDumper::prettyPrintClassIntro(const ClassLayout &Layout) {
48 DumpedAnything = false;
Zachary Turnere98c9132017-04-10 19:33:29 +000049 Printer.NewLine();
Zachary Turnere98c9132017-04-10 19:33:29 +000050
Zachary Turner0e8b7072017-04-24 17:47:24 +000051 uint32_t Size = Layout.getSize();
Zachary Turner67dcd802017-04-12 23:18:51 +000052 const PDBSymbolTypeUDT &Class = Layout.getClass();
53
Zachary Turner903e31c2018-09-14 22:29:19 +000054 if (Layout.getClass().isConstType())
55 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
56 if (Layout.getClass().isVolatileType())
57 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
58 if (Layout.getClass().isUnalignedType())
59 WithColor(Printer, PDB_ColorItem::Keyword).get() << "unaligned ";
60
Zachary Turner0c7c98a2015-03-02 04:39:56 +000061 WithColor(Printer, PDB_ColorItem::Keyword).get() << Class.getUdtKind() << " ";
Zachary Turner756b8232015-02-27 09:15:59 +000062 WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
Zachary Turner7724dc62017-04-12 23:18:21 +000063 WithColor(Printer, PDB_ColorItem::Comment).get() << " [sizeof = " << Size
64 << "]";
Zachary Turner10683342017-04-13 21:11:00 +000065 uint32_t BaseCount = Layout.bases().size();
Zachary Turner0e8b7072017-04-24 17:47:24 +000066 if (BaseCount > 0) {
Zachary Turner0c7c98a2015-03-02 04:39:56 +000067 Printer.Indent();
Zachary Turner0e8b7072017-04-24 17:47:24 +000068 char NextSeparator = ':';
Zachary Turner10683342017-04-13 21:11:00 +000069 for (auto BC : Layout.bases()) {
Zachary Turner67dcd802017-04-12 23:18:51 +000070 const auto &Base = BC->getBase();
Zachary Turner0e8b7072017-04-24 17:47:24 +000071 if (Base.isIndirectVirtualBaseClass())
72 continue;
73
74 Printer.NewLine();
75 Printer << NextSeparator << " ";
76 WithColor(Printer, PDB_ColorItem::Keyword).get() << Base.getAccess();
77 if (BC->isVirtualBase())
78 WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
79
80 WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base.getName();
81 NextSeparator = ',';
Zachary Turner10683342017-04-13 21:11:00 +000082 }
83
Zachary Turner0c7c98a2015-03-02 04:39:56 +000084 Printer.Unindent();
85 }
86
Zachary Turner756b8232015-02-27 09:15:59 +000087 Printer << " {";
Zachary Turnerb4706102017-04-06 23:43:39 +000088 Printer.Indent();
Zachary Turner67dcd802017-04-12 23:18:51 +000089}
Zachary Turner92d755e2015-02-23 05:58:34 +000090
Zachary Turner67dcd802017-04-12 23:18:51 +000091void ClassDefinitionDumper::prettyPrintClassOutro(const ClassLayout &Layout) {
Zachary Turnerb4706102017-04-06 23:43:39 +000092 Printer.Unindent();
Zachary Turner7724dc62017-04-12 23:18:21 +000093 if (DumpedAnything)
Zachary Turner756b8232015-02-27 09:15:59 +000094 Printer.NewLine();
Zachary Turner53e4b562015-03-01 06:51:29 +000095 Printer << "}";
Zachary Turnere98c9132017-04-10 19:33:29 +000096 Printer.NewLine();
Zachary Turner7724dc62017-04-12 23:18:21 +000097 if (Layout.deepPaddingSize() > 0) {
Zachary Turner67dcd802017-04-12 23:18:51 +000098 APFloat Pct(100.0 * (double)Layout.deepPaddingSize() /
Zachary Turner0e8b7072017-04-24 17:47:24 +000099 (double)Layout.getSize());
Zachary Turnere98c9132017-04-10 19:33:29 +0000100 SmallString<8> PctStr;
101 Pct.toString(PctStr, 4);
102 WithColor(Printer, PDB_ColorItem::Padding).get()
Zachary Turner7724dc62017-04-12 23:18:21 +0000103 << "Total padding " << Layout.deepPaddingSize() << " bytes (" << PctStr
Zachary Turnere98c9132017-04-10 19:33:29 +0000104 << "% of class size)";
105 Printer.NewLine();
Zachary Turner5cec42a2017-04-25 20:22:29 +0000106 APFloat Pct2(100.0 * (double)Layout.immediatePadding() /
107 (double)Layout.getSize());
108 PctStr.clear();
109 Pct2.toString(PctStr, 4);
110 WithColor(Printer, PDB_ColorItem::Padding).get()
111 << "Immediate padding " << Layout.immediatePadding() << " bytes ("
112 << PctStr << "% of class size)";
113 Printer.NewLine();
Zachary Turnere98c9132017-04-10 19:33:29 +0000114 }
Zachary Turner92d755e2015-02-23 05:58:34 +0000115}