Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 26 additions & 33 deletions c2rust-transpile/tests/snapshots.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -47,22 +46,7 @@ fn config() -> TranspilerConfig {
}
}

fn transpile(c_path: &Path) {
let parent_dir_name = c_path
.parent()
.and_then(|dir| dir.file_name())
.and_then(|file_name| file_name.to_str())
.unwrap_or_default();
// Some things transpile differently on Linux vs. macOS,
// as they use `unsigned long` and `unsigned long long` differently for builtins.
// This makes snapshot tests trickier, as the output will be OS-dependent.
// We only test Linux here, as that should be sufficient for these specific tests,
// and because cross-compiling with transpilation is not super straightforward,
// so generating the macOS snapshots locally on Linux is annoying.
if parent_dir_name == "linux" && !cfg!(target_os = "linux") {
return;
}

fn transpile(platform: Option<&str>, c_path: &Path) {
let status = Command::new("clang")
.args(&["-c", "-o", "/dev/null"])
.arg(c_path)
Expand All @@ -77,28 +61,37 @@ fn transpile(c_path: &Path) {
let rs_path = c_path.with_extension("rs");
let rs = fs::read_to_string(&rs_path).unwrap();
let debug_expr = format!("cat {}", rs_path.display());
insta::assert_snapshot!("transpile", &rs, &debug_expr);

let name = platform
.map(|platform| ["transpile", platform].join("-"))
.unwrap_or("transpile".into());

insta::assert_snapshot!(name, &rs, &debug_expr);

let status = Command::new("rustc")
.args(&["--crate-type", "lib", "--edition", "2021"])
.args(&["--crate-type", "lib", "--edition", "2021", "-o", "-"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is a lot simpler way of doing this. This writes to stout? How does that work vs /dev/null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/dev/null would be a more obvious bit-bucket, but rustc attempts to create temporary files in the output directory and can't for /dev.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What gets printed to stdout with -?

Copy link
Contributor Author

@fw-immunant fw-immunant Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The --emit=link contents, whatever they are. In this case the output is not captured, so it doesn't really matter what it is.

.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();
}

#[test]
fn transpile_all() {
// We need to do this as a single glob,
// as `insta` removes the common prefix to all matches files,
// and if we do this as separate globs (for linux-only files),
// they'll overwrite each other.
insta::glob!("snapshots/**/*.c", transpile);
insta::glob!("snapshots/*.c", |x| transpile(None, x));

// Some things transpile differently on Linux vs. macOS,
// as they use `unsigned long` and `unsigned long long` differently for builtins.
// This makes snapshot tests trickier, as the output will be OS-dependent.
// We handle this by adding OS name to the snapshot result filename.
#[allow(unused)]
let platform = "unknown";

#[cfg(target_os = "linux")]
let platform = "linux";
#[cfg(target_os = "macos")]
let platform = "macos";

insta::with_settings!({snapshot_suffix => platform}, {
insta::glob!("snapshots/platform-specific/*.c", |x| transpile(Some(platform), x));
});
}
2 changes: 2 additions & 0 deletions c2rust-transpile/tests/snapshots/platform-specific/dummy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* empty file until we have multiple tests; if we glob only one test, insta
omits the `@file.c` suffix from snapshots */
9 changes: 9 additions & 0 deletions c2rust-transpile/tests/snapshots/platform-specific/dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(
dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
16 changes: 16 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
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
)]

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
source: c2rust-transpile/tests/snapshots.rs
expression: cat tests/snapshots/linux/rotate.rs
input_file: c2rust-transpile/tests/snapshots/linux/rotate.c
assertion_line: 67
expression: cat tests/snapshots/platform-specific/rotate.rs
input_file: c2rust-transpile/tests/snapshots/platform-specific/rotate.c
---
#![allow(
dead_code,
Expand All @@ -28,3 +29,4 @@ pub unsafe extern "C" fn rotate_right_64(
.rotate_right(4 as std::ffi::c_int as std::ffi::c_ulong as u32)
as std::ffi::c_ulonglong;
}

16 changes: 16 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
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
)]

28 changes: 28 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we generate the macOS snapshots on Linux easily?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no way to do so from Linux without a macOS VM; we need to compile against Apple's proprietary (though nominally open-source) headers. If we want to support macOS, this is the price we pay.

We could improve the workflow by having snapshot .new files produced as an artifact from our macOS CI pipeline.

source: c2rust-transpile/tests/snapshots.rs
assertion_line: 67
expression: cat tests/snapshots/platform-specific/rotate.rs
input_file: c2rust-transpile/tests/snapshots/platform-specific/rotate.c
---
#![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);
}

2 changes: 2 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: c2rust-transpile/tests/snapshots.rs
assertion_line: 67
expression: cat tests/snapshots/atomics.rs
input_file: c2rust-transpile/tests/snapshots/atomics.c
---
Expand Down Expand Up @@ -44,3 +45,4 @@ pub unsafe extern "C" fn c11_atomics(mut x: std::ffi::c_int) -> std::ffi::c_int
fresh1.1;
return x;
}

2 changes: 2 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: c2rust-transpile/tests/snapshots.rs
assertion_line: 67
expression: cat tests/snapshots/factorial.rs
input_file: c2rust-transpile/tests/snapshots/factorial.c
---
Expand All @@ -24,3 +25,4 @@ pub unsafe extern "C" fn factorial(mut n: std::ffi::c_ushort) -> std::ffi::c_ush
}
return result;
}

2 changes: 2 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: c2rust-transpile/tests/snapshots.rs
assertion_line: 67
expression: cat tests/snapshots/gotos.rs
input_file: c2rust-transpile/tests/snapshots/gotos.c
---
Expand All @@ -22,3 +23,4 @@ pub unsafe extern "C" fn sum(mut count: std::ffi::c_int) -> std::ffi::c_int {
}
return x;
}

2 changes: 2 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: c2rust-transpile/tests/snapshots.rs
assertion_line: 67
expression: cat tests/snapshots/insertion.rs
input_file: c2rust-transpile/tests/snapshots/insertion.c
---
Expand Down Expand Up @@ -30,3 +31,4 @@ pub unsafe extern "C" fn insertion_sort(n: std::ffi::c_int, p: *mut std::ffi::c_
i;
}
}

2 changes: 2 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: c2rust-transpile/tests/snapshots.rs
assertion_line: 67
expression: cat tests/snapshots/rotate.rs
input_file: c2rust-transpile/tests/snapshots/rotate.c
---
Expand Down Expand Up @@ -40,3 +41,4 @@ pub unsafe extern "C" fn rotate_right_16(
pub unsafe extern "C" fn rotate_right_32(mut x: std::ffi::c_uint) -> std::ffi::c_uint {
return x.rotate_right(3 as std::ffi::c_int as std::ffi::c_uint as u32);
}

2 changes: 2 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: c2rust-transpile/tests/snapshots.rs
assertion_line: 67
expression: cat tests/snapshots/spin.rs
input_file: c2rust-transpile/tests/snapshots/spin.c
---
Expand All @@ -16,3 +17,4 @@ input_file: c2rust-transpile/tests/snapshots/spin.c
pub unsafe extern "C" fn spin() {
::core::hint::spin_loop();
}