Skip to content

Commit c46c7c7

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 c46c7c7

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 41 additions & 21 deletions
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,58 @@ 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].
157+
pub fn dump_mir_to_writer<'tcx, F>(
158+
tcx: TyCtxt<'tcx>,
159+
pass_name: &str,
160+
disambiguator: &dyn Display,
161+
body: &Body<'tcx>,
162+
w: &mut dyn io::Write,
163+
mut extra_data: F,
164+
options: PrettyPrintMirOptions,
165+
) -> io::Result<()>
166+
where
167+
F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
168+
{
169+
// see notes on #41697 above
170+
let def_path =
171+
ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
172+
// ignore-tidy-odd-backticks the literal below is fine
173+
write!(w, "// MIR for `{def_path}")?;
174+
match body.source.promoted {
175+
None => write!(w, "`")?,
176+
Some(promoted) => write!(w, "::{promoted:?}`")?,
177+
}
178+
writeln!(w, " {disambiguator} {pass_name}")?;
179+
if let Some(ref layout) = body.coroutine_layout_raw() {
180+
writeln!(w, "/* coroutine_layout = {layout:#?} */")?;
181+
}
182+
writeln!(w)?;
183+
extra_data(PassWhere::BeforeCFG, w)?;
184+
write_user_type_annotations(tcx, body, w)?;
185+
write_mir_fn(tcx, body, &mut extra_data, w, options)?;
186+
extra_data(PassWhere::AfterCFG, w)
187+
}
188+
152189
fn dump_matched_mir_node<'tcx, F>(
153190
tcx: TyCtxt<'tcx>,
154191
pass_num: bool,
155192
pass_name: &str,
156193
disambiguator: &dyn Display,
157194
body: &Body<'tcx>,
158-
mut extra_data: F,
195+
extra_data: F,
159196
options: PrettyPrintMirOptions,
160197
) where
161198
F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
162199
{
163200
let _: io::Result<()> = try {
164201
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)?;
202+
dump_mir_to_writer(tcx, pass_name, disambiguator, body, &mut file, extra_data, options)?;
183203
};
184204

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

0 commit comments

Comments
 (0)