Skip to content

Commit 008e5d8

Browse files
committed
transpile: save platform-specific .rs files, too
We already save platform-specific `.snap` files for snapshot tests, but the `.rs` files still clash, and we aren't checking them in for all platforms. This moves the `.rs` files to `.{platform}.rs`. And since the crate name can't have a `.` in it, we need to explicitly set the crate name to the file stem now.
1 parent 5fa0d5e commit 008e5d8

File tree

5 files changed

+85
-16
lines changed

5 files changed

+85
-16
lines changed

c2rust-transpile/tests/snapshots.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::env::current_dir;
2-
use std::ffi::OsString;
32
use std::fs;
4-
use std::path::{Path, PathBuf};
3+
use std::path::Path;
54
use std::process::Command;
65

76
use c2rust_transpile::{ReplaceMode, TranspilerConfig};
@@ -59,26 +58,45 @@ fn transpile(platform: Option<&str>, c_path: &Path) {
5958
c2rust_transpile::transpile(config(), &temp_path, &[]);
6059
let cwd = current_dir().unwrap();
6160
let c_path = c_path.strip_prefix(&cwd).unwrap();
61+
// The crate name can't have `.`s in it, so use the file stem.
62+
// This is also why we set it explicitly with `--crate-name`,
63+
// as once we add `.{platform}`, the crate name derived from
64+
// the file name won't be valid anymore.
65+
let crate_name = c_path.file_stem().unwrap().to_str().unwrap();
6266
let rs_path = c_path.with_extension("rs");
67+
// We need to move the `.rs` file to a platform-specific name
68+
// so that they don't overwrite each other.
69+
let rs_path = match platform {
70+
None => rs_path,
71+
Some(platform) => {
72+
let platform_rs_path = rs_path.with_extension(format!("{platform}.rs"));
73+
fs::rename(&rs_path, &platform_rs_path).unwrap();
74+
platform_rs_path
75+
}
76+
};
6377
let rs = fs::read_to_string(&rs_path).unwrap();
6478
let debug_expr = format!("cat {}", rs_path.display());
6579

66-
let name = platform
67-
.map(|platform| ["transpile", platform].join("-"))
68-
.unwrap_or("transpile".into());
69-
70-
insta::assert_snapshot!(name, &rs, &debug_expr);
71-
72-
let rlib_path = {
73-
let mut file_name = OsString::new();
74-
file_name.push("lib");
75-
file_name.push(rs_path.file_stem().unwrap());
76-
file_name.push(".rlib");
77-
PathBuf::from(file_name)
80+
let snapshot_name = match platform {
81+
None => "transpile".into(),
82+
Some(platform) => format!("transpile-{platform}"),
7883
};
84+
insta::assert_snapshot!(snapshot_name, &rs, &debug_expr);
85+
86+
// Don't need to worry about platform clashes here, as this is immediately deleted.
87+
let rlib_path = format!("lib{crate_name}.rlib");
7988
let status = Command::new("rustc")
80-
.args(&["--crate-type", "lib", "--edition", "2021", "-o"])
81-
.args(&[&rlib_path, &rs_path])
89+
.args(&[
90+
"--crate-type",
91+
"lib",
92+
"--edition",
93+
"2021",
94+
"--crate-name",
95+
crate_name,
96+
"-o",
97+
&rlib_path,
98+
])
99+
.arg(&rs_path)
82100
.status();
83101
assert!(status.unwrap().success());
84102
fs::remove_file(&rlib_path).unwrap();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![allow(
2+
dead_code,
3+
mutable_transmutes,
4+
non_camel_case_types,
5+
non_snake_case,
6+
non_upper_case_globals,
7+
unused_assignments,
8+
unused_mut
9+
)]
10+
extern "C" {
11+
fn abs(_: std::ffi::c_int) -> std::ffi::c_int;
12+
}
13+
pub type int32_t = std::ffi::c_int;
14+
pub type uint32_t = std::ffi::c_uint;
15+
#[no_mangle]
16+
pub static mut cur_rand_seed: uint32_t = 0 as std::ffi::c_int as uint32_t;
17+
#[no_mangle]
18+
pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) {
19+
cur_rand_seed = s;
20+
}
21+
#[no_mangle]
22+
pub unsafe extern "C" fn get_rand_seed() -> uint32_t {
23+
let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t;
24+
let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t;
25+
cur_rand_seed = MULTIPLIER.wrapping_mul(cur_rand_seed).wrapping_add(INCREMENT);
26+
let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t;
27+
return ret;
28+
}
29+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![allow(
2+
dead_code,
3+
mutable_transmutes,
4+
non_camel_case_types,
5+
non_snake_case,
6+
non_upper_case_globals,
7+
unused_assignments,
8+
unused_mut
9+
)]
10+
#[no_mangle]
11+
pub unsafe extern "C" fn rotate_left_64(
12+
mut x: std::ffi::c_ulonglong,
13+
) -> std::ffi::c_ulonglong {
14+
return x.rotate_left(4 as std::ffi::c_int as std::ffi::c_ulonglong as u32);
15+
}
16+
#[no_mangle]
17+
pub unsafe extern "C" fn rotate_right_64(
18+
mut x: std::ffi::c_ulonglong,
19+
) -> std::ffi::c_ulonglong {
20+
return x.rotate_right(4 as std::ffi::c_int as std::ffi::c_ulonglong as u32);
21+
}
22+

0 commit comments

Comments
 (0)