diff --git a/addons/misra.py b/addons/misra.py index fc68b0038d4..551f4fbf028 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -830,6 +830,7 @@ def isFloatCounterInWhileLoop(whileToken): lpar = whileToken.next rpar = lpar.link counterTokens = findCounterTokens(lpar.astOperand2) + tok_varid = tuple(tok.varId for tok in counterTokens if tok.varId) whileBodyStart = None if simpleMatch(rpar, ') {'): whileBodyStart = rpar.next @@ -840,13 +841,21 @@ def isFloatCounterInWhileLoop(whileToken): token = whileBodyStart while token != whileBodyStart.link: token = token.next - for counterToken in counterTokens: - if not counterToken.valueType or not counterToken.valueType.isFloat(): - continue - if token.isAssignmentOp and token.astOperand1.str == counterToken.str: + if not token.varId: + continue + if token.varId not in tok_varid: + continue + if not token.astParent or not token.valueType or not token.valueType.isFloat(): + continue + parent = token.astParent + if parent.str in ('++', '--'): + return True + while parent: + if parent.isAssignmentOp and parent.str != "=" and parent.astOperand1 == token: return True - if token.str == counterToken.str and token.astParent and token.astParent.str in ('++', '--'): + if parent.str == "=" and parent.astOperand1.str == token.str and parent.astOperand1 != token: return True + parent = parent.astParent return False diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 67ffc8e5e30..6e18e7610c5 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -1231,7 +1231,34 @@ static void misra_14_1(void) { } while ( a < 10.0f ); // no-warning } +//13143 +static bool misra_14_1_is_increasing (const double* const x, const size_t n) +{ + double last = -INFINITY; + size_t i = ((size_t) 0); + while ((i < n) && isfinite(x[i]) && (x[i] > last)) { //no-warning for 14_1 + last = x[i]; + i++; + } + return i == n; +} + +static void misra_14_1_compliant1_foo(float x){ + int i = 0; + while(i < x){ // 10.4 + i += x + x; // 10.3 10.4 no-warning for 14_1 + } +} +static void misra_14_1_compliant2_foo(void){ + int i = 0; + float f = 0f; + while (f < 10 && i < 10) { //10.4 12.1 + float f = 0f; + f = f + i; // 10.3 10.4 no warning for 14_1 + i++; + } +} static void misra_14_2_init_value(int32_t *var) { *var = 0; }