Douglas Gregor | 0218936 | 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 | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 20 | #include "llvm/Support/GraphWriter.h" |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 22 | #include <map> |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace clang { |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 27 | |
| 28 | /// InheritanceHierarchyWriter - Helper class that writes out a |
| 29 | /// GraphViz file that diagrams the inheritance hierarchy starting at |
| 30 | /// a given C++ class type. Note that we do not use LLVM's |
| 31 | /// GraphWriter, because the interface does not permit us to properly |
| 32 | /// differentiate between uses of types as virtual bases |
| 33 | /// vs. non-virtual bases. |
| 34 | class InheritanceHierarchyWriter { |
| 35 | ASTContext& Context; |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 36 | llvm::raw_ostream &Out; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 37 | std::map<QualType, int, QualTypeOrdering> DirectBaseCount; |
| 38 | std::set<QualType, QualTypeOrdering> KnownVirtualBases; |
| 39 | |
| 40 | public: |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 41 | InheritanceHierarchyWriter(ASTContext& Context, llvm::raw_ostream& Out) |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 42 | : Context(Context), Out(Out) { } |
| 43 | |
| 44 | void WriteGraph(QualType Type) { |
| 45 | Out << "digraph \"" << DOT::EscapeString(Type.getAsString()) << "\" {\n"; |
| 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. |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 58 | llvm::raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual); |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) { |
| 62 | QualType CanonType = Context.getCanonicalType(Type); |
| 63 | |
| 64 | if (FromVirtual) { |
| 65 | if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end()) |
| 66 | return; |
| 67 | |
| 68 | // We haven't seen this virtual base before, so display it and |
| 69 | // its bases. |
| 70 | KnownVirtualBases.insert(CanonType); |
| 71 | } |
| 72 | |
| 73 | // Declare the node itself. |
| 74 | Out << " "; |
| 75 | WriteNodeReference(Type, FromVirtual); |
| 76 | |
| 77 | // Give the node a label based on the name of the class. |
| 78 | std::string TypeName = Type.getAsString(); |
| 79 | Out << " [ shape=\"box\", label=\"" << DOT::EscapeString(TypeName); |
| 80 | |
| 81 | // If the name of the class was a typedef or something different |
| 82 | // from the "real" class name, show the real class name in |
| 83 | // parentheses so we don't confuse ourselves. |
| 84 | if (TypeName != CanonType.getAsString()) { |
| 85 | Out << "\\n(" << CanonType.getAsString() << ")"; |
| 86 | } |
| 87 | |
| 88 | // Finished describing the node. |
| 89 | Out << " \"];\n"; |
| 90 | |
| 91 | // Display the base classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | const CXXRecordDecl *Decl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 93 | = static_cast<const CXXRecordDecl *>(Type->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 94 | for (CXXRecordDecl::base_class_const_iterator Base = Decl->bases_begin(); |
| 95 | Base != Decl->bases_end(); ++Base) { |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 96 | QualType CanonBaseType = Context.getCanonicalType(Base->getType()); |
| 97 | |
| 98 | // If this is not virtual inheritance, bump the direct base |
| 99 | // count for the type. |
| 100 | if (!Base->isVirtual()) |
| 101 | ++DirectBaseCount[CanonBaseType]; |
| 102 | |
| 103 | // Write out the node (if we need to). |
| 104 | WriteNode(Base->getType(), Base->isVirtual()); |
| 105 | |
| 106 | // Write out the edge. |
| 107 | Out << " "; |
| 108 | WriteNodeReference(Type, FromVirtual); |
| 109 | Out << " -> "; |
| 110 | WriteNodeReference(Base->getType(), Base->isVirtual()); |
| 111 | |
| 112 | // Write out edge attributes to show the kind of inheritance. |
| 113 | if (Base->isVirtual()) { |
| 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. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | llvm::raw_ostream& |
| 124 | InheritanceHierarchyWriter::WriteNodeReference(QualType Type, |
Douglas Gregor | 0218936 | 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 | 1f81230 | 2008-10-24 19:53:54 +0000 | [diff] [blame] | 136 | void CXXRecordDecl::viewInheritance(ASTContext& Context) const { |
| 137 | QualType Self = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this)); |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 138 | std::string ErrMsg; |
| 139 | sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg); |
| 140 | if (Filename.isEmpty()) { |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame] | 141 | llvm::errs() << "Error: " << ErrMsg << "\n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 142 | return; |
| 143 | } |
Douglas Gregor | 1f81230 | 2008-10-24 19:53:54 +0000 | [diff] [blame] | 144 | Filename.appendComponent(Self.getAsString() + ".dot"); |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 145 | if (Filename.makeUnique(true,&ErrMsg)) { |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame] | 146 | llvm::errs() << "Error: " << ErrMsg << "\n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame] | 150 | llvm::errs() << "Writing '" << Filename.c_str() << "'... "; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 151 | |
Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 152 | llvm::raw_fd_ostream O(Filename.c_str(), ErrMsg); |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 153 | |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 154 | if (ErrMsg.empty()) { |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 155 | InheritanceHierarchyWriter Writer(Context, O); |
Douglas Gregor | 1f81230 | 2008-10-24 19:53:54 +0000 | [diff] [blame] | 156 | Writer.WriteGraph(Self); |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame] | 157 | llvm::errs() << " done. \n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 158 | |
| 159 | O.close(); |
| 160 | |
| 161 | // Display the graph |
| 162 | DisplayGraph(Filename); |
| 163 | } else { |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 164 | llvm::errs() << "error opening file for writing!\n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 165 | } |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | } |