misra.py: catch all 15.6 bugs in misra exemplar suite

This commit is contained in:
Daniel Marjamäki 2018-03-13 14:22:25 +01:00
parent 49c8e42b30
commit 348232a599
1 changed files with 32 additions and 2 deletions

View File

@ -46,6 +46,21 @@ def simpleMatch(token, pattern):
token = token.next token = token.next
return True return True
def rawlink(rawtoken):
if rawtoken.str == '}':
indent = 0
while rawtoken:
if rawtoken.str == '}':
indent = indent + 1
elif rawtoken.str == '{':
indent = indent - 1
if indent == 0:
break
rawtoken = rawtoken.previous
else:
rawtoken = None
return rawtoken
KEYWORDS = { KEYWORDS = {
'auto', 'auto',
'break', 'break',
@ -747,14 +762,29 @@ def misra_15_6(rawTokens):
indent = 0 indent = 0
tok1 = None tok1 = None
for token in rawTokens: for token in rawTokens:
if token.str in {'if', 'for', 'while'}: if token.str in ['if', 'for', 'while']:
if simpleMatch(token.previous, '# if'): if simpleMatch(token.previous, '# if'):
continue continue
if simpleMatch(token.previous, "} while"): if simpleMatch(token.previous, "} while"):
continue # is there a 'do { .. } while'?
start = rawlink(token.previous)
if start and simpleMatch(start.previous, 'do {'):
continue
if state == 2:
reportError(tok1, 15, 6)
state = 1 state = 1
indent = 0 indent = 0
tok1 = token tok1 = token
elif token.str == 'else':
if simpleMatch(token.previous, '# else'):
continue
if simpleMatch(token, 'else if'):
continue
if state == 2:
reportError(tok1, 15, 6)
state = 2
indent = 0
tok1 = token
elif state == 1: elif state == 1:
if indent == 0 and token.str != '(': if indent == 0 and token.str != '(':
state = 0 state = 0