22
33use std:: borrow:: BorrowMut ;
44use std:: ffi:: OsString ;
5- use std:: fs;
65use std:: path:: PathBuf ;
76
87use rustc_ast as ast;
@@ -12,7 +11,7 @@ use rustc_hir::def_id::DefId;
1211use rustc_index:: bit_set:: BitSet ;
1312use rustc_index:: vec:: { Idx , IndexVec } ;
1413use rustc_middle:: mir:: { self , traversal, BasicBlock } ;
15- use rustc_middle:: ty:: { self , TyCtxt } ;
14+ use rustc_middle:: ty:: TyCtxt ;
1615use rustc_span:: symbol:: { sym, Symbol } ;
1716
1817use super :: fmt:: DebugWithContext ;
@@ -21,7 +20,7 @@ use super::{
2120 visit_results, Analysis , Direction , GenKill , GenKillAnalysis , GenKillSet , JoinSemiLattice ,
2221 ResultsCursor , ResultsVisitor ,
2322} ;
24- use crate :: util:: pretty:: dump_enabled;
23+ use crate :: util:: pretty:: { create_dump_file , dump_enabled} ;
2524
2625/// A dataflow analysis that has converged to fixpoint.
2726pub struct Results < ' tcx , A >
@@ -270,6 +269,9 @@ where
270269 A : Analysis < ' tcx > ,
271270 A :: Domain : DebugWithContext < A > ,
272271{
272+ use std:: fs;
273+ use std:: io:: { self , Write } ;
274+
273275 let def_id = body. source . def_id ( ) ;
274276 let attrs = match RustcMirAttrs :: parse ( tcx, def_id) {
275277 Ok ( attrs) => attrs,
@@ -278,35 +280,36 @@ where
278280 Err ( ( ) ) => return Ok ( ( ) ) ,
279281 } ;
280282
281- let path = match attrs. output_path ( A :: NAME ) {
282- Some ( path) => path,
283+ let mut file = match attrs. output_path ( A :: NAME ) {
284+ Some ( path) => {
285+ debug ! ( "printing dataflow results for {:?} to {}" , def_id, path. display( ) ) ;
286+ if let Some ( parent) = path. parent ( ) {
287+ fs:: create_dir_all ( parent) ?;
288+ }
289+ io:: BufWriter :: new ( fs:: File :: create ( & path) ?)
290+ }
283291
284292 None if tcx. sess . opts . debugging_opts . dump_mir_dataflow
285293 && dump_enabled ( tcx, A :: NAME , def_id) =>
286294 {
287- // FIXME: Use some variant of `pretty::dump_path` for this
288- let mut path = PathBuf :: from ( & tcx. sess . opts . debugging_opts . dump_mir_dir ) ;
289-
290- let crate_name = tcx. crate_name ( def_id. krate ) ;
291- let item_name = ty:: print:: with_forced_impl_filename_line ( || {
292- tcx. def_path ( def_id) . to_filename_friendly_no_crate ( )
293- } ) ;
294-
295- let pass_name = pass_name. map ( |s| format ! ( ".{}" , s) ) . unwrap_or_default ( ) ;
296-
297- path. push ( format ! ( "{}.{}.{}{}.dot" , crate_name, item_name, A :: NAME , pass_name) ) ;
298- path
295+ create_dump_file (
296+ tcx,
297+ ".dot" ,
298+ None ,
299+ A :: NAME ,
300+ & pass_name. unwrap_or ( "-----" ) ,
301+ body. source ,
302+ ) ?
299303 }
300304
301- None => return Ok ( ( ) ) ,
305+ _ => return Ok ( ( ) ) ,
302306 } ;
303307
304308 let style = match attrs. formatter {
305309 Some ( sym:: two_phase) => graphviz:: OutputStyle :: BeforeAndAfter ,
306310 _ => graphviz:: OutputStyle :: AfterOnly ,
307311 } ;
308312
309- debug ! ( "printing dataflow results for {:?} to {}" , def_id, path. display( ) ) ;
310313 let mut buf = Vec :: new ( ) ;
311314
312315 let graphviz = graphviz:: Formatter :: new ( body, results, style) ;
@@ -317,10 +320,7 @@ where
317320 }
318321 dot:: render_opts ( & graphviz, & mut buf, & render_opts) ?;
319322
320- if let Some ( parent) = path. parent ( ) {
321- fs:: create_dir_all ( parent) ?;
322- }
323- fs:: write ( & path, buf) ?;
323+ file. write_all ( & buf) ?;
324324
325325 Ok ( ( ) )
326326}
0 commit comments