PrettyClassDefinitionDumper.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include "PrettyClassDefinitionDumper.h"
  9. #include "PrettyClassLayoutGraphicalDumper.h"
  10. #include "llvm-pdbutil.h"
  11. #include "llvm/ADT/APFloat.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
  14. #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
  15. #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
  16. #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
  17. #include "llvm/DebugInfo/PDB/UDTLayout.h"
  18. #include "llvm/Support/Format.h"
  19. using namespace llvm;
  20. using namespace llvm::pdb;
  21. ClassDefinitionDumper::ClassDefinitionDumper(LinePrinter &P)
  22. : PDBSymDumper(true), Printer(P) {}
  23. void ClassDefinitionDumper::start(const PDBSymbolTypeUDT &Class) {
  24. assert(opts::pretty::ClassFormat !=
  25. opts::pretty::ClassDefinitionFormat::None);
  26. ClassLayout Layout(Class);
  27. start(Layout);
  28. }
  29. void ClassDefinitionDumper::start(const ClassLayout &Layout) {
  30. prettyPrintClassIntro(Layout);
  31. PrettyClassLayoutGraphicalDumper Dumper(Printer, 1, 0);
  32. DumpedAnything |= Dumper.start(Layout);
  33. prettyPrintClassOutro(Layout);
  34. }
  35. void ClassDefinitionDumper::prettyPrintClassIntro(const ClassLayout &Layout) {
  36. DumpedAnything = false;
  37. Printer.NewLine();
  38. uint32_t Size = Layout.getSize();
  39. const PDBSymbolTypeUDT &Class = Layout.getClass();
  40. if (Layout.getClass().isConstType())
  41. WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
  42. if (Layout.getClass().isVolatileType())
  43. WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
  44. if (Layout.getClass().isUnalignedType())
  45. WithColor(Printer, PDB_ColorItem::Keyword).get() << "unaligned ";
  46. WithColor(Printer, PDB_ColorItem::Keyword).get() << Class.getUdtKind() << " ";
  47. WithColor(Printer, PDB_ColorItem::Type).get() << Class.getName();
  48. WithColor(Printer, PDB_ColorItem::Comment).get() << " [sizeof = " << Size
  49. << "]";
  50. uint32_t BaseCount = Layout.bases().size();
  51. if (BaseCount > 0) {
  52. Printer.Indent();
  53. char NextSeparator = ':';
  54. for (auto *BC : Layout.bases()) {
  55. const auto &Base = BC->getBase();
  56. if (Base.isIndirectVirtualBaseClass())
  57. continue;
  58. Printer.NewLine();
  59. Printer << NextSeparator << " ";
  60. WithColor(Printer, PDB_ColorItem::Keyword).get() << Base.getAccess();
  61. if (BC->isVirtualBase())
  62. WithColor(Printer, PDB_ColorItem::Keyword).get() << " virtual";
  63. WithColor(Printer, PDB_ColorItem::Type).get() << " " << Base.getName();
  64. NextSeparator = ',';
  65. }
  66. Printer.Unindent();
  67. }
  68. Printer << " {";
  69. Printer.Indent();
  70. }
  71. void ClassDefinitionDumper::prettyPrintClassOutro(const ClassLayout &Layout) {
  72. Printer.Unindent();
  73. if (DumpedAnything)
  74. Printer.NewLine();
  75. Printer << "}";
  76. Printer.NewLine();
  77. if (Layout.deepPaddingSize() > 0) {
  78. APFloat Pct(100.0 * (double)Layout.deepPaddingSize() /
  79. (double)Layout.getSize());
  80. SmallString<8> PctStr;
  81. Pct.toString(PctStr, 4);
  82. WithColor(Printer, PDB_ColorItem::Padding).get()
  83. << "Total padding " << Layout.deepPaddingSize() << " bytes (" << PctStr
  84. << "% of class size)";
  85. Printer.NewLine();
  86. APFloat Pct2(100.0 * (double)Layout.immediatePadding() /
  87. (double)Layout.getSize());
  88. PctStr.clear();
  89. Pct2.toString(PctStr, 4);
  90. WithColor(Printer, PDB_ColorItem::Padding).get()
  91. << "Immediate padding " << Layout.immediatePadding() << " bytes ("
  92. << PctStr << "% of class size)";
  93. Printer.NewLine();
  94. }
  95. }