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" |
| 20 | #include "llvm/ADT/GraphTraits.h" |
| 21 | #include "llvm/ADT/DepthFirstIterator.h" |
| 22 | #include "llvm/Support/GraphWriter.h" |
| 23 | #include <fstream> |
| 24 | #include <iterator> |
| 25 | #include <map> |
| 26 | #include <sstream> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace clang { |
| 31 | /// InheritanceGraphOutEdgeIterator - Enumerates the base classes |
| 32 | /// based on base specifiers. |
| 33 | class InheritanceGraphOutEdgeIterator { |
| 34 | const CXXBaseSpecifier *Base; |
| 35 | |
| 36 | public: |
| 37 | typedef const RecordType *value_type; |
| 38 | typedef const RecordType *reference; |
| 39 | typedef const RecordType **pointer; |
| 40 | typedef ptrdiff_t difference_type; |
| 41 | typedef std::forward_iterator_tag iterator_category; |
| 42 | |
| 43 | InheritanceGraphOutEdgeIterator(const CXXBaseSpecifier* Base = 0) |
| 44 | : Base(Base) { } |
| 45 | |
| 46 | reference operator*() const { |
| 47 | assert(Base->getType()->getAsRecordType()); |
| 48 | return Base->getType()->getAsRecordType(); |
| 49 | } |
| 50 | |
| 51 | pointer operator->() const { return 0; } |
| 52 | |
| 53 | InheritanceGraphOutEdgeIterator& operator++() { |
| 54 | ++Base; |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | InheritanceGraphOutEdgeIterator operator++(int) { |
| 59 | return InheritanceGraphOutEdgeIterator(Base++); |
| 60 | } |
| 61 | |
| 62 | friend bool operator==(InheritanceGraphOutEdgeIterator const& x, |
| 63 | InheritanceGraphOutEdgeIterator const& y) { |
| 64 | return x.Base == y.Base; |
| 65 | } |
| 66 | |
| 67 | friend bool operator!=(InheritanceGraphOutEdgeIterator const& x, |
| 68 | InheritanceGraphOutEdgeIterator const& y) { |
| 69 | return x.Base != y.Base; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | } // end namespace clang |
| 74 | |
| 75 | namespace llvm { |
| 76 | template<> struct GraphTraits<const clang::RecordType *> { |
| 77 | typedef const clang::RecordType NodeType; |
| 78 | typedef clang::InheritanceGraphOutEdgeIterator ChildIteratorType; |
| 79 | typedef llvm::df_iterator<const clang::RecordType *> nodes_iterator; |
| 80 | |
| 81 | static const NodeType *getEntryNode(const clang::RecordType *Type) { |
| 82 | return Type; |
| 83 | } |
| 84 | |
| 85 | static ChildIteratorType child_begin(const clang::RecordType *Type) { |
| 86 | const clang::CXXRecordDecl *Decl |
| 87 | = dyn_cast_or_null<clang::CXXRecordDecl>(Type->getDecl()); |
| 88 | if (Decl->getNumBases() == 0) |
| 89 | return clang::InheritanceGraphOutEdgeIterator(0); |
| 90 | else |
| 91 | return clang::InheritanceGraphOutEdgeIterator(Decl->getBase(0)); |
| 92 | } |
| 93 | static ChildIteratorType child_end(const clang::RecordType *Type) { |
| 94 | const clang::CXXRecordDecl *Decl |
| 95 | = dyn_cast_or_null<clang::CXXRecordDecl>(Type->getDecl()); |
| 96 | if (Decl->getNumBases() == 0) |
| 97 | return clang::InheritanceGraphOutEdgeIterator(0); |
| 98 | else |
| 99 | return clang::InheritanceGraphOutEdgeIterator(Decl->getBase(0) + |
| 100 | Decl->getNumBases()); |
| 101 | } |
| 102 | |
| 103 | static nodes_iterator nodes_begin(const clang::RecordType *Type) { |
| 104 | return df_begin(Type); |
| 105 | } |
| 106 | |
| 107 | static nodes_iterator nodes_end(const clang::RecordType *Type) { |
| 108 | return df_end(Type); |
| 109 | } |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | namespace clang { |
| 114 | |
| 115 | /// InheritanceHierarchyWriter - Helper class that writes out a |
| 116 | /// GraphViz file that diagrams the inheritance hierarchy starting at |
| 117 | /// a given C++ class type. Note that we do not use LLVM's |
| 118 | /// GraphWriter, because the interface does not permit us to properly |
| 119 | /// differentiate between uses of types as virtual bases |
| 120 | /// vs. non-virtual bases. |
| 121 | class InheritanceHierarchyWriter { |
| 122 | ASTContext& Context; |
| 123 | std::ostream &Out; |
| 124 | std::map<QualType, int, QualTypeOrdering> DirectBaseCount; |
| 125 | std::set<QualType, QualTypeOrdering> KnownVirtualBases; |
| 126 | |
| 127 | public: |
| 128 | InheritanceHierarchyWriter(ASTContext& Context, std::ostream& Out) |
| 129 | : Context(Context), Out(Out) { } |
| 130 | |
| 131 | void WriteGraph(QualType Type) { |
| 132 | Out << "digraph \"" << DOT::EscapeString(Type.getAsString()) << "\" {\n"; |
| 133 | WriteNode(Type, false); |
| 134 | Out << "}\n"; |
| 135 | } |
| 136 | |
| 137 | protected: |
| 138 | /// WriteNode - Write out the description of node in the inheritance |
| 139 | /// diagram, which may be a base class or it may be the root node. |
| 140 | void WriteNode(QualType Type, bool FromVirtual); |
| 141 | |
| 142 | /// WriteNodeReference - Write out a reference to the given node, |
| 143 | /// using a unique identifier for each direct base and for the |
| 144 | /// (only) virtual base. |
| 145 | std::ostream& WriteNodeReference(QualType Type, bool FromVirtual); |
| 146 | }; |
| 147 | |
| 148 | void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) { |
| 149 | QualType CanonType = Context.getCanonicalType(Type); |
| 150 | |
| 151 | if (FromVirtual) { |
| 152 | if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end()) |
| 153 | return; |
| 154 | |
| 155 | // We haven't seen this virtual base before, so display it and |
| 156 | // its bases. |
| 157 | KnownVirtualBases.insert(CanonType); |
| 158 | } |
| 159 | |
| 160 | // Declare the node itself. |
| 161 | Out << " "; |
| 162 | WriteNodeReference(Type, FromVirtual); |
| 163 | |
| 164 | // Give the node a label based on the name of the class. |
| 165 | std::string TypeName = Type.getAsString(); |
| 166 | Out << " [ shape=\"box\", label=\"" << DOT::EscapeString(TypeName); |
| 167 | |
| 168 | // If the name of the class was a typedef or something different |
| 169 | // from the "real" class name, show the real class name in |
| 170 | // parentheses so we don't confuse ourselves. |
| 171 | if (TypeName != CanonType.getAsString()) { |
| 172 | Out << "\\n(" << CanonType.getAsString() << ")"; |
| 173 | } |
| 174 | |
| 175 | // Finished describing the node. |
| 176 | Out << " \"];\n"; |
| 177 | |
| 178 | // Display the base classes. |
| 179 | const CXXRecordDecl *Decl |
| 180 | = static_cast<const CXXRecordDecl *>(Type->getAsRecordType()->getDecl()); |
| 181 | for (unsigned idx = 0; idx < Decl->getNumBases(); ++idx) { |
| 182 | const CXXBaseSpecifier *Base = Decl->getBase(idx); |
| 183 | QualType CanonBaseType = Context.getCanonicalType(Base->getType()); |
| 184 | |
| 185 | // If this is not virtual inheritance, bump the direct base |
| 186 | // count for the type. |
| 187 | if (!Base->isVirtual()) |
| 188 | ++DirectBaseCount[CanonBaseType]; |
| 189 | |
| 190 | // Write out the node (if we need to). |
| 191 | WriteNode(Base->getType(), Base->isVirtual()); |
| 192 | |
| 193 | // Write out the edge. |
| 194 | Out << " "; |
| 195 | WriteNodeReference(Type, FromVirtual); |
| 196 | Out << " -> "; |
| 197 | WriteNodeReference(Base->getType(), Base->isVirtual()); |
| 198 | |
| 199 | // Write out edge attributes to show the kind of inheritance. |
| 200 | if (Base->isVirtual()) { |
| 201 | Out << " [ style=\"dashed\" ]"; |
| 202 | } |
| 203 | Out << ";"; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /// WriteNodeReference - Write out a reference to the given node, |
| 208 | /// using a unique identifier for each direct base and for the |
| 209 | /// (only) virtual base. |
| 210 | std::ostream& |
| 211 | InheritanceHierarchyWriter::WriteNodeReference(QualType Type, |
| 212 | bool FromVirtual) { |
| 213 | QualType CanonType = Context.getCanonicalType(Type); |
| 214 | |
| 215 | Out << "Class_" << CanonType.getAsOpaquePtr(); |
| 216 | if (!FromVirtual) |
| 217 | Out << "_" << DirectBaseCount[CanonType]; |
| 218 | return Out; |
| 219 | } |
| 220 | |
| 221 | /// viewInheritance - Display the inheritance hierarchy of this C++ |
| 222 | /// class using GraphViz. |
| 223 | void QualType::viewInheritance(ASTContext& Context) { |
| 224 | if (!(*this)->getAsRecordType()) { |
| 225 | cerr << "Type " << getAsString() << " is not a C++ class type.\n"; |
| 226 | } |
| 227 | #ifndef NDEBUG |
| 228 | // std::string Title = "Inheritance graph for " + getAsString(); |
| 229 | // llvm::ViewGraph((*this)->getAsRecordType(), Title.c_str()); |
| 230 | std::string ErrMsg; |
| 231 | sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg); |
| 232 | if (Filename.isEmpty()) { |
| 233 | cerr << "Error: " << ErrMsg << "\n"; |
| 234 | return; |
| 235 | } |
| 236 | Filename.appendComponent(getAsString() + ".dot"); |
| 237 | if (Filename.makeUnique(true,&ErrMsg)) { |
| 238 | cerr << "Error: " << ErrMsg << "\n"; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | cerr << "Writing '" << Filename << "'... "; |
| 243 | |
| 244 | std::ofstream O(Filename.c_str()); |
| 245 | |
| 246 | if (O.good()) { |
| 247 | InheritanceHierarchyWriter Writer(Context, O); |
| 248 | Writer.WriteGraph(*this); |
| 249 | cerr << " done. \n"; |
| 250 | |
| 251 | O.close(); |
| 252 | |
| 253 | // Display the graph |
| 254 | DisplayGraph(Filename); |
| 255 | } else { |
| 256 | cerr << "error opening file for writing!\n"; |
| 257 | Filename.clear(); |
| 258 | } |
| 259 | #else |
| 260 | cerr << "QualType::viewInheritance is only available in debug " |
| 261 | << "builds on systems with Graphviz or gv!\n"; |
| 262 | #endif |
| 263 | } |
| 264 | |
| 265 | } |