|
1 | 1 | use std::collections::BTreeSet;
|
2 | 2 | use std::fmt::{Display, Write as _};
|
3 |
| -use std::fs; |
4 |
| -use std::io::{self, Write as _}; |
5 | 3 | use std::path::{Path, PathBuf};
|
| 4 | +use std::{fs, io}; |
6 | 5 |
|
7 | 6 | use rustc_abi::Size;
|
8 | 7 | use rustc_ast::InlineAsmTemplatePiece;
|
@@ -149,37 +148,59 @@ pub fn dump_enabled(tcx: TyCtxt<'_>, pass_name: &str, def_id: DefId) -> bool {
|
149 | 148 | // `def_path_str()` would otherwise trigger `type_of`, and this can
|
150 | 149 | // run while we are already attempting to evaluate `type_of`.
|
151 | 150 |
|
| 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 | + |
152 | 190 | fn dump_matched_mir_node<'tcx, F>(
|
153 | 191 | tcx: TyCtxt<'tcx>,
|
154 | 192 | pass_num: bool,
|
155 | 193 | pass_name: &str,
|
156 | 194 | disambiguator: &dyn Display,
|
157 | 195 | body: &Body<'tcx>,
|
158 |
| - mut extra_data: F, |
| 196 | + extra_data: F, |
159 | 197 | options: PrettyPrintMirOptions,
|
160 | 198 | ) where
|
161 | 199 | F: FnMut(PassWhere, &mut dyn io::Write) -> io::Result<()>,
|
162 | 200 | {
|
163 | 201 | let _: io::Result<()> = try {
|
164 | 202 | 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)?; |
183 | 204 | };
|
184 | 205 |
|
185 | 206 | if tcx.sess.opts.unstable_opts.dump_mir_graphviz {
|
|
0 commit comments