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(
38073807 const rhs_qt = getExprQualType(c, rhs);
38083808 const is_signed = cIsSignedInteger(lhs_qt);
38093809 const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt);
3810 const requires_int_cast = blk: {
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 };
3810 const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_op_signed;
38153811
38163812 if (used == .unused) {
38173813 // common case
......@@ -3822,7 +3818,7 @@ fn transCreateCompoundAssign(
38223818 if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node);
38233819
38243820 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);
38263822 const operands = .{ .lhs = lhs_node, .rhs = rhs_node };
38273823 const builtin = if (is_mod)
38283824 try Tag.signed_remainder.create(c.arena, operands)
......@@ -3834,7 +3830,7 @@ fn transCreateCompoundAssign(
38343830
38353831 if (is_shift) {
38363832 rhs_node = try Tag.int_cast.create(c.arena, rhs_node);
3837 } else if (requires_int_cast) {
3833 } else if (requires_cast) {
38383834 rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node);
38393835 }
38403836 return transCreateNodeInfixOp(c, op, lhs_node, rhs_node, .used);
......@@ -3861,7 +3857,7 @@ fn transCreateCompoundAssign(
38613857 var rhs_node = try transExpr(c, &block_scope.base, rhs, .used);
38623858 if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node);
38633859 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);
38653861 const operands = .{ .lhs = ref_node, .rhs = rhs_node };
38663862 const builtin = if (is_mod)
38673863 try Tag.signed_remainder.create(c.arena, operands)
......@@ -3873,7 +3869,7 @@ fn transCreateCompoundAssign(
38733869 } else {
38743870 if (is_shift) {
38753871 rhs_node = try Tag.int_cast.create(c.arena, rhs_node);
3876 } else if (requires_int_cast) {
3872 } else if (requires_cast) {
38773873 rhs_node = try transCCast(c, &block_scope.base, loc, lhs_qt, rhs_qt, rhs_node);
38783874 }
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