InheritViz.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //===- InheritViz.cpp - Graphviz visualization for inheritance --*- 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. //
  9. // This file implements CXXRecordDecl::viewInheritance, which
  10. // generates a GraphViz DOT file that depicts the class inheritance
  11. // diagram and then calls Graphviz/dot+gv on it.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclCXX.h"
  17. #include "clang/AST/TypeOrdering.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/GraphWriter.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <map>
  22. #include <set>
  23. using namespace clang;
  24. namespace {
  25. /// InheritanceHierarchyWriter - Helper class that writes out a
  26. /// GraphViz file that diagrams the inheritance hierarchy starting at
  27. /// a given C++ class type. Note that we do not use LLVM's
  28. /// GraphWriter, because the interface does not permit us to properly
  29. /// differentiate between uses of types as virtual bases
  30. /// vs. non-virtual bases.
  31. class InheritanceHierarchyWriter {
  32. ASTContext& Context;
  33. raw_ostream &Out;
  34. std::map<QualType, int, QualTypeOrdering> DirectBaseCount;
  35. std::set<QualType, QualTypeOrdering> KnownVirtualBases;
  36. public:
  37. InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)
  38. : Context(Context), Out(Out) { }
  39. void WriteGraph(QualType Type) {
  40. Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString())
  41. << "\" {\n";
  42. WriteNode(Type, false);
  43. Out << "}\n";
  44. }
  45. protected:
  46. /// WriteNode - Write out the description of node in the inheritance
  47. /// diagram, which may be a base class or it may be the root node.
  48. void WriteNode(QualType Type, bool FromVirtual);
  49. /// WriteNodeReference - Write out a reference to the given node,
  50. /// using a unique identifier for each direct base and for the
  51. /// (only) virtual base.
  52. raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);
  53. };
  54. } // namespace
  55. void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {
  56. QualType CanonType = Context.getCanonicalType(Type);
  57. if (FromVirtual) {
  58. if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end())
  59. return;
  60. // We haven't seen this virtual base before, so display it and
  61. // its bases.
  62. KnownVirtualBases.insert(CanonType);
  63. }
  64. // Declare the node itself.
  65. Out << " ";
  66. WriteNodeReference(Type, FromVirtual);
  67. // Give the node a label based on the name of the class.
  68. std::string TypeName = Type.getAsString();
  69. Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName);
  70. // If the name of the class was a typedef or something different
  71. // from the "real" class name, show the real class name in
  72. // parentheses so we don't confuse ourselves.
  73. if (TypeName != CanonType.getAsString()) {
  74. Out << "\\n(" << CanonType.getAsString() << ")";
  75. }
  76. // Finished describing the node.
  77. Out << " \"];\n";
  78. // Display the base classes.
  79. const auto *Decl =
  80. static_cast<const CXXRecordDecl *>(Type->castAs<RecordType>()->getDecl());
  81. for (const auto &Base : Decl->bases()) {
  82. QualType CanonBaseType = Context.getCanonicalType(Base.getType());
  83. // If this is not virtual inheritance, bump the direct base
  84. // count for the type.
  85. if (!Base.isVirtual())
  86. ++DirectBaseCount[CanonBaseType];
  87. // Write out the node (if we need to).
  88. WriteNode(Base.getType(), Base.isVirtual());
  89. // Write out the edge.
  90. Out << " ";
  91. WriteNodeReference(Type, FromVirtual);
  92. Out << " -> ";
  93. WriteNodeReference(Base.getType(), Base.isVirtual());
  94. // Write out edge attributes to show the kind of inheritance.
  95. if (Base.isVirtual()) {
  96. Out << " [ style=\"dashed\" ]";
  97. }
  98. Out << ";";
  99. }
  100. }
  101. /// WriteNodeReference - Write out a reference to the given node,
  102. /// using a unique identifier for each direct base and for the
  103. /// (only) virtual base.
  104. raw_ostream&
  105. InheritanceHierarchyWriter::WriteNodeReference(QualType Type,
  106. bool FromVirtual) {
  107. QualType CanonType = Context.getCanonicalType(Type);
  108. Out << "Class_" << CanonType.getAsOpaquePtr();
  109. if (!FromVirtual)
  110. Out << "_" << DirectBaseCount[CanonType];
  111. return Out;
  112. }
  113. /// viewInheritance - Display the inheritance hierarchy of this C++
  114. /// class using GraphViz.
  115. void CXXRecordDecl::viewInheritance(ASTContext& Context) const {
  116. QualType Self = Context.getTypeDeclType(this);
  117. int FD;
  118. SmallString<128> Filename;
  119. if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
  120. Self.getAsString(), "dot", FD, Filename)) {
  121. llvm::errs() << "Error: " << EC.message() << "\n";
  122. return;
  123. }
  124. llvm::errs() << "Writing '" << Filename << "'... ";
  125. llvm::raw_fd_ostream O(FD, true);
  126. InheritanceHierarchyWriter Writer(Context, O);
  127. Writer.WriteGraph(Self);
  128. llvm::errs() << " done. \n";
  129. O.close();
  130. // Display the graph
  131. DisplayGraph(Filename);
  132. }