1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/* Helper code for graphviz output.
Copyright (C) 2019-2025 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef GCC_GRAPHVIZ_H
#define GCC_GRAPHVIZ_H
#include "pretty-print.h" /* for ATTRIBUTE_GCC_PPDIAG. */
namespace xml { class node; }
namespace dot {
/* A class for writing .dot output to a pretty_printer with
indentation to show nesting. */
class writer {
public:
writer (pretty_printer &pp);
void indent () { m_indent++; }
void outdent () { m_indent--; }
void write_indent ();
void write_character (char ch)
{
pp_character (&m_pp, ch);
}
void write_string (const char *str)
{
pp_string (&m_pp, str);
}
void write_newline ()
{
pp_newline (&m_pp);
}
pretty_printer *get_pp () const { return &m_pp; }
private:
pretty_printer &m_pp;
int m_indent;
};
// An AST for the dot language
// See https://graphviz.org/doc/info/lang.html
// Forward decls
struct id;
struct node_id;
struct port;
struct kv_pair;
struct graph;
struct attr_list;
struct stmt_list;
struct stmt;
struct node_stmt;
struct attr_stmt;
struct kv_stmt;
struct edge_stmt;
struct subgraph;
// Decls
struct ast_node
{
virtual ~ast_node () {}
virtual void print (writer &w) const = 0;
void dump () const;
};
struct id : public ast_node
{
enum class kind
{
identifier,
quoted,
html
};
id (std::string str);
/* For HTML labels: see https://graphviz.org/doc/info/shapes.html#html */
id (const xml::node &n);
void print (writer &w) const final override;
static bool is_identifier_p (const char *);
std::string m_str;
enum kind m_kind;
};
/* ID '=' ID */
struct kv_pair : ast_node
{
kv_pair (id key, id value)
: m_key (std::move (key)),
m_value (std::move (value))
{
}
void print (writer &w) const final override;
id m_key;
id m_value;
};
/* attr_list: '[' [ a_list ] ']' [ attr_list ] */
struct attr_list : public ast_node
{
void print (writer &w) const;
void add (id key, id value)
{
m_kvs.push_back ({std::move (key), std::move (value)});
}
std::vector<kv_pair> m_kvs;
};
/* stmt_list : [ stmt [ ';' ] stmt_list ] */
struct stmt_list : public ast_node
{
void print (writer &w) const final override;
void add_stmt (std::unique_ptr<stmt> s)
{
m_stmts.push_back (std::move (s));
}
void add_edge (node_id src_id, node_id dst_id);
void add_attr (id key, id value);
std::vector<std::unique_ptr<stmt>> m_stmts;
};
/* graph : [ strict ] (graph | digraph) [ ID ] '{' stmt_list '}' */
struct graph : public ast_node
{
graph ()
: m_id (nullptr)
{
}
graph (id id_)
: m_id (std::make_unique<id> (std::move (id_)))
{
}
void print (writer &w) const final override;
void add_stmt (std::unique_ptr<stmt> s)
{
m_stmt_list.add_stmt (std::move (s));
}
std::unique_ptr<id> m_id; // optional
stmt_list m_stmt_list;
};
/* Abstract base class.
stmt : node_stmt
| edge_stmt
| attr_stmt
| ID '=' ID ("kv_stmt")
| subgraph */
struct stmt
{
virtual ~stmt () {}
virtual void print (writer &w) const = 0;
};
struct stmt_with_attr_list : public stmt
{
void set_attr (id key, id value)
{
m_attrs.add (std::move (key), std::move (value));
}
void set_label (dot::id label);
attr_list m_attrs;
};
struct node_stmt : public stmt_with_attr_list
{
node_stmt (id id_)
: m_id (id_)
{
}
void print (writer &w) const final override;
id m_id;
};
struct attr_stmt : public stmt_with_attr_list
{
enum class kind { graph, node, edge };
attr_stmt (enum kind kind_)
: m_kind (kind_)
{
}
void print (writer &w) const final override;
enum kind m_kind;
};
/* "ID '=' ID" as a stmt. */
struct kv_stmt : public stmt
{
kv_stmt (kv_pair kv)
: m_kv (std::move (kv))
{}
void print (writer &w) const final override;
kv_pair m_kv;
};
/* node_id : ID [ port ] */
enum class compass_pt
{
n, ne, e, se, s, sw, w, nw, c
/* "_" clashes with intl macro */
};
/* port : ':' ID [ ':' compass_pt ]
| ':' compass_pt
*/
struct port : public ast_node
{
port (id id_)
: m_id (std::make_unique<id> (std::move (id_))),
m_compass_pt (nullptr)
{
}
port (enum compass_pt compass_pt_)
: m_id (nullptr),
m_compass_pt (std::make_unique<compass_pt> (compass_pt_))
{
}
port (id id_,
enum compass_pt compass_pt_)
: m_id (std::make_unique<id> (std::move (id_))),
m_compass_pt (std::make_unique<compass_pt> (compass_pt_))
{
}
port (const port &other)
: m_id (nullptr),
m_compass_pt (nullptr)
{
if (other.m_id)
m_id = std::make_unique<id> (*other.m_id);
if (other.m_compass_pt)
m_compass_pt = std::make_unique<enum compass_pt> (*other.m_compass_pt);
}
void print (writer &w) const final override;
std::unique_ptr<id> m_id; // would be std::optional
std::unique_ptr<enum compass_pt> m_compass_pt; // would be std::optional
};
struct node_id : public ast_node
{
node_id (id id_)
: m_id (id_),
m_port (nullptr)
{
}
node_id (id id_, port port_)
: m_id (id_),
m_port (std::make_unique<port> (std::move (port_)))
{
}
node_id (const node_id &other)
: m_id (other.m_id),
m_port (nullptr)
{
if (other.m_port)
m_port = std::make_unique<port> (*other.m_port);
}
void print (writer &w) const final override;
id m_id;
std::unique_ptr<port> m_port; // would be std::optional
};
/* The full grammar for edge_stmt is:
edge_stmt : (node_id | subgraph) edgeRHS [ attr_list ]
edgeRHS : edgeop (node_id | subgraph) [ edgeRHS ]
This class support the subsets where all are "node_id", rather than
"subgraph", and doesn't yet support "port" giving effectively:
node_id (edgeop node_id)+ [ attr_list]
*/
struct edge_stmt : public stmt_with_attr_list
{
edge_stmt (node_id src_id, node_id dst_id)
{
m_node_ids.push_back (std::move (src_id));
m_node_ids.push_back (std::move (dst_id));
}
void print (writer &w) const final override;
std::vector<node_id> m_node_ids; // should have 2 or more elements
};
/* [ subgraph [ ID ] ] '{' stmt_list '}' */
struct subgraph : public stmt
{
subgraph (id id_)
: m_id (id_)
{
}
void print (writer &w) const final override;
void add_stmt (std::unique_ptr<stmt> s)
{
m_stmt_list.add_stmt (std::move (s));
}
void add_attr (id key, id value)
{
m_stmt_list.add_stmt
(std::make_unique <kv_stmt> (kv_pair (std::move (key),
std::move (value))));
}
id m_id;
stmt_list m_stmt_list;
};
extern std::unique_ptr<xml::node>
make_svg_from_graph (const graph &g);
} // namespace dot
/* A class for writing .dot output to a pretty_printer with
indentation to show nesting. */
class graphviz_out : public dot::writer {
public:
graphviz_out (pretty_printer *pp);
void print (const char *fmt, ...)
ATTRIBUTE_GCC_PPDIAG(2,3);
void println (const char *fmt, ...)
ATTRIBUTE_GCC_PPDIAG(2,3);
void begin_tr ();
void end_tr ();
void begin_td ();
void end_td ();
void begin_trtd ();
void end_tdtr ();
};
#endif /* GCC_GRAPHVIZ_H */
|