Skip to content

Commit 745974e

Browse files
committed
ast-exporter: Do not process macros for the syntactic form of InitListExpr
1 parent 733bdf0 commit 745974e

3 files changed

Lines changed: 32 additions & 80 deletions

File tree

c2rust-ast-exporter/src/AstExporter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,14 @@ class TranslateASTVisitor final
14111411
// if (!E->isConstantInitializer(*Context, false))
14121412
// return true;
14131413

1414+
if (const InitListExpr* ILE = dyn_cast<InitListExpr>(E)) {
1415+
// Do not process macros for the syntactic form,
1416+
// so that they can be processed for the semantic form.
1417+
if (ILE->isSyntacticForm()) {
1418+
return true;
1419+
}
1420+
}
1421+
14141422
auto &Mgr = Context->getSourceManager();
14151423
auto Range = E->getSourceRange();
14161424
LLVM_DEBUG(dbgs() << "Checking expr for macro expansion: ");

c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.clang15.snap

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ pub const LITERAL_FLOAT: ::core::ffi::c_double = 3.14f64;
4343
pub const LITERAL_CHAR: ::core::ffi::c_int = 'x' as ::core::ffi::c_int;
4444
pub const LITERAL_STR: [::core::ffi::c_char; 6] =
4545
unsafe { ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0") };
46+
pub const LITERAL_ARRAY: [::core::ffi::c_int; 3] = [
47+
1 as ::core::ffi::c_int,
48+
2 as ::core::ffi::c_int,
49+
3 as ::core::ffi::c_int,
50+
];
4651
pub const LITERAL_STRUCT: S = S {
4752
i: 5 as ::core::ffi::c_int,
4853
};
@@ -51,6 +56,7 @@ pub const NESTED_BOOL: ::core::ffi::c_int = LITERAL_BOOL;
5156
pub const NESTED_FLOAT: ::core::ffi::c_double = LITERAL_FLOAT;
5257
pub const NESTED_CHAR: ::core::ffi::c_int = LITERAL_CHAR;
5358
pub const NESTED_STR: [::core::ffi::c_char; 6] = LITERAL_STR;
59+
pub const NESTED_ARRAY: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
5460
pub const NESTED_STRUCT: S = LITERAL_STRUCT;
5561
pub const PARENS: ::core::ffi::c_int = NESTED_INT * (LITERAL_CHAR + true_0);
5662
pub const PTR_ARITHMETIC: *const ::core::ffi::c_char = unsafe {
@@ -69,23 +75,15 @@ pub unsafe extern "C" fn local_muts() {
6975
let mut literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char;
7076
let mut literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
7177
let mut literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
72-
let mut literal_array: [::core::ffi::c_int; 3] = [
73-
1 as ::core::ffi::c_int,
74-
2 as ::core::ffi::c_int,
75-
3 as ::core::ffi::c_int,
76-
];
78+
let mut literal_array: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
7779
let mut literal_struct: S = LITERAL_STRUCT;
7880
let mut nested_int: ::core::ffi::c_int = NESTED_INT;
7981
let mut nested_bool: bool = NESTED_BOOL != 0;
8082
let mut nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float;
8183
let mut nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char;
8284
let mut nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
8385
let mut nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
84-
let mut nested_array: [::core::ffi::c_int; 3] = [
85-
1 as ::core::ffi::c_int,
86-
2 as ::core::ffi::c_int,
87-
3 as ::core::ffi::c_int,
88-
];
86+
let mut nested_array: [::core::ffi::c_int; 3] = NESTED_ARRAY;
8987
let mut nested_struct: S = NESTED_STRUCT;
9088
let mut negative_int: ::core::ffi::c_int = -LITERAL_INT;
9189
let mut int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int;
@@ -142,23 +140,15 @@ pub unsafe extern "C" fn local_consts() {
142140
let literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char;
143141
let literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
144142
let literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
145-
let literal_array: [::core::ffi::c_int; 3] = [
146-
1 as ::core::ffi::c_int,
147-
2 as ::core::ffi::c_int,
148-
3 as ::core::ffi::c_int,
149-
];
143+
let literal_array: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
150144
let literal_struct: S = LITERAL_STRUCT;
151145
let nested_int: ::core::ffi::c_int = NESTED_INT;
152146
let nested_bool: bool = NESTED_BOOL != 0;
153147
let nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float;
154148
let nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char;
155149
let nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
156150
let nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
157-
let nested_array: [::core::ffi::c_int; 3] = [
158-
1 as ::core::ffi::c_int,
159-
2 as ::core::ffi::c_int,
160-
3 as ::core::ffi::c_int,
161-
];
151+
let nested_array: [::core::ffi::c_int; 3] = NESTED_ARRAY;
162152
let nested_struct: S = NESTED_STRUCT;
163153
let negative_int: ::core::ffi::c_int = -LITERAL_INT;
164154
let int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int;
@@ -213,11 +203,7 @@ static mut global_static_const_literal_char: ::core::ffi::c_char =
213203
LITERAL_CHAR as ::core::ffi::c_char;
214204
static mut global_static_const_literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
215205
static mut global_static_const_literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
216-
static mut global_static_const_literal_array: [::core::ffi::c_int; 3] = [
217-
1 as ::core::ffi::c_int,
218-
2 as ::core::ffi::c_int,
219-
3 as ::core::ffi::c_int,
220-
];
206+
static mut global_static_const_literal_array: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
221207
static mut global_static_const_literal_struct: S = LITERAL_STRUCT;
222208
static mut global_static_const_nested_int: ::core::ffi::c_int = NESTED_INT;
223209
static mut global_static_const_nested_bool: bool = NESTED_BOOL != 0;
@@ -227,11 +213,7 @@ static mut global_static_const_nested_char: ::core::ffi::c_char =
227213
NESTED_CHAR as ::core::ffi::c_char;
228214
static mut global_static_const_nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
229215
static mut global_static_const_nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
230-
static mut global_static_const_nested_array: [::core::ffi::c_int; 3] = [
231-
1 as ::core::ffi::c_int,
232-
2 as ::core::ffi::c_int,
233-
3 as ::core::ffi::c_int,
234-
];
216+
static mut global_static_const_nested_array: [::core::ffi::c_int; 3] = NESTED_ARRAY;
235217
static mut global_static_const_nested_struct: S = NESTED_STRUCT;
236218
static mut global_static_const_negative_int: ::core::ffi::c_int = -LITERAL_INT;
237219
static mut global_static_const_int_arithmetic: ::core::ffi::c_int =
@@ -275,11 +257,7 @@ pub static mut global_const_literal_str_ptr: *const ::core::ffi::c_char = LITERA
275257
#[no_mangle]
276258
pub static mut global_const_literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
277259
#[no_mangle]
278-
pub static mut global_const_literal_array: [::core::ffi::c_int; 3] = [
279-
1 as ::core::ffi::c_int,
280-
2 as ::core::ffi::c_int,
281-
3 as ::core::ffi::c_int,
282-
];
260+
pub static mut global_const_literal_array: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
283261
#[no_mangle]
284262
pub static mut global_const_literal_struct: S = LITERAL_STRUCT;
285263
#[no_mangle]
@@ -296,11 +274,7 @@ pub static mut global_const_nested_str_ptr: *const ::core::ffi::c_char = NESTED_
296274
#[no_mangle]
297275
pub static mut global_const_nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
298276
#[no_mangle]
299-
pub static mut global_const_nested_array: [::core::ffi::c_int; 3] = [
300-
1 as ::core::ffi::c_int,
301-
2 as ::core::ffi::c_int,
302-
3 as ::core::ffi::c_int,
303-
];
277+
pub static mut global_const_nested_array: [::core::ffi::c_int; 3] = NESTED_ARRAY;
304278
#[no_mangle]
305279
pub static mut global_const_nested_struct: S = NESTED_STRUCT;
306280
#[no_mangle]

c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2024.clang15.snap

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub const LITERAL_FLOAT: ::core::ffi::c_double = 3.14f64;
4343
pub const LITERAL_CHAR: ::core::ffi::c_int = 'x' as ::core::ffi::c_int;
4444
pub const LITERAL_STR: [::core::ffi::c_char; 6] =
4545
unsafe { ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0") };
46+
pub const LITERAL_ARRAY: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
4647
pub const LITERAL_STRUCT: S = S {
4748
i: 5 as ::core::ffi::c_int,
4849
};
@@ -51,6 +52,7 @@ pub const NESTED_BOOL: ::core::ffi::c_int = LITERAL_BOOL;
5152
pub const NESTED_FLOAT: ::core::ffi::c_double = LITERAL_FLOAT;
5253
pub const NESTED_CHAR: ::core::ffi::c_int = LITERAL_CHAR;
5354
pub const NESTED_STR: [::core::ffi::c_char; 6] = LITERAL_STR;
55+
pub const NESTED_ARRAY: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
5456
pub const NESTED_STRUCT: S = LITERAL_STRUCT;
5557
pub const PARENS: ::core::ffi::c_int = NESTED_INT * (LITERAL_CHAR + true_0);
5658
pub const PTR_ARITHMETIC: *const ::core::ffi::c_char = unsafe {
@@ -69,23 +71,15 @@ pub unsafe extern "C" fn local_muts() {
6971
let mut literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char;
7072
let mut literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
7173
let mut literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
72-
let mut literal_array: [::core::ffi::c_int; 3] = [
73-
1 as ::core::ffi::c_int,
74-
2 as ::core::ffi::c_int,
75-
3 as ::core::ffi::c_int,
76-
];
74+
let mut literal_array: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
7775
let mut literal_struct: S = LITERAL_STRUCT;
7876
let mut nested_int: ::core::ffi::c_int = NESTED_INT;
7977
let mut nested_bool: bool = NESTED_BOOL != 0;
8078
let mut nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float;
8179
let mut nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char;
8280
let mut nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
8381
let mut nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
84-
let mut nested_array: [::core::ffi::c_int; 3] = [
85-
1 as ::core::ffi::c_int,
86-
2 as ::core::ffi::c_int,
87-
3 as ::core::ffi::c_int,
88-
];
82+
let mut nested_array: [::core::ffi::c_int; 3] = NESTED_ARRAY;
8983
let mut nested_struct: S = NESTED_STRUCT;
9084
let mut negative_int: ::core::ffi::c_int = -LITERAL_INT;
9185
let mut int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int;
@@ -142,23 +136,15 @@ pub unsafe extern "C" fn local_consts() {
142136
let literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char;
143137
let literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
144138
let literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
145-
let literal_array: [::core::ffi::c_int; 3] = [
146-
1 as ::core::ffi::c_int,
147-
2 as ::core::ffi::c_int,
148-
3 as ::core::ffi::c_int,
149-
];
139+
let literal_array: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
150140
let literal_struct: S = LITERAL_STRUCT;
151141
let nested_int: ::core::ffi::c_int = NESTED_INT;
152142
let nested_bool: bool = NESTED_BOOL != 0;
153143
let nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float;
154144
let nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char;
155145
let nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
156146
let nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
157-
let nested_array: [::core::ffi::c_int; 3] = [
158-
1 as ::core::ffi::c_int,
159-
2 as ::core::ffi::c_int,
160-
3 as ::core::ffi::c_int,
161-
];
147+
let nested_array: [::core::ffi::c_int; 3] = NESTED_ARRAY;
162148
let nested_struct: S = NESTED_STRUCT;
163149
let negative_int: ::core::ffi::c_int = -LITERAL_INT;
164150
let int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int;
@@ -213,11 +199,7 @@ static mut global_static_const_literal_char: ::core::ffi::c_char =
213199
LITERAL_CHAR as ::core::ffi::c_char;
214200
static mut global_static_const_literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
215201
static mut global_static_const_literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
216-
static mut global_static_const_literal_array: [::core::ffi::c_int; 3] = [
217-
1 as ::core::ffi::c_int,
218-
2 as ::core::ffi::c_int,
219-
3 as ::core::ffi::c_int,
220-
];
202+
static mut global_static_const_literal_array: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
221203
static mut global_static_const_literal_struct: S = LITERAL_STRUCT;
222204
static mut global_static_const_nested_int: ::core::ffi::c_int = NESTED_INT;
223205
static mut global_static_const_nested_bool: bool = NESTED_BOOL != 0;
@@ -227,11 +209,7 @@ static mut global_static_const_nested_char: ::core::ffi::c_char =
227209
NESTED_CHAR as ::core::ffi::c_char;
228210
static mut global_static_const_nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
229211
static mut global_static_const_nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
230-
static mut global_static_const_nested_array: [::core::ffi::c_int; 3] = [
231-
1 as ::core::ffi::c_int,
232-
2 as ::core::ffi::c_int,
233-
3 as ::core::ffi::c_int,
234-
];
212+
static mut global_static_const_nested_array: [::core::ffi::c_int; 3] = NESTED_ARRAY;
235213
static mut global_static_const_nested_struct: S = NESTED_STRUCT;
236214
static mut global_static_const_negative_int: ::core::ffi::c_int = -LITERAL_INT;
237215
static mut global_static_const_int_arithmetic: ::core::ffi::c_int =
@@ -275,11 +253,7 @@ pub static mut global_const_literal_str_ptr: *const ::core::ffi::c_char = LITERA
275253
#[unsafe(no_mangle)]
276254
pub static mut global_const_literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
277255
#[unsafe(no_mangle)]
278-
pub static mut global_const_literal_array: [::core::ffi::c_int; 3] = [
279-
1 as ::core::ffi::c_int,
280-
2 as ::core::ffi::c_int,
281-
3 as ::core::ffi::c_int,
282-
];
256+
pub static mut global_const_literal_array: [::core::ffi::c_int; 3] = LITERAL_ARRAY;
283257
#[unsafe(no_mangle)]
284258
pub static mut global_const_literal_struct: S = LITERAL_STRUCT;
285259
#[unsafe(no_mangle)]
@@ -296,11 +270,7 @@ pub static mut global_const_nested_str_ptr: *const ::core::ffi::c_char = NESTED_
296270
#[unsafe(no_mangle)]
297271
pub static mut global_const_nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
298272
#[unsafe(no_mangle)]
299-
pub static mut global_const_nested_array: [::core::ffi::c_int; 3] = [
300-
1 as ::core::ffi::c_int,
301-
2 as ::core::ffi::c_int,
302-
3 as ::core::ffi::c_int,
303-
];
273+
pub static mut global_const_nested_array: [::core::ffi::c_int; 3] = NESTED_ARRAY;
304274
#[unsafe(no_mangle)]
305275
pub static mut global_const_nested_struct: S = NESTED_STRUCT;
306276
#[unsafe(no_mangle)]

0 commit comments

Comments
 (0)