Commit da506aaf6e

february cozzocrea <91439207+f-cozzocrea@users.noreply.github.com>
2024-01-16 17:12:05
translate-c: Explicit cast bool from float fix
1 parent 5045748
Changed files (2)
src
test
cases
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,
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