1 //===- InheritViz.cpp - Graphviz visualization for inheritance --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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/Support/FileSystem.h"
21 #include "llvm/Support/GraphWriter.h"
22 #include "llvm/Support/raw_ostream.h"
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 {
39 std::map<QualType, int, QualTypeOrdering> DirectBaseCount;
40 std::set<QualType, QualTypeOrdering> KnownVirtualBases;
43 InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)
44 : Context(Context), Out(Out) { }
46 void WriteGraph(QualType Type) {
47 Out << "digraph \"" << DOT::EscapeString(Type.getAsString()) << "\" {\n";
48 WriteNode(Type, false);
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);
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.
60 raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);
63 void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {
64 QualType CanonType = Context.getCanonicalType(Type);
67 if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end())
70 // We haven't seen this virtual base before, so display it and
72 KnownVirtualBases.insert(CanonType);
75 // Declare the node itself.
77 WriteNodeReference(Type, FromVirtual);
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);
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() << ")";
90 // Finished describing the node.
93 // Display the base classes.
94 const CXXRecordDecl *Decl
95 = static_cast<const CXXRecordDecl *>(Type->getAs<RecordType>()->getDecl());
96 for (CXXRecordDecl::base_class_const_iterator Base = Decl->bases_begin();
97 Base != Decl->bases_end(); ++Base) {
98 QualType CanonBaseType = Context.getCanonicalType(Base->getType());
100 // If this is not virtual inheritance, bump the direct base
101 // count for the type.
102 if (!Base->isVirtual())
103 ++DirectBaseCount[CanonBaseType];
105 // Write out the node (if we need to).
106 WriteNode(Base->getType(), Base->isVirtual());
108 // Write out the edge.
110 WriteNodeReference(Type, FromVirtual);
112 WriteNodeReference(Base->getType(), Base->isVirtual());
114 // Write out edge attributes to show the kind of inheritance.
115 if (Base->isVirtual()) {
116 Out << " [ style=\"dashed\" ]";
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.
126 InheritanceHierarchyWriter::WriteNodeReference(QualType Type,
128 QualType CanonType = Context.getCanonicalType(Type);
130 Out << "Class_" << CanonType.getAsOpaquePtr();
132 Out << "_" << DirectBaseCount[CanonType];
136 /// viewInheritance - Display the inheritance hierarchy of this C++
137 /// class using GraphViz.
138 void CXXRecordDecl::viewInheritance(ASTContext& Context) const {
139 QualType Self = Context.getTypeDeclType(this);
142 SmallString<128> Filename;
144 sys::fs::createTemporaryFile(Self.getAsString(), "dot", FD, Filename);
146 llvm::errs() << "Error: " << EC.message() << "\n";
150 llvm::errs() << "Writing '" << Filename << "'... ";
152 llvm::raw_fd_ostream O(FD, true);
154 InheritanceHierarchyWriter Writer(Context, O);
155 Writer.WriteGraph(Self);
156 llvm::errs() << " done. \n";
161 DisplayGraph(Filename);