authorgravatar for twostepted@gmail.comTravis Staloch <twostepted@gmail.com> 2024-04-12 03:10:42-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-04-12 10:10:42+00:00
log05d9755766e45e454a16440bfc9f98abef992247
tree8df2c9139d92760d80944e278db5dae20fc2eb3f
parent10ff81c26457ff02b7a7ed580aaf773297ad8025
signaturebadge-check Signed by PGP key B5690EEEBB952194

translate-c: allow str literals in bool expressions

this is a follow up to #19610 with fix suggested by Vexu in https://github.com/ziglang/zig/issues/14642#issuecomment-2048999384

2 files changed, 15 insertions(+), 1 deletions(-)

src/translate_c.zig+7-1
......@@ -2086,6 +2086,11 @@ fn finishBoolExpr(
20862086 }
20872087 },
20882088 .Pointer => {
2089 if (node.tag() == .string_literal) {
2090 // @intFromPtr(node) != 0
2091 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, node);
2092 return Tag.not_equal.create(c.arena, .{ .lhs = int_from_ptr, .rhs = Tag.zero_literal.init() });
2093 }
20892094 // node != null
20902095 return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.null_literal.init() });
20912096 },
......@@ -5794,10 +5799,11 @@ fn macroIntToBool(c: *Context, node: Node) !Node {
57945799 return node;
57955800 }
57965801 if (node.tag() == .string_literal) {
5802 // @intFromPtr(node) != 0
57975803 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, node);
57985804 return Tag.not_equal.create(c.arena, .{ .lhs = int_from_ptr, .rhs = Tag.zero_literal.init() });
57995805 }
5800
5806 // node != 0
58015807 return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() });
58025808}
58035809
test/cases/translate_c/strlit_as_bool.c created+8
......@@ -0,0 +1,8 @@
1void foo() { if(0 && "error message") {} }
2
3// translate-c
4// c_frontend=clang
5//
6// pub export fn foo() void {
7// if (false and (@intFromPtr("error message") != 0)) {}
8// }