Skip to content

Commit 0f97e7d

Browse files
committed
transpile: add statment expression test to macros.c snapshot
1 parent f423f64 commit 0f97e7d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

c2rust-transpile/tests/snapshots/macros.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ struct S {
3737
#define TERNARY LITERAL_BOOL ? 1 : 2
3838
#define MEMBER LITERAL_STRUCT.i
3939

40+
#define STMT_EXPR \
41+
({ \
42+
int builtin = BUILTIN; \
43+
char indexing = INDEXING; \
44+
float mixed = MIXED_ARITHMETIC; \
45+
for (int i = 0; i < builtin; i++) { \
46+
mixed += (float)indexing; \
47+
} \
48+
mixed; \
49+
})
50+
4051
void local_muts() {
4152
int literal_int = LITERAL_INT;
4253
bool literal_bool = LITERAL_BOOL;
@@ -71,6 +82,7 @@ void local_muts() {
7182
const struct S *ref_struct = REF_LITERAL;
7283
int ternary = TERNARY;
7384
int member = MEMBER;
85+
float stmt_expr = STMT_EXPR;
7486
}
7587

7688
void local_consts() {
@@ -107,6 +119,7 @@ void local_consts() {
107119
const struct S *const ref_struct = REF_LITERAL;
108120
const int ternary = TERNARY;
109121
const int member = MEMBER;
122+
const float stmt_expr = STMT_EXPR;
110123
}
111124

112125
// TODO These are declared in the global scope and thus clash,
@@ -146,6 +159,7 @@ void local_static_consts() {
146159
static const struct S *const ref_struct = REF_LITERAL;
147160
static const int ternary = TERNARY;
148161
static const int member = MEMBER;
162+
static const float stmt_expr = STMT_EXPR;
149163
}
150164
#endif
151165

@@ -186,6 +200,7 @@ static const char *const global_static_const_ref_indexing = REF_MACRO;
186200
static const struct S *const global_static_const_ref_struct = REF_LITERAL;
187201
static const int global_static_const_ternary = TERNARY;
188202
static const int global_static_const_member = MEMBER;
203+
// static const float global_static_const_stmt_expr = STMT_EXPR; // Statement expression not allowed at file scope.
189204

190205
void global_static_consts() {
191206
// Need to use `static`s or else they'll be removed when translated.
@@ -222,6 +237,7 @@ void global_static_consts() {
222237
(void)global_static_const_ref_struct;
223238
(void)global_static_const_ternary;
224239
(void)global_static_const_member;
240+
// (void)global_static_const_stmt_expr;
225241
}
226242

227243
// global consts
@@ -259,6 +275,7 @@ const char *const global_const_ref_indexing = REF_MACRO;
259275
const struct S *const global_const_ref_struct = REF_LITERAL;
260276
const int global_const_ternary = TERNARY;
261277
const int global_const_member = MEMBER;
278+
// const float global_const_stmt_expr = STMT_EXPR; // Statement expression not allowed at file scope.
262279

263280
typedef unsigned long long U64;
264281

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ pub const PTR_ARITHMETIC: *const std::ffi::c_char = unsafe {
6969
};
7070
pub const WIDENING_CAST: std::ffi::c_int = unsafe { LITERAL_INT };
7171
pub const CONVERSION_CAST: std::ffi::c_int = unsafe { LITERAL_INT };
72+
pub const STMT_EXPR: std::ffi::c_float = unsafe {
73+
({
74+
let mut builtin: std::ffi::c_int = (LITERAL_INT as std::ffi::c_uint).leading_zeros() as i32;
75+
let mut indexing: std::ffi::c_char =
76+
(*::core::mem::transmute::<&[u8; 6], &[std::ffi::c_char; 6]>(b"hello\0"))
77+
[LITERAL_FLOAT as std::ffi::c_int as usize];
78+
let mut mixed: std::ffi::c_float =
79+
(LITERAL_INT as std::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as std::ffi::c_double
80+
- true_0 as std::ffi::c_double) as std::ffi::c_float;
81+
let mut i: std::ffi::c_int = 0 as std::ffi::c_int;
82+
while i < builtin {
83+
mixed += indexing as std::ffi::c_float;
84+
i += 1;
85+
i;
86+
}
87+
mixed
88+
})
89+
};
7290
#[no_mangle]
7391
pub unsafe extern "C" fn local_muts() {
7492
let mut literal_int: std::ffi::c_int = LITERAL_INT;
@@ -123,6 +141,7 @@ pub unsafe extern "C" fn local_muts() {
123141
2 as std::ffi::c_int
124142
};
125143
let mut member: std::ffi::c_int = LITERAL_STRUCT.i;
144+
let mut stmt_expr: std::ffi::c_float = STMT_EXPR;
126145
}
127146
#[no_mangle]
128147
pub unsafe extern "C" fn local_consts() {
@@ -178,6 +197,7 @@ pub unsafe extern "C" fn local_consts() {
178197
2 as std::ffi::c_int
179198
};
180199
let member: std::ffi::c_int = LITERAL_STRUCT.i;
200+
let stmt_expr: std::ffi::c_float = STMT_EXPR;
181201
}
182202
static mut global_static_const_literal_int: std::ffi::c_int = LITERAL_INT;
183203
static mut global_static_const_literal_bool: bool = LITERAL_BOOL != 0;

0 commit comments

Comments
 (0)