From 3944e70e02d3000c574c1adef6de05cb56fa6aad Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Fri, 25 Sep 2020 22:02:34 +0300 Subject: [PATCH] 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 --- addons/misra.py | 9 ++++++++- addons/test/misra/misra-test.c | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/addons/misra.py b/addons/misra.py index 4ee33b739..dca73b665 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -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 diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index d7f6136b9..7e5a722ec 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -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