GCC Middle and Back End API Reference
digraph.h
Go to the documentation of this file.
1/* Template classes for directed graphs.
2 Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_DIGRAPH_H
22#define GCC_DIGRAPH_H
23
24#include "diagnostic.h"
25#include "tree-diagnostic.h" /* for default_tree_printer. */
26#include "graphviz.h"
27
28/* Templates for a family of classes: digraph, node, edge, and cluster.
29 This assumes a traits type with the following typedefs:
30 node_t: the node class
31 edge_t: the edge class
32 dump_args_t: additional args for dot-dumps
33 cluster_t: the cluster class (for use when generating .dot files).
34
35 Using a template allows for typesafe nodes and edges: a node's
36 predecessor and successor edges can be of a node-specific edge
37 subclass, without needing casting. */
38
39/* Abstract base class for a node in a directed graph. */
40
41template <typename GraphTraits>
42class dnode
43{
44 public:
45 typedef typename GraphTraits::edge_t edge_t;
46 typedef typename GraphTraits::dump_args_t dump_args_t;
47
48 virtual ~dnode () {}
49 virtual void dump_dot (graphviz_out *gv, const dump_args_t &args) const = 0;
50
52 {
53 m_preds.safe_push (e);
54 }
56 {
57 m_preds.unordered_remove (find_edge_idx (m_preds, e));
58 }
60 {
61 m_succs.safe_push (e);
62 }
64 {
65 m_succs.unordered_remove (find_edge_idx (m_succs, e));
66 }
67
68public:
71
72private:
73 static unsigned
75 {
76 for (unsigned i = 0; i < edges.length (); ++i)
77 if (edges[i] == e)
78 return i;
80 }
81};
82
83/* Abstract base class for an edge in a directed graph. */
84
85template <typename GraphTraits>
86class dedge
87{
88 public:
89 typedef typename GraphTraits::node_t node_t;
90 typedef typename GraphTraits::edge_t edge_t;
91 typedef typename GraphTraits::dump_args_t dump_args_t;
92
93 dedge (node_t *src, node_t *dest)
94 : m_src (src), m_dest (dest) {}
95
96 virtual ~dedge () {}
97
98 virtual void dump_dot (graphviz_out *gv, const dump_args_t &args) const = 0;
99
100 void set_dest (node_t *new_dest)
101 {
102 node_t *old_dest = m_dest;
103 if (new_dest != old_dest)
104 {
105 old_dest->remove_in_edge (static_cast<edge_t *> (this));
106 m_dest = new_dest;
107 new_dest->add_in_edge (static_cast<edge_t *> (this));
108 }
109 }
110
113};
114
115/* Abstract base class for a directed graph.
116 This class maintains the vectors of nodes and edges,
117 and owns the nodes and edges. */
118
119template <typename GraphTraits>
121{
122 public:
123 typedef typename GraphTraits::node_t node_t;
124 typedef typename GraphTraits::edge_t edge_t;
125 typedef typename GraphTraits::dump_args_t dump_args_t;
126 typedef typename GraphTraits::cluster_t cluster_t;
127
129 virtual ~digraph () {}
130
132 cluster_t *root_cluster,
133 const dump_args_t &args) const;
134 void dump_dot_to_file (FILE *fp,
135 cluster_t *root_cluster,
136 const dump_args_t &args) const;
137 void dump_dot (const char *path,
138 cluster_t *root_cluster,
139 const dump_args_t &args) const;
140
143
144 virtual void
146 {
147 // no-op hook
148 }
149
152};
153
154/* Abstract base class for splitting dnodes into hierarchical clusters
155 in the generated .dot file.
156
157 See "Subgraphs and Clusters" within
158 https://www.graphviz.org/doc/info/lang.html
159 and e.g.
160 https://graphviz.gitlab.io/_pages/Gallery/directed/cluster.html
161
162 If a root_cluster is passed to dump_dot*, then all nodes will be
163 added to it at the start of dumping, via calls to add_node.
164
165 The root cluster can organize the nodes into a hierarchy of
166 child clusters.
167
168 After all nodes are added to the root cluster, dump_dot will then
169 be called on it (and not on the nodes themselves). */
170
171template <typename GraphTraits>
173{
174 public:
175 typedef typename GraphTraits::node_t node_t;
176 typedef typename GraphTraits::dump_args_t dump_args_t;
177
178 virtual ~cluster () {}
179
180 virtual void add_node (node_t *node) = 0;
181
182 /* Recursively dump the cluster, all nodes, and child clusters. */
183 virtual void dump_dot (graphviz_out *gv, const dump_args_t &) const = 0;
184};
185
186/* Write .dot information for this graph to PP, passing ARGS to the nodes
187 and edges.
188 If ROOT_CLUSTER is non-NULL, use it to organize the nodes into clusters. */
189
190template <typename GraphTraits>
191inline void
193 cluster_t *root_cluster,
194 const dump_args_t &args) const
195{
196 graphviz_out gv (pp);
197
198 pp_string (pp, "digraph \"");
199 pp_string (pp, "base");
200 pp_string (pp, "\" {\n");
201
202 gv.indent ();
203
204 pp_string (pp, "overlap=false;\n");
205 pp_string (pp, "compound=true;\n");
206
207 /* If using clustering, emit all nodes via clusters. */
208 if (root_cluster)
209 {
210 int i;
211 node_t *n;
213 root_cluster->add_node (n);
214 root_cluster->dump_dot (&gv, args);
215 }
216 else
217 {
218 /* Otherwise, display all nodes at top level. */
219 int i;
220 node_t *n;
222 n->dump_dot (&gv, args);
223 }
224
225 /* Edges. */
226 int i;
227 edge_t *e;
229 e->dump_dot (&gv, args);
230
232
233 /* Terminate "digraph" */
234 gv.outdent ();
235 pp_string (pp, "}");
236 pp_newline (pp);
237}
238
239/* Write .dot information for this graph to FP, passing ARGS to the nodes
240 and edges.
241 If ROOT_CLUSTER is non-NULL, use it to organize the nodes into clusters. */
242
243template <typename GraphTraits>
244inline void
246 cluster_t *root_cluster,
247 const dump_args_t &args) const
248{
250 // TODO:
252 pp.set_output_stream (fp);
253 dump_dot_to_pp (&pp, root_cluster, args);
254 pp_flush (&pp);
255}
256
257/* Write .dot information for this graph to a file at PATH, passing ARGS
258 to the nodes and edges.
259 If ROOT_CLUSTER is non-NULL, use it to organize the nodes into clusters. */
260
261template <typename GraphTraits>
262inline void
264 cluster_t *root_cluster,
265 const dump_args_t &args) const
266{
267 FILE *fp = fopen (path, "w");
268 dump_dot_to_file (fp, root_cluster, args);
269 fclose (fp);
270}
271
272/* Add NODE to this DIGRAPH, taking ownership. */
273
274template <typename GraphTraits>
275inline void
280
281/* Add EDGE to this digraph, and to the preds/succs of its endpoints.
282 Take ownership of EDGE. */
283
284template <typename GraphTraits>
285inline void
287{
288 m_edges.safe_push (edge);
289 edge->m_dest->m_preds.safe_push (edge);
290 edge->m_src->m_succs.safe_push (edge);
291
292}
293
294#endif /* GCC_DIGRAPH_H */
Definition vec.h:1813
Definition vec.h:1667
Definition digraph.h:173
virtual void dump_dot(graphviz_out *gv, const dump_args_t &) const =0
GraphTraits::dump_args_t dump_args_t
Definition digraph.h:176
GraphTraits::node_t node_t
Definition digraph.h:175
virtual void add_node(node_t *node)=0
virtual ~cluster()
Definition digraph.h:178
virtual void dump_dot(graphviz_out *gv, const dump_args_t &args) const =0
node_t * m_dest
Definition digraph.h:112
dedge(node_t *src, node_t *dest)
Definition digraph.h:93
void set_dest(node_t *new_dest)
Definition digraph.h:100
GraphTraits::node_t node_t
Definition digraph.h:89
node_t * m_src
Definition digraph.h:111
GraphTraits::edge_t edge_t
Definition digraph.h:90
GraphTraits::dump_args_t dump_args_t
Definition digraph.h:91
virtual ~dedge()
Definition digraph.h:96
void add_node(std::unique_ptr< node > n)
Definition diagnostics/digraphs.h:205
void add_edge(std::unique_ptr< edge > e)
Definition diagnostics/digraphs.h:212
GraphTraits::node_t node_t
Definition digraph.h:123
GraphTraits::edge_t edge_t
Definition digraph.h:124
auto_delete_vec< edge_t > m_edges
Definition digraph.h:151
void dump_dot_to_pp(pretty_printer *pp, cluster_t *root_cluster, const dump_args_t &args) const
Definition digraph.h:192
void dump_dot_to_file(FILE *fp, cluster_t *root_cluster, const dump_args_t &args) const
Definition digraph.h:245
auto_delete_vec< node_t > m_nodes
Definition digraph.h:150
void add_edge(edge_t *edge)
Definition digraph.h:286
void dump_dot(const char *path, cluster_t *root_cluster, const dump_args_t &args) const
Definition digraph.h:263
virtual void add_any_extra_stmts(graphviz_out &) const
Definition digraph.h:145
virtual ~digraph()
Definition digraph.h:129
GraphTraits::cluster_t cluster_t
Definition digraph.h:126
GraphTraits::dump_args_t dump_args_t
Definition digraph.h:125
void add_node(node_t *node)
Definition digraph.h:276
digraph()
Definition digraph.h:128
Definition digraph.h:43
void add_in_edge(edge_t *e)
Definition digraph.h:51
GraphTraits::edge_t edge_t
Definition digraph.h:45
virtual void dump_dot(graphviz_out *gv, const dump_args_t &args) const =0
void remove_out_edge(edge_t *e)
Definition digraph.h:63
virtual ~dnode()
Definition digraph.h:48
GraphTraits::dump_args_t dump_args_t
Definition digraph.h:46
static unsigned find_edge_idx(auto_vec< edge_t * > &edges, edge_t *e)
Definition digraph.h:74
auto_vec< edge_t * > m_preds
Definition digraph.h:69
void add_out_edge(edge_t *e)
Definition digraph.h:59
void remove_in_edge(edge_t *e)
Definition digraph.h:55
auto_vec< edge_t * > m_succs
Definition digraph.h:70
void indent()
Definition graphviz.h:37
void outdent()
Definition graphviz.h:38
Definition graphviz.h:385
Definition pretty-print.h:241
void set_output_stream(FILE *outfile)
Definition pretty-print.h:274
static struct path_prefix cpath path
Definition collect2.cc:514
class edge_def * edge
Definition coretypes.h:369
Definition custom-sarif-properties/state-graphs.h:33
i
Definition poly-int.h:776
void pp_flush(pretty_printer *pp)
Definition pretty-print.cc:2462
void pp_newline(pretty_printer *pp)
Definition pretty-print.cc:2737
void pp_string(pretty_printer *pp, const char *str)
Definition pretty-print.cc:2764
printer_fn & pp_format_decoder(pretty_printer *pp)
Definition pretty-print.h:446
#define gcc_unreachable()
Definition system.h:844
#define fopen(PATH, MODE)
Definition system.h:62
bool default_tree_printer(pretty_printer *pp, text_info *text, const char *spec, int precision, bool wide, bool set_locus, bool hash, bool *, pp_token_list &)
Definition tree-diagnostic.cc:58
#define FOR_EACH_VEC_ELT(V, I, P)
Definition vec.h:1895