authorgravatar for february.cozzocrea@gmail.comfebruary cozzocrea <february.cozzocrea@gmail.com> 2024-01-16 07:57:31-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-16 17:57:31+02:00
log50457482b16dc402ad0eff9f7990258257e2dd62
treef566cd63f37e1d76c365bf8e789fc37b409d3e73
parent195eeed2d8da8b2c8037ff234649c3feda6cac3d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

translate-c: Fix for compound assign implicit cast error


2 files changed, 25 insertions(+), 9 deletions(-)

src/translate_c.zig+5-9
...@@ -3807,11 +3807,7 @@ fn transCreateCompoundAssign(...@@ -3807,11 +3807,7 @@ fn transCreateCompoundAssign(
3807 const rhs_qt = getExprQualType(c, rhs);3807 const rhs_qt = getExprQualType(c, rhs);
3808 const is_signed = cIsSignedInteger(lhs_qt);3808 const is_signed = cIsSignedInteger(lhs_qt);
3809 const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt);3809 const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt);
3810 const requires_int_cast = blk: {3810 const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_op_signed;
3811 const are_integers = cIsInteger(lhs_qt) and cIsInteger(rhs_qt);
3812 const are_same_sign = cIsSignedInteger(lhs_qt) == cIsSignedInteger(rhs_qt);
3813 break :blk are_integers and !(are_same_sign and cIntTypeCmp(lhs_qt, rhs_qt) == .eq);
3814 };
38153811
3816 if (used == .unused) {3812 if (used == .unused) {
3817 // common case3813 // common case
...@@ -3822,7 +3818,7 @@ fn transCreateCompoundAssign(...@@ -3822,7 +3818,7 @@ fn transCreateCompoundAssign(
3822 if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node);3818 if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node);
38233819
3824 if ((is_mod or is_div) and is_signed) {3820 if ((is_mod or is_div) and is_signed) {
3825 if (requires_int_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);3821 if (requires_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);
3826 const operands = .{ .lhs = lhs_node, .rhs = rhs_node };3822 const operands = .{ .lhs = lhs_node, .rhs = rhs_node };
3827 const builtin = if (is_mod)3823 const builtin = if (is_mod)
3828 try Tag.signed_remainder.create(c.arena, operands)3824 try Tag.signed_remainder.create(c.arena, operands)
...@@ -3834,7 +3830,7 @@ fn transCreateCompoundAssign(...@@ -3834,7 +3830,7 @@ fn transCreateCompoundAssign(
38343830
3835 if (is_shift) {3831 if (is_shift) {
3836 rhs_node = try Tag.int_cast.create(c.arena, rhs_node);3832 rhs_node = try Tag.int_cast.create(c.arena, rhs_node);
3837 } else if (requires_int_cast) {3833 } else if (requires_cast) {
3838 rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);3834 rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);
3839 }3835 }
3840 return transCreateNodeInfixOp(c, op, lhs_node, rhs_node, .used);3836 return transCreateNodeInfixOp(c, op, lhs_node, rhs_node, .used);
...@@ -3861,7 +3857,7 @@ fn transCreateCompoundAssign(...@@ -3861,7 +3857,7 @@ fn transCreateCompoundAssign(
3861 var rhs_node = try transExpr(c, &block_scope.base, rhs, .used);3857 var rhs_node = try transExpr(c, &block_scope.base, rhs, .used);
3862 if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node);3858 if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node);
3863 if ((is_mod or is_div) and is_signed) {3859 if ((is_mod or is_div) and is_signed) {
3864 if (requires_int_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);3860 if (requires_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);
3865 const operands = .{ .lhs = ref_node, .rhs = rhs_node };3861 const operands = .{ .lhs = ref_node, .rhs = rhs_node };
3866 const builtin = if (is_mod)3862 const builtin = if (is_mod)
3867 try Tag.signed_remainder.create(c.arena, operands)3863 try Tag.signed_remainder.create(c.arena, operands)
...@@ -3873,7 +3869,7 @@ fn transCreateCompoundAssign(...@@ -3873,7 +3869,7 @@ fn transCreateCompoundAssign(
3873 } else {3869 } else {
3874 if (is_shift) {3870 if (is_shift) {
3875 rhs_node = try Tag.int_cast.create(c.arena, rhs_node);3871 rhs_node = try Tag.int_cast.create(c.arena, rhs_node);
3876 } else if (requires_int_cast) {3872 } else if (requires_cast) {
3877 rhs_node = try transCCast(c, &block_scope.base, loc, lhs_qt, rhs_qt, rhs_node);3873 rhs_node = try transCCast(c, &block_scope.base, loc, lhs_qt, rhs_qt, rhs_node);
3878 }3874 }
38793875
test/cases/run_translated_c/compound_assignments_with_implicit_casts.c created+20
...@@ -0,0 +1,20 @@
1int main() {
2 int i = 2;
3 float f = 3.2f;
4
5 i += 1.7;
6 if (i != 3) return 1;
7 i += f;
8 if (i != 6) return 2;
9
10
11 f += 2UL;
12 if (f <= 5.1999 || f >= 5.2001) return 3;
13 f += i;
14 if (f <= 11.1999 || f >= 11.2001) return 4;
15
16 return 0;
17}
18
19// run-translated-c
20// c_frontends=aro,clang