Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/InheritViz.cpp
Line
Count
Source (jump to first uncovered line)
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
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/Decl.h"
17
#include "clang/AST/DeclCXX.h"
18
#include "clang/AST/TypeOrdering.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>
24
using namespace clang;
25
26
namespace {
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.
33
class InheritanceHierarchyWriter {
34
  ASTContext& Context;
35
  raw_ostream &Out;
36
  std::map<QualType, int, QualTypeOrdering> DirectBaseCount;
37
  std::set<QualType, QualTypeOrdering> KnownVirtualBases;
38
39
public:
40
  InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)
41
0
    : Context(Context), Out(Out) { }
42
43
0
  void WriteGraph(QualType Type) {
44
0
    Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString())
45
0
        << "\" {\n";
46
0
    WriteNode(Type, false);
47
0
    Out << "}\n";
48
0
  }
49
50
protected:
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
62
0
void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {
63
0
  QualType CanonType = Context.getCanonicalType(Type);
64
0
65
0
  if (FromVirtual) {
66
0
    if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end())
67
0
      return;
68
0
69
0
    // We haven't seen this virtual base before, so display it and
70
0
    // its bases.
71
0
    KnownVirtualBases.insert(CanonType);
72
0
  }
73
0
74
0
  // Declare the node itself.
75
0
  Out << "  ";
76
0
  WriteNodeReference(Type, FromVirtual);
77
0
78
0
  // Give the node a label based on the name of the class.
79
0
  std::string TypeName = Type.getAsString();
80
0
  Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName);
81
0
82
0
  // If the name of the class was a typedef or something different
83
0
  // from the "real" class name, show the real class name in
84
0
  // parentheses so we don't confuse ourselves.
85
0
  if (TypeName != CanonType.getAsString()) {
86
0
    Out << "\\n(" << CanonType.getAsString() << ")";
87
0
  }
88
0
89
0
  // Finished describing the node.
90
0
  Out << " \"];\n";
91
0
92
0
  // Display the base classes.
93
0
  const auto *Decl =
94
0
      static_cast<const CXXRecordDecl *>(Type->castAs<RecordType>()->getDecl());
95
0
  for (const auto &Base : Decl->bases()) {
96
0
    QualType CanonBaseType = Context.getCanonicalType(Base.getType());
97
0
98
0
    // If this is not virtual inheritance, bump the direct base
99
0
    // count for the type.
100
0
    if (!Base.isVirtual())
101
0
      ++DirectBaseCount[CanonBaseType];
102
0
103
0
    // Write out the node (if we need to).
104
0
    WriteNode(Base.getType(), Base.isVirtual());
105
0
106
0
    // Write out the edge.
107
0
    Out << "  ";
108
0
    WriteNodeReference(Type, FromVirtual);
109
0
    Out << " -> ";
110
0
    WriteNodeReference(Base.getType(), Base.isVirtual());
111
0
112
0
    // Write out edge attributes to show the kind of inheritance.
113
0
    if (Base.isVirtual()) {
114
0
      Out << " [ style=\"dashed\" ]";
115
0
    }
116
0
    Out << ";";
117
0
  }
118
0
}
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.
123
raw_ostream&
124
InheritanceHierarchyWriter::WriteNodeReference(QualType Type,
125
0
                                               bool FromVirtual) {
126
0
  QualType CanonType = Context.getCanonicalType(Type);
127
0
128
0
  Out << "Class_" << CanonType.getAsOpaquePtr();
129
0
  if (!FromVirtual)
130
0
    Out << "_" << DirectBaseCount[CanonType];
131
0
  return Out;
132
0
}
133
134
/// viewInheritance - Display the inheritance hierarchy of this C++
135
/// class using GraphViz.
136
0
void CXXRecordDecl::viewInheritance(ASTContext& Context) const {
137
0
  QualType Self = Context.getTypeDeclType(this);
138
0
139
0
  int FD;
140
0
  SmallString<128> Filename;
141
0
  if (std::error_code EC = llvm::sys::fs::createTemporaryFile(
142
0
          Self.getAsString(), "dot", FD, Filename)) {
143
0
    llvm::errs() << "Error: " << EC.message() << "\n";
144
0
    return;
145
0
  }
146
0
147
0
  llvm::errs() << "Writing '" << Filename << "'... ";
148
0
149
0
  llvm::raw_fd_ostream O(FD, true);
150
0
151
0
  InheritanceHierarchyWriter Writer(Context, O);
152
0
  Writer.WriteGraph(Self);
153
0
  llvm::errs() << " done. \n";
154
0
155
0
  O.close();
156
0
157
0
  // Display the graph
158
0
  DisplayGraph(Filename);
159
0
}