translate-c: fix translation of "ptr += uint"

The right-hand side was incorrectly cast to a pointer, since only signed
ints were being interpreted correctly as pointer arithmetic.

Fixes #20285.
This commit is contained in:
Devin J. Pohly 2024-06-13 23:49:29 -05:00 committed by Isaac Freund
parent 7829be6ee0
commit ffb1a6d9a7
2 changed files with 23 additions and 1 deletions

View File

@ -3824,8 +3824,9 @@ fn transCreateCompoundAssign(
const lhs_qt = getExprQualType(c, lhs); const lhs_qt = getExprQualType(c, lhs);
const rhs_qt = getExprQualType(c, rhs); const rhs_qt = getExprQualType(c, rhs);
const is_signed = cIsSignedInteger(lhs_qt); const is_signed = cIsSignedInteger(lhs_qt);
const is_ptr_arithmetic = qualTypeIsPtr(lhs_qt) and cIsInteger(rhs_qt);
const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt); const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt);
const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_op_signed; const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_arithmetic;
if (used == .unused) { if (used == .unused) {
// common case // common case

View File

@ -0,0 +1,21 @@
int main() {
const char *s = "forgreatjustice";
unsigned int add = 1;
s += add;
if (*s != 'o') return 1;
s += 1UL;
if (*s != 'r') return 2;
const char *s2 = (s += add);
if (*s2 != 'g') return 3;
s2 -= add;
if (*s2 != 'r') return 4;
return 0;
}
// run-translated-c
// c_frontend=clang