blob: 0b82da133fa76ac1ccfe223022afc415fb53ddff [file] [log] [blame]
Douglas Gregordff6a8e2008-10-22 21:13:31 +00001//===- 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"
Rafael Espindola36da8542013-06-24 15:00:11 +000020#include "llvm/Support/FileSystem.h"
Douglas Gregordff6a8e2008-10-22 21:13:31 +000021#include "llvm/Support/GraphWriter.h"
Ted Kremenek40fcc592008-10-23 23:51:23 +000022#include "llvm/Support/raw_ostream.h"
Douglas Gregordff6a8e2008-10-22 21:13:31 +000023#include <map>
Rafael Espindola76993a02013-06-19 15:31:27 +000024#include <set>
Benjamin Kramer6afa1682015-03-09 15:03:20 +000025using namespace clang;
Douglas Gregordff6a8e2008-10-22 21:13:31 +000026
Benjamin Kramer6afa1682015-03-09 15:03:20 +000027namespace {
Douglas Gregordff6a8e2008-10-22 21:13:31 +000028/// 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.
34class InheritanceHierarchyWriter {
35 ASTContext& Context;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000036 raw_ostream &Out;
Douglas Gregordff6a8e2008-10-22 21:13:31 +000037 std::map<QualType, int, QualTypeOrdering> DirectBaseCount;
38 std::set<QualType, QualTypeOrdering> KnownVirtualBases;
39
40public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +000041 InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)
Douglas Gregordff6a8e2008-10-22 21:13:31 +000042 : Context(Context), Out(Out) { }
43
44 void WriteGraph(QualType Type) {
Benjamin Kramer6afa1682015-03-09 15:03:20 +000045 Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString())
46 << "\" {\n";
Douglas Gregordff6a8e2008-10-22 21:13:31 +000047 WriteNode(Type, false);
48 Out << "}\n";
49 }
50
51protected:
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.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000059 raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);
Douglas Gregordff6a8e2008-10-22 21:13:31 +000060};
Benjamin Kramer6afa1682015-03-09 15:03:20 +000061} // namespace
Douglas Gregordff6a8e2008-10-22 21:13:31 +000062
63void 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();
Benjamin Kramer6afa1682015-03-09 15:03:20 +000081 Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName);
Douglas Gregordff6a8e2008-10-22 21:13:31 +000082
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 Stump11289f42009-09-09 15:08:12 +000094 const CXXRecordDecl *Decl
Ted Kremenekc23c7e62009-07-29 21:53:49 +000095 = static_cast<const CXXRecordDecl *>(Type->getAs<RecordType>()->getDecl());
Aaron Ballman574705e2014-03-13 15:41:46 +000096 for (const auto &Base : Decl->bases()) {
97 QualType CanonBaseType = Context.getCanonicalType(Base.getType());
Douglas Gregordff6a8e2008-10-22 21:13:31 +000098
99 // If this is not virtual inheritance, bump the direct base
100 // count for the type.
Aaron Ballman574705e2014-03-13 15:41:46 +0000101 if (!Base.isVirtual())
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000102 ++DirectBaseCount[CanonBaseType];
103
104 // Write out the node (if we need to).
Aaron Ballman574705e2014-03-13 15:41:46 +0000105 WriteNode(Base.getType(), Base.isVirtual());
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000106
107 // Write out the edge.
108 Out << " ";
109 WriteNodeReference(Type, FromVirtual);
110 Out << " -> ";
Aaron Ballman574705e2014-03-13 15:41:46 +0000111 WriteNodeReference(Base.getType(), Base.isVirtual());
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000112
113 // Write out edge attributes to show the kind of inheritance.
Aaron Ballman574705e2014-03-13 15:41:46 +0000114 if (Base.isVirtual()) {
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000115 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.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000124raw_ostream&
Mike Stump11289f42009-09-09 15:08:12 +0000125InheritanceHierarchyWriter::WriteNodeReference(QualType Type,
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000126 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}
134
135/// viewInheritance - Display the inheritance hierarchy of this C++
136/// class using GraphViz.
Douglas Gregor3dfef1f2008-10-24 19:53:54 +0000137void CXXRecordDecl::viewInheritance(ASTContext& Context) const {
Dmitri Gribenkoaaf7f292013-01-14 00:25:25 +0000138 QualType Self = Context.getTypeDeclType(this);
Rafael Espindola36da8542013-06-24 15:00:11 +0000139
140 int FD;
141 SmallString<128> Filename;
Benjamin Kramer6afa1682015-03-09 15:03:20 +0000142 if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
143 Self.getAsString(), "dot", FD, Filename)) {
Rafael Espindola36da8542013-06-24 15:00:11 +0000144 llvm::errs() << "Error: " << EC.message() << "\n";
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000145 return;
146 }
147
Rafael Espindola36da8542013-06-24 15:00:11 +0000148 llvm::errs() << "Writing '" << Filename << "'... ";
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000149
Rafael Espindola36da8542013-06-24 15:00:11 +0000150 llvm::raw_fd_ostream O(FD, true);
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000151
Rafael Espindola36da8542013-06-24 15:00:11 +0000152 InheritanceHierarchyWriter Writer(Context, O);
153 Writer.WriteGraph(Self);
154 llvm::errs() << " done. \n";
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000155
Rafael Espindola36da8542013-06-24 15:00:11 +0000156 O.close();
Dan Gohman72346e92011-03-01 19:50:49 +0000157
Rafael Espindola36da8542013-06-24 15:00:11 +0000158 // Display the graph
159 DisplayGraph(Filename);
Douglas Gregordff6a8e2008-10-22 21:13:31 +0000160}