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 | |
Douglas Gregor | 7a64a03 | 2008-10-23 03:52:39 +0000 | [diff] [blame] | 28 | #ifndef NDEBUG |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 29 | /// InheritanceHierarchyWriter - Helper class that writes out a |
| 30 | /// GraphViz file that diagrams the inheritance hierarchy starting at |
| 31 | /// a given C++ class type. Note that we do not use LLVM's |
| 32 | /// GraphWriter, because the interface does not permit us to properly |
| 33 | /// differentiate between uses of types as virtual bases |
| 34 | /// vs. non-virtual bases. |
| 35 | class InheritanceHierarchyWriter { |
| 36 | ASTContext& Context; |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 37 | llvm::raw_ostream &Out; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 38 | std::map<QualType, int, QualTypeOrdering> DirectBaseCount; |
| 39 | std::set<QualType, QualTypeOrdering> KnownVirtualBases; |
| 40 | |
| 41 | public: |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 42 | InheritanceHierarchyWriter(ASTContext& Context, llvm::raw_ostream& Out) |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 43 | : Context(Context), Out(Out) { } |
| 44 | |
| 45 | void WriteGraph(QualType Type) { |
| 46 | Out << "digraph \"" << DOT::EscapeString(Type.getAsString()) << "\" {\n"; |
| 47 | WriteNode(Type, false); |
| 48 | Out << "}\n"; |
| 49 | } |
| 50 | |
| 51 | protected: |
| 52 | /// WriteNode - Write out the description of node in the inheritance |
| 53 | /// diagram, which may be a base class or it may be the root node. |
| 54 | void WriteNode(QualType Type, bool FromVirtual); |
| 55 | |
| 56 | /// WriteNodeReference - Write out a reference to the given node, |
| 57 | /// using a unique identifier for each direct base and for the |
| 58 | /// (only) virtual base. |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 59 | llvm::raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual); |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 60 | }; |
| 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(); |
| 80 | Out << " [ shape=\"box\", label=\"" << DOT::EscapeString(TypeName); |
| 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. |
| 93 | const CXXRecordDecl *Decl |
| 94 | = static_cast<const CXXRecordDecl *>(Type->getAsRecordType()->getDecl()); |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 95 | for (CXXRecordDecl::base_class_const_iterator Base = Decl->bases_begin(); |
| 96 | Base != Decl->bases_end(); ++Base) { |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 97 | QualType CanonBaseType = Context.getCanonicalType(Base->getType()); |
| 98 | |
| 99 | // If this is not virtual inheritance, bump the direct base |
| 100 | // count for the type. |
| 101 | if (!Base->isVirtual()) |
| 102 | ++DirectBaseCount[CanonBaseType]; |
| 103 | |
| 104 | // Write out the node (if we need to). |
| 105 | WriteNode(Base->getType(), Base->isVirtual()); |
| 106 | |
| 107 | // Write out the edge. |
| 108 | Out << " "; |
| 109 | WriteNodeReference(Type, FromVirtual); |
| 110 | Out << " -> "; |
| 111 | WriteNodeReference(Base->getType(), Base->isVirtual()); |
| 112 | |
| 113 | // Write out edge attributes to show the kind of inheritance. |
| 114 | if (Base->isVirtual()) { |
| 115 | Out << " [ style=\"dashed\" ]"; |
| 116 | } |
| 117 | Out << ";"; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// WriteNodeReference - Write out a reference to the given node, |
| 122 | /// using a unique identifier for each direct base and for the |
| 123 | /// (only) virtual base. |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 124 | llvm::raw_ostream& |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 125 | InheritanceHierarchyWriter::WriteNodeReference(QualType Type, |
| 126 | bool FromVirtual) { |
| 127 | QualType CanonType = Context.getCanonicalType(Type); |
| 128 | |
| 129 | Out << "Class_" << CanonType.getAsOpaquePtr(); |
| 130 | if (!FromVirtual) |
| 131 | Out << "_" << DirectBaseCount[CanonType]; |
| 132 | return Out; |
| 133 | } |
Douglas Gregor | 7a64a03 | 2008-10-23 03:52:39 +0000 | [diff] [blame] | 134 | #endif |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 135 | |
| 136 | /// viewInheritance - Display the inheritance hierarchy of this C++ |
| 137 | /// class using GraphViz. |
| 138 | void QualType::viewInheritance(ASTContext& Context) { |
| 139 | if (!(*this)->getAsRecordType()) { |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame^] | 140 | llvm::errs() << "Type " << getAsString() << " is not a C++ class type.\n"; |
Douglas Gregor | 5dea189 | 2008-10-22 21:25:12 +0000 | [diff] [blame] | 141 | return; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 142 | } |
| 143 | #ifndef NDEBUG |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 144 | std::string ErrMsg; |
| 145 | sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg); |
| 146 | if (Filename.isEmpty()) { |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame^] | 147 | llvm::errs() << "Error: " << ErrMsg << "\n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | Filename.appendComponent(getAsString() + ".dot"); |
| 151 | if (Filename.makeUnique(true,&ErrMsg)) { |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame^] | 152 | llvm::errs() << "Error: " << ErrMsg << "\n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame^] | 156 | llvm::errs() << "Writing '" << Filename.c_str() << "'... "; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 158 | llvm::raw_fd_ostream O(Filename.c_str(), ErrMsg); |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 160 | if (ErrMsg.empty()) { |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 161 | InheritanceHierarchyWriter Writer(Context, O); |
| 162 | Writer.WriteGraph(*this); |
Ted Kremenek | 6ee9b0f | 2008-10-23 23:55:40 +0000 | [diff] [blame^] | 163 | llvm::errs() << " done. \n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 164 | |
| 165 | O.close(); |
| 166 | |
| 167 | // Display the graph |
| 168 | DisplayGraph(Filename); |
| 169 | } else { |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 170 | llvm::errs() << "error opening file for writing!\n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 171 | } |
| 172 | #else |
Ted Kremenek | 4ebd7f5 | 2008-10-23 23:51:23 +0000 | [diff] [blame] | 173 | llvm::errs() << "QualType::viewInheritance is only available in debug " |
| 174 | << "builds on systems with Graphviz or gv!\n"; |
Douglas Gregor | 0218936 | 2008-10-22 21:13:31 +0000 | [diff] [blame] | 175 | #endif |
| 176 | } |
| 177 | |
| 178 | } |