Merge pull request #1876 from bbbradsmith/constant_overflow_warning
Emit warning for signed integer constant overflow
This commit is contained in:
@@ -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 */
|
/* Limit the constant value of the expression to the range of its type */
|
||||||
{
|
{
|
||||||
switch (GetUnderlyingTypeCode (Expr->Type)) {
|
switch (GetUnderlyingTypeCode (Expr->Type)) {
|
||||||
case T_INT:
|
case T_INT:
|
||||||
case T_SHORT:
|
case T_SHORT:
|
||||||
|
if (WarnOverflow && ((Expr->IVal < -0x8000) || (Expr->IVal > 0x7FFF))) {
|
||||||
|
Warning ("Signed integer constant overflow");
|
||||||
|
}
|
||||||
Expr->IVal = (int16_t)Expr->IVal;
|
Expr->IVal = (int16_t)Expr->IVal;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -218,6 +221,9 @@ void LimitExprValue (ExprDesc* Expr)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case T_SCHAR:
|
case T_SCHAR:
|
||||||
|
if (WarnOverflow && ((Expr->IVal < -0x80) || (Expr->IVal > 0x7F))) {
|
||||||
|
Warning ("Signed character constant overflow");
|
||||||
|
}
|
||||||
Expr->IVal = (int8_t)Expr->IVal;
|
Expr->IVal = (int8_t)Expr->IVal;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -1822,7 +1828,7 @@ static void UnaryOp (ExprDesc* Expr)
|
|||||||
Expr->Type = IntPromotion (Expr->Type);
|
Expr->Type = IntPromotion (Expr->Type);
|
||||||
|
|
||||||
/* Limit the calculated value to the range of its type */
|
/* Limit the calculated value to the range of its type */
|
||||||
LimitExprValue (Expr);
|
LimitExprValue (Expr, 1);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
unsigned Flags;
|
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 */
|
/* Limit the calculated value to the range of its type */
|
||||||
LimitExprValue (Expr);
|
LimitExprValue (Expr, 1);
|
||||||
|
|
||||||
} else if (lconst && (Gen->Flags & GEN_COMM) && !rconst) {
|
} else if (lconst && (Gen->Flags & GEN_COMM) && !rconst) {
|
||||||
/* If the LHS constant is an int that fits into an unsigned char, change the
|
/* 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;
|
Expr->Type = rhst;
|
||||||
} else {
|
} else {
|
||||||
/* Limit the calculated value to the range of its type */
|
/* Limit the calculated value to the range of its type */
|
||||||
LimitExprValue (Expr);
|
LimitExprValue (Expr, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The result is always an rvalue */
|
/* The result is always an rvalue */
|
||||||
@@ -3260,7 +3266,7 @@ static void parsesub (ExprDesc* Expr)
|
|||||||
/* Just adjust the result type */
|
/* Just adjust the result type */
|
||||||
Expr->Type = ArithmeticConvert (Expr->Type, Expr2.Type);
|
Expr->Type = ArithmeticConvert (Expr->Type, Expr2.Type);
|
||||||
/* And limit the calculated value to the range of it */
|
/* And limit the calculated value to the range of it */
|
||||||
LimitExprValue (Expr);
|
LimitExprValue (Expr, 1);
|
||||||
}
|
}
|
||||||
/* The result is always an rvalue */
|
/* The result is always an rvalue */
|
||||||
ED_MarkExprAsRVal (Expr);
|
ED_MarkExprAsRVal (Expr);
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ void MarkedExprWithCheck (void (*Func) (ExprDesc*), ExprDesc* Expr);
|
|||||||
** generated code.
|
** 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 */
|
/* Limit the constant value of the expression to the range of its type */
|
||||||
|
|
||||||
void PushAddr (const ExprDesc* Expr);
|
void PushAddr (const ExprDesc* Expr);
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ void ShiftExpr (struct ExprDesc* Expr)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Limit the calculated value to the range of its type */
|
/* Limit the calculated value to the range of its type */
|
||||||
LimitExprValue (Expr);
|
LimitExprValue (Expr, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Result is already got, remove the generated code */
|
/* Result is already got, remove the generated code */
|
||||||
|
|||||||
Reference in New Issue
Block a user