Skip to content

Commit

Permalink
translate-c: support macro with 'assert(false && "error message")'
Browse files Browse the repository at this point in the history
closes ziglang#14642 with modified fix suggested by Vexu in
ziglang#14642 (comment)
  • Loading branch information
travisstaloch committed Apr 11, 2024
1 parent f7a76bd commit 3d16520
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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() });
}
Expand Down
8 changes: 8 additions & 0 deletions test/cases/translate_c/assert_with_strlit.c
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit 3d16520

Please sign in to comment.