1
1
use std:: fs;
2
+ use std:: path:: Path ;
2
3
3
4
pub struct MiroptTestFiles {
4
5
pub expected_file : std:: path:: PathBuf ,
@@ -8,18 +9,52 @@ pub struct MiroptTestFiles {
8
9
pub passes : Vec < String > ,
9
10
}
10
11
11
- pub fn files_for_miropt_test ( testfile : & std:: path:: Path , bit_width : u32 ) -> Vec < MiroptTestFiles > {
12
+ pub enum PanicStrategy {
13
+ Unwind ,
14
+ Abort ,
15
+ }
16
+
17
+ pub fn output_file_suffix (
18
+ testfile : & Path ,
19
+ bit_width : u32 ,
20
+ panic_strategy : PanicStrategy ,
21
+ ) -> String {
22
+ let mut each_bit_width = false ;
23
+ let mut each_panic_strategy = false ;
24
+ for line in fs:: read_to_string ( testfile) . unwrap ( ) . lines ( ) {
25
+ if line == "// EMIT_MIR_FOR_EACH_BIT_WIDTH" {
26
+ each_bit_width = true ;
27
+ }
28
+ if line == "// EMIT_MIR_FOR_EACH_PANIC_STRATEGY" {
29
+ each_panic_strategy = true ;
30
+ }
31
+ }
32
+
33
+ let mut suffix = String :: new ( ) ;
34
+ if each_bit_width {
35
+ suffix. push_str ( & format ! ( ".{}bit" , bit_width) ) ;
36
+ }
37
+ if each_panic_strategy {
38
+ match panic_strategy {
39
+ PanicStrategy :: Unwind => suffix. push_str ( ".panic-unwind" ) ,
40
+ PanicStrategy :: Abort => suffix. push_str ( ".panic-abort" ) ,
41
+ }
42
+ }
43
+ suffix
44
+ }
45
+
46
+ pub fn files_for_miropt_test (
47
+ testfile : & std:: path:: Path ,
48
+ bit_width : u32 ,
49
+ panic_strategy : PanicStrategy ,
50
+ ) -> Vec < MiroptTestFiles > {
12
51
let mut out = Vec :: new ( ) ;
13
52
let test_file_contents = fs:: read_to_string ( & testfile) . unwrap ( ) ;
14
53
15
54
let test_dir = testfile. parent ( ) . unwrap ( ) ;
16
55
let test_crate = testfile. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . replace ( '-' , "_" ) ;
17
56
18
- let bit_width = if test_file_contents. lines ( ) . any ( |l| l == "// EMIT_MIR_FOR_EACH_BIT_WIDTH" ) {
19
- format ! ( ".{}bit" , bit_width)
20
- } else {
21
- String :: new ( )
22
- } ;
57
+ let suffix = output_file_suffix ( testfile, bit_width, panic_strategy) ;
23
58
24
59
for l in test_file_contents. lines ( ) {
25
60
if l. starts_with ( "// EMIT_MIR " ) {
@@ -37,7 +72,7 @@ pub fn files_for_miropt_test(testfile: &std::path::Path, bit_width: u32) -> Vec<
37
72
passes. push ( trimmed. split ( '.' ) . last ( ) . unwrap ( ) . to_owned ( ) ) ;
38
73
let test_against = format ! ( "{}.after.mir" , trimmed) ;
39
74
from_file = format ! ( "{}.before.mir" , trimmed) ;
40
- expected_file = format ! ( "{}{}.diff" , trimmed, bit_width ) ;
75
+ expected_file = format ! ( "{}{}.diff" , trimmed, suffix ) ;
41
76
assert ! ( test_names. next( ) . is_none( ) , "two mir pass names specified for MIR diff" ) ;
42
77
to_file = Some ( test_against) ;
43
78
} else if let Some ( first_pass) = test_names. next ( ) {
@@ -51,7 +86,7 @@ pub fn files_for_miropt_test(testfile: &std::path::Path, bit_width: u32) -> Vec<
51
86
assert ! ( test_names. next( ) . is_none( ) , "three mir pass names specified for MIR diff" ) ;
52
87
53
88
expected_file =
54
- format ! ( "{}{}.{}-{}.diff" , test_name, bit_width , first_pass, second_pass) ;
89
+ format ! ( "{}{}.{}-{}.diff" , test_name, suffix , first_pass, second_pass) ;
55
90
let second_file = format ! ( "{}.{}.mir" , test_name, second_pass) ;
56
91
from_file = format ! ( "{}.{}.mir" , test_name, first_pass) ;
57
92
to_file = Some ( second_file) ;
@@ -64,7 +99,7 @@ pub fn files_for_miropt_test(testfile: &std::path::Path, bit_width: u32) -> Vec<
64
99
let extension = cap. get ( 1 ) . unwrap ( ) . as_str ( ) ;
65
100
66
101
expected_file =
67
- format ! ( "{}{}{}" , test_name. trim_end_matches( extension) , bit_width , extension, ) ;
102
+ format ! ( "{}{}{}" , test_name. trim_end_matches( extension) , suffix , extension, ) ;
68
103
from_file = test_name. to_string ( ) ;
69
104
assert ! ( test_names. next( ) . is_none( ) , "two mir pass names specified for MIR dump" ) ;
70
105
to_file = None ;
0 commit comments