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(...@@ -2086,6 +2086,11 @@ fn finishBoolExpr(
2086 }2086 }
2087 },2087 },
2088 .Pointer => {2088 .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 }
2089 // node != null2094 // node != null
2090 return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.null_literal.init() });2095 return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.null_literal.init() });
2091 },2096 },
...@@ -5794,10 +5799,11 @@ fn macroIntToBool(c: *Context, node: Node) !Node {...@@ -5794,10 +5799,11 @@ fn macroIntToBool(c: *Context, node: Node) !Node {
5794 return node;5799 return node;
5795 }5800 }
5796 if (node.tag() == .string_literal) {5801 if (node.tag() == .string_literal) {
5802 // @intFromPtr(node) != 0
5797 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, node);5803 const int_from_ptr = try Tag.int_from_ptr.create(c.arena, node);
5798 return Tag.not_equal.create(c.arena, .{ .lhs = int_from_ptr, .rhs = Tag.zero_literal.init() });5804 return Tag.not_equal.create(c.arena, .{ .lhs = int_from_ptr, .rhs = Tag.zero_literal.init() });
5799 }5805 }
58005806 // node != 0
5801 return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() });5807 return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() });
5802}5808}
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// }