1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 23:29:39 +00:00

Merge pull request #1876 from bbbradsmith/constant_overflow_warning

Emit warning for signed integer constant overflow
This commit is contained in:
Bob Andrews 2022-10-16 22:15:22 +02:00 committed by GitHub
commit 89031594eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -193,12 +193,15 @@ static unsigned typeadjust (ExprDesc* lhs, const ExprDesc* rhs, int NoPush)
void LimitExprValue (ExprDesc* Expr)
void LimitExprValue (ExprDesc* Expr, int WarnOverflow)
/* Limit the constant value of the expression to the range of its type */
{
switch (GetUnderlyingTypeCode (Expr->Type)) {
case T_INT:
case T_SHORT:
if (WarnOverflow && ((Expr->IVal < -0x8000) || (Expr->IVal > 0x7FFF))) {
Warning ("Signed integer constant overflow");
}
Expr->IVal = (int16_t)Expr->IVal;
break;
@ -218,6 +221,9 @@ void LimitExprValue (ExprDesc* Expr)
break;
case T_SCHAR:
if (WarnOverflow && ((Expr->IVal < -0x80) || (Expr->IVal > 0x7F))) {
Warning ("Signed character constant overflow");
}
Expr->IVal = (int8_t)Expr->IVal;
break;
@ -1822,7 +1828,7 @@ static void UnaryOp (ExprDesc* Expr)
Expr->Type = IntPromotion (Expr->Type);
/* Limit the calculated value to the range of its type */
LimitExprValue (Expr);
LimitExprValue (Expr, 1);
} else {
unsigned Flags;
@ -2162,7 +2168,7 @@ static void hie_internal (const GenDesc* Ops, /* List of generators */
}
/* Limit the calculated value to the range of its type */
LimitExprValue (Expr);
LimitExprValue (Expr, 1);
} else if (lconst && (Gen->Flags & GEN_COMM) && !rconst) {
/* If the LHS constant is an int that fits into an unsigned char, change the
@ -2789,7 +2795,7 @@ static void parseadd (ExprDesc* Expr, int DoArrayRef)
Expr->Type = rhst;
} else {
/* Limit the calculated value to the range of its type */
LimitExprValue (Expr);
LimitExprValue (Expr, 1);
}
/* The result is always an rvalue */
@ -3260,7 +3266,7 @@ static void parsesub (ExprDesc* Expr)
/* Just adjust the result type */
Expr->Type = ArithmeticConvert (Expr->Type, Expr2.Type);
/* And limit the calculated value to the range of it */
LimitExprValue (Expr);
LimitExprValue (Expr, 1);
}
/* The result is always an rvalue */
ED_MarkExprAsRVal (Expr);

View File

@ -59,7 +59,7 @@ void MarkedExprWithCheck (void (*Func) (ExprDesc*), ExprDesc* Expr);
** generated code.
*/
void LimitExprValue (ExprDesc* Expr);
void LimitExprValue (ExprDesc* Expr, int WarnOverflow);
/* Limit the constant value of the expression to the range of its type */
void PushAddr (const ExprDesc* Expr);

View File

@ -173,7 +173,7 @@ void ShiftExpr (struct ExprDesc* Expr)
}
/* Limit the calculated value to the range of its type */
LimitExprValue (Expr);
LimitExprValue (Expr, 1);
}
/* Result is already got, remove the generated code */