commit 8f2f0d8f0805f10523ced7e6a166ce81a097a54a (tree)
parent 98681b2da070755c29065d21d2ffb17be37d9619
Author: Veikka Tuominen <git@vexu.eu>
Date: Wed, 29 Jun 2022 21:36:13 +0300
Merge pull request #11962 from LordMZTE/fix/cast-or-call-parens
translate-c: fix cast or call macro with parenthesis
Diffstat:
3 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/src/translate_c.zig b/src/translate_c.zig
@@ -5109,7 +5109,30 @@ const PatternList = struct {
[2][]const u8{ "Ull_SUFFIX(X) (X ## Ull)", "ULL_SUFFIX" },
[2][]const u8{ "ULL_SUFFIX(X) (X ## ULL)", "ULL_SUFFIX" },
+ [2][]const u8{ "f_SUFFIX(X) X ## f", "F_SUFFIX" },
+ [2][]const u8{ "F_SUFFIX(X) X ## F", "F_SUFFIX" },
+
+ [2][]const u8{ "u_SUFFIX(X) X ## u", "U_SUFFIX" },
+ [2][]const u8{ "U_SUFFIX(X) X ## U", "U_SUFFIX" },
+
+ [2][]const u8{ "l_SUFFIX(X) X ## l", "L_SUFFIX" },
+ [2][]const u8{ "L_SUFFIX(X) X ## L", "L_SUFFIX" },
+
+ [2][]const u8{ "ul_SUFFIX(X) X ## ul", "UL_SUFFIX" },
+ [2][]const u8{ "uL_SUFFIX(X) X ## uL", "UL_SUFFIX" },
+ [2][]const u8{ "Ul_SUFFIX(X) X ## Ul", "UL_SUFFIX" },
+ [2][]const u8{ "UL_SUFFIX(X) X ## UL", "UL_SUFFIX" },
+
+ [2][]const u8{ "ll_SUFFIX(X) X ## ll", "LL_SUFFIX" },
+ [2][]const u8{ "LL_SUFFIX(X) X ## LL", "LL_SUFFIX" },
+
+ [2][]const u8{ "ull_SUFFIX(X) X ## ull", "ULL_SUFFIX" },
+ [2][]const u8{ "uLL_SUFFIX(X) X ## uLL", "ULL_SUFFIX" },
+ [2][]const u8{ "Ull_SUFFIX(X) X ## Ull", "ULL_SUFFIX" },
+ [2][]const u8{ "ULL_SUFFIX(X) X ## ULL", "ULL_SUFFIX" },
+
[2][]const u8{ "CAST_OR_CALL(X, Y) (X)(Y)", "CAST_OR_CALL" },
+ [2][]const u8{ "CAST_OR_CALL(X, Y) ((X)(Y))", "CAST_OR_CALL" },
[2][]const u8{
\\wl_container_of(ptr, sample, member) \
@@ -5303,6 +5326,7 @@ test "Macro matching" {
try helper.checkMacro(allocator, pattern_list, "NO_MATCH(X, Y) (X + Y)", null);
try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) (X)(Y)", "CAST_OR_CALL");
+ try helper.checkMacro(allocator, pattern_list, "CAST_OR_CALL(X, Y) ((X)(Y))", "CAST_OR_CALL");
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (void)(X)", "DISCARD");
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) ((void)(X))", "DISCARD");
try helper.checkMacro(allocator, pattern_list, "IGNORE_ME(X) (const void)(X)", "DISCARD");
diff --git a/test/behavior/translate_c_macros.h b/test/behavior/translate_c_macros.h
@@ -37,5 +37,6 @@ union U {
#define IGNORE_ME_10(x) (volatile const void)(x)
#define UNION_CAST(X) (union U)(X)
+#define CAST_OR_CALL_WITH_PARENS(type_or_fn, val) ((type_or_fn)(val))
#define NESTED_COMMA_OPERATOR (1, (2, 3))
diff --git a/test/behavior/translate_c_macros.zig b/test/behavior/translate_c_macros.zig
@@ -70,6 +70,26 @@ test "casting to union with a macro" {
try expect(d == casted.d);
}
+test "casting or calling a value with a paren-surrounded macro" {
+ if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
+ if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
+
+ const l: c_long = 42;
+ const casted = h.CAST_OR_CALL_WITH_PARENS(c_int, l);
+ try expect(casted == @intCast(c_int, l));
+
+ const Helper = struct {
+ fn foo(n: c_int) !void {
+ try expect(n == 42);
+ }
+ };
+
+ try h.CAST_OR_CALL_WITH_PARENS(Helper.foo, 42);
+}
+
test "nested comma operator" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO