Skip to content

Commit 67aa7b2

Browse files
Implement --unpretty mir
1 parent f3619ce commit 67aa7b2

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/librustc_driver/pretty.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ use rustc_borrowck::graphviz as borrowck_dot;
3030
use rustc_resolve as resolve;
3131
use rustc_metadata::cstore::CStore;
3232

33+
use rustc_mir::pretty::write_mir_pretty;
34+
3335
use syntax::ast::{self, BlockCheckMode};
3436
use syntax::codemap;
3537
use syntax::fold::{self, Folder};
@@ -77,6 +79,7 @@ pub enum PpMode {
7779
PpmSource(PpSourceMode),
7880
PpmHir(PpSourceMode),
7981
PpmFlowGraph(PpFlowGraphMode),
82+
PpmMir,
8083
}
8184

8285
pub fn parse_pretty(sess: &Session,
@@ -96,14 +99,15 @@ pub fn parse_pretty(sess: &Session,
9699
("hir", true) => PpmHir(PpmNormal),
97100
("hir,identified", true) => PpmHir(PpmIdentified),
98101
("hir,typed", true) => PpmHir(PpmTyped),
102+
("mir", true) => PpmMir,
99103
("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
100104
("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges),
101105
_ => {
102106
if extended {
103107
sess.fatal(&format!("argument to `unpretty` must be one of `normal`, \
104108
`expanded`, `flowgraph[,unlabelled]=<nodeid>`, \
105109
`identified`, `expanded,identified`, `everybody_loops`, \
106-
`hir`, `hir,identified`, or `hir,typed`; got {}",
110+
`hir`, `hir,identified`, `hir,typed`, or `mir`; got {}",
107111
name));
108112
} else {
109113
sess.fatal(&format!("argument to `pretty` must be one of `normal`, `expanded`, \
@@ -569,6 +573,7 @@ fn needs_ast_map(ppm: &PpMode, opt_uii: &Option<UserIdentifiedItem>) -> bool {
569573
PpmSource(PpmExpandedIdentified) |
570574
PpmSource(PpmExpandedHygiene) |
571575
PpmHir(_) |
576+
PpmMir |
572577
PpmFlowGraph(_) => true,
573578
PpmSource(PpmTyped) => panic!("invalid state"),
574579
}
@@ -584,6 +589,7 @@ fn needs_expansion(ppm: &PpMode) -> bool {
584589
PpmSource(PpmExpandedIdentified) |
585590
PpmSource(PpmExpandedHygiene) |
586591
PpmHir(_) |
592+
PpmMir |
587593
PpmFlowGraph(_) => true,
588594
PpmSource(PpmTyped) => panic!("invalid state"),
589595
}
@@ -801,6 +807,48 @@ pub fn pretty_print_input(sess: Session,
801807
})
802808
}
803809

810+
(PpmMir, None) => {
811+
debug!("pretty printing MIR for whole crate");
812+
let ast_map = ast_map.expect("--unpretty mir missing ast_map");
813+
abort_on_err(driver::phase_3_run_analysis_passes(&sess,
814+
&cstore,
815+
ast_map,
816+
&arenas,
817+
&id,
818+
resolve::MakeGlobMap::No,
819+
|tcx, mir_map, _, _| {
820+
let mir_map = mir_map.unwrap();
821+
822+
for (nodeid, mir) in &mir_map.map {
823+
try!(writeln!(out, "MIR for {}", tcx.map.node_to_string(*nodeid)));
824+
try!(write_mir_pretty(mir, &mut out));
825+
}
826+
827+
Ok(())
828+
}), &sess)
829+
}
830+
831+
(PpmMir, Some(uii)) => {
832+
debug!("pretty printing MIR for {:?}", uii);
833+
let ast_map = ast_map.expect("--unpretty mir missing ast_map");
834+
let nodeid = uii.to_one_node_id("--unpretty", &sess, &ast_map);
835+
836+
abort_on_err(driver::phase_3_run_analysis_passes(&sess,
837+
&cstore,
838+
ast_map,
839+
&arenas,
840+
&id,
841+
resolve::MakeGlobMap::No,
842+
|tcx, mir_map, _, _| {
843+
let mir_map = mir_map.unwrap();
844+
try!(writeln!(out, "MIR for {}", tcx.map.node_to_string(nodeid)));
845+
let mir = mir_map.map.get(&nodeid).unwrap_or_else(|| {
846+
sess.fatal(&format!("no MIR map entry for node {}", nodeid))
847+
});
848+
write_mir_pretty(mir, &mut out)
849+
}), &sess)
850+
}
851+
804852
(PpmFlowGraph(mode), opt_uii) => {
805853
debug!("pretty printing flow graph for {:?}", opt_uii);
806854
let uii = opt_uii.unwrap_or_else(|| {

0 commit comments

Comments
 (0)