| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 1 | //===- InheritViz.cpp - Graphviz visualization for inheritance --*- C++ -*-===// | 
|  | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 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 |  | 
|  | 15 | #include "clang/AST/ASTContext.h" | 
|  | 16 | #include "clang/AST/Decl.h" | 
|  | 17 | #include "clang/AST/DeclCXX.h" | 
|  | 18 | #include "clang/AST/TypeOrdering.h" | 
| Rafael Espindola | 36da854 | 2013-06-24 15:00:11 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 20 | #include "llvm/Support/GraphWriter.h" | 
| Ted Kremenek | 40fcc59 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 22 | #include <map> | 
| Rafael Espindola | 76993a0 | 2013-06-19 15:31:27 +0000 | [diff] [blame] | 23 | #include <set> | 
| Benjamin Kramer | 6afa168 | 2015-03-09 15:03:20 +0000 | [diff] [blame] | 24 | using namespace clang; | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 25 |  | 
| Benjamin Kramer | 6afa168 | 2015-03-09 15:03:20 +0000 | [diff] [blame] | 26 | namespace { | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | /// InheritanceHierarchyWriter - Helper class that writes out a | 
|  | 28 | /// GraphViz file that diagrams the inheritance hierarchy starting at | 
|  | 29 | /// a given C++ class type. Note that we do not use LLVM's | 
|  | 30 | /// GraphWriter, because the interface does not permit us to properly | 
|  | 31 | /// differentiate between uses of types as virtual bases | 
|  | 32 | /// vs. non-virtual bases. | 
|  | 33 | class InheritanceHierarchyWriter { | 
|  | 34 | ASTContext& Context; | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 35 | raw_ostream &Out; | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 36 | std::map<QualType, int, QualTypeOrdering> DirectBaseCount; | 
|  | 37 | std::set<QualType, QualTypeOrdering> KnownVirtualBases; | 
|  | 38 |  | 
|  | 39 | public: | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 40 | InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out) | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 41 | : Context(Context), Out(Out) { } | 
|  | 42 |  | 
|  | 43 | void WriteGraph(QualType Type) { | 
| Benjamin Kramer | 6afa168 | 2015-03-09 15:03:20 +0000 | [diff] [blame] | 44 | Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString()) | 
|  | 45 | << "\" {\n"; | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 46 | WriteNode(Type, false); | 
|  | 47 | Out << "}\n"; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | protected: | 
|  | 51 | /// WriteNode - Write out the description of node in the inheritance | 
|  | 52 | /// diagram, which may be a base class or it may be the root node. | 
|  | 53 | void WriteNode(QualType Type, bool FromVirtual); | 
|  | 54 |  | 
|  | 55 | /// WriteNodeReference - Write out a reference to the given node, | 
|  | 56 | /// using a unique identifier for each direct base and for the | 
|  | 57 | /// (only) virtual base. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 58 | raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual); | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 59 | }; | 
| Benjamin Kramer | 6afa168 | 2015-03-09 15:03:20 +0000 | [diff] [blame] | 60 | } // namespace | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 61 |  | 
|  | 62 | void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) { | 
|  | 63 | QualType CanonType = Context.getCanonicalType(Type); | 
|  | 64 |  | 
|  | 65 | if (FromVirtual) { | 
|  | 66 | if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end()) | 
|  | 67 | return; | 
|  | 68 |  | 
|  | 69 | // We haven't seen this virtual base before, so display it and | 
|  | 70 | // its bases. | 
|  | 71 | KnownVirtualBases.insert(CanonType); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | // Declare the node itself. | 
|  | 75 | Out << "  "; | 
|  | 76 | WriteNodeReference(Type, FromVirtual); | 
|  | 77 |  | 
|  | 78 | // Give the node a label based on the name of the class. | 
|  | 79 | std::string TypeName = Type.getAsString(); | 
| Benjamin Kramer | 6afa168 | 2015-03-09 15:03:20 +0000 | [diff] [blame] | 80 | Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName); | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 81 |  | 
|  | 82 | // If the name of the class was a typedef or something different | 
|  | 83 | // from the "real" class name, show the real class name in | 
|  | 84 | // parentheses so we don't confuse ourselves. | 
|  | 85 | if (TypeName != CanonType.getAsString()) { | 
|  | 86 | Out << "\\n(" << CanonType.getAsString() << ")"; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | // Finished describing the node. | 
|  | 90 | Out << " \"];\n"; | 
|  | 91 |  | 
|  | 92 | // Display the base classes. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | const CXXRecordDecl *Decl | 
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 94 | = static_cast<const CXXRecordDecl *>(Type->getAs<RecordType>()->getDecl()); | 
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 95 | for (const auto &Base : Decl->bases()) { | 
|  | 96 | QualType CanonBaseType = Context.getCanonicalType(Base.getType()); | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 97 |  | 
|  | 98 | // If this is not virtual inheritance, bump the direct base | 
|  | 99 | // count for the type. | 
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 100 | if (!Base.isVirtual()) | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 101 | ++DirectBaseCount[CanonBaseType]; | 
|  | 102 |  | 
|  | 103 | // Write out the node (if we need to). | 
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 104 | WriteNode(Base.getType(), Base.isVirtual()); | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 105 |  | 
|  | 106 | // Write out the edge. | 
|  | 107 | Out << "  "; | 
|  | 108 | WriteNodeReference(Type, FromVirtual); | 
|  | 109 | Out << " -> "; | 
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 110 | WriteNodeReference(Base.getType(), Base.isVirtual()); | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 111 |  | 
|  | 112 | // Write out edge attributes to show the kind of inheritance. | 
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 113 | if (Base.isVirtual()) { | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 114 | Out << " [ style=\"dashed\" ]"; | 
|  | 115 | } | 
|  | 116 | Out << ";"; | 
|  | 117 | } | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | /// WriteNodeReference - Write out a reference to the given node, | 
|  | 121 | /// using a unique identifier for each direct base and for the | 
|  | 122 | /// (only) virtual base. | 
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 123 | raw_ostream& | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | InheritanceHierarchyWriter::WriteNodeReference(QualType Type, | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 125 | bool FromVirtual) { | 
|  | 126 | QualType CanonType = Context.getCanonicalType(Type); | 
|  | 127 |  | 
|  | 128 | Out << "Class_" << CanonType.getAsOpaquePtr(); | 
|  | 129 | if (!FromVirtual) | 
|  | 130 | Out << "_" << DirectBaseCount[CanonType]; | 
|  | 131 | return Out; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | /// viewInheritance - Display the inheritance hierarchy of this C++ | 
|  | 135 | /// class using GraphViz. | 
| Douglas Gregor | 3dfef1f | 2008-10-24 19:53:54 +0000 | [diff] [blame] | 136 | void CXXRecordDecl::viewInheritance(ASTContext& Context) const { | 
| Dmitri Gribenko | aaf7f29 | 2013-01-14 00:25:25 +0000 | [diff] [blame] | 137 | QualType Self = Context.getTypeDeclType(this); | 
| Rafael Espindola | 36da854 | 2013-06-24 15:00:11 +0000 | [diff] [blame] | 138 |  | 
|  | 139 | int FD; | 
|  | 140 | SmallString<128> Filename; | 
| Benjamin Kramer | 6afa168 | 2015-03-09 15:03:20 +0000 | [diff] [blame] | 141 | if (std::error_code EC = llvm::sys::fs::createTemporaryFile( | 
|  | 142 | Self.getAsString(), "dot", FD, Filename)) { | 
| Rafael Espindola | 36da854 | 2013-06-24 15:00:11 +0000 | [diff] [blame] | 143 | llvm::errs() << "Error: " << EC.message() << "\n"; | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 144 | return; | 
|  | 145 | } | 
|  | 146 |  | 
| Rafael Espindola | 36da854 | 2013-06-24 15:00:11 +0000 | [diff] [blame] | 147 | llvm::errs() << "Writing '" << Filename << "'... "; | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 148 |  | 
| Rafael Espindola | 36da854 | 2013-06-24 15:00:11 +0000 | [diff] [blame] | 149 | llvm::raw_fd_ostream O(FD, true); | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 150 |  | 
| Rafael Espindola | 36da854 | 2013-06-24 15:00:11 +0000 | [diff] [blame] | 151 | InheritanceHierarchyWriter Writer(Context, O); | 
|  | 152 | Writer.WriteGraph(Self); | 
|  | 153 | llvm::errs() << " done. \n"; | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 154 |  | 
| Rafael Espindola | 36da854 | 2013-06-24 15:00:11 +0000 | [diff] [blame] | 155 | O.close(); | 
| Dan Gohman | 72346e9 | 2011-03-01 19:50:49 +0000 | [diff] [blame] | 156 |  | 
| Rafael Espindola | 36da854 | 2013-06-24 15:00:11 +0000 | [diff] [blame] | 157 | // Display the graph | 
|  | 158 | DisplayGraph(Filename); | 
| Douglas Gregor | dff6a8e | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 159 | } |