]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc_mir/util/graphviz.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / librustc_mir / util / graphviz.rs
index cf13a80e677b1266bac748cd8cfd43360ff00758..ea4495b484c39e32a2c956d631eefd85e425307a 100644 (file)
@@ -14,45 +14,52 @@ use rustc::mir::*;
 use rustc::ty::TyCtxt;
 use std::fmt::Debug;
 use std::io::{self, Write};
-use syntax::ast::NodeId;
 
 use rustc_data_structures::indexed_vec::Idx;
 
 use super::pretty::dump_mir_def_ids;
 
 /// Write a graphviz DOT graph of a list of MIRs.
-pub fn write_mir_graphviz<'a, 'tcx, W>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
-                                       single: Option<DefId>,
-                                       w: &mut W)
-                                       -> io::Result<()>
+pub fn write_mir_graphviz<'tcx, W>(tcx: TyCtxt<'_, '_, 'tcx>,
+                                   single: Option<DefId>,
+                                   w: &mut W)
+                                   -> io::Result<()>
     where W: Write
 {
     for def_id in dump_mir_def_ids(tcx, single) {
-        let nodeid = tcx.hir.as_local_node_id(def_id).unwrap();
         let mir = &tcx.optimized_mir(def_id);
+        write_mir_fn_graphviz(tcx, def_id, mir, w)?;
+    }
+    Ok(())
+}
 
-        writeln!(w, "digraph Mir_{} {{", nodeid)?;
+/// Write a graphviz DOT graph of the MIR.
+pub fn write_mir_fn_graphviz<'tcx, W>(tcx: TyCtxt<'_, '_, 'tcx>,
+                                      def_id: DefId,
+                                      mir: &Mir,
+                                      w: &mut W) -> io::Result<()>
+    where W: Write
+{
+    writeln!(w, "digraph Mir_{} {{", tcx.hir.as_local_node_id(def_id).unwrap())?;
 
-        // Global graph properties
-        writeln!(w, r#"    graph [fontname="monospace"];"#)?;
-        writeln!(w, r#"    node [fontname="monospace"];"#)?;
-        writeln!(w, r#"    edge [fontname="monospace"];"#)?;
+    // Global graph properties
+    writeln!(w, r#"    graph [fontname="monospace"];"#)?;
+    writeln!(w, r#"    node [fontname="monospace"];"#)?;
+    writeln!(w, r#"    edge [fontname="monospace"];"#)?;
 
-        // Graph label
-        write_graph_label(tcx, nodeid, mir, w)?;
+    // Graph label
+    write_graph_label(tcx, def_id, mir, w)?;
 
-        // Nodes
-        for (block, _) in mir.basic_blocks().iter_enumerated() {
-            write_node(block, mir, w)?;
-        }
+    // Nodes
+    for (block, _) in mir.basic_blocks().iter_enumerated() {
+        write_node(block, mir, w)?;
+    }
 
-        // Edges
-        for (source, _) in mir.basic_blocks().iter_enumerated() {
-            write_edges(source, mir, w)?;
-        }
-        writeln!(w, "}}")?
+    // Edges
+    for (source, _) in mir.basic_blocks().iter_enumerated() {
+        write_edges(source, mir, w)?;
     }
-    Ok(())
+    writeln!(w, "}}")
 }
 
 /// Write a graphviz HTML-styled label for the given basic block, with
@@ -128,12 +135,12 @@ fn write_edges<W: Write>(source: BasicBlock, mir: &Mir, w: &mut W) -> io::Result
 /// Write the graphviz DOT label for the overall graph. This is essentially a block of text that
 /// will appear below the graph, showing the type of the `fn` this MIR represents and the types of
 /// all the variables and temporaries.
-fn write_graph_label<'a, 'tcx, W: Write>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
-                                         nid: NodeId,
-                                         mir: &Mir,
-                                         w: &mut W)
-                                         -> io::Result<()> {
-    write!(w, "    label=<fn {}(", dot::escape_html(&tcx.node_path_str(nid)))?;
+fn write_graph_label<'a, 'gcx, 'tcx, W: Write>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
+                                               def_id: DefId,
+                                               mir: &Mir,
+                                               w: &mut W)
+                                               -> io::Result<()> {
+    write!(w, "    label=<fn {}(", dot::escape_html(&tcx.item_path_str(def_id)))?;
 
     // fn argument types.
     for (i, arg) in mir.args_iter().enumerate() {
@@ -143,7 +150,7 @@ fn write_graph_label<'a, 'tcx, W: Write>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
         write!(w, "{:?}: {}", Lvalue::Local(arg), escape(&mir.local_decls[arg].ty))?;
     }
 
-    write!(w, ") -&gt; {}", escape(mir.return_ty))?;
+    write!(w, ") -&gt; {}", escape(mir.return_ty()))?;
     write!(w, r#"<br align="left"/>"#)?;
 
     for local in mir.vars_and_temps_iter() {