2
2
3
3
use std:: borrow:: BorrowMut ;
4
4
use std:: ffi:: OsString ;
5
- use std:: fs;
6
5
use std:: path:: PathBuf ;
7
6
8
7
use rustc_ast as ast;
@@ -12,7 +11,7 @@ use rustc_hir::def_id::DefId;
12
11
use rustc_index:: bit_set:: BitSet ;
13
12
use rustc_index:: vec:: { Idx , IndexVec } ;
14
13
use rustc_middle:: mir:: { self , traversal, BasicBlock } ;
15
- use rustc_middle:: ty:: { self , TyCtxt } ;
14
+ use rustc_middle:: ty:: TyCtxt ;
16
15
use rustc_span:: symbol:: { sym, Symbol } ;
17
16
18
17
use super :: fmt:: DebugWithContext ;
@@ -21,7 +20,7 @@ use super::{
21
20
visit_results, Analysis , Direction , GenKill , GenKillAnalysis , GenKillSet , JoinSemiLattice ,
22
21
ResultsCursor , ResultsVisitor ,
23
22
} ;
24
- use crate :: util:: pretty:: dump_enabled;
23
+ use crate :: util:: pretty:: { create_dump_file , dump_enabled} ;
25
24
26
25
/// A dataflow analysis that has converged to fixpoint.
27
26
pub struct Results < ' tcx , A >
@@ -249,7 +248,7 @@ where
249
248
250
249
let res = write_graphviz_results ( tcx, & body, & results, pass_name) ;
251
250
if let Err ( e) = res {
252
- warn ! ( "Failed to write graphviz dataflow results: {}" , e) ;
251
+ error ! ( "Failed to write graphviz dataflow results: {}" , e) ;
253
252
}
254
253
255
254
results
@@ -270,6 +269,9 @@ where
270
269
A : Analysis < ' tcx > ,
271
270
A :: Domain : DebugWithContext < A > ,
272
271
{
272
+ use std:: fs;
273
+ use std:: io:: { self , Write } ;
274
+
273
275
let def_id = body. source . def_id ( ) ;
274
276
let attrs = match RustcMirAttrs :: parse ( tcx, def_id) {
275
277
Ok ( attrs) => attrs,
@@ -278,35 +280,36 @@ where
278
280
Err ( ( ) ) => return Ok ( ( ) ) ,
279
281
} ;
280
282
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
+ }
283
291
284
292
None if tcx. sess . opts . debugging_opts . dump_mir_dataflow
285
293
&& dump_enabled ( tcx, A :: NAME , def_id) =>
286
294
{
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
+ ) ?
299
303
}
300
304
301
- None => return Ok ( ( ) ) ,
305
+ _ => return Ok ( ( ) ) ,
302
306
} ;
303
307
304
308
let style = match attrs. formatter {
305
309
Some ( sym:: two_phase) => graphviz:: OutputStyle :: BeforeAndAfter ,
306
310
_ => graphviz:: OutputStyle :: AfterOnly ,
307
311
} ;
308
312
309
- debug ! ( "printing dataflow results for {:?} to {}" , def_id, path. display( ) ) ;
310
313
let mut buf = Vec :: new ( ) ;
311
314
312
315
let graphviz = graphviz:: Formatter :: new ( body, results, style) ;
@@ -317,10 +320,7 @@ where
317
320
}
318
321
dot:: render_opts ( & graphviz, & mut buf, & render_opts) ?;
319
322
320
- if let Some ( parent) = path. parent ( ) {
321
- fs:: create_dir_all ( parent) ?;
322
- }
323
- fs:: write ( & path, buf) ?;
323
+ file. write_all ( & buf) ?;
324
324
325
325
Ok ( ( ) )
326
326
}
0 commit comments