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