Merge pull request #2084 from bbbradsmith/numerical_constant_errors-int

Numerical constant errors and improvements (integer)
This commit is contained in:
Bob Andrews
2023-05-06 21:36:59 +02:00
committed by GitHub
9 changed files with 77 additions and 7 deletions

View File

@@ -79,6 +79,7 @@ IntStack WarnUnusedLabel = INTSTACK(1); /* - unused labels */
IntStack WarnUnusedParam = INTSTACK(1); /* - unused parameters */
IntStack WarnUnusedVar = INTSTACK(1); /* - unused variables */
IntStack WarnUnusedFunc = INTSTACK(1); /* - unused functions */
IntStack WarnConstOverflow = INTSTACK(0); /* - overflow conversion of numerical constants */
/* Map the name of a warning to the intstack that holds its state */
typedef struct WarnMapEntry WarnMapEntry;
@@ -102,6 +103,7 @@ static WarnMapEntry WarnMap[] = {
{ &WarnUnusedLabel, "unused-label" },
{ &WarnUnusedParam, "unused-param" },
{ &WarnUnusedVar, "unused-var" },
{ &WarnConstOverflow, "const-overflow" },
};
Collection DiagnosticStrBufs;

View File

@@ -76,6 +76,7 @@ extern IntStack WarnUnusedLabel; /* - unused labels */
extern IntStack WarnUnusedParam; /* - unused parameters */
extern IntStack WarnUnusedVar; /* - unused variables */
extern IntStack WarnUnusedFunc; /* - unused functions */
extern IntStack WarnConstOverflow; /* - overflow conversion of numerical constants */
/* Forward */
struct StrBuf;

View File

@@ -39,6 +39,8 @@
#include <errno.h>
#include <ctype.h>
#include <math.h>
#include <inttypes.h>
#include <limits.h>
/* common */
#include "chartype.h"
@@ -151,6 +153,11 @@ static const struct Keyword {
#define IT_ULONG 0x08
/* Internal type for numeric constant scanning.
** Size must be explicit for cross-platform uniformity.
*/
typedef uint32_t scan_t;
/*****************************************************************************/
/* code */
@@ -521,7 +528,8 @@ static void NumericConst (void)
int IsFloat;
char C;
unsigned DigitVal;
unsigned long IVal; /* Value */
scan_t IVal; /* Scanned value. */
int Overflow;
/* Get the pp-number first, then parse on it */
CopyPPNumber (&Src);
@@ -575,6 +583,7 @@ static void NumericConst (void)
/* Since we now know the correct base, convert the input into a number */
SB_SetIndex (&Src, Index);
IVal = 0;
Overflow = 0;
while ((C = SB_Peek (&Src)) != '\0' && (Base <= 10 ? IsDigit (C) : IsXDigit (C))) {
DigitVal = HexVal (C);
if (DigitVal >= Base) {
@@ -582,9 +591,17 @@ static void NumericConst (void)
SB_Clear (&Src);
break;
}
IVal = (IVal * Base) + DigitVal;
/* Test result of adding digit for overflow. */
if (((scan_t)(IVal * Base + DigitVal) / Base) != IVal) {
Overflow = 1;
}
IVal = IVal * Base + DigitVal;
SB_Skip (&Src);
}
if (Overflow) {
Error ("Numerical constant \"%s\" too large for internal %d-bit representation",
SB_GetConstBuf (&Src), (int)(sizeof(IVal)*CHAR_BIT));
}
/* Distinguish between integer and floating point constants */
if (!IsFloat) {

View File

@@ -55,7 +55,7 @@
static void DoConversion (ExprDesc* Expr, const Type* NewType)
static void DoConversion (ExprDesc* Expr, const Type* NewType, int Explicit)
/* Emit code to convert the given expression to a new type. */
{
const Type* OldType;
@@ -128,6 +128,7 @@ static void DoConversion (ExprDesc* Expr, const Type* NewType)
** internally already represented by a long.
*/
if (NewBits <= OldBits) {
long OldVal = Expr->IVal;
/* Cut the value to the new size */
Expr->IVal &= (0xFFFFFFFFUL >> (32 - NewBits));
@@ -139,6 +140,10 @@ static void DoConversion (ExprDesc* Expr, const Type* NewType)
Expr->IVal |= shl_l (~0UL, NewBits);
}
}
if ((OldVal != Expr->IVal) && IS_Get (&WarnConstOverflow) && !Explicit) {
Warning ("Implicit conversion of constant overflows %d-bit destination", NewBits);
}
}
/* Do the integer constant <-> absolute address conversion if necessary */
@@ -283,7 +288,7 @@ void TypeConversion (ExprDesc* Expr, const Type* NewType)
/* Both types must be complete */
if (!IsIncompleteESUType (NewType) && !IsIncompleteESUType (Expr->Type)) {
/* Do the actual conversion */
DoConversion (Expr, NewType);
DoConversion (Expr, NewType, 0);
} else {
/* We should have already generated error elsewhere so that we
** could just silently fail here to avoid excess errors, but to
@@ -330,7 +335,7 @@ void TypeCast (ExprDesc* Expr)
ReplaceType (Expr, NewType);
} else if (IsCastType (Expr->Type)) {
/* Convert the value. The result has always the new type */
DoConversion (Expr, NewType);
DoConversion (Expr, NewType, 1);
} else {
TypeCompatibilityDiagnostic (NewType, Expr->Type, 1,
"Cast to incompatible type '%s' from '%s'");