Skip to content

Commit bdc176e

Browse files
committed
Implement --unpretty mir-cfg for graphviz output
Also change output for --unpretty mir to output function names in a prettier way.
1 parent e30ff06 commit bdc176e

File tree

3 files changed

+68
-59
lines changed

3 files changed

+68
-59
lines changed

src/librustc_driver/pretty.rs

+28-29
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc_resolve as resolve;
3131
use rustc_metadata::cstore::CStore;
3232

3333
use rustc_mir::pretty::write_mir_pretty;
34+
use rustc_mir::graphviz::write_mir_graphviz;
3435

3536
use syntax::ast::{self, BlockCheckMode};
3637
use syntax::codemap;
@@ -44,6 +45,7 @@ use graphviz as dot;
4445

4546
use std::fs::File;
4647
use std::io::{self, Write};
48+
use std::iter;
4749
use std::option;
4850
use std::path::PathBuf;
4951
use std::str::FromStr;
@@ -80,6 +82,7 @@ pub enum PpMode {
8082
PpmHir(PpSourceMode),
8183
PpmFlowGraph(PpFlowGraphMode),
8284
PpmMir,
85+
PpmMirCFG,
8386
}
8487

8588
pub fn parse_pretty(sess: &Session,
@@ -100,6 +103,7 @@ pub fn parse_pretty(sess: &Session,
100103
("hir,identified", true) => PpmHir(PpmIdentified),
101104
("hir,typed", true) => PpmHir(PpmTyped),
102105
("mir", true) => PpmMir,
106+
("mir-cfg", true) => PpmMirCFG,
103107
("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
104108
("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges),
105109
_ => {
@@ -574,6 +578,7 @@ fn needs_ast_map(ppm: &PpMode, opt_uii: &Option<UserIdentifiedItem>) -> bool {
574578
PpmSource(PpmExpandedHygiene) |
575579
PpmHir(_) |
576580
PpmMir |
581+
PpmMirCFG |
577582
PpmFlowGraph(_) => true,
578583
PpmSource(PpmTyped) => panic!("invalid state"),
579584
}
@@ -590,6 +595,7 @@ fn needs_expansion(ppm: &PpMode) -> bool {
590595
PpmSource(PpmExpandedHygiene) |
591596
PpmHir(_) |
592597
PpmMir |
598+
PpmMirCFG |
593599
PpmFlowGraph(_) => true,
594600
PpmSource(PpmTyped) => panic!("invalid state"),
595601
}
@@ -807,9 +813,15 @@ pub fn pretty_print_input(sess: Session,
807813
})
808814
}
809815

810-
(PpmMir, None) => {
811-
debug!("pretty printing MIR for whole crate");
812-
let ast_map = ast_map.expect("--unpretty mir missing ast_map");
816+
(pp_type@PpmMir, uii) | (pp_type@PpmMirCFG, uii) => {
817+
let ast_map = ast_map.expect("--unpretty missing ast_map");
818+
let nodeid = if let Some(uii) = uii {
819+
debug!("pretty printing MIR for {:?}", uii);
820+
Some(uii.to_one_node_id("--unpretty", &sess, &ast_map))
821+
} else {
822+
debug!("pretty printing MIR for whole crate");
823+
None
824+
};
813825
abort_on_err(driver::phase_3_run_analysis_passes(&sess,
814826
&cstore,
815827
ast_map,
@@ -818,38 +830,25 @@ pub fn pretty_print_input(sess: Session,
818830
resolve::MakeGlobMap::No,
819831
|tcx, mir_map, _, _| {
820832
if let Some(mir_map) = mir_map {
821-
for (nodeid, mir) in &mir_map.map {
822-
try!(writeln!(out, "MIR for {}", tcx.map.node_to_string(*nodeid)));
823-
try!(write_mir_pretty(mir, &mut out));
833+
if let Some(nodeid) = nodeid {
834+
let mir = mir_map.map.get(&nodeid).unwrap_or_else(|| {
835+
sess.fatal(&format!("no MIR map entry for node {}", nodeid))
836+
});
837+
try!(match pp_type {
838+
PpmMir => write_mir_pretty(tcx, iter::once((&nodeid, mir)), &mut out),
839+
_ => write_mir_graphviz(tcx, iter::once((&nodeid, mir)), &mut out)
840+
});
841+
} else {
842+
try!(match pp_type {
843+
PpmMir => write_mir_pretty(tcx, mir_map.map.iter(), &mut out),
844+
_ => write_mir_graphviz(tcx, mir_map.map.iter(), &mut out)
845+
});
824846
}
825847
}
826848
Ok(())
827849
}), &sess)
828850
}
829851

830-
(PpmMir, Some(uii)) => {
831-
debug!("pretty printing MIR for {:?}", uii);
832-
let ast_map = ast_map.expect("--unpretty mir missing ast_map");
833-
let nodeid = uii.to_one_node_id("--unpretty", &sess, &ast_map);
834-
835-
abort_on_err(driver::phase_3_run_analysis_passes(&sess,
836-
&cstore,
837-
ast_map,
838-
&arenas,
839-
&id,
840-
resolve::MakeGlobMap::No,
841-
|tcx, mir_map, _, _| {
842-
if let Some(mir_map) = mir_map {
843-
try!(writeln!(out, "MIR for {}", tcx.map.node_to_string(nodeid)));
844-
let mir = mir_map.map.get(&nodeid).unwrap_or_else(|| {
845-
sess.fatal(&format!("no MIR map entry for node {}", nodeid))
846-
});
847-
try!(write_mir_pretty(mir, &mut out));
848-
}
849-
Ok(())
850-
}), &sess)
851-
}
852-
853852
(PpmFlowGraph(mode), opt_uii) => {
854853
debug!("pretty printing flow graph for {:?}", opt_uii);
855854
let uii = opt_uii.unwrap_or_else(|| {

src/librustc_mir/graphviz.rs

+25-20
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,34 @@ use rustc::mir::repr::*;
1313
use rustc::middle::ty;
1414
use std::fmt::Debug;
1515
use std::io::{self, Write};
16+
use syntax::ast::NodeId;
1617

17-
/// Write a graphviz DOT graph for the given MIR.
18-
pub fn write_mir_graphviz<W: Write>(mir: &Mir, w: &mut W) -> io::Result<()> {
19-
try!(writeln!(w, "digraph Mir {{"));
18+
/// Write a graphviz DOT graph of a list of MIRs.
19+
pub fn write_mir_graphviz<'a, 't, W, I>(tcx: &ty::TyCtxt<'t>, iter: I, w: &mut W) -> io::Result<()>
20+
where W: Write, I: Iterator<Item=(&'a NodeId, &'a Mir<'a>)> {
21+
for (&nodeid, mir) in iter {
22+
try!(writeln!(w, "digraph Mir_{} {{", nodeid));
2023

21-
// Global graph properties
22-
try!(writeln!(w, r#" graph [fontname="monospace"];"#));
23-
try!(writeln!(w, r#" node [fontname="monospace"];"#));
24-
try!(writeln!(w, r#" edge [fontname="monospace"];"#));
24+
// Global graph properties
25+
try!(writeln!(w, r#" graph [fontname="monospace"];"#));
26+
try!(writeln!(w, r#" node [fontname="monospace"];"#));
27+
try!(writeln!(w, r#" edge [fontname="monospace"];"#));
2528

26-
// Graph label
27-
try!(write_graph_label(mir, w));
29+
// Graph label
30+
try!(write_graph_label(tcx, nodeid, mir, w));
2831

29-
// Nodes
30-
for block in mir.all_basic_blocks() {
31-
try!(write_node(block, mir, w));
32-
}
32+
// Nodes
33+
for block in mir.all_basic_blocks() {
34+
try!(write_node(block, mir, w));
35+
}
3336

34-
// Edges
35-
for source in mir.all_basic_blocks() {
36-
try!(write_edges(source, mir, w));
37+
// Edges
38+
for source in mir.all_basic_blocks() {
39+
try!(write_edges(source, mir, w));
40+
}
41+
try!(writeln!(w, "}}"))
3742
}
38-
39-
writeln!(w, "}}")
43+
Ok(())
4044
}
4145

4246
/// Write a graphviz DOT node for the given basic block.
@@ -84,8 +88,9 @@ fn write_edges<W: Write>(source: BasicBlock, mir: &Mir, w: &mut W) -> io::Result
8488
/// Write the graphviz DOT label for the overall graph. This is essentially a block of text that
8589
/// will appear below the graph, showing the type of the `fn` this MIR represents and the types of
8690
/// all the variables and temporaries.
87-
fn write_graph_label<W: Write>(mir: &Mir, w: &mut W) -> io::Result<()> {
88-
try!(write!(w, " label=<fn("));
91+
fn write_graph_label<W: Write>(tcx: &ty::TyCtxt, nid: NodeId, mir: &Mir, w: &mut W)
92+
-> io::Result<()> {
93+
try!(write!(w, " label=<fn {}(", dot::escape_html(&tcx.map.path_to_string(nid))));
8994

9095
// fn argument types.
9196
for (i, arg) in mir.arg_decls.iter().enumerate() {

src/librustc_mir/pretty.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
1111
use rustc::mir::repr::*;
1212
use rustc::middle::ty;
1313
use std::io::{self, Write};
14+
use syntax::ast::NodeId;
1415

1516
const INDENT: &'static str = " ";
1617

1718
/// Write out a human-readable textual representation for the given MIR.
18-
pub fn write_mir_pretty<W: Write>(mir: &Mir, w: &mut W) -> io::Result<()> {
19-
try!(write_mir_intro(mir, w));
20-
21-
// Nodes
22-
for block in mir.all_basic_blocks() {
23-
try!(write_basic_block(block, mir, w));
19+
pub fn write_mir_pretty<'a, 't, W, I>(tcx: &ty::TyCtxt<'t>, iter: I, w: &mut W) -> io::Result<()>
20+
where W: Write, I: Iterator<Item=(&'a NodeId, &'a Mir<'a>)> {
21+
for (&nodeid, mir) in iter {
22+
try!(write_mir_intro(tcx, nodeid, mir, w));
23+
// Nodes
24+
for block in mir.all_basic_blocks() {
25+
try!(write_basic_block(block, mir, w));
26+
}
27+
try!(writeln!(w, "}}"))
2428
}
25-
26-
writeln!(w, "}}")
29+
Ok(())
2730
}
2831

2932
/// Write out a human-readable textual representation for the given basic block.
@@ -46,8 +49,10 @@ fn write_basic_block<W: Write>(block: BasicBlock, mir: &Mir, w: &mut W) -> io::R
4649

4750
/// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
4851
/// local variables (both user-defined bindings and compiler temporaries).
49-
fn write_mir_intro<W: Write>(mir: &Mir, w: &mut W) -> io::Result<()> {
50-
try!(write!(w, "fn("));
52+
fn write_mir_intro<W: Write>(tcx: &ty::TyCtxt, nid: NodeId, mir: &Mir, w: &mut W)
53+
-> io::Result<()> {
54+
55+
try!(write!(w, "fn {}(", tcx.map.path_to_string(nid)));
5156

5257
// fn argument types.
5358
for (i, arg) in mir.arg_decls.iter().enumerate() {

0 commit comments

Comments
 (0)