misra.py: Treat enum constants as constant known at compile time (#2823)

C89 standard defines enum members as enumeration contants at ch.
6.4.4.3, and they are always known at compile time.

This commit fix false positives for rule 18.8 (and possible other rules
that check "constentess") with enumeration members.

Fix Trac#9913
This commit is contained in:
Georgy Komarov 2020-09-25 22:02:34 +03:00 committed by GitHub
parent 5856fef83b
commit 3944e70e02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -625,10 +625,17 @@ def isBoolExpression(expr):
return expr.str in ['!', '==', '!=', '<', '<=', '>', '>=', '&&', '||', '0', '1', 'true', 'false']
def isEnumConstant(expr):
if not expr or not expr.values:
return False
values = expr.values
return len(values) == 1 and values[0].valueKind == 'known'
def isConstantExpression(expr):
if expr.isNumber:
return True
if expr.isName:
if expr.isName and not isEnumConstant(expr):
return False
if simpleMatch(expr.previous, 'sizeof ('):
return True

View File

@ -1009,11 +1009,17 @@ struct {
uint8_t data_2[ ]; // 18.7
} r18_7_struct;
typedef enum {
R18_8_ENUM_CONSTANT_0,
R18_8_ENUM_CONSTANT_1,
} r18_8_enum;
void misra_18_8(int x) {
int buf1[10];
int buf2[sizeof(int)];
int vla[x]; // 18.8
static const unsigned char arr18_8_1[] = UNDEFINED_ID;
static uint32_t enum_test_0[R18_8_ENUM_CONSTANT_0] = {0};
}
union misra_19_2 { }; // 19.2