Skip to content

Commit 33923cf

Browse files
committed
transpile: tests: add compound literals tests, incl. address-taken ones
1 parent 24c9461 commit 33923cf

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int static_single_int = (int) { 42 };
2+
int *static_single_int_ptr = &((int) { 42 });
3+
int *static_int_array_ptr = (int[]) { 42, 9001 };
4+
// Currently generates broken Rust code, see
5+
// https://github.com/immunant/c2rust/issues/1410
6+
//char *static_char_array_ptr = (char[]) { "hello" };
7+
8+
#define SINGLE_INT ((int) { 42 })
9+
#define INT_ARRAY ((int[]) { 42, 9001 })
10+
#define CHAR_ARRAY ((char[]) { "hello" })
11+
12+
void local_compound_literals() {
13+
int single_int = (int) { 42 };
14+
int *single_int_ptr = &((int) { 42 });
15+
int *int_array_ptr = (int[]) { 42, 9001 };
16+
char *char_array_ptr = (char[]) { "hello" };
17+
18+
int macro_single_int = SINGLE_INT;
19+
int *macro_single_int_ptr = &SINGLE_INT;
20+
int *macro_int_array_ptr = INT_ARRAY;
21+
char *macro_char_array_ptr = CHAR_ARRAY;
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
source: c2rust-transpile/tests/snapshots.rs
3+
expression: cat tests/snapshots/compound_literals.rs
4+
input_file: c2rust-transpile/tests/snapshots/compound_literals.c
5+
---
6+
#![allow(
7+
dead_code,
8+
non_camel_case_types,
9+
non_snake_case,
10+
non_upper_case_globals,
11+
unused_assignments,
12+
unused_mut
13+
)]
14+
#[no_mangle]
15+
pub static mut static_single_int: core::ffi::c_int = 42;
16+
#[no_mangle]
17+
pub static mut static_single_int_ptr: *mut core::ffi::c_int =
18+
&42 as *const core::ffi::c_int as *mut core::ffi::c_int;
19+
#[no_mangle]
20+
pub static mut static_int_array_ptr: *mut core::ffi::c_int = [42, 9001].as_ptr() as *mut _;
21+
pub const SINGLE_INT: core::ffi::c_int = 42 as core::ffi::c_int;
22+
pub const INT_ARRAY: [core::ffi::c_int; 2] = [42 as core::ffi::c_int, 9001 as core::ffi::c_int];
23+
pub const CHAR_ARRAY: [core::ffi::c_char; 6] =
24+
unsafe { ::core::mem::transmute::<[u8; 6], [core::ffi::c_char; 6]>(*b"hello\0") };
25+
#[no_mangle]
26+
pub unsafe extern "C" fn local_compound_literals() {
27+
let mut single_int: core::ffi::c_int = 42 as core::ffi::c_int;
28+
let mut single_int_ptr: *mut core::ffi::c_int =
29+
&mut (42 as core::ffi::c_int) as *mut core::ffi::c_int;
30+
let mut int_array_ptr: *mut core::ffi::c_int =
31+
[42 as core::ffi::c_int, 9001 as core::ffi::c_int].as_mut_ptr();
32+
let mut char_array_ptr: *mut core::ffi::c_char =
33+
(::core::mem::transmute::<[u8; 6], [core::ffi::c_char; 6]>(*b"hello\0")).as_mut_ptr();
34+
let mut macro_single_int: core::ffi::c_int = SINGLE_INT;
35+
let mut macro_single_int_ptr: *mut core::ffi::c_int = &mut SINGLE_INT as *mut core::ffi::c_int;
36+
let mut macro_int_array_ptr: *mut core::ffi::c_int = INT_ARRAY.as_mut_ptr();
37+
let mut macro_char_array_ptr: *mut core::ffi::c_char = CHAR_ARRAY.as_mut_ptr();
38+
}

0 commit comments

Comments
 (0)