1*67e74705SXin Li //===- InheritViz.cpp - Graphviz visualization for inheritance --*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file implements CXXRecordDecl::viewInheritance, which
11*67e74705SXin Li // generates a GraphViz DOT file that depicts the class inheritance
12*67e74705SXin Li // diagram and then calls Graphviz/dot+gv on it.
13*67e74705SXin Li //
14*67e74705SXin Li //===----------------------------------------------------------------------===//
15*67e74705SXin Li
16*67e74705SXin Li #include "clang/AST/ASTContext.h"
17*67e74705SXin Li #include "clang/AST/Decl.h"
18*67e74705SXin Li #include "clang/AST/DeclCXX.h"
19*67e74705SXin Li #include "clang/AST/TypeOrdering.h"
20*67e74705SXin Li #include "llvm/Support/FileSystem.h"
21*67e74705SXin Li #include "llvm/Support/GraphWriter.h"
22*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
23*67e74705SXin Li #include <map>
24*67e74705SXin Li #include <set>
25*67e74705SXin Li using namespace clang;
26*67e74705SXin Li
27*67e74705SXin Li namespace {
28*67e74705SXin Li /// InheritanceHierarchyWriter - Helper class that writes out a
29*67e74705SXin Li /// GraphViz file that diagrams the inheritance hierarchy starting at
30*67e74705SXin Li /// a given C++ class type. Note that we do not use LLVM's
31*67e74705SXin Li /// GraphWriter, because the interface does not permit us to properly
32*67e74705SXin Li /// differentiate between uses of types as virtual bases
33*67e74705SXin Li /// vs. non-virtual bases.
34*67e74705SXin Li class InheritanceHierarchyWriter {
35*67e74705SXin Li ASTContext& Context;
36*67e74705SXin Li raw_ostream &Out;
37*67e74705SXin Li std::map<QualType, int, QualTypeOrdering> DirectBaseCount;
38*67e74705SXin Li std::set<QualType, QualTypeOrdering> KnownVirtualBases;
39*67e74705SXin Li
40*67e74705SXin Li public:
InheritanceHierarchyWriter(ASTContext & Context,raw_ostream & Out)41*67e74705SXin Li InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)
42*67e74705SXin Li : Context(Context), Out(Out) { }
43*67e74705SXin Li
WriteGraph(QualType Type)44*67e74705SXin Li void WriteGraph(QualType Type) {
45*67e74705SXin Li Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString())
46*67e74705SXin Li << "\" {\n";
47*67e74705SXin Li WriteNode(Type, false);
48*67e74705SXin Li Out << "}\n";
49*67e74705SXin Li }
50*67e74705SXin Li
51*67e74705SXin Li protected:
52*67e74705SXin Li /// WriteNode - Write out the description of node in the inheritance
53*67e74705SXin Li /// diagram, which may be a base class or it may be the root node.
54*67e74705SXin Li void WriteNode(QualType Type, bool FromVirtual);
55*67e74705SXin Li
56*67e74705SXin Li /// WriteNodeReference - Write out a reference to the given node,
57*67e74705SXin Li /// using a unique identifier for each direct base and for the
58*67e74705SXin Li /// (only) virtual base.
59*67e74705SXin Li raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);
60*67e74705SXin Li };
61*67e74705SXin Li } // namespace
62*67e74705SXin Li
WriteNode(QualType Type,bool FromVirtual)63*67e74705SXin Li void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {
64*67e74705SXin Li QualType CanonType = Context.getCanonicalType(Type);
65*67e74705SXin Li
66*67e74705SXin Li if (FromVirtual) {
67*67e74705SXin Li if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end())
68*67e74705SXin Li return;
69*67e74705SXin Li
70*67e74705SXin Li // We haven't seen this virtual base before, so display it and
71*67e74705SXin Li // its bases.
72*67e74705SXin Li KnownVirtualBases.insert(CanonType);
73*67e74705SXin Li }
74*67e74705SXin Li
75*67e74705SXin Li // Declare the node itself.
76*67e74705SXin Li Out << " ";
77*67e74705SXin Li WriteNodeReference(Type, FromVirtual);
78*67e74705SXin Li
79*67e74705SXin Li // Give the node a label based on the name of the class.
80*67e74705SXin Li std::string TypeName = Type.getAsString();
81*67e74705SXin Li Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName);
82*67e74705SXin Li
83*67e74705SXin Li // If the name of the class was a typedef or something different
84*67e74705SXin Li // from the "real" class name, show the real class name in
85*67e74705SXin Li // parentheses so we don't confuse ourselves.
86*67e74705SXin Li if (TypeName != CanonType.getAsString()) {
87*67e74705SXin Li Out << "\\n(" << CanonType.getAsString() << ")";
88*67e74705SXin Li }
89*67e74705SXin Li
90*67e74705SXin Li // Finished describing the node.
91*67e74705SXin Li Out << " \"];\n";
92*67e74705SXin Li
93*67e74705SXin Li // Display the base classes.
94*67e74705SXin Li const CXXRecordDecl *Decl
95*67e74705SXin Li = static_cast<const CXXRecordDecl *>(Type->getAs<RecordType>()->getDecl());
96*67e74705SXin Li for (const auto &Base : Decl->bases()) {
97*67e74705SXin Li QualType CanonBaseType = Context.getCanonicalType(Base.getType());
98*67e74705SXin Li
99*67e74705SXin Li // If this is not virtual inheritance, bump the direct base
100*67e74705SXin Li // count for the type.
101*67e74705SXin Li if (!Base.isVirtual())
102*67e74705SXin Li ++DirectBaseCount[CanonBaseType];
103*67e74705SXin Li
104*67e74705SXin Li // Write out the node (if we need to).
105*67e74705SXin Li WriteNode(Base.getType(), Base.isVirtual());
106*67e74705SXin Li
107*67e74705SXin Li // Write out the edge.
108*67e74705SXin Li Out << " ";
109*67e74705SXin Li WriteNodeReference(Type, FromVirtual);
110*67e74705SXin Li Out << " -> ";
111*67e74705SXin Li WriteNodeReference(Base.getType(), Base.isVirtual());
112*67e74705SXin Li
113*67e74705SXin Li // Write out edge attributes to show the kind of inheritance.
114*67e74705SXin Li if (Base.isVirtual()) {
115*67e74705SXin Li Out << " [ style=\"dashed\" ]";
116*67e74705SXin Li }
117*67e74705SXin Li Out << ";";
118*67e74705SXin Li }
119*67e74705SXin Li }
120*67e74705SXin Li
121*67e74705SXin Li /// WriteNodeReference - Write out a reference to the given node,
122*67e74705SXin Li /// using a unique identifier for each direct base and for the
123*67e74705SXin Li /// (only) virtual base.
124*67e74705SXin Li raw_ostream&
WriteNodeReference(QualType Type,bool FromVirtual)125*67e74705SXin Li InheritanceHierarchyWriter::WriteNodeReference(QualType Type,
126*67e74705SXin Li bool FromVirtual) {
127*67e74705SXin Li QualType CanonType = Context.getCanonicalType(Type);
128*67e74705SXin Li
129*67e74705SXin Li Out << "Class_" << CanonType.getAsOpaquePtr();
130*67e74705SXin Li if (!FromVirtual)
131*67e74705SXin Li Out << "_" << DirectBaseCount[CanonType];
132*67e74705SXin Li return Out;
133*67e74705SXin Li }
134*67e74705SXin Li
135*67e74705SXin Li /// viewInheritance - Display the inheritance hierarchy of this C++
136*67e74705SXin Li /// class using GraphViz.
viewInheritance(ASTContext & Context) const137*67e74705SXin Li void CXXRecordDecl::viewInheritance(ASTContext& Context) const {
138*67e74705SXin Li QualType Self = Context.getTypeDeclType(this);
139*67e74705SXin Li
140*67e74705SXin Li int FD;
141*67e74705SXin Li SmallString<128> Filename;
142*67e74705SXin Li if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
143*67e74705SXin Li Self.getAsString(), "dot", FD, Filename)) {
144*67e74705SXin Li llvm::errs() << "Error: " << EC.message() << "\n";
145*67e74705SXin Li return;
146*67e74705SXin Li }
147*67e74705SXin Li
148*67e74705SXin Li llvm::errs() << "Writing '" << Filename << "'... ";
149*67e74705SXin Li
150*67e74705SXin Li llvm::raw_fd_ostream O(FD, true);
151*67e74705SXin Li
152*67e74705SXin Li InheritanceHierarchyWriter Writer(Context, O);
153*67e74705SXin Li Writer.WriteGraph(Self);
154*67e74705SXin Li llvm::errs() << " done. \n";
155*67e74705SXin Li
156*67e74705SXin Li O.close();
157*67e74705SXin Li
158*67e74705SXin Li // Display the graph
159*67e74705SXin Li DisplayGraph(Filename);
160*67e74705SXin Li }
161