InheritViz.cpp revision 355940
1238431Sdteske//===- InheritViz.cpp - Graphviz visualization for inheritance --*- C++ -*-===//
2222417Sjulian//
3222417Sjulian// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4222417Sjulian// See https://llvm.org/LICENSE.txt for license information.
5222417Sjulian// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6222417Sjulian//
7222417Sjulian//===----------------------------------------------------------------------===//
8222417Sjulian//
9222417Sjulian//  This file implements CXXRecordDecl::viewInheritance, which
10222417Sjulian//  generates a GraphViz DOT file that depicts the class inheritance
11222417Sjulian//  diagram and then calls Graphviz/dot+gv on it.
12222417Sjulian//
13222417Sjulian//===----------------------------------------------------------------------===//
14222417Sjulian
15222417Sjulian#include "clang/AST/ASTContext.h"
16222417Sjulian#include "clang/AST/Decl.h"
17222417Sjulian#include "clang/AST/DeclCXX.h"
18222417Sjulian#include "clang/AST/TypeOrdering.h"
19222417Sjulian#include "llvm/Support/FileSystem.h"
20222417Sjulian#include "llvm/Support/GraphWriter.h"
21222417Sjulian#include "llvm/Support/raw_ostream.h"
22222417Sjulian#include <map>
23222417Sjulian#include <set>
24222417Sjulianusing namespace clang;
25222417Sjulian
26222417Sjuliannamespace {
27222417Sjulian/// InheritanceHierarchyWriter - Helper class that writes out a
28222417Sjulian/// GraphViz file that diagrams the inheritance hierarchy starting at
29222417Sjulian/// a given C++ class type. Note that we do not use LLVM's
30222417Sjulian/// GraphWriter, because the interface does not permit us to properly
31222417Sjulian/// differentiate between uses of types as virtual bases
32222417Sjulian/// vs. non-virtual bases.
33222417Sjulianclass InheritanceHierarchyWriter {
34222417Sjulian  ASTContext& Context;
35222417Sjulian  raw_ostream &Out;
36222417Sjulian  std::map<QualType, int, QualTypeOrdering> DirectBaseCount;
37222417Sjulian  std::set<QualType, QualTypeOrdering> KnownVirtualBases;
38222417Sjulian
39222417Sjulianpublic:
40222417Sjulian  InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)
41222417Sjulian    : Context(Context), Out(Out) { }
42222417Sjulian
43222417Sjulian  void WriteGraph(QualType Type) {
44222417Sjulian    Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString())
45222417Sjulian        << "\" {\n";
46222417Sjulian    WriteNode(Type, false);
47222417Sjulian    Out << "}\n";
48222417Sjulian  }
49222417Sjulian
50222417Sjulianprotected:
51222417Sjulian  /// WriteNode - Write out the description of node in the inheritance
52222417Sjulian  /// diagram, which may be a base class or it may be the root node.
53222417Sjulian  void WriteNode(QualType Type, bool FromVirtual);
54222417Sjulian
55222417Sjulian  /// WriteNodeReference - Write out a reference to the given node,
56222417Sjulian  /// using a unique identifier for each direct base and for the
57222417Sjulian  /// (only) virtual base.
58222417Sjulian  raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);
59267010Srodrigc};
60267010Srodrigc} // namespace
61222417Sjulian
62222417Sjulianvoid InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {
63222417Sjulian  QualType CanonType = Context.getCanonicalType(Type);
64222417Sjulian
65222417Sjulian  if (FromVirtual) {
66222417Sjulian    if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end())
67222417Sjulian      return;
68222417Sjulian
69222417Sjulian    // We haven't seen this virtual base before, so display it and
70222417Sjulian    // its bases.
71222417Sjulian    KnownVirtualBases.insert(CanonType);
72222417Sjulian  }
73222417Sjulian
74222417Sjulian  // Declare the node itself.
75222417Sjulian  Out << "  ";
76222417Sjulian  WriteNodeReference(Type, FromVirtual);
77222417Sjulian
78222417Sjulian  // Give the node a label based on the name of the class.
79222417Sjulian  std::string TypeName = Type.getAsString();
80222417Sjulian  Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName);
81222417Sjulian
82222417Sjulian  // If the name of the class was a typedef or something different
83222417Sjulian  // from the "real" class name, show the real class name in
84222417Sjulian  // parentheses so we don't confuse ourselves.
85222417Sjulian  if (TypeName != CanonType.getAsString()) {
86222417Sjulian    Out << "\\n(" << CanonType.getAsString() << ")";
87222417Sjulian  }
88222417Sjulian
89222417Sjulian  // Finished describing the node.
90222417Sjulian  Out << " \"];\n";
91222417Sjulian
92267010Srodrigc  // Display the base classes.
93267010Srodrigc  const CXXRecordDecl *Decl
94267010Srodrigc    = static_cast<const CXXRecordDecl *>(Type->getAs<RecordType>()->getDecl());
95267010Srodrigc  for (const auto &Base : Decl->bases()) {
96267010Srodrigc    QualType CanonBaseType = Context.getCanonicalType(Base.getType());
97267010Srodrigc
98267010Srodrigc    // If this is not virtual inheritance, bump the direct base
99267010Srodrigc    // count for the type.
100267010Srodrigc    if (!Base.isVirtual())
101222417Sjulian      ++DirectBaseCount[CanonBaseType];
102222417Sjulian
103    // Write out the node (if we need to).
104    WriteNode(Base.getType(), Base.isVirtual());
105
106    // Write out the edge.
107    Out << "  ";
108    WriteNodeReference(Type, FromVirtual);
109    Out << " -> ";
110    WriteNodeReference(Base.getType(), Base.isVirtual());
111
112    // Write out edge attributes to show the kind of inheritance.
113    if (Base.isVirtual()) {
114      Out << " [ style=\"dashed\" ]";
115    }
116    Out << ";";
117  }
118}
119
120/// WriteNodeReference - Write out a reference to the given node,
121/// using a unique identifier for each direct base and for the
122/// (only) virtual base.
123raw_ostream&
124InheritanceHierarchyWriter::WriteNodeReference(QualType Type,
125                                               bool FromVirtual) {
126  QualType CanonType = Context.getCanonicalType(Type);
127
128  Out << "Class_" << CanonType.getAsOpaquePtr();
129  if (!FromVirtual)
130    Out << "_" << DirectBaseCount[CanonType];
131  return Out;
132}
133
134/// viewInheritance - Display the inheritance hierarchy of this C++
135/// class using GraphViz.
136void CXXRecordDecl::viewInheritance(ASTContext& Context) const {
137  QualType Self = Context.getTypeDeclType(this);
138
139  int FD;
140  SmallString<128> Filename;
141  if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
142          Self.getAsString(), "dot", FD, Filename)) {
143    llvm::errs() << "Error: " << EC.message() << "\n";
144    return;
145  }
146
147  llvm::errs() << "Writing '" << Filename << "'... ";
148
149  llvm::raw_fd_ostream O(FD, true);
150
151  InheritanceHierarchyWriter Writer(Context, O);
152  Writer.WriteGraph(Self);
153  llvm::errs() << " done. \n";
154
155  O.close();
156
157  // Display the graph
158  DisplayGraph(Filename);
159}
160