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
use rustc_data_structures::graph::{self, iterate};
use rustc_graphviz as dot;
use rustc_middle::ty::TyCtxt;
use std::io::{self, Write};
pub struct GraphvizWriter<
'a,
G: graph::DirectedGraph + graph::WithSuccessors + graph::WithStartNode + graph::WithNumNodes,
NodeContentFn: Fn(<G as graph::DirectedGraph>::Node) -> Vec<String>,
EdgeLabelsFn: Fn(<G as graph::DirectedGraph>::Node) -> Vec<String>,
> {
graph: &'a G,
is_subgraph: bool,
graphviz_name: String,
graph_label: Option<String>,
node_content_fn: NodeContentFn,
edge_labels_fn: EdgeLabelsFn,
}
impl<
'a,
G: graph::DirectedGraph + graph::WithSuccessors + graph::WithStartNode + graph::WithNumNodes,
NodeContentFn: Fn(<G as graph::DirectedGraph>::Node) -> Vec<String>,
EdgeLabelsFn: Fn(<G as graph::DirectedGraph>::Node) -> Vec<String>,
> GraphvizWriter<'a, G, NodeContentFn, EdgeLabelsFn>
{
pub fn new(
graph: &'a G,
graphviz_name: &str,
node_content_fn: NodeContentFn,
edge_labels_fn: EdgeLabelsFn,
) -> Self {
Self {
graph,
is_subgraph: false,
graphviz_name: graphviz_name.to_owned(),
graph_label: None,
node_content_fn,
edge_labels_fn,
}
}
pub fn set_graph_label(&mut self, graph_label: &str) {
self.graph_label = Some(graph_label.to_owned());
}
pub fn write_graphviz<'tcx, W>(&self, tcx: TyCtxt<'tcx>, w: &mut W) -> io::Result<()>
where
W: Write,
{
let kind = if self.is_subgraph { "subgraph" } else { "digraph" };
let cluster = if self.is_subgraph { "cluster_" } else { "" }; writeln!(w, "{} {}{} {{", kind, cluster, self.graphviz_name)?;
let font = format!(r#"fontname="{}""#, tcx.sess.opts.unstable_opts.graphviz_font);
let mut graph_attrs = vec![&font[..]];
let mut content_attrs = vec![&font[..]];
let dark_mode = tcx.sess.opts.unstable_opts.graphviz_dark_mode;
if dark_mode {
graph_attrs.push(r#"bgcolor="black""#);
graph_attrs.push(r#"fontcolor="white""#);
content_attrs.push(r#"color="white""#);
content_attrs.push(r#"fontcolor="white""#);
}
writeln!(w, r#" graph [{}];"#, graph_attrs.join(" "))?;
let content_attrs_str = content_attrs.join(" ");
writeln!(w, r#" node [{}];"#, content_attrs_str)?;
writeln!(w, r#" edge [{}];"#, content_attrs_str)?;
if let Some(graph_label) = &self.graph_label {
self.write_graph_label(graph_label, w)?;
}
for node in iterate::post_order_from(self.graph, self.graph.start_node()) {
self.write_node(node, dark_mode, w)?;
}
for source in iterate::post_order_from(self.graph, self.graph.start_node()) {
self.write_edges(source, w)?;
}
writeln!(w, "}}")
}
pub fn write_node<W>(&self, node: G::Node, dark_mode: bool, w: &mut W) -> io::Result<()>
where
W: Write,
{
write!(w, r#" {} [shape="none", label=<"#, self.node(node))?;
write!(w, r#"<table border="0" cellborder="1" cellspacing="0">"#)?;
let color = if dark_mode { "dimgray" } else { "gray" };
let (blk, bgcolor) = (format!("{:?}", node), color);
write!(
w,
r#"<tr><td bgcolor="{bgcolor}" {attrs} colspan="{colspan}">{blk}</td></tr>"#,
attrs = r#"align="center""#,
colspan = 1,
blk = blk,
bgcolor = bgcolor
)?;
for section in (self.node_content_fn)(node) {
write!(
w,
r#"<tr><td align="left" balign="left">{}</td></tr>"#,
dot::escape_html(§ion).replace('\n', "<br/>")
)?;
}
write!(w, "</table>")?;
writeln!(w, ">];")
}
fn write_edges<W>(&self, source: G::Node, w: &mut W) -> io::Result<()>
where
W: Write,
{
let edge_labels = (self.edge_labels_fn)(source);
for (index, target) in self.graph.successors(source).enumerate() {
let src = self.node(source);
let trg = self.node(target);
let escaped_edge_label = if let Some(edge_label) = edge_labels.get(index) {
dot::escape_html(edge_label).replace('\n', r#"<br align="left"/>"#)
} else {
"".to_owned()
};
writeln!(w, r#" {} -> {} [label=<{}>];"#, src, trg, escaped_edge_label)?;
}
Ok(())
}
fn write_graph_label<W>(&self, label: &str, w: &mut W) -> io::Result<()>
where
W: Write,
{
let lines = label.split('\n').map(|s| dot::escape_html(s)).collect::<Vec<_>>();
let escaped_label = lines.join(r#"<br align="left"/>"#);
writeln!(w, r#" label=<<br/><br/>{}<br align="left"/><br/><br/><br/>>;"#, escaped_label)
}
fn node(&self, node: G::Node) -> String {
format!("{:?}__{}", node, self.graphviz_name)
}
}