Path: blob/main/contrib/llvm-project/clang/lib/AST/InheritViz.cpp
35259 views
//===- InheritViz.cpp - Graphviz visualization for inheritance --*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements CXXRecordDecl::viewInheritance, which9// generates a GraphViz DOT file that depicts the class inheritance10// diagram and then calls Graphviz/dot+gv on it.11//12//===----------------------------------------------------------------------===//1314#include "clang/AST/ASTContext.h"15#include "clang/AST/Decl.h"16#include "clang/AST/DeclCXX.h"17#include "clang/AST/TypeOrdering.h"18#include "llvm/Support/FileSystem.h"19#include "llvm/Support/GraphWriter.h"20#include "llvm/Support/raw_ostream.h"21#include <map>22#include <set>23using namespace clang;2425namespace {26/// InheritanceHierarchyWriter - Helper class that writes out a27/// GraphViz file that diagrams the inheritance hierarchy starting at28/// a given C++ class type. Note that we do not use LLVM's29/// GraphWriter, because the interface does not permit us to properly30/// differentiate between uses of types as virtual bases31/// vs. non-virtual bases.32class InheritanceHierarchyWriter {33ASTContext& Context;34raw_ostream &Out;35std::map<QualType, int, QualTypeOrdering> DirectBaseCount;36std::set<QualType, QualTypeOrdering> KnownVirtualBases;3738public:39InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)40: Context(Context), Out(Out) { }4142void WriteGraph(QualType Type) {43Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString())44<< "\" {\n";45WriteNode(Type, false);46Out << "}\n";47}4849protected:50/// WriteNode - Write out the description of node in the inheritance51/// diagram, which may be a base class or it may be the root node.52void WriteNode(QualType Type, bool FromVirtual);5354/// WriteNodeReference - Write out a reference to the given node,55/// using a unique identifier for each direct base and for the56/// (only) virtual base.57raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);58};59} // namespace6061void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {62QualType CanonType = Context.getCanonicalType(Type);6364if (FromVirtual) {65if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end())66return;6768// We haven't seen this virtual base before, so display it and69// its bases.70KnownVirtualBases.insert(CanonType);71}7273// Declare the node itself.74Out << " ";75WriteNodeReference(Type, FromVirtual);7677// Give the node a label based on the name of the class.78std::string TypeName = Type.getAsString();79Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName);8081// If the name of the class was a typedef or something different82// from the "real" class name, show the real class name in83// parentheses so we don't confuse ourselves.84if (TypeName != CanonType.getAsString()) {85Out << "\\n(" << CanonType.getAsString() << ")";86}8788// Finished describing the node.89Out << " \"];\n";9091// Display the base classes.92const auto *Decl =93static_cast<const CXXRecordDecl *>(Type->castAs<RecordType>()->getDecl());94for (const auto &Base : Decl->bases()) {95QualType CanonBaseType = Context.getCanonicalType(Base.getType());9697// If this is not virtual inheritance, bump the direct base98// count for the type.99if (!Base.isVirtual())100++DirectBaseCount[CanonBaseType];101102// Write out the node (if we need to).103WriteNode(Base.getType(), Base.isVirtual());104105// Write out the edge.106Out << " ";107WriteNodeReference(Type, FromVirtual);108Out << " -> ";109WriteNodeReference(Base.getType(), Base.isVirtual());110111// Write out edge attributes to show the kind of inheritance.112if (Base.isVirtual()) {113Out << " [ style=\"dashed\" ]";114}115Out << ";";116}117}118119/// WriteNodeReference - Write out a reference to the given node,120/// using a unique identifier for each direct base and for the121/// (only) virtual base.122raw_ostream&123InheritanceHierarchyWriter::WriteNodeReference(QualType Type,124bool FromVirtual) {125QualType CanonType = Context.getCanonicalType(Type);126127Out << "Class_" << CanonType.getAsOpaquePtr();128if (!FromVirtual)129Out << "_" << DirectBaseCount[CanonType];130return Out;131}132133/// viewInheritance - Display the inheritance hierarchy of this C++134/// class using GraphViz.135void CXXRecordDecl::viewInheritance(ASTContext& Context) const {136QualType Self = Context.getTypeDeclType(this);137138int FD;139SmallString<128> Filename;140if (std::error_code EC = llvm::sys::fs::createTemporaryFile(141Self.getAsString(), "dot", FD, Filename)) {142llvm::errs() << "Error: " << EC.message() << "\n";143return;144}145146llvm::errs() << "Writing '" << Filename << "'... ";147148llvm::raw_fd_ostream O(FD, true);149150InheritanceHierarchyWriter Writer(Context, O);151Writer.WriteGraph(Self);152llvm::errs() << " done. \n";153154O.close();155156// Display the graph157DisplayGraph(Filename);158}159160161