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

translate-c: fix cast or call macro with parenthesis
This commit is contained in:
Veikka Tuominen 2022-06-29 21:36:13 +03:00 committed by GitHub
commit 8f2f0d8f08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 0 deletions

View File

@ -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");

View File

@ -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))

View File

@ -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