22
33use rustc:: hir;
44use rustc:: hir:: map as hir_map;
5- use rustc:: hir:: map:: blocks;
65use rustc:: hir:: print as pprust_hir;
76use rustc:: hir:: def_id:: LOCAL_CRATE ;
87use rustc:: session:: Session ;
98use rustc:: session:: config:: Input ;
109use rustc:: ty:: { self , TyCtxt } ;
1110use rustc:: util:: common:: ErrorReported ;
1211use rustc_interface:: util:: ReplaceBodyWithLoop ;
13- use rustc_ast_borrowck as borrowck;
14- use rustc_ast_borrowck:: graphviz as borrowck_dot;
15- use rustc_ast_borrowck:: cfg:: { self , graphviz:: LabelledCFG } ;
1612use rustc_mir:: util:: { write_mir_pretty, write_mir_graphviz} ;
1713
1814use syntax:: ast;
1915use syntax:: mut_visit:: MutVisitor ;
2016use syntax:: print:: { pprust} ;
2117use syntax_pos:: FileName ;
2218
23- use graphviz as dot;
24-
2519use std:: cell:: Cell ;
2620use std:: fs:: File ;
27- use std:: io:: { self , Write } ;
21+ use std:: io:: Write ;
2822use std:: option;
2923use std:: path:: Path ;
3024use std:: str:: FromStr ;
@@ -48,21 +42,11 @@ pub enum PpSourceMode {
4842 PpmTyped ,
4943}
5044
51- #[ derive( Copy , Clone , PartialEq , Debug ) ]
52- pub enum PpFlowGraphMode {
53- Default ,
54- /// Drops the labels from the edges in the flowgraph output. This
55- /// is mostly for use in the -Z unpretty flowgraph run-make tests,
56- /// since the labels are largely uninteresting in those cases and
57- /// have become a pain to maintain.
58- UnlabelledEdges ,
59- }
6045#[ derive( Copy , Clone , PartialEq , Debug ) ]
6146pub enum PpMode {
6247 PpmSource ( PpSourceMode ) ,
6348 PpmHir ( PpSourceMode ) ,
6449 PpmHirTree ( PpSourceMode ) ,
65- PpmFlowGraph ( PpFlowGraphMode ) ,
6650 PpmMir ,
6751 PpmMirCFG ,
6852}
@@ -80,15 +64,14 @@ impl PpMode {
8064 PpmHir ( _) |
8165 PpmHirTree ( _) |
8266 PpmMir |
83- PpmMirCFG |
84- PpmFlowGraph ( _) => true ,
67+ PpmMirCFG => true ,
8568 PpmSource ( PpmTyped ) => panic ! ( "invalid state" ) ,
8669 }
8770 }
8871
8972 pub fn needs_analysis ( & self ) -> bool {
9073 match * self {
91- PpmMir | PpmMirCFG | PpmFlowGraph ( _ ) => true ,
74+ PpmMir | PpmMirCFG => true ,
9275 _ => false ,
9376 }
9477 }
@@ -114,15 +97,13 @@ pub fn parse_pretty(sess: &Session,
11497 ( "hir-tree" , true ) => PpmHirTree ( PpmNormal ) ,
11598 ( "mir" , true ) => PpmMir ,
11699 ( "mir-cfg" , true ) => PpmMirCFG ,
117- ( "flowgraph" , true ) => PpmFlowGraph ( PpFlowGraphMode :: Default ) ,
118- ( "flowgraph,unlabelled" , true ) => PpmFlowGraph ( PpFlowGraphMode :: UnlabelledEdges ) ,
119100 _ => {
120101 if extended {
121102 sess. fatal ( & format ! ( "argument to `unpretty` must be one of `normal`, \
122- `expanded`, `flowgraph[,unlabelled]=<nodeid> `, \
123- `identified `, `expanded,identified `, `everybody_loops `, \
124- `hir`, `hir,identified `, `hir,typed`, `hir-tree`, \
125- `mir` or `mir-cfg`; got {}",
103+ `expanded`, `identified`, `expanded,identified `, \
104+ `everybody_loops `, `hir `, `hir,identified `, \
105+ `hir,typed `, `hir-tree `, `mir` or `mir-cfg`; \
106+ got {}",
126107 name) ) ;
127108 } else {
128109 sess. fatal ( & format ! ( "argument to `pretty` must be one of `normal`, `expanded`, \
@@ -501,24 +482,6 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
501482 }
502483}
503484
504- fn gather_flowgraph_variants ( sess : & Session ) -> Vec < borrowck_dot:: Variant > {
505- let print_loans = sess. opts . debugging_opts . flowgraph_print_loans ;
506- let print_moves = sess. opts . debugging_opts . flowgraph_print_moves ;
507- let print_assigns = sess. opts . debugging_opts . flowgraph_print_assigns ;
508- let print_all = sess. opts . debugging_opts . flowgraph_print_all ;
509- let mut variants = Vec :: new ( ) ;
510- if print_all || print_loans {
511- variants. push ( borrowck_dot:: Loans ) ;
512- }
513- if print_all || print_moves {
514- variants. push ( borrowck_dot:: Moves ) ;
515- }
516- if print_all || print_assigns {
517- variants. push ( borrowck_dot:: Assigns ) ;
518- }
519- variants
520- }
521-
522485#[ derive( Clone , Debug ) ]
523486pub enum UserIdentifiedItem {
524487 ItemViaNode ( ast:: NodeId ) ,
@@ -609,81 +572,6 @@ impl UserIdentifiedItem {
609572 }
610573}
611574
612- fn print_flowgraph < ' tcx , W : Write > (
613- variants : Vec < borrowck_dot:: Variant > ,
614- tcx : TyCtxt < ' tcx > ,
615- code : blocks:: Code < ' tcx > ,
616- mode : PpFlowGraphMode ,
617- mut out : W ,
618- ) -> io:: Result < ( ) > {
619- let body_id = match code {
620- blocks:: Code :: Expr ( expr) => {
621- // Find the function this expression is from.
622- let mut hir_id = expr. hir_id ;
623- loop {
624- let node = tcx. hir ( ) . get ( hir_id) ;
625- if let Some ( n) = hir:: map:: blocks:: FnLikeNode :: from_node ( node) {
626- break n. body ( ) ;
627- }
628- let parent = tcx. hir ( ) . get_parent_node ( hir_id) ;
629- assert_ne ! ( hir_id, parent) ;
630- hir_id = parent;
631- }
632- }
633- blocks:: Code :: FnLike ( fn_like) => fn_like. body ( ) ,
634- } ;
635- let body = tcx. hir ( ) . body ( body_id) ;
636- let cfg = cfg:: CFG :: new ( tcx, & body) ;
637- let labelled_edges = mode != PpFlowGraphMode :: UnlabelledEdges ;
638- let hir_id = code. id ( ) ;
639- // We have to disassemble the hir_id because name must be ASCII
640- // alphanumeric. This does not appear in the rendered graph, so it does not
641- // have to be user friendly.
642- let name = format ! (
643- "hir_id_{}_{}" ,
644- hir_id. owner. index( ) ,
645- hir_id. local_id. index( ) ,
646- ) ;
647- let lcfg = LabelledCFG {
648- tcx,
649- cfg : & cfg,
650- name,
651- labelled_edges,
652- } ;
653-
654- match code {
655- _ if variants. is_empty ( ) => {
656- let r = dot:: render ( & lcfg, & mut out) ;
657- return expand_err_details ( r) ;
658- }
659- blocks:: Code :: Expr ( _) => {
660- tcx. sess . err ( "--pretty flowgraph with -Z flowgraph-print annotations requires \
661- fn-like node id.") ;
662- return Ok ( ( ) ) ;
663- }
664- blocks:: Code :: FnLike ( fn_like) => {
665- let ( bccx, analysis_data) =
666- borrowck:: build_borrowck_dataflow_data_for_fn ( tcx, fn_like. body ( ) , & cfg) ;
667-
668- let lcfg = borrowck_dot:: DataflowLabeller {
669- inner : lcfg,
670- variants,
671- borrowck_ctxt : & bccx,
672- analysis_data : & analysis_data,
673- } ;
674- let r = dot:: render ( & lcfg, & mut out) ;
675- return expand_err_details ( r) ;
676- }
677- }
678-
679- fn expand_err_details ( r : io:: Result < ( ) > ) -> io:: Result < ( ) > {
680- r. map_err ( |ioerr| {
681- io:: Error :: new ( io:: ErrorKind :: Other ,
682- format ! ( "graphviz::render failed: {}" , ioerr) )
683- } )
684- }
685- }
686-
687575pub fn visit_crate ( sess : & Session , krate : & mut ast:: Crate , ppm : PpMode ) {
688576 if let PpmSource ( PpmEveryBodyLoops ) = ppm {
689577 ReplaceBodyWithLoop :: new ( sess) . visit_crate ( krate) ;
@@ -872,55 +760,17 @@ fn print_with_analysis(
872760
873761 tcx. analysis ( LOCAL_CRATE ) ?;
874762
875- let mut print = || match ppm {
763+ match ppm {
876764 PpmMir | PpmMirCFG => {
877- if let Some ( nodeid) = nodeid {
878- let def_id = tcx. hir ( ) . local_def_id_from_node_id ( nodeid) ;
879- match ppm {
880- PpmMir => write_mir_pretty ( tcx, Some ( def_id) , & mut out) ,
881- PpmMirCFG => write_mir_graphviz ( tcx, Some ( def_id) , & mut out) ,
882- _ => unreachable ! ( ) ,
883- } ?;
884- } else {
885- match ppm {
886- PpmMir => write_mir_pretty ( tcx, None , & mut out) ,
887- PpmMirCFG => write_mir_graphviz ( tcx, None , & mut out) ,
888- _ => unreachable ! ( ) ,
889- } ?;
890- }
891- Ok ( ( ) )
892- }
893- PpmFlowGraph ( mode) => {
894- let nodeid =
895- nodeid. expect ( "`pretty flowgraph=..` needs NodeId (int) or unique path \
896- suffix (b::c::d)") ;
897- let hir_id = tcx. hir ( ) . node_to_hir_id ( nodeid) ;
898- let node = tcx. hir ( ) . find ( hir_id) . unwrap_or_else ( || {
899- tcx. sess . fatal ( & format ! ( "`--pretty=flowgraph` couldn't find ID: {}" , nodeid) )
900- } ) ;
901-
902- match blocks:: Code :: from_node ( & tcx. hir ( ) , hir_id) {
903- Some ( code) => {
904- let variants = gather_flowgraph_variants ( tcx. sess ) ;
905-
906- let out: & mut dyn Write = & mut out;
907-
908- print_flowgraph ( variants, tcx, code, mode, out)
909- }
910- None => {
911- let message = format ! ( "`--pretty=flowgraph` needs block, fn, or method; \
912- got {:?}",
913- node) ;
914-
915- let hir_id = tcx. hir ( ) . node_to_hir_id ( nodeid) ;
916- tcx. sess . span_fatal ( tcx. hir ( ) . span ( hir_id) , & message)
917- }
765+ let def_id = nodeid. map ( |nid| tcx. hir ( ) . local_def_id_from_node_id ( nid) ) ;
766+ match ppm {
767+ PpmMir => write_mir_pretty ( tcx, def_id, & mut out) ,
768+ PpmMirCFG => write_mir_graphviz ( tcx, def_id, & mut out) ,
769+ _ => unreachable ! ( ) ,
918770 }
919771 }
920772 _ => unreachable ! ( ) ,
921- } ;
922-
923- print ( ) . unwrap ( ) ;
773+ } . unwrap ( ) ;
924774
925775 write_output ( out, ofile) ;
926776
0 commit comments