mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
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
This commit is contained in:
parent
10ff81c264
commit
05d9755766
@ -2086,6 +2086,11 @@ fn finishBoolExpr(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
.Pointer => {
|
.Pointer => {
|
||||||
|
if (node.tag() == .string_literal) {
|
||||||
|
// @intFromPtr(node) != 0
|
||||||
|
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() });
|
||||||
|
}
|
||||||
// node != null
|
// node != null
|
||||||
return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.null_literal.init() });
|
return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.null_literal.init() });
|
||||||
},
|
},
|
||||||
@ -5794,10 +5799,11 @@ fn macroIntToBool(c: *Context, node: Node) !Node {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
if (node.tag() == .string_literal) {
|
if (node.tag() == .string_literal) {
|
||||||
|
// @intFromPtr(node) != 0
|
||||||
const int_from_ptr = try Tag.int_from_ptr.create(c.arena, node);
|
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 = int_from_ptr, .rhs = Tag.zero_literal.init() });
|
||||||
}
|
}
|
||||||
|
// node != 0
|
||||||
return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() });
|
return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
test/cases/translate_c/strlit_as_bool.c
Normal file
8
test/cases/translate_c/strlit_as_bool.c
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
void foo() { if(0 && "error message") {} }
|
||||||
|
|
||||||
|
// translate-c
|
||||||
|
// c_frontend=clang
|
||||||
|
//
|
||||||
|
// pub export fn foo() void {
|
||||||
|
// if (false and (@intFromPtr("error message") != 0)) {}
|
||||||
|
// }
|
||||||
Loading…
x
Reference in New Issue
Block a user