zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit da506aaf6ea731c72daaac649dba788407db0d6c (tree)
parent 50457482b16dc402ad0eff9f7990258257e2dd62
Author: february cozzocrea <91439207+f-cozzocrea@users.noreply.github.com>
Date:   Tue, 16 Jan 2024 08:12:05 -0800

translate-c: Explicit cast bool from float fix


Diffstat:
Msrc/translate_c.zig | 8++++++++
Atest/cases/run_translated_c/explicit_cast_bool_from_float.c | 10++++++++++
2 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/src/translate_c.zig b/src/translate_c.zig @@ -2380,6 +2380,14 @@ fn transCCast( }); } if (cIsFloating(src_type) and !cIsFloating(dst_type)) { + // bool expression: floating val != 0 + if (qualTypeIsBoolean(dst_type)) { + return Tag.not_equal.create(c.arena, .{ + .lhs = expr, + .rhs = Tag.zero_literal.init(), + }); + } + // @as(dest_type, @intFromFloat(val)) return Tag.as.create(c.arena, .{ .lhs = dst_node, diff --git a/test/cases/run_translated_c/explicit_cast_bool_from_float.c b/test/cases/run_translated_c/explicit_cast_bool_from_float.c @@ -0,0 +1,10 @@ +#include <stdbool.h> + +int main() { + float f = 2.0f; + bool b = (bool) f; + return 0; +} + +// run-translated-c +// c_frontends=aro,clang