authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-29 21:36:13+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-06-29 21:36:13+03:00
log8f2f0d8f0805f10523ced7e6a166ce81a097a54a
tree5459b95fc4a943bb805fc52f0b411ecf3f05ed39
parent98681b2da070755c29065d21d2ffb17be37d9619
parentb4ecc02471299a75af8a74ccac82a50d66be0425
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11962 from LordMZTE/fix/cast-or-call-parens

translate-c: fix cast or call macro with parenthesis

3 files changed, 45 insertions(+), 0 deletions(-)

src/translate_c.zig+24
...@@ -5109,7 +5109,30 @@ const PatternList = struct {...@@ -5109,7 +5109,30 @@ const PatternList = struct {
5109 [2][]const u8{ "Ull_SUFFIX(X) (X ## Ull)", "ULL_SUFFIX" },5109 [2][]const u8{ "Ull_SUFFIX(X) (X ## Ull)", "ULL_SUFFIX" },
5110 [2][]const u8{ "ULL_SUFFIX(X) (X ## ULL)", "ULL_SUFFIX" },5110 [2][]const u8{ "ULL_SUFFIX(X) (X ## ULL)", "ULL_SUFFIX" },
51115111
5112 [2][]const u8{ "f_SUFFIX(X) X ## f", "F_SUFFIX" },
5113 [2][]const u8{ "F_SUFFIX(X) X ## F", "F_SUFFIX" },
5114
5115 [2][]const u8{ "u_SUFFIX(X) X ## u", "U_SUFFIX" },
5116 [2][]const u8{ "U_SUFFIX(X) X ## U", "U_SUFFIX" },
5117
5118 [2][]const u8{ "l_SUFFIX(X) X ## l", "L_SUFFIX" },
5119 [2][]const u8{ "L_SUFFIX(X) X ## L", "L_SUFFIX" },
5120
5121 [2][]const u8{ "ul_SUFFIX(X) X ## ul", "UL_SUFFIX" },
5122 [2][]const u8{ "uL_SUFFIX(X) X ## uL", "UL_SUFFIX" },
5123 [2][]const u8{ "Ul_SUFFIX(X) X ## Ul", "UL_SUFFIX" },
5124 [2][]const u8{ "UL_SUFFIX(X) X ## UL", "UL_SUFFIX" },
5125
5126 [2][]const u8{ "ll_SUFFIX(X) X ## ll", "LL_SUFFIX" },
5127 [2][]const u8{ "LL_SUFFIX(X) X ## LL", "LL_SUFFIX" },
5128
5129 [2][]const u8{ "ull_SUFFIX(X) X ## ull", "ULL_SUFFIX" },
5130 [2][]const u8{ "uLL_SUFFIX(X) X ## uLL", "ULL_SUFFIX" },
5131 [2][]const u8{ "Ull_SUFFIX(X) X ## Ull", "ULL_SUFFIX" },
5132 [2][]const u8{ "ULL_SUFFIX(X) X ## ULL", "ULL_SUFFIX" },
5133
5112 [2][]const u8{ "CAST_OR_CALL(X, Y) (X)(Y)", "CAST_OR_CALL" },5134 [2][]const u8{ "CAST_OR_CALL(X, Y) (X)(Y)", "CAST_OR_CALL" },
5135 [2][]const u8{ "CAST_OR_CALL(X, Y) ((X)(Y))", "CAST_OR_CALL" },
51135136
5114 [2][]const u8{5137 [2][]const u8{
5115 \\wl_container_of(ptr, sample, member) \5138 \\wl_container_of(ptr, sample, member) \
...@@ -5303,6 +5326,7 @@ test "Macro matching" {...@@ -5303,6 +5326,7 @@ test "Macro matching" {
53035326
5304 try helper.checkMacro(allocator, pattern_list, "NO_MATCH(X, Y) (X + Y)", null);5327 try helper.checkMacro(allocator, pattern_list, "NO_MATCH(X, Y) (X + Y)", null);
5305 try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) (X)(Y)", "CAST_OR_CALL");5328 try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) (X)(Y)", "CAST_OR_CALL");
5329 try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) ((X)(Y))", "CAST_OR_CALL");
5306 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (void)(X)", "DISCARD");5330 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (void)(X)", "DISCARD");
5307 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((void)(X))", "DISCARD");5331 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((void)(X))", "DISCARD");
5308 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (const void)(X)", "DISCARD");5332 try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (const void)(X)", "DISCARD");
test/behavior/translate_c_macros.h+1
...@@ -37,5 +37,6 @@ union U {...@@ -37,5 +37,6 @@ union U {
37#define IGNORE_ME_10(x) (volatile const void)(x)37#define IGNORE_ME_10(x) (volatile const void)(x)
3838
39#define UNION_CAST(X) (union U)(X)39#define UNION_CAST(X) (union U)(X)
40#define CAST_OR_CALL_WITH_PARENS(type_or_fn, val) ((type_or_fn)(val))
4041
41#define NESTED_COMMA_OPERATOR (1, (2, 3))42#define NESTED_COMMA_OPERATOR (1, (2, 3))
test/behavior/translate_c_macros.zig+20
...@@ -70,6 +70,26 @@ test "casting to union with a macro" {...@@ -70,6 +70,26 @@ test "casting to union with a macro" {
70 try expect(d == casted.d);70 try expect(d == casted.d);
71}71}
7272
73test "casting or calling a value with a paren-surrounded macro" {
74 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
75 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
76 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
77 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
78 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
79
80 const l: c_long = 42;
81 const casted = h.CAST_OR_CALL_WITH_PARENS(c_int, l);
82 try expect(casted == @intCast(c_int, l));
83
84 const Helper = struct {
85 fn foo(n: c_int) !void {
86 try expect(n == 42);
87 }
88 };
89
90 try h.CAST_OR_CALL_WITH_PARENS(Helper.foo, 42);
91}
92
73test "nested comma operator" {93test "nested comma operator" {
74 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO94 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
75 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO95 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO