diff --git a/addons/misra.py b/addons/misra.py index e3b3802e3..4ad437fdd 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -2832,14 +2832,41 @@ class MisraChecker: state = 0 indent = 0 tok1 = None + def tokAt(tok,i): + while i < 0 and tok: + tok = tok.previous + if tok.str.startswith('//') or tok.str.startswith('/*'): + continue + i += 1 + while i > 0 and tok: + tok = tok.next + if tok.str.startswith('//') or tok.str.startswith('/*'): + continue + i -= 1 + return tok + + def strtokens(tok, i1, i2): + tok1 = tokAt(tok, i1) + tok2 = tokAt(tok, i2) + tok = tok1 + s = '' + while tok != tok2: + if tok.str.startswith('//') or tok.str.startswith('/*'): + tok = tok.next + continue + s += ' ' + tok.str + tok = tok.next + s += ' ' + tok.str + return s[1:] + for token in rawTokens: if token.str in ['if', 'for', 'while']: - if simpleMatch(token.previous, '# if'): + if strtokens(token,-1,0) == '# if': continue - if simpleMatch(token.previous, "} while"): + if strtokens(token,-1,0) == "} while": # is there a 'do { .. } while'? - start = rawlink(token.previous) - if start and simpleMatch(start.previous, 'do {'): + start = rawlink(tokAt(token,-1)) + if start and strtokens(start, -1, 0) == 'do {': continue if state == 2: self.reportError(tok1, 15, 6) @@ -2847,9 +2874,9 @@ class MisraChecker: indent = 0 tok1 = token elif token.str == 'else': - if simpleMatch(token.previous, '# else'): + if strtokens(token,-1,0) == '# else': continue - if simpleMatch(token, 'else if'): + if strtokens(token,0,1) == 'else if': continue if state == 2: self.reportError(tok1, 15, 6) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 66e29b724..573563c51 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -1414,6 +1414,16 @@ static void misra_15_6(void) { do {} while (x<0); // no-warning } +static void misra_15_6_fp(void) +{ + uint8_t value = 0U; + do // Test + { + value++; + } + while (value < 2U); +} + #if defined(M_20_9) && M_20_9 > 1 // no-warning (#10380) #endif