Optional warning for implicit constant conversion overflow
This commit is contained in:
@@ -754,6 +754,8 @@ Here is a description of all the command line options:
|
|||||||
Warn about unused function parameters.
|
Warn about unused function parameters.
|
||||||
<tag><tt/unused-var/</tag>
|
<tag><tt/unused-var/</tag>
|
||||||
Warn about unused variables.
|
Warn about unused variables.
|
||||||
|
<tag><tt/const-overflow/</tag>
|
||||||
|
Warn if numerical constant conversion implies overflow. (Disabled by default.)
|
||||||
</descrip>
|
</descrip>
|
||||||
|
|
||||||
The full list of available warning names can be retrieved by using the
|
The full list of available warning names can be retrieved by using the
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ IntStack WarnUnusedLabel = INTSTACK(1); /* - unused labels */
|
|||||||
IntStack WarnUnusedParam = INTSTACK(1); /* - unused parameters */
|
IntStack WarnUnusedParam = INTSTACK(1); /* - unused parameters */
|
||||||
IntStack WarnUnusedVar = INTSTACK(1); /* - unused variables */
|
IntStack WarnUnusedVar = INTSTACK(1); /* - unused variables */
|
||||||
IntStack WarnUnusedFunc = INTSTACK(1); /* - unused functions */
|
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 */
|
/* Map the name of a warning to the intstack that holds its state */
|
||||||
typedef struct WarnMapEntry WarnMapEntry;
|
typedef struct WarnMapEntry WarnMapEntry;
|
||||||
@@ -102,6 +103,7 @@ static WarnMapEntry WarnMap[] = {
|
|||||||
{ &WarnUnusedLabel, "unused-label" },
|
{ &WarnUnusedLabel, "unused-label" },
|
||||||
{ &WarnUnusedParam, "unused-param" },
|
{ &WarnUnusedParam, "unused-param" },
|
||||||
{ &WarnUnusedVar, "unused-var" },
|
{ &WarnUnusedVar, "unused-var" },
|
||||||
|
{ &WarnConstOverflow, "const-overflow" },
|
||||||
};
|
};
|
||||||
|
|
||||||
Collection DiagnosticStrBufs;
|
Collection DiagnosticStrBufs;
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ extern IntStack WarnUnusedLabel; /* - unused labels */
|
|||||||
extern IntStack WarnUnusedParam; /* - unused parameters */
|
extern IntStack WarnUnusedParam; /* - unused parameters */
|
||||||
extern IntStack WarnUnusedVar; /* - unused variables */
|
extern IntStack WarnUnusedVar; /* - unused variables */
|
||||||
extern IntStack WarnUnusedFunc; /* - unused functions */
|
extern IntStack WarnUnusedFunc; /* - unused functions */
|
||||||
|
extern IntStack WarnConstOverflow; /* - overflow conversion of numerical constants */
|
||||||
|
|
||||||
/* Forward */
|
/* Forward */
|
||||||
struct StrBuf;
|
struct StrBuf;
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ static void DoConversion (ExprDesc* Expr, const Type* NewType)
|
|||||||
** internally already represented by a long.
|
** internally already represented by a long.
|
||||||
*/
|
*/
|
||||||
if (NewBits <= OldBits) {
|
if (NewBits <= OldBits) {
|
||||||
|
unsigned long OldVal = Expr->IVal;
|
||||||
|
|
||||||
/* Cut the value to the new size */
|
/* Cut the value to the new size */
|
||||||
Expr->IVal &= (0xFFFFFFFFUL >> (32 - NewBits));
|
Expr->IVal &= (0xFFFFFFFFUL >> (32 - NewBits));
|
||||||
@@ -139,6 +140,10 @@ static void DoConversion (ExprDesc* Expr, const Type* NewType)
|
|||||||
Expr->IVal |= shl_l (~0UL, NewBits);
|
Expr->IVal |= shl_l (~0UL, NewBits);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((OldVal != Expr->IVal) && IS_Get (&WarnConstOverflow)) {
|
||||||
|
Warning ("Implicit conversion of constant overflows %d-bit destination", NewBits);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Do the integer constant <-> absolute address conversion if necessary */
|
/* Do the integer constant <-> absolute address conversion if necessary */
|
||||||
|
|||||||
Reference in New Issue
Block a user