InheritViz.cpp 5.1 KB

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