Skip to content

Commit 05d9755

Browse files
translate-c: allow str literals in bool expressions
this is a follow up to #19610 with fix suggested by Vexu in #14642 (comment)
1 parent 10ff81c commit 05d9755

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/translate_c.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
void 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+
// }

0 commit comments

Comments
 (0)