]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/util/pretty.rs
Rollup merge of #52019 - michaelwoerister:cross-lto-auto-plugin, r=alexcrichton
[rust.git] / src / librustc_mir / util / pretty.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::hir;
12 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
13 use rustc::mir::*;
14 use rustc::mir::visit::Visitor;
15 use rustc::ty::{self, TyCtxt};
16 use rustc::ty::item_path;
17 use rustc_data_structures::fx::FxHashMap;
18 use rustc_data_structures::indexed_vec::Idx;
19 use std::fmt::Display;
20 use std::fs;
21 use std::io::{self, Write};
22 use std::path::{Path, PathBuf};
23 use super::graphviz::write_mir_fn_graphviz;
24 use transform::MirSource;
25
26 const INDENT: &'static str = "    ";
27 /// Alignment for lining up comments following MIR statements
28 pub(crate) const ALIGN: usize = 40;
29
30 /// An indication of where we are in the control flow graph. Used for printing
31 /// extra information in `dump_mir`
32 pub enum PassWhere {
33     /// We have not started dumping the control flow graph, but we are about to.
34     BeforeCFG,
35
36     /// We just finished dumping the control flow graph. This is right before EOF
37     AfterCFG,
38
39     /// We are about to start dumping the given basic block.
40     BeforeBlock(BasicBlock),
41
42     /// We are just about to dump the given statement or terminator.
43     BeforeLocation(Location),
44
45     /// We just dumped the given statement or terminator.
46     AfterLocation(Location),
47
48     /// We just dumped the terminator for a block but not the closing `}`.
49     AfterTerminator(BasicBlock),
50 }
51
52 /// If the session is properly configured, dumps a human-readable
53 /// representation of the mir into:
54 ///
55 /// ```text
56 /// rustc.node<node_id>.<pass_num>.<pass_name>.<disambiguator>
57 /// ```
58 ///
59 /// Output from this function is controlled by passing `-Z dump-mir=<filter>`,
60 /// where `<filter>` takes the following forms:
61 ///
62 /// - `all` -- dump MIR for all fns, all passes, all everything
63 /// - a filter defined by a set of substrings combined with `&` and `|`
64 ///   (`&` has higher precedence). At least one of the `|`-separated groups
65 ///   must match; an `|`-separated group matches if all of its `&`-separated
66 ///   substrings are matched.
67 ///
68 /// Example:
69 ///
70 /// - `nll` == match if `nll` appears in the name
71 /// - `foo & nll` == match if `foo` and `nll` both appear in the name
72 /// - `foo & nll | typeck` == match if `foo` and `nll` both appear in the name
73 ///   or `typeck` appears in the name.
74 /// - `foo & nll | bar & typeck` == match if `foo` and `nll` both appear in the name
75 ///   or `typeck` and `bar` both appear in the name.
76 pub fn dump_mir<'a, 'gcx, 'tcx, F>(
77     tcx: TyCtxt<'a, 'gcx, 'tcx>,
78     pass_num: Option<&dyn Display>,
79     pass_name: &str,
80     disambiguator: &dyn Display,
81     source: MirSource,
82     mir: &Mir<'tcx>,
83     extra_data: F,
84 ) where
85     F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
86 {
87     if !dump_enabled(tcx, pass_name, source) {
88         return;
89     }
90
91     let node_path = item_path::with_forced_impl_filename_line(|| {
92         // see notes on #41697 below
93         tcx.item_path_str(source.def_id)
94     });
95     dump_matched_mir_node(
96         tcx,
97         pass_num,
98         pass_name,
99         &node_path,
100         disambiguator,
101         source,
102         mir,
103         extra_data,
104     );
105 }
106
107 pub fn dump_enabled<'a, 'gcx, 'tcx>(
108     tcx: TyCtxt<'a, 'gcx, 'tcx>,
109     pass_name: &str,
110     source: MirSource,
111 ) -> bool {
112     let filters = match tcx.sess.opts.debugging_opts.dump_mir {
113         None => return false,
114         Some(ref filters) => filters,
115     };
116     let node_path = item_path::with_forced_impl_filename_line(|| {
117         // see notes on #41697 below
118         tcx.item_path_str(source.def_id)
119     });
120     filters.split("|").any(|or_filter| {
121         or_filter.split("&").all(|and_filter| {
122             and_filter == "all" || pass_name.contains(and_filter) || node_path.contains(and_filter)
123         })
124     })
125 }
126
127 // #41697 -- we use `with_forced_impl_filename_line()` because
128 // `item_path_str()` would otherwise trigger `type_of`, and this can
129 // run while we are already attempting to evaluate `type_of`.
130
131 fn dump_matched_mir_node<'a, 'gcx, 'tcx, F>(
132     tcx: TyCtxt<'a, 'gcx, 'tcx>,
133     pass_num: Option<&dyn Display>,
134     pass_name: &str,
135     node_path: &str,
136     disambiguator: &dyn Display,
137     source: MirSource,
138     mir: &Mir<'tcx>,
139     mut extra_data: F,
140 ) where
141     F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
142 {
143     let _: io::Result<()> = do catch {
144         let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, source)?;
145         writeln!(file, "// MIR for `{}`", node_path)?;
146         writeln!(file, "// source = {:?}", source)?;
147         writeln!(file, "// pass_name = {}", pass_name)?;
148         writeln!(file, "// disambiguator = {}", disambiguator)?;
149         if let Some(ref layout) = mir.generator_layout {
150             writeln!(file, "// generator_layout = {:?}", layout)?;
151         }
152         writeln!(file, "")?;
153         extra_data(PassWhere::BeforeCFG, &mut file)?;
154         write_mir_fn(tcx, source, mir, &mut extra_data, &mut file)?;
155         extra_data(PassWhere::AfterCFG, &mut file)?;
156     };
157
158     if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
159         let _: io::Result<()> = do catch {
160             let mut file =
161                 create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, source)?;
162             write_mir_fn_graphviz(tcx, source.def_id, mir, &mut file)?;
163         };
164     }
165 }
166
167 /// Returns the path to the filename where we should dump a given MIR.
168 /// Also used by other bits of code (e.g., NLL inference) that dump
169 /// graphviz data or other things.
170 fn dump_path(
171     tcx: TyCtxt<'_, '_, '_>,
172     extension: &str,
173     pass_num: Option<&dyn Display>,
174     pass_name: &str,
175     disambiguator: &dyn Display,
176     source: MirSource,
177 ) -> PathBuf {
178     let promotion_id = match source.promoted {
179         Some(id) => format!("-{:?}", id),
180         None => String::new(),
181     };
182
183     let pass_num = if tcx.sess.opts.debugging_opts.dump_mir_exclude_pass_number {
184         format!("")
185     } else {
186         match pass_num {
187             None => format!(".-------"),
188             Some(pass_num) => format!(".{}", pass_num),
189         }
190     };
191
192     let mut file_path = PathBuf::new();
193     file_path.push(Path::new(&tcx.sess.opts.debugging_opts.dump_mir_dir));
194
195     let item_name = tcx.hir
196         .def_path(source.def_id)
197         .to_filename_friendly_no_crate();
198
199     let file_name = format!(
200         "rustc.{}{}{}.{}.{}.{}",
201         item_name,
202         promotion_id,
203         pass_num,
204         pass_name,
205         disambiguator,
206         extension,
207     );
208
209     file_path.push(&file_name);
210
211     file_path
212 }
213
214 /// Attempts to open a file where we should dump a given MIR or other
215 /// bit of MIR-related data. Used by `mir-dump`, but also by other
216 /// bits of code (e.g., NLL inference) that dump graphviz data or
217 /// other things, and hence takes the extension as an argument.
218 pub(crate) fn create_dump_file(
219     tcx: TyCtxt<'_, '_, '_>,
220     extension: &str,
221     pass_num: Option<&dyn Display>,
222     pass_name: &str,
223     disambiguator: &dyn Display,
224     source: MirSource,
225 ) -> io::Result<fs::File> {
226     let file_path = dump_path(tcx, extension, pass_num, pass_name, disambiguator, source);
227     if let Some(parent) = file_path.parent() {
228         fs::create_dir_all(parent)?;
229     }
230     fs::File::create(&file_path)
231 }
232
233 /// Write out a human-readable textual representation for the given MIR.
234 pub fn write_mir_pretty<'a, 'gcx, 'tcx>(
235     tcx: TyCtxt<'a, 'gcx, 'tcx>,
236     single: Option<DefId>,
237     w: &mut dyn Write,
238 ) -> io::Result<()> {
239     writeln!(
240         w,
241         "// WARNING: This output format is intended for human consumers only"
242     )?;
243     writeln!(
244         w,
245         "// and is subject to change without notice. Knock yourself out."
246     )?;
247
248     let mut first = true;
249     for def_id in dump_mir_def_ids(tcx, single) {
250         let mir = &tcx.optimized_mir(def_id);
251
252         if first {
253             first = false;
254         } else {
255             // Put empty lines between all items
256             writeln!(w, "")?;
257         }
258
259         write_mir_fn(tcx, MirSource::item(def_id), mir, &mut |_, _| Ok(()), w)?;
260
261         for (i, mir) in mir.promoted.iter_enumerated() {
262             writeln!(w, "")?;
263             let src = MirSource {
264                 def_id,
265                 promoted: Some(i),
266             };
267             write_mir_fn(tcx, src, mir, &mut |_, _| Ok(()), w)?;
268         }
269     }
270     Ok(())
271 }
272
273 pub fn write_mir_fn<'a, 'gcx, 'tcx, F>(
274     tcx: TyCtxt<'a, 'gcx, 'tcx>,
275     src: MirSource,
276     mir: &Mir<'tcx>,
277     extra_data: &mut F,
278     w: &mut dyn Write,
279 ) -> io::Result<()>
280 where
281     F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
282 {
283     write_mir_intro(tcx, src, mir, w)?;
284     for block in mir.basic_blocks().indices() {
285         extra_data(PassWhere::BeforeBlock(block), w)?;
286         write_basic_block(tcx, block, mir, extra_data, w)?;
287         if block.index() + 1 != mir.basic_blocks().len() {
288             writeln!(w, "")?;
289         }
290     }
291
292     writeln!(w, "}}")?;
293     Ok(())
294 }
295
296 /// Write out a human-readable textual representation for the given basic block.
297 pub fn write_basic_block<'cx, 'gcx, 'tcx, F>(
298     tcx: TyCtxt<'cx, 'gcx, 'tcx>,
299     block: BasicBlock,
300     mir: &Mir<'tcx>,
301     extra_data: &mut F,
302     w: &mut dyn Write,
303 ) -> io::Result<()>
304 where
305     F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
306 {
307     let data = &mir[block];
308
309     // Basic block label at the top.
310     let cleanup_text = if data.is_cleanup { " // cleanup" } else { "" };
311     let lbl = format!("{}{:?}: {{", INDENT, block);
312     writeln!(w, "{0:1$}{2}", lbl, ALIGN, cleanup_text)?;
313
314     // List of statements in the middle.
315     let mut current_location = Location {
316         block: block,
317         statement_index: 0,
318     };
319     for statement in &data.statements {
320         extra_data(PassWhere::BeforeLocation(current_location), w)?;
321         let indented_mir = format!("{0}{0}{1:?};", INDENT, statement);
322         writeln!(
323             w,
324             "{:A$} // {:?}: {}",
325             indented_mir,
326             current_location,
327             comment(tcx, statement.source_info),
328             A = ALIGN,
329         )?;
330
331         write_extra(tcx, w, |visitor| {
332             visitor.visit_statement(current_location.block, statement, current_location);
333         })?;
334
335         extra_data(PassWhere::AfterLocation(current_location), w)?;
336
337         current_location.statement_index += 1;
338     }
339
340     // Terminator at the bottom.
341     extra_data(PassWhere::BeforeLocation(current_location), w)?;
342     let indented_terminator = format!("{0}{0}{1:?};", INDENT, data.terminator().kind);
343     writeln!(
344         w,
345         "{:A$} // {:?}: {}",
346         indented_terminator,
347         current_location,
348         comment(tcx, data.terminator().source_info),
349         A = ALIGN,
350     )?;
351
352     write_extra(tcx, w, |visitor| {
353         visitor.visit_terminator(current_location.block, data.terminator(), current_location);
354     })?;
355
356     extra_data(PassWhere::AfterLocation(current_location), w)?;
357     extra_data(PassWhere::AfterTerminator(block), w)?;
358
359     writeln!(w, "{}}}", INDENT)
360 }
361
362 /// After we print the main statement, we sometimes dump extra
363 /// information. There's often a lot of little things "nuzzled up" in
364 /// a statement.
365 fn write_extra<'cx, 'gcx, 'tcx, F>(
366     tcx: TyCtxt<'cx, 'gcx, 'tcx>,
367     write: &mut dyn Write,
368     mut visit_op: F,
369 ) -> io::Result<()>
370 where
371     F: FnMut(&mut ExtraComments<'cx, 'gcx, 'tcx>),
372 {
373     let mut extra_comments = ExtraComments {
374         _tcx: tcx,
375         comments: vec![],
376     };
377     visit_op(&mut extra_comments);
378     for comment in extra_comments.comments {
379         writeln!(write, "{:A$} // {}", "", comment, A = ALIGN)?;
380     }
381     Ok(())
382 }
383
384 struct ExtraComments<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
385     _tcx: TyCtxt<'cx, 'gcx, 'tcx>, // don't need it now, but bet we will soon
386     comments: Vec<String>,
387 }
388
389 impl<'cx, 'gcx, 'tcx> ExtraComments<'cx, 'gcx, 'tcx> {
390     fn push(&mut self, lines: &str) {
391         for line in lines.split("\n") {
392             self.comments.push(line.to_string());
393         }
394     }
395 }
396
397 impl<'cx, 'gcx, 'tcx> Visitor<'tcx> for ExtraComments<'cx, 'gcx, 'tcx> {
398     fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
399         self.super_constant(constant, location);
400         let Constant { span, ty, literal } = constant;
401         self.push(&format!("mir::Constant"));
402         self.push(&format!("+ span: {:?}", span));
403         self.push(&format!("+ ty: {:?}", ty));
404         self.push(&format!("+ literal: {:?}", literal));
405     }
406
407     fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, _: Location) {
408         self.super_const(constant);
409         let ty::Const { ty, val, .. } = constant;
410         self.push(&format!("ty::Const"));
411         self.push(&format!("+ ty: {:?}", ty));
412         self.push(&format!("+ val: {:?}", val));
413     }
414
415     fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
416         self.super_rvalue(rvalue, location);
417         match rvalue {
418             Rvalue::Aggregate(kind, _) => match **kind {
419                 AggregateKind::Closure(def_id, substs) => {
420                     self.push(&format!("closure"));
421                     self.push(&format!("+ def_id: {:?}", def_id));
422                     self.push(&format!("+ substs: {:#?}", substs));
423                 }
424
425                 AggregateKind::Generator(def_id, substs, movability) => {
426                     self.push(&format!("generator"));
427                     self.push(&format!("+ def_id: {:?}", def_id));
428                     self.push(&format!("+ substs: {:#?}", substs));
429                     self.push(&format!("+ movability: {:?}", movability));
430                 }
431
432                 _ => {}
433             },
434
435             _ => {}
436         }
437     }
438 }
439
440 fn comment(tcx: TyCtxt, SourceInfo { span, scope }: SourceInfo) -> String {
441     format!(
442         "scope {} at {}",
443         scope.index(),
444         tcx.sess.codemap().span_to_string(span)
445     )
446 }
447
448 /// Prints user-defined variables in a scope tree.
449 ///
450 /// Returns the total number of variables printed.
451 fn write_scope_tree(
452     tcx: TyCtxt,
453     mir: &Mir,
454     scope_tree: &FxHashMap<SourceScope, Vec<SourceScope>>,
455     w: &mut dyn Write,
456     parent: SourceScope,
457     depth: usize,
458 ) -> io::Result<()> {
459     let indent = depth * INDENT.len();
460
461     let children = match scope_tree.get(&parent) {
462         Some(childs) => childs,
463         None => return Ok(()),
464     };
465
466     for &child in children {
467         let data = &mir.source_scopes[child];
468         assert_eq!(data.parent_scope, Some(parent));
469         writeln!(w, "{0:1$}scope {2} {{", "", indent, child.index())?;
470
471         // User variable types (including the user's name in a comment).
472         for local in mir.vars_iter() {
473             let var = &mir.local_decls[local];
474             let (name, source_info) = if var.source_info.scope == child {
475                 (var.name.unwrap(), var.source_info)
476             } else {
477                 // Not a variable or not declared in this scope.
478                 continue;
479             };
480
481             let mut_str = if var.mutability == Mutability::Mut {
482                 "mut "
483             } else {
484                 ""
485             };
486
487             let indent = indent + INDENT.len();
488             let indented_var = format!(
489                 "{0:1$}let {2}{3:?}: {4:?};",
490                 INDENT,
491                 indent,
492                 mut_str,
493                 local,
494                 var.ty
495             );
496             writeln!(
497                 w,
498                 "{0:1$} // \"{2}\" in {3}",
499                 indented_var,
500                 ALIGN,
501                 name,
502                 comment(tcx, source_info)
503             )?;
504         }
505
506         write_scope_tree(tcx, mir, scope_tree, w, child, depth + 1)?;
507
508         writeln!(w, "{0:1$}}}", "", depth * INDENT.len())?;
509     }
510
511     Ok(())
512 }
513
514 /// Write out a human-readable textual representation of the MIR's `fn` type and the types of its
515 /// local variables (both user-defined bindings and compiler temporaries).
516 pub fn write_mir_intro<'a, 'gcx, 'tcx>(
517     tcx: TyCtxt<'a, 'gcx, 'tcx>,
518     src: MirSource,
519     mir: &Mir,
520     w: &mut dyn Write,
521 ) -> io::Result<()> {
522     write_mir_sig(tcx, src, mir, w)?;
523     writeln!(w, "{{")?;
524
525     // construct a scope tree and write it out
526     let mut scope_tree: FxHashMap<SourceScope, Vec<SourceScope>> = FxHashMap();
527     for (index, scope_data) in mir.source_scopes.iter().enumerate() {
528         if let Some(parent) = scope_data.parent_scope {
529             scope_tree
530                 .entry(parent)
531                 .or_insert(vec![])
532                 .push(SourceScope::new(index));
533         } else {
534             // Only the argument scope has no parent, because it's the root.
535             assert_eq!(index, OUTERMOST_SOURCE_SCOPE.index());
536         }
537     }
538
539     // Print return place
540     let indented_retptr = format!("{}let mut {:?}: {};",
541                                   INDENT,
542                                   RETURN_PLACE,
543                                   mir.local_decls[RETURN_PLACE].ty);
544     writeln!(w, "{0:1$} // return place",
545              indented_retptr,
546              ALIGN)?;
547
548     write_scope_tree(tcx, mir, &scope_tree, w, OUTERMOST_SOURCE_SCOPE, 1)?;
549
550     write_temp_decls(mir, w)?;
551
552     // Add an empty line before the first block is printed.
553     writeln!(w, "")?;
554
555     Ok(())
556 }
557
558 fn write_mir_sig(tcx: TyCtxt, src: MirSource, mir: &Mir, w: &mut dyn Write) -> io::Result<()> {
559     let id = tcx.hir.as_local_node_id(src.def_id).unwrap();
560     let body_owner_kind = tcx.hir.body_owner_kind(id);
561     match (body_owner_kind, src.promoted) {
562         (_, Some(i)) => write!(w, "{:?} in", i)?,
563         (hir::BodyOwnerKind::Fn, _) => write!(w, "fn")?,
564         (hir::BodyOwnerKind::Const, _) => write!(w, "const")?,
565         (hir::BodyOwnerKind::Static(hir::MutImmutable), _) => write!(w, "static")?,
566         (hir::BodyOwnerKind::Static(hir::MutMutable), _) => write!(w, "static mut")?,
567     }
568
569     item_path::with_forced_impl_filename_line(|| {
570         // see notes on #41697 elsewhere
571         write!(w, " {}", tcx.item_path_str(src.def_id))
572     })?;
573
574     match (body_owner_kind, src.promoted) {
575         (hir::BodyOwnerKind::Fn, None) => {
576             write!(w, "(")?;
577
578             // fn argument types.
579             for (i, arg) in mir.args_iter().enumerate() {
580                 if i != 0 {
581                     write!(w, ", ")?;
582                 }
583                 write!(w, "{:?}: {}", Place::Local(arg), mir.local_decls[arg].ty)?;
584             }
585
586             write!(w, ") -> {}", mir.return_ty())?;
587         }
588         (hir::BodyOwnerKind::Const, _) | (hir::BodyOwnerKind::Static(_), _) | (_, Some(_)) => {
589             assert_eq!(mir.arg_count, 0);
590             write!(w, ": {} =", mir.return_ty())?;
591         }
592     }
593
594     if let Some(yield_ty) = mir.yield_ty {
595         writeln!(w)?;
596         writeln!(w, "yields {}", yield_ty)?;
597     }
598
599     Ok(())
600 }
601
602 fn write_temp_decls(mir: &Mir, w: &mut dyn Write) -> io::Result<()> {
603     // Compiler-introduced temporary types.
604     for temp in mir.temps_iter() {
605         writeln!(
606             w,
607             "{}let mut {:?}: {};",
608             INDENT,
609             temp,
610             mir.local_decls[temp].ty
611         )?;
612     }
613
614     Ok(())
615 }
616
617 pub fn dump_mir_def_ids(tcx: TyCtxt, single: Option<DefId>) -> Vec<DefId> {
618     if let Some(i) = single {
619         vec![i]
620     } else {
621         tcx.mir_keys(LOCAL_CRATE).iter().cloned().collect()
622     }
623 }