clang API Documentation

InheritViz.cpp
Go to the documentation of this file.
00001 //===- InheritViz.cpp - Graphviz visualization for inheritance --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements CXXRecordDecl::viewInheritance, which
00011 //  generates a GraphViz DOT file that depicts the class inheritance
00012 //  diagram and then calls Graphviz/dot+gv on it.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "clang/AST/ASTContext.h"
00017 #include "clang/AST/Decl.h"
00018 #include "clang/AST/DeclCXX.h"
00019 #include "clang/AST/TypeOrdering.h"
00020 #include "llvm/Support/FileSystem.h"
00021 #include "llvm/Support/GraphWriter.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 #include <map>
00024 #include <set>
00025 
00026 using namespace llvm;
00027 
00028 namespace clang {
00029 
00030 /// InheritanceHierarchyWriter - Helper class that writes out a
00031 /// GraphViz file that diagrams the inheritance hierarchy starting at
00032 /// a given C++ class type. Note that we do not use LLVM's
00033 /// GraphWriter, because the interface does not permit us to properly
00034 /// differentiate between uses of types as virtual bases
00035 /// vs. non-virtual bases.
00036 class InheritanceHierarchyWriter {
00037   ASTContext& Context;
00038   raw_ostream &Out;
00039   std::map<QualType, int, QualTypeOrdering> DirectBaseCount;
00040   std::set<QualType, QualTypeOrdering> KnownVirtualBases;
00041 
00042 public:
00043   InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)
00044     : Context(Context), Out(Out) { }
00045 
00046   void WriteGraph(QualType Type) {
00047     Out << "digraph \"" << DOT::EscapeString(Type.getAsString()) << "\" {\n";
00048     WriteNode(Type, false);
00049     Out << "}\n";
00050   }
00051 
00052 protected:
00053   /// WriteNode - Write out the description of node in the inheritance
00054   /// diagram, which may be a base class or it may be the root node.
00055   void WriteNode(QualType Type, bool FromVirtual);
00056 
00057   /// WriteNodeReference - Write out a reference to the given node,
00058   /// using a unique identifier for each direct base and for the
00059   /// (only) virtual base.
00060   raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);
00061 };
00062 
00063 void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {
00064   QualType CanonType = Context.getCanonicalType(Type);
00065 
00066   if (FromVirtual) {
00067     if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end())
00068       return;
00069 
00070     // We haven't seen this virtual base before, so display it and
00071     // its bases.
00072     KnownVirtualBases.insert(CanonType);
00073   }
00074 
00075   // Declare the node itself.
00076   Out << "  ";
00077   WriteNodeReference(Type, FromVirtual);
00078 
00079   // Give the node a label based on the name of the class.
00080   std::string TypeName = Type.getAsString();
00081   Out << " [ shape=\"box\", label=\"" << DOT::EscapeString(TypeName);
00082 
00083   // If the name of the class was a typedef or something different
00084   // from the "real" class name, show the real class name in
00085   // parentheses so we don't confuse ourselves.
00086   if (TypeName != CanonType.getAsString()) {
00087     Out << "\\n(" << CanonType.getAsString() << ")";
00088   }
00089 
00090   // Finished describing the node.
00091   Out << " \"];\n";
00092 
00093   // Display the base classes.
00094   const CXXRecordDecl *Decl
00095     = static_cast<const CXXRecordDecl *>(Type->getAs<RecordType>()->getDecl());
00096   for (const auto &Base : Decl->bases()) {
00097     QualType CanonBaseType = Context.getCanonicalType(Base.getType());
00098 
00099     // If this is not virtual inheritance, bump the direct base
00100     // count for the type.
00101     if (!Base.isVirtual())
00102       ++DirectBaseCount[CanonBaseType];
00103 
00104     // Write out the node (if we need to).
00105     WriteNode(Base.getType(), Base.isVirtual());
00106 
00107     // Write out the edge.
00108     Out << "  ";
00109     WriteNodeReference(Type, FromVirtual);
00110     Out << " -> ";
00111     WriteNodeReference(Base.getType(), Base.isVirtual());
00112 
00113     // Write out edge attributes to show the kind of inheritance.
00114     if (Base.isVirtual()) {
00115       Out << " [ style=\"dashed\" ]";
00116     }
00117     Out << ";";
00118   }
00119 }
00120 
00121 /// WriteNodeReference - Write out a reference to the given node,
00122 /// using a unique identifier for each direct base and for the
00123 /// (only) virtual base.
00124 raw_ostream&
00125 InheritanceHierarchyWriter::WriteNodeReference(QualType Type,
00126                                                bool FromVirtual) {
00127   QualType CanonType = Context.getCanonicalType(Type);
00128 
00129   Out << "Class_" << CanonType.getAsOpaquePtr();
00130   if (!FromVirtual)
00131     Out << "_" << DirectBaseCount[CanonType];
00132   return Out;
00133 }
00134 
00135 /// viewInheritance - Display the inheritance hierarchy of this C++
00136 /// class using GraphViz.
00137 void CXXRecordDecl::viewInheritance(ASTContext& Context) const {
00138   QualType Self = Context.getTypeDeclType(this);
00139 
00140   int FD;
00141   SmallString<128> Filename;
00142   std::error_code EC =
00143       sys::fs::createTemporaryFile(Self.getAsString(), "dot", FD, Filename);
00144   if (EC) {
00145     llvm::errs() << "Error: " << EC.message() << "\n";
00146     return;
00147   }
00148 
00149   llvm::errs() << "Writing '" << Filename << "'... ";
00150 
00151   llvm::raw_fd_ostream O(FD, true);
00152 
00153   InheritanceHierarchyWriter Writer(Context, O);
00154   Writer.WriteGraph(Self);
00155   llvm::errs() << " done. \n";
00156 
00157   O.close();
00158 
00159   // Display the graph
00160   DisplayGraph(Filename);
00161 }
00162 
00163 }