From 7b32062775cca303d8a2fc72b17545b0246f9f9a Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Wed, 29 Jun 2022 16:03:29 +0200 Subject: [PATCH 1/2] translate-c: fix cast or call macro with parenthesis --- src/translate_c.zig | 2 ++ test/behavior/translate_c_macros.h | 1 + test/behavior/translate_c_macros.zig | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/translate_c.zig b/src/translate_c.zig index 3bbe1e6f46..84fb4ba99e 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -5110,6 +5110,7 @@ const PatternList = struct { [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 +5304,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 index 490ca35726..547194d35a 100644 --- 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 index e44996b990..96676b5b78 100644 --- 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 From b4ecc02471299a75af8a74ccac82a50d66be0425 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Wed, 29 Jun 2022 16:59:18 +0200 Subject: [PATCH 2/2] translate-c: fix token pasting operator without parens --- src/translate_c.zig | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/translate_c.zig b/src/translate_c.zig index 84fb4ba99e..306f5d3ea1 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -5109,6 +5109,28 @@ 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" },