diff --git a/c2rust-transpile/tests/snapshots.rs b/c2rust-transpile/tests/snapshots.rs index 83c631b0de..a927571f18 100644 --- a/c2rust-transpile/tests/snapshots.rs +++ b/c2rust-transpile/tests/snapshots.rs @@ -1,7 +1,6 @@ use std::env::current_dir; -use std::ffi::OsString; use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::Command; use c2rust_transpile::{ReplaceMode, TranspilerConfig}; @@ -59,28 +58,47 @@ fn transpile(platform: Option<&str>, c_path: &Path) { c2rust_transpile::transpile(config(), &temp_path, &[]); let cwd = current_dir().unwrap(); let c_path = c_path.strip_prefix(&cwd).unwrap(); + // The crate name can't have `.`s in it, so use the file stem. + // This is also why we set it explicitly with `--crate-name`, + // as once we add `.{platform}`, the crate name derived from + // the file name won't be valid anymore. + let crate_name = c_path.file_stem().unwrap().to_str().unwrap(); let rs_path = c_path.with_extension("rs"); + // We need to move the `.rs` file to a platform-specific name + // so that they don't overwrite each other. + let rs_path = match platform { + None => rs_path, + Some(platform) => { + let platform_rs_path = rs_path.with_extension(format!("{platform}.rs")); + fs::rename(&rs_path, &platform_rs_path).unwrap(); + platform_rs_path + } + }; let rs = fs::read_to_string(&rs_path).unwrap(); let debug_expr = format!("cat {}", rs_path.display()); - let name = platform - .map(|platform| ["transpile", platform].join("-")) - .unwrap_or("transpile".into()); - - insta::assert_snapshot!(name, &rs, &debug_expr); + let snapshot_name = match platform { + None => "transpile".into(), + Some(platform) => format!("transpile-{platform}"), + }; + insta::assert_snapshot!(snapshot_name, &rs, &debug_expr); + // Don't need to worry about platform clashes here, as this is immediately deleted. + let rlib_path = format!("lib{crate_name}.rlib"); let status = Command::new("rustc") - .args(&["--crate-type", "lib", "--edition", "2021"]) + .args(&[ + "--crate-type", + "lib", + "--edition", + "2021", + "--crate-name", + crate_name, + "-o", + &rlib_path, + ]) .arg(&rs_path) .status(); assert!(status.unwrap().success()); - let rlib_path = { - let mut file_name = OsString::new(); - file_name.push("lib"); - file_name.push(rs_path.file_stem().unwrap()); - file_name.push(".rlib"); - PathBuf::from(file_name) - }; fs::remove_file(&rlib_path).unwrap(); } diff --git a/c2rust-transpile/tests/snapshots/platform-specific/dummy.c b/c2rust-transpile/tests/snapshots/platform-specific/dummy.c deleted file mode 100644 index f658d3bd1e..0000000000 --- a/c2rust-transpile/tests/snapshots/platform-specific/dummy.c +++ /dev/null @@ -1,2 +0,0 @@ -/* empty file until we have multiple tests; if we glob only one test, insta -omits the `@file.c` suffix from snapshots */ diff --git a/c2rust-transpile/tests/snapshots/platform-specific/dummy.rs b/c2rust-transpile/tests/snapshots/platform-specific/dummy.rs deleted file mode 100644 index d5e9115dc0..0000000000 --- a/c2rust-transpile/tests/snapshots/platform-specific/dummy.rs +++ /dev/null @@ -1,9 +0,0 @@ -#![allow( - dead_code, - mutable_transmutes, - non_camel_case_types, - non_snake_case, - non_upper_case_globals, - unused_assignments, - unused_mut -)] diff --git a/c2rust-transpile/tests/snapshots/platform-specific/rnd.rs b/c2rust-transpile/tests/snapshots/platform-specific/rnd.linux.rs similarity index 100% rename from c2rust-transpile/tests/snapshots/platform-specific/rnd.rs rename to c2rust-transpile/tests/snapshots/platform-specific/rnd.linux.rs diff --git a/c2rust-transpile/tests/snapshots/platform-specific/rnd.macos.rs b/c2rust-transpile/tests/snapshots/platform-specific/rnd.macos.rs new file mode 100644 index 0000000000..f8d84fc2d2 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/platform-specific/rnd.macos.rs @@ -0,0 +1,29 @@ +#![allow( + dead_code, + mutable_transmutes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +extern "C" { + fn abs(_: std::ffi::c_int) -> std::ffi::c_int; +} +pub type int32_t = std::ffi::c_int; +pub type uint32_t = std::ffi::c_uint; +#[no_mangle] +pub static mut cur_rand_seed: uint32_t = 0 as std::ffi::c_int as uint32_t; +#[no_mangle] +pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) { + cur_rand_seed = s; +} +#[no_mangle] +pub unsafe extern "C" fn get_rand_seed() -> uint32_t { + let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t; + let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t; + cur_rand_seed = MULTIPLIER.wrapping_mul(cur_rand_seed).wrapping_add(INCREMENT); + let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t; + return ret; +} + diff --git a/c2rust-transpile/tests/snapshots/platform-specific/rotate.rs b/c2rust-transpile/tests/snapshots/platform-specific/rotate.linux.rs similarity index 100% rename from c2rust-transpile/tests/snapshots/platform-specific/rotate.rs rename to c2rust-transpile/tests/snapshots/platform-specific/rotate.linux.rs diff --git a/c2rust-transpile/tests/snapshots/platform-specific/rotate.macos.rs b/c2rust-transpile/tests/snapshots/platform-specific/rotate.macos.rs new file mode 100644 index 0000000000..90236c1054 --- /dev/null +++ b/c2rust-transpile/tests/snapshots/platform-specific/rotate.macos.rs @@ -0,0 +1,22 @@ +#![allow( + dead_code, + mutable_transmutes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] +#[no_mangle] +pub unsafe extern "C" fn rotate_left_64( + mut x: std::ffi::c_ulonglong, +) -> std::ffi::c_ulonglong { + return x.rotate_left(4 as std::ffi::c_int as std::ffi::c_ulonglong as u32); +} +#[no_mangle] +pub unsafe extern "C" fn rotate_right_64( + mut x: std::ffi::c_ulonglong, +) -> std::ffi::c_ulonglong { + return x.rotate_right(4 as std::ffi::c_int as std::ffi::c_ulonglong as u32); +} + diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@dummy.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@dummy.c.snap deleted file mode 100644 index 99cb238cc0..0000000000 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-linux@dummy.c.snap +++ /dev/null @@ -1,16 +0,0 @@ ---- -source: c2rust-transpile/tests/snapshots.rs -assertion_line: 69 -expression: cat tests/snapshots/platform-specific/dummy.rs -input_file: c2rust-transpile/tests/snapshots/platform-specific/dummy.c ---- -#![allow( - dead_code, - mutable_transmutes, - non_camel_case_types, - non_snake_case, - non_upper_case_globals, - unused_assignments, - unused_mut -)] - diff --git a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@dummy.c.snap b/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@dummy.c.snap deleted file mode 100644 index 99cb238cc0..0000000000 --- a/c2rust-transpile/tests/snapshots/snapshots__transpile-macos@dummy.c.snap +++ /dev/null @@ -1,16 +0,0 @@ ---- -source: c2rust-transpile/tests/snapshots.rs -assertion_line: 69 -expression: cat tests/snapshots/platform-specific/dummy.rs -input_file: c2rust-transpile/tests/snapshots/platform-specific/dummy.c ---- -#![allow( - dead_code, - mutable_transmutes, - non_camel_case_types, - non_snake_case, - non_upper_case_globals, - unused_assignments, - unused_mut -)] -