1353358Sdim//===-- llvm/Support/DOTGraphTraits.h - Customize .dot output ---*- C++ -*-===//
2193323Sed//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6193323Sed//
7193323Sed//===----------------------------------------------------------------------===//
8193323Sed//
9193323Sed// This file defines a template class that can be used to customize dot output
10193323Sed// graphs generated by the GraphWriter.h file.  The default implementation of
11193323Sed// this file will produce a simple, but not very polished graph.  By
12193323Sed// specializing this template, lots of customization opportunities are possible.
13193323Sed//
14193323Sed//===----------------------------------------------------------------------===//
15193323Sed
16193323Sed#ifndef LLVM_SUPPORT_DOTGRAPHTRAITS_H
17193323Sed#define LLVM_SUPPORT_DOTGRAPHTRAITS_H
18193323Sed
19193323Sed#include <string>
20193323Sed
21193323Sednamespace llvm {
22193323Sed
23193323Sed/// DefaultDOTGraphTraits - This class provides the default implementations of
24193323Sed/// all of the DOTGraphTraits methods.  If a specialization does not need to
25193323Sed/// override all methods here it should inherit so that it can get the default
26193323Sed/// implementations.
27193323Sed///
28193323Sedstruct DefaultDOTGraphTraits {
29199989Srdivackyprivate:
30199989Srdivacky  bool IsSimple;
31199989Srdivacky
32199989Srdivackyprotected:
33199989Srdivacky  bool isSimple() {
34199989Srdivacky    return IsSimple;
35199989Srdivacky  }
36199989Srdivacky
37199989Srdivackypublic:
38208599Srdivacky  explicit DefaultDOTGraphTraits(bool simple=false) : IsSimple (simple) {}
39199989Srdivacky
40193323Sed  /// getGraphName - Return the label for the graph as a whole.  Printed at the
41193323Sed  /// top of the graph.
42193323Sed  ///
43193323Sed  template<typename GraphType>
44234353Sdim  static std::string getGraphName(const GraphType &) { return ""; }
45193323Sed
46193323Sed  /// getGraphProperties - Return any custom properties that should be included
47193323Sed  /// in the top level graph structure for dot.
48193323Sed  ///
49193323Sed  template<typename GraphType>
50234353Sdim  static std::string getGraphProperties(const GraphType &) {
51193323Sed    return "";
52193323Sed  }
53193323Sed
54193323Sed  /// renderGraphFromBottomUp - If this function returns true, the graph is
55193323Sed  /// emitted bottom-up instead of top-down.  This requires graphviz 2.0 to work
56193323Sed  /// though.
57193323Sed  static bool renderGraphFromBottomUp() {
58193323Sed    return false;
59193323Sed  }
60193323Sed
61208599Srdivacky  /// isNodeHidden - If the function returns true, the given node is not
62208599Srdivacky  /// displayed in the graph.
63234353Sdim  static bool isNodeHidden(const void *) {
64208599Srdivacky    return false;
65208599Srdivacky  }
66208599Srdivacky
67193323Sed  /// getNodeLabel - Given a node and a pointer to the top level graph, return
68193323Sed  /// the label to print in the node.
69193323Sed  template<typename GraphType>
70234353Sdim  std::string getNodeLabel(const void *, const GraphType &) {
71193323Sed    return "";
72193323Sed  }
73193323Sed
74296417Sdim  // getNodeIdentifierLabel - Returns a string representing the
75296417Sdim  // address or other unique identifier of the node. (Only used if
76296417Sdim  // non-empty.)
77296417Sdim  template <typename GraphType>
78296417Sdim  static std::string getNodeIdentifierLabel(const void *, const GraphType &) {
79296417Sdim    return "";
80193323Sed  }
81193323Sed
82249423Sdim  template<typename GraphType>
83249423Sdim  static std::string getNodeDescription(const void *, const GraphType &) {
84249423Sdim    return "";
85249423Sdim  }
86249423Sdim
87193323Sed  /// If you want to specify custom node attributes, this is the place to do so
88193323Sed  ///
89193323Sed  template<typename GraphType>
90234353Sdim  static std::string getNodeAttributes(const void *,
91234353Sdim                                       const GraphType &) {
92193323Sed    return "";
93193323Sed  }
94193323Sed
95193323Sed  /// If you want to override the dot attributes printed for a particular edge,
96193323Sed  /// override this method.
97221345Sdim  template<typename EdgeIter, typename GraphType>
98234353Sdim  static std::string getEdgeAttributes(const void *, EdgeIter,
99234353Sdim                                       const GraphType &) {
100193323Sed    return "";
101193323Sed  }
102193323Sed
103193323Sed  /// getEdgeSourceLabel - If you want to label the edge source itself,
104193323Sed  /// implement this method.
105193323Sed  template<typename EdgeIter>
106234353Sdim  static std::string getEdgeSourceLabel(const void *, EdgeIter) {
107193323Sed    return "";
108193323Sed  }
109193323Sed
110193323Sed  /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
111193323Sed  /// should actually target another edge source, not a node.  If this method is
112193323Sed  /// implemented, getEdgeTarget should be implemented.
113193323Sed  template<typename EdgeIter>
114234353Sdim  static bool edgeTargetsEdgeSource(const void *, EdgeIter) {
115193323Sed    return false;
116193323Sed  }
117193323Sed
118193323Sed  /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
119193323Sed  /// called to determine which outgoing edge of Node is the target of this
120193323Sed  /// edge.
121193323Sed  template<typename EdgeIter>
122234353Sdim  static EdgeIter getEdgeTarget(const void *, EdgeIter I) {
123193323Sed    return I;
124193323Sed  }
125193323Sed
126193323Sed  /// hasEdgeDestLabels - If this function returns true, the graph is able
127193323Sed  /// to provide labels for edge destinations.
128193323Sed  static bool hasEdgeDestLabels() {
129193323Sed    return false;
130193323Sed  }
131193323Sed
132193323Sed  /// numEdgeDestLabels - If hasEdgeDestLabels, this function returns the
133193323Sed  /// number of incoming edge labels the given node has.
134234353Sdim  static unsigned numEdgeDestLabels(const void *) {
135193323Sed    return 0;
136193323Sed  }
137193323Sed
138193323Sed  /// getEdgeDestLabel - If hasEdgeDestLabels, this function returns the
139193323Sed  /// incoming edge label with the given index in the given node.
140234353Sdim  static std::string getEdgeDestLabel(const void *, unsigned) {
141193323Sed    return "";
142193323Sed  }
143193323Sed
144193323Sed  /// addCustomGraphFeatures - If a graph is made up of more than just
145193323Sed  /// straight-forward nodes and edges, this is the place to put all of the
146193323Sed  /// custom stuff necessary.  The GraphWriter object, instantiated with your
147193323Sed  /// GraphType is passed in as an argument.  You may call arbitrary methods on
148193323Sed  /// it to add things to the output graph.
149193323Sed  ///
150193323Sed  template<typename GraphType, typename GraphWriter>
151234353Sdim  static void addCustomGraphFeatures(const GraphType &, GraphWriter &) {}
152193323Sed};
153193323Sed
154193323Sed
155193323Sed/// DOTGraphTraits - Template class that can be specialized to customize how
156193323Sed/// graphs are converted to 'dot' graphs.  When specializing, you may inherit
157193323Sed/// from DefaultDOTGraphTraits if you don't need to override everything.
158193323Sed///
159193323Sedtemplate <typename Ty>
160199989Srdivackystruct DOTGraphTraits : public DefaultDOTGraphTraits {
161199989Srdivacky  DOTGraphTraits (bool simple=false) : DefaultDOTGraphTraits (simple) {}
162199989Srdivacky};
163193323Sed
164193323Sed} // End llvm namespace
165193323Sed
166193323Sed#endif
167