Implement -Wnumeric-string[=0|1|2] (#935)

Fixes #934
This commit is contained in:
Rangi
2021-11-12 17:09:35 -05:00
committed by GitHub
parent 55a02981b5
commit 0bb815edc0
14 changed files with 135 additions and 23 deletions

View File

@@ -52,16 +52,21 @@ static void lowerstring(char *dest, char const *src)
*dest = '\0';
}
static uint32_t str2int2(uint8_t *s, int32_t length)
static uint32_t str2int2(uint8_t *s, uint32_t length)
{
int32_t i;
if (length > 4)
warning(WARNING_NUMERIC_STRING_1,
"Treating string as a number ignores first %" PRIu32 " character%s\n",
length - 4, length == 5 ? "" : "s");
else if (length > 1)
warning(WARNING_NUMERIC_STRING_2,
"Treating %" PRIu32 "-character string as a number\n", length);
uint32_t r = 0;
i = length < 4 ? 0 : length - 4;
while (i < length) {
for (uint32_t i = length < 4 ? 0 : length - 4; i < length; i++) {
r <<= 8;
r |= s[i];
i++;
}
return r;
@@ -1311,7 +1316,7 @@ constlist_8bit_entry : reloc_8bit_no_str {
}
| string {
uint8_t *output = malloc(strlen($1)); /* Cannot be larger than that */
int32_t length = charmap_Convert($1, output);
uint32_t length = charmap_Convert($1, output);
sect_AbsByteGroup(output, length);
free(output);
@@ -1327,7 +1332,7 @@ constlist_16bit_entry : reloc_16bit_no_str {
}
| string {
uint8_t *output = malloc(strlen($1)); /* Cannot be larger than that */
int32_t length = charmap_Convert($1, output);
uint32_t length = charmap_Convert($1, output);
sect_AbsWordGroup(output, length);
free(output);
@@ -1344,7 +1349,7 @@ constlist_32bit_entry : relocexpr_no_str {
| string {
// Charmaps cannot increase the length of a string
uint8_t *output = malloc(strlen($1));
int32_t length = charmap_Convert($1, output);
uint32_t length = charmap_Convert($1, output);
sect_AbsLongGroup(output, length);
free(output);
@@ -1390,7 +1395,7 @@ relocexpr : relocexpr_no_str
| string {
// Charmaps cannot increase the length of a string
uint8_t *output = malloc(strlen($1));
int32_t length = charmap_Convert($1, output);
uint32_t length = charmap_Convert($1, output);
uint32_t r = str2int2(output, length);
free(output);

View File

@@ -243,6 +243,18 @@ Warn when obsolete constructs such as the
constant or
.Ic PRINTT
directive are encountered.
.It Fl Wnumeric-string=
Warn when a multi-character string is treated as a number.
.Fl Wnumeric-string=0
or
.Fl Wno-numeric-string
disables this warning.
.Fl Wnumeric-string=1
or just
.Fl Wnumeric-string
warns about strings longer than four characters, since four or fewer characters fit within a 32-bit integer.
.Fl Wnumeric-string=2
warns about any multi-character string.
.It Fl Wshift
Warn when shifting right a negative value.
Use a division by 2**N instead.

View File

@@ -628,7 +628,7 @@ void sect_AbsByte(uint8_t b)
writebyte(b);
}
void sect_AbsByteGroup(uint8_t const *s, int32_t length)
void sect_AbsByteGroup(uint8_t const *s, uint32_t length)
{
if (!checkcodesection())
return;
@@ -639,7 +639,7 @@ void sect_AbsByteGroup(uint8_t const *s, int32_t length)
writebyte(*s++);
}
void sect_AbsWordGroup(uint8_t const *s, int32_t length)
void sect_AbsWordGroup(uint8_t const *s, uint32_t length)
{
if (!checkcodesection())
return;
@@ -650,7 +650,7 @@ void sect_AbsWordGroup(uint8_t const *s, int32_t length)
writeword(*s++);
}
void sect_AbsLongGroup(uint8_t const *s, int32_t length)
void sect_AbsLongGroup(uint8_t const *s, uint32_t length)
{
if (!checkcodesection())
return;
@@ -664,7 +664,7 @@ void sect_AbsLongGroup(uint8_t const *s, int32_t length)
/*
* Skip this many bytes
*/
void sect_Skip(int32_t skip, bool ds)
void sect_Skip(uint32_t skip, bool ds)
{
if (!checksection())
return;

View File

@@ -40,6 +40,8 @@ static const enum WarningState defaultWarnings[ARRAY_SIZE(warningStates)] = {
[WARNING_SHIFT_AMOUNT] = WARNING_DISABLED,
[WARNING_USER] = WARNING_ENABLED,
[WARNING_NUMERIC_STRING_1] = WARNING_ENABLED,
[WARNING_NUMERIC_STRING_2] = WARNING_DISABLED,
[WARNING_TRUNCATION_1] = WARNING_ENABLED,
[WARNING_TRUNCATION_2] = WARNING_DISABLED,
};
@@ -86,6 +88,8 @@ static const char * const warningFlags[NB_WARNINGS] = {
"user",
// Parametric warnings
"numeric-string",
"numeric-string",
"truncation",
"truncation",
@@ -100,6 +104,7 @@ static const struct {
uint8_t nbLevels;
uint8_t defaultLevel;
} paramWarnings[] = {
{ "numeric-string", 2, 1 },
{ "truncation", 2, 2 },
};
@@ -155,6 +160,7 @@ static uint8_t const _wallCommands[] = {
WARNING_LONG_STR,
WARNING_NESTED_COMMENT,
WARNING_OBSOLETE,
WARNING_NUMERIC_STRING_1,
META_WARNING_DONE
};
@@ -164,6 +170,7 @@ static uint8_t const _wextraCommands[] = {
WARNING_MACRO_SHIFT,
WARNING_NESTED_COMMENT,
WARNING_OBSOLETE,
WARNING_NUMERIC_STRING_2,
WARNING_TRUNCATION_1,
WARNING_TRUNCATION_2,
META_WARNING_DONE
@@ -184,6 +191,8 @@ static uint8_t const _weverythingCommands[] = {
WARNING_OBSOLETE,
WARNING_SHIFT,
WARNING_SHIFT_AMOUNT,
WARNING_NUMERIC_STRING_1,
WARNING_NUMERIC_STRING_2,
WARNING_TRUNCATION_1,
WARNING_TRUNCATION_2,
/* WARNING_USER, */