1 use gsgdt::GraphvizSettings;
2 use rustc_graphviz as dot;
3 use rustc_hir::def_id::DefId;
4 use rustc_middle::mir::*;
5 use rustc_middle::ty::{self, TyCtxt};
6 use std::fmt::Debug;
7 use std::io::{self, Write};
8
9 use super::generic_graph::mir_fn_to_generic_graph;
10 use super::pretty::dump_mir_def_ids;
11
12 /// Write a graphviz DOT graph of a list of MIRs.
write_mir_graphviz<W>(tcx: TyCtxt<'_>, single: Option<DefId>, w: &mut W) -> io::Result<()> where W: Write,13 pub fn write_mir_graphviz<W>(tcx: TyCtxt<'_>, single: Option<DefId>, w: &mut W) -> io::Result<()>
14 where
15 W: Write,
16 {
17 let def_ids = dump_mir_def_ids(tcx, single);
18
19 let mirs =
20 def_ids
21 .iter()
22 .flat_map(|def_id| {
23 if tcx.is_const_fn_raw(*def_id) {
24 vec![tcx.optimized_mir(*def_id), tcx.mir_for_ctfe(*def_id)]
25 } else {
26 vec![tcx.instance_mir(ty::InstanceDef::Item(ty::WithOptConstParam::unknown(
27 *def_id,
28 )))]
29 }
30 })
31 .collect::<Vec<_>>();
32
33 let use_subgraphs = mirs.len() > 1;
34 if use_subgraphs {
35 writeln!(w, "digraph __crate__ {{")?;
36 }
37
38 for mir in mirs {
39 write_mir_fn_graphviz(tcx, mir, use_subgraphs, w)?;
40 }
41
42 if use_subgraphs {
43 writeln!(w, "}}")?;
44 }
45
46 Ok(())
47 }
48
49 /// Write a graphviz DOT graph of the MIR.
write_mir_fn_graphviz<'tcx, W>( tcx: TyCtxt<'tcx>, body: &Body<'_>, subgraph: bool, w: &mut W, ) -> io::Result<()> where W: Write,50 pub fn write_mir_fn_graphviz<'tcx, W>(
51 tcx: TyCtxt<'tcx>,
52 body: &Body<'_>,
53 subgraph: bool,
54 w: &mut W,
55 ) -> io::Result<()>
56 where
57 W: Write,
58 {
59 // Global graph properties
60 let font = format!(r#"fontname="{}""#, tcx.sess.opts.debugging_opts.graphviz_font);
61 let mut graph_attrs = vec![&font[..]];
62 let mut content_attrs = vec![&font[..]];
63
64 let dark_mode = tcx.sess.opts.debugging_opts.graphviz_dark_mode;
65 if dark_mode {
66 graph_attrs.push(r#"bgcolor="black""#);
67 graph_attrs.push(r#"fontcolor="white""#);
68 content_attrs.push(r#"color="white""#);
69 content_attrs.push(r#"fontcolor="white""#);
70 }
71
72 // Graph label
73 let mut label = String::from("");
74 // FIXME: remove this unwrap
75 write_graph_label(tcx, body, &mut label).unwrap();
76 let g = mir_fn_to_generic_graph(tcx, body);
77 let settings = GraphvizSettings {
78 graph_attrs: Some(graph_attrs.join(" ")),
79 node_attrs: Some(content_attrs.join(" ")),
80 edge_attrs: Some(content_attrs.join(" ")),
81 graph_label: Some(label),
82 };
83 g.to_dot(w, &settings, subgraph)
84 }
85
86 /// Write the graphviz DOT label for the overall graph. This is essentially a block of text that
87 /// will appear below the graph, showing the type of the `fn` this MIR represents and the types of
88 /// all the variables and temporaries.
write_graph_label<'tcx, W: std::fmt::Write>( tcx: TyCtxt<'tcx>, body: &Body<'_>, w: &mut W, ) -> std::fmt::Result89 fn write_graph_label<'tcx, W: std::fmt::Write>(
90 tcx: TyCtxt<'tcx>,
91 body: &Body<'_>,
92 w: &mut W,
93 ) -> std::fmt::Result {
94 let def_id = body.source.def_id();
95
96 write!(w, "fn {}(", dot::escape_html(&tcx.def_path_str(def_id)))?;
97
98 // fn argument types.
99 for (i, arg) in body.args_iter().enumerate() {
100 if i > 0 {
101 write!(w, ", ")?;
102 }
103 write!(w, "{:?}: {}", Place::from(arg), escape(&body.local_decls[arg].ty))?;
104 }
105
106 write!(w, ") -> {}", escape(&body.return_ty()))?;
107 write!(w, r#"<br align="left"/>"#)?;
108
109 for local in body.vars_and_temps_iter() {
110 let decl = &body.local_decls[local];
111
112 write!(w, "let ")?;
113 if decl.mutability == Mutability::Mut {
114 write!(w, "mut ")?;
115 }
116
117 write!(w, r#"{:?}: {};<br align="left"/>"#, Place::from(local), escape(&decl.ty))?;
118 }
119
120 for var_debug_info in &body.var_debug_info {
121 write!(
122 w,
123 r#"debug {} => {};<br align="left"/>"#,
124 var_debug_info.name,
125 escape(&var_debug_info.value),
126 )?;
127 }
128
129 Ok(())
130 }
131
escape<T: Debug>(t: &T) -> String132 fn escape<T: Debug>(t: &T) -> String {
133 dot::escape_html(&format!("{:?}", t))
134 }
135