Skip to content

Commit cf1c95a

Browse files
committed
transpile: Add tests for #1238 and #1240
1 parent bf97325 commit cf1c95a

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

c2rust-transpile/tests/snapshots/arrays.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include <stdlib.h>
22

3-
static char simple[] = "mystring";
4-
static char *foo = "mystring";
3+
char static_char_array[] = "mystring";
4+
char *static_char_ptr = "mystring";
5+
void *static_void_ptr = (void*) static_char_array;
56

67
void entry(void) {
78
int int_2d[1][1] = { 1 };
@@ -27,7 +28,7 @@ void entry(void) {
2728

2829
int *int_var_ptr = int_empty_init;
2930
int (*int_var_array_ptr)[16] = &int_empty_init;
30-
char *char_var_ptr_var = char_with_string;
31+
char *char_var_ptr = char_with_string;
3132
char (*char_var_array_ptr)[4] = &char_with_string;
3233
const char *const_char_lit_ptr = "abc";
3334
const char (*const_char_lit_array_ptr)[4] = &"abc";
@@ -44,8 +45,8 @@ void entry(void) {
4445
#endif
4546

4647
// Test that we can get the address of the element past the end of the array
47-
char *past_end = &simple[sizeof(simple)];
48-
past_end = &foo[8];
48+
char *past_end = &static_char_array[sizeof(static_char_array)];
49+
past_end = &static_char_ptr[8];
4950
}
5051

5152
void short_initializer() {

c2rust-transpile/tests/snapshots/compound_literals.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
int static_single_int = (int) { 42 };
22
int *static_single_int_ptr = &((int) { 42 });
33
int *static_int_ptr_to_array = (int[]) { 42, 9001 };
4+
volatile int *static_volatile_int_ptr_to_array = (int[]) { 42, 9001 };
5+
int (*static_int_array_ptr)[2] = &(int[]) { 42, 9001 };
6+
47
// Currently generates broken Rust code, see
58
// https://github.com/immunant/c2rust/issues/1410
69
//char *static_char_ptr_to_array = (char[]) { "hello" };
7-
int (*static_int_array_ptr)[2] = &(int[]) { 42, 9001 };
8-
// As above
910
//char (*static_char_array_ptr)[6] = &(char[]) { "hello" };
1011

1112
#define SINGLE_INT ((int) { 42 })

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ pub struct C2RustUnnamed_2 {
3434
pub x: ::core::ffi::c_short,
3535
pub y: ::core::ffi::c_int,
3636
}
37-
static mut simple: [::core::ffi::c_char; 9] =
37+
#[no_mangle]
38+
pub static mut static_char_array: [::core::ffi::c_char; 9] =
3839
unsafe { ::core::mem::transmute::<[u8; 9], [::core::ffi::c_char; 9]>(*b"mystring\0") };
39-
static mut foo: *mut ::core::ffi::c_char =
40+
#[no_mangle]
41+
pub static mut static_char_ptr: *mut ::core::ffi::c_char =
4042
b"mystring\0" as *const u8 as *const ::core::ffi::c_char as *mut ::core::ffi::c_char;
4143
#[no_mangle]
44+
pub static mut static_void_ptr: *mut ::core::ffi::c_void = unsafe {
45+
static_char_array.as_ptr() as *mut _ as *mut ::core::ffi::c_void
46+
};
47+
#[no_mangle]
4248
pub unsafe extern "C" fn entry() {
4349
let mut int_2d: [[::core::ffi::c_int; 1]; 1] = [[1 as ::core::ffi::c_int]];
4450
int_2d[0 as ::core::ffi::c_int as usize][0 as ::core::ffi::c_int as usize] +=
@@ -81,7 +87,7 @@ pub unsafe extern "C" fn entry() {
8187
);
8288
let mut int_var_ptr: *mut ::core::ffi::c_int = int_empty_init.as_mut_ptr();
8389
let mut int_var_array_ptr: *mut [::core::ffi::c_int; 16] = &mut int_empty_init;
84-
let mut char_var_ptr_var: *mut ::core::ffi::c_char = char_with_string.as_mut_ptr();
90+
let mut char_var_ptr: *mut ::core::ffi::c_char = char_with_string.as_mut_ptr();
8591
let mut char_var_array_ptr: *mut [::core::ffi::c_char; 4] = &mut char_with_string;
8692
let mut const_char_lit_ptr: *const ::core::ffi::c_char =
8793
b"abc\0" as *const u8 as *const ::core::ffi::c_char;
@@ -92,11 +98,12 @@ pub unsafe extern "C" fn entry() {
9298
let mut char_lit_array_ptr: *mut [::core::ffi::c_char; 4] = b"abc\0" as *const [u8; 4]
9399
as *const [::core::ffi::c_char; 4]
94100
as *mut [::core::ffi::c_char; 4];
95-
let mut past_end: *mut ::core::ffi::c_char = &mut *simple
101+
let mut past_end: *mut ::core::ffi::c_char = &mut *static_char_array
96102
.as_mut_ptr()
97103
.offset(::core::mem::size_of::<[::core::ffi::c_char; 9]>() as isize)
98104
as *mut ::core::ffi::c_char;
99-
past_end = &mut *foo.offset(8 as ::core::ffi::c_int as isize) as *mut ::core::ffi::c_char;
105+
past_end =
106+
&mut *static_char_ptr.offset(8 as ::core::ffi::c_int as isize) as *mut ::core::ffi::c_char;
100107
}
101108
#[no_mangle]
102109
pub unsafe extern "C" fn short_initializer() {

c2rust-transpile/tests/snapshots/snapshots__transpile@compound_literals.c.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub static mut static_single_int_ptr: *mut ::core::ffi::c_int =
1919
#[no_mangle]
2020
pub static mut static_int_ptr_to_array: *mut ::core::ffi::c_int = [42, 9001].as_ptr() as *mut _;
2121
#[no_mangle]
22+
pub static mut static_volatile_int_ptr_to_array: *mut ::core::ffi::c_int =
23+
[42, 9001].as_ptr() as *mut _ as *mut ::core::ffi::c_int;
24+
#[no_mangle]
2225
pub static mut static_int_array_ptr: *mut [::core::ffi::c_int; 2] =
2326
&[42, 9001] as *const [::core::ffi::c_int; 2] as *mut [::core::ffi::c_int; 2];
2427
pub const SINGLE_INT: ::core::ffi::c_int = 42 as ::core::ffi::c_int;

0 commit comments

Comments
 (0)