compiletest/runtest/
mir_opt.rs1use std::fs;
2
3use camino::{Utf8Path, Utf8PathBuf};
4use glob::glob;
5use miropt_test_tools::{MiroptTest, MiroptTestFile, files_for_miropt_test};
6use tracing::debug;
7
8use super::{Emit, TestCx, WillExecute};
9use crate::compute_diff::write_diff;
10
11impl TestCx<'_> {
12 pub(super) fn run_mir_opt_test(&self) {
13 let pm = self.pass_mode();
14 let should_run = self.should_run(pm);
15
16 let mut test_info = files_for_miropt_test(
17 &self.testpaths.file.as_std_path(),
18 self.config.get_pointer_width(),
19 self.config.target_cfg().panic.for_miropt_test_tools(),
20 );
21
22 let passes = std::mem::take(&mut test_info.passes);
23
24 let proc_res = self.compile_test_with_passes(should_run, Emit::Mir, passes);
25 if !proc_res.status.success() {
26 self.fatal_proc_rec("compilation failed!", &proc_res);
27 }
28 self.check_mir_dump(test_info);
29
30 if let WillExecute::Yes = should_run {
31 let proc_res = self.exec_compiled_test();
32
33 if !proc_res.status.success() {
34 self.fatal_proc_rec("test run failed!", &proc_res);
35 }
36 }
37 }
38
39 fn check_mir_dump(&self, test_info: MiroptTest) {
40 let test_dir = self.testpaths.file.parent().unwrap();
41 let test_crate = self.testpaths.file.file_stem().unwrap().replace('-', "_");
42
43 let MiroptTest { run_filecheck, suffix, files, passes: _ } = test_info;
44
45 if self.config.bless {
46 for e in glob(&format!("{}/{}.*{}.mir", test_dir, test_crate, suffix)).unwrap() {
47 fs::remove_file(e.unwrap()).unwrap();
48 }
49 for e in glob(&format!("{}/{}.*{}.diff", test_dir, test_crate, suffix)).unwrap() {
50 fs::remove_file(e.unwrap()).unwrap();
51 }
52 }
53
54 for MiroptTestFile { from_file, to_file, expected_file } in files {
55 let dumped_string = if let Some(after) = to_file {
56 self.diff_mir_files(from_file.into(), after.into())
57 } else {
58 let mut output_file = Utf8PathBuf::new();
59 output_file.push(self.output_base_dir());
60 output_file.push(&from_file);
61 debug!("comparing the contents of: {} with {:?}", output_file, expected_file);
62 if !output_file.exists() {
63 panic!(
64 "Output file `{}` from test does not exist, available files are in `{}`",
65 output_file,
66 output_file.parent().unwrap()
67 );
68 }
69 self.check_mir_test_timestamp(&from_file, &output_file);
70 let dumped_string = fs::read_to_string(&output_file).unwrap();
71 self.normalize_output(&dumped_string, &[])
72 };
73
74 if self.config.bless {
75 let _ = fs::remove_file(&expected_file);
76 fs::write(expected_file, dumped_string.as_bytes()).unwrap();
77 } else {
78 if !expected_file.exists() {
79 panic!("Output file `{}` from test does not exist", expected_file.display());
80 }
81 let expected_string = fs::read_to_string(&expected_file).unwrap();
82 if dumped_string != expected_string {
83 print!("{}", write_diff(&expected_string, &dumped_string, 3));
84 panic!(
85 "Actual MIR output differs from expected MIR output {}",
86 expected_file.display()
87 );
88 }
89 }
90 }
91
92 if run_filecheck {
93 let output_path = self.output_base_name().with_extension("mir");
94 let proc_res = self.verify_with_filecheck(&output_path);
95 if !proc_res.status.success() {
96 self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);
97 }
98 }
99 }
100
101 fn diff_mir_files(&self, before: Utf8PathBuf, after: Utf8PathBuf) -> String {
102 let to_full_path = |path: Utf8PathBuf| {
103 let full = self.output_base_dir().join(&path);
104 if !full.exists() {
105 panic!(
106 "the mir dump file for {} does not exist (requested in {})",
107 path, self.testpaths.file,
108 );
109 }
110 full
111 };
112 let before = to_full_path(before);
113 let after = to_full_path(after);
114 debug!("comparing the contents of: {} with {}", before, after);
115 let before = fs::read_to_string(before).unwrap();
116 let after = fs::read_to_string(after).unwrap();
117 let before = self.normalize_output(&before, &[]);
118 let after = self.normalize_output(&after, &[]);
119 let mut dumped_string = String::new();
120 for result in diff::lines(&before, &after) {
121 use std::fmt::Write;
122 match result {
123 diff::Result::Left(s) => writeln!(dumped_string, "- {}", s).unwrap(),
124 diff::Result::Right(s) => writeln!(dumped_string, "+ {}", s).unwrap(),
125 diff::Result::Both(s, _) => writeln!(dumped_string, " {}", s).unwrap(),
126 }
127 }
128 dumped_string
129 }
130
131 fn check_mir_test_timestamp(&self, test_name: &str, output_file: &Utf8Path) {
132 let t = |file: &Utf8Path| fs::metadata(file.as_std_path()).unwrap().modified().unwrap();
133 let source_file = &self.testpaths.file;
134 let output_time = t(output_file);
135 let source_time = t(source_file);
136 if source_time > output_time {
137 debug!("source file time: {:?} output file time: {:?}", source_time, output_time);
138 panic!(
139 "test source file `{}` is newer than potentially stale output file `{}`.",
140 source_file, test_name
141 );
142 }
143 }
144}