clang 20.0.0git
InheritViz.cpp
Go to the documentation of this file.
1//===- InheritViz.cpp - Graphviz visualization for inheritance --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements CXXRecordDecl::viewInheritance, which
10// generates a GraphViz DOT file that depicts the class inheritance
11// diagram and then calls Graphviz/dot+gv on it.
12//
13//===----------------------------------------------------------------------===//
14
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
19#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/GraphWriter.h"
21#include "llvm/Support/raw_ostream.h"
22#include <map>
23#include <set>
24using namespace clang;
25
26namespace {
27/// InheritanceHierarchyWriter - Helper class that writes out a
28/// GraphViz file that diagrams the inheritance hierarchy starting at
29/// a given C++ class type. Note that we do not use LLVM's
30/// GraphWriter, because the interface does not permit us to properly
31/// differentiate between uses of types as virtual bases
32/// vs. non-virtual bases.
33class InheritanceHierarchyWriter {
34 ASTContext& Context;
35 raw_ostream &Out;
36 std::map<QualType, int, QualTypeOrdering> DirectBaseCount;
37 std::set<QualType, QualTypeOrdering> KnownVirtualBases;
38
39public:
40 InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)
41 : Context(Context), Out(Out) { }
42
43 void WriteGraph(QualType Type) {
44 Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString())
45 << "\" {\n";
46 WriteNode(Type, false);
47 Out << "}\n";
48 }
49
50protected:
51 /// WriteNode - Write out the description of node in the inheritance
52 /// diagram, which may be a base class or it may be the root node.
53 void WriteNode(QualType Type, bool FromVirtual);
54
55 /// WriteNodeReference - Write out a reference to the given node,
56 /// using a unique identifier for each direct base and for the
57 /// (only) virtual base.
58 raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);
59};
60} // namespace
61
62void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {
63 QualType CanonType = Context.getCanonicalType(Type);
64
65 if (FromVirtual) {
66 if (!KnownVirtualBases.insert(CanonType).second)
67 return;
68
69 // We haven't seen this virtual base before, so display it and
70 // its bases.
71 }
72
73 // Declare the node itself.
74 Out << " ";
75 WriteNodeReference(Type, FromVirtual);
76
77 // Give the node a label based on the name of the class.
78 std::string TypeName = Type.getAsString();
79 Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName);
80
81 // If the name of the class was a typedef or something different
82 // from the "real" class name, show the real class name in
83 // parentheses so we don't confuse ourselves.
84 if (TypeName != CanonType.getAsString()) {
85 Out << "\\n(" << CanonType.getAsString() << ")";
86 }
87
88 // Finished describing the node.
89 Out << " \"];\n";
90
91 // Display the base classes.
92 const auto *Decl =
93 static_cast<const CXXRecordDecl *>(Type->castAs<RecordType>()->getDecl());
94 for (const auto &Base : Decl->bases()) {
95 QualType CanonBaseType = Context.getCanonicalType(Base.getType());
96
97 // If this is not virtual inheritance, bump the direct base
98 // count for the type.
99 if (!Base.isVirtual())
100 ++DirectBaseCount[CanonBaseType];
101
102 // Write out the node (if we need to).
103 WriteNode(Base.getType(), Base.isVirtual());
104
105 // Write out the edge.
106 Out << " ";
107 WriteNodeReference(Type, FromVirtual);
108 Out << " -> ";
109 WriteNodeReference(Base.getType(), Base.isVirtual());
110
111 // Write out edge attributes to show the kind of inheritance.
112 if (Base.isVirtual()) {
113 Out << " [ style=\"dashed\" ]";
114 }
115 Out << ";";
116 }
117}
118
119/// WriteNodeReference - Write out a reference to the given node,
120/// using a unique identifier for each direct base and for the
121/// (only) virtual base.
122raw_ostream&
123InheritanceHierarchyWriter::WriteNodeReference(QualType Type,
124 bool FromVirtual) {
125 QualType CanonType = Context.getCanonicalType(Type);
126
127 Out << "Class_" << CanonType.getAsOpaquePtr();
128 if (!FromVirtual)
129 Out << "_" << DirectBaseCount[CanonType];
130 return Out;
131}
132
133/// viewInheritance - Display the inheritance hierarchy of this C++
134/// class using GraphViz.
136 QualType Self = Context.getTypeDeclType(this);
137
138 int FD;
140 if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
141 Self.getAsString(), "dot", FD, Filename)) {
142 llvm::errs() << "Error: " << EC.message() << "\n";
143 return;
144 }
145
146 llvm::errs() << "Writing '" << Filename << "'... ";
147
148 llvm::raw_fd_ostream O(FD, true);
149
150 InheritanceHierarchyWriter Writer(Context, O);
151 Writer.WriteGraph(Self);
152 llvm::errs() << " done. \n";
153
154 O.close();
155
156 // Display the graph
157 DisplayGraph(Filename);
158}
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
StringRef Filename
Definition: Format.cpp:3032
Allows QualTypes to be sorted and hence used in maps and sets.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2716
QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const
Return the unique reference to the type for the specified type declaration.
Definition: ASTContext.h:1703
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
void viewInheritance(ASTContext &Context) const
Renders and displays an inheritance diagram for this C++ class and all of its base classes (transitiv...
Definition: InheritViz.cpp:135
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
A (possibly-)qualified type.
Definition: Type.h:929
void * getAsOpaquePtr() const
Definition: Type.h:976
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:1327
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:6072
The base class of the type hierarchy.
Definition: Type.h:1828
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:8800
The JSON file list parser is used to communicate input to InstallAPI.
@ Self
'self' clause, allowed on Compute and Combined Constructs, plus 'update'.