Skip to content

Commit 492d985

Browse files
committed
extract principal MIR dump function
for cases where we want to dump the MIR to a given writer instead of a new file as the default does. this will be used when dumping the MIR to a buffer to process differently, e.g. post-process to escape for an HTML dump.
1 parent 061ee95 commit 492d985

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

compiler/rustc_middle/src/mir/pretty.rs

+42-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::collections::BTreeSet;
22
use std::fmt::{Display, Write as _};
3-
use std::fs;
4-
use std::io::{self, Write as _};
53
use std::path::{Path, PathBuf};
4+
use std::{fs, io};
65

76
use rustc_abi::Size;
87
use rustc_ast::InlineAsmTemplatePiece;
@@ -149,37 +148,59 @@ pub fn dump_enabled(tcx: TyCtxt<'_>, pass_name: &str, def_id: DefId) -> bool {
149148
// `def_path_str()` would otherwise trigger `type_of`, and this can
150149
// run while we are already attempting to evaluate `type_of`.
151150

151+
/// Most use-cases of dumping MIR should use the [dump_mir] entrypoint instead, which will also
152+
/// check if dumping MIR is enabled, and if this body matches the filters passed on the CLI.
153+
///
154+
/// That being said, if the above requirements have been validated already, this function is where
155+
/// most of the MIR dumping occurs, if one needs to export it to a file they have created with
156+
/// [create_dump_file], rather than to a new file created as part of [dump_mir], or to stdout/stderr
157+
/// for debugging purposes.
158+
pub fn dump_mir_to_writer<'tcx, F>(
159+
tcx: TyCtxt<'tcx>,
160+
pass_name: &str,
161+
disambiguator: &dyn Display,
162+
body: &Body<'tcx>,
163+
w: &mut dyn io::Write,
164+
mut extra_data: F,
165+
options: PrettyPrintMirOptions,
166+
) -> io::Result<()>
167+
where
168+
F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
169+
{
170+
// see notes on #41697 above
171+
let def_path =
172+
ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
173+
// ignore-tidy-odd-backticks the literal below is fine
174+
write!(w, "// MIR for `{def_path}")?;
175+
match body.source.promoted {
176+
None => write!(w, "`")?,
177+
Some(promoted) => write!(w, "::{promoted:?}`")?,
178+
}
179+
writeln!(w, " {disambiguator} {pass_name}")?;
180+
if let Some(ref layout) = body.coroutine_layout_raw() {
181+
writeln!(w, "/* coroutine_layout = {layout:#?} */")?;
182+
}
183+
writeln!(w)?;
184+
extra_data(PassWhere::BeforeCFG, w)?;
185+
write_user_type_annotations(tcx, body, w)?;
186+
write_mir_fn(tcx, body, &mut extra_data, w, options)?;
187+
extra_data(PassWhere::AfterCFG, w)
188+
}
189+
152190
fn dump_matched_mir_node<'tcx, F>(
153191
tcx: TyCtxt<'tcx>,
154192
pass_num: bool,
155193
pass_name: &str,
156194
disambiguator: &dyn Display,
157195
body: &Body<'tcx>,
158-
mut extra_data: F,
196+
extra_data: F,
159197
options: PrettyPrintMirOptions,
160198
) where
161199
F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
162200
{
163201
let _: io::Result<()> = try {
164202
let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, body)?;
165-
// see notes on #41697 above
166-
let def_path =
167-
ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
168-
// ignore-tidy-odd-backticks the literal below is fine
169-
write!(file, "// MIR for `{def_path}")?;
170-
match body.source.promoted {
171-
None => write!(file, "`")?,
172-
Some(promoted) => write!(file, "::{promoted:?}`")?,
173-
}
174-
writeln!(file, " {disambiguator} {pass_name}")?;
175-
if let Some(ref layout) = body.coroutine_layout_raw() {
176-
writeln!(file, "/* coroutine_layout = {layout:#?} */")?;
177-
}
178-
writeln!(file)?;
179-
extra_data(PassWhere::BeforeCFG, &mut file)?;
180-
write_user_type_annotations(tcx, body, &mut file)?;
181-
write_mir_fn(tcx, body, &mut extra_data, &mut file, options)?;
182-
extra_data(PassWhere::AfterCFG, &mut file)?;
203+
dump_mir_to_writer(tcx, pass_name, disambiguator, body, &mut file, extra_data, options)?;
183204
};
184205

185206
if tcx.sess.opts.unstable_opts.dump_mir_graphviz {

0 commit comments

Comments
 (0)