From 3d1652070acd01b7c871615702916425cd292a8a Mon Sep 17 00:00:00 2001 From: travisstaloch <1562827+travisstaloch@users.noreply.github.com> Date: Thu, 11 Apr 2024 07:39:47 -0700 Subject: [PATCH] translate-c: support macro with 'assert(false && "error message")' closes #14642 with modified fix suggested by Vexu in https://github.com/ziglang/zig/issues/14642#issuecomment-1775476042 --- src/translate_c.zig | 4 ++++ test/cases/translate_c/assert_with_strlit.c | 8 ++++++++ 2 files changed, 12 insertions(+) create mode 100644 test/cases/translate_c/assert_with_strlit.c diff --git a/src/translate_c.zig b/src/translate_c.zig index 16c2060163a1..2e9213a8c737 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -5793,6 +5793,10 @@ fn macroIntToBool(c: *Context, node: Node) !Node { if (isBoolRes(node)) { return node; } + if (node.tag() == .string_literal) { + const int_from_ptr = try Tag.int_from_ptr.create(c.arena, node); + return Tag.not_equal.create(c.arena, .{ .lhs = int_from_ptr, .rhs = Tag.zero_literal.init() }); + } return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() }); } diff --git a/test/cases/translate_c/assert_with_strlit.c b/test/cases/translate_c/assert_with_strlit.c new file mode 100644 index 000000000000..445428bcf143 --- /dev/null +++ b/test/cases/translate_c/assert_with_strlit.c @@ -0,0 +1,8 @@ + +void assert(int x) {} +#define FOO assert(0 && "error message") + +// translate-c +// c_frontend=clang +// +// pub const FOO = assert((@as(c_int, 0) != 0) and (@intFromPtr("error message") != 0));