Misra: Add rule 20.4

This commit is contained in:
Daniel Marjamäki 2017-04-14 22:33:59 +02:00
parent c8d3cccc21
commit bcf815ab10
1 changed files with 55 additions and 3 deletions

View File

@ -38,6 +38,39 @@ CHAR_BITS = 8
SHORT_BITS = 16 SHORT_BITS = 16
INT_BITS = 32 INT_BITS = 32
KEYWORDS = ['auto',
'break',
'case',
'char',
'const',
'continue',
'default',
'do',
'double',
'else',
'enum',
'extern',
'float',
'for',
'goto',
'if',
'int',
'long',
'register',
'return',
'short',
'signed',
'sizeof',
'static',
'struct',
'switch',
'typedef',
'union',
'unsigned',
'void',
'volatile',
'while']
def getEssentialType(expr): def getEssentialType(expr):
if not expr: if not expr:
return None return None
@ -554,9 +587,7 @@ def misra_20_1(rawTokens):
linenr = -1 linenr = -1
state = 1 state = 1
for token in rawTokens: for token in rawTokens:
if token.str.startswith('/'): if token.str.startswith('/') or token.linenr == linenr:
continue
if token.linenr == linenr:
continue continue
linenr = token.linenr linenr = token.linenr
if token.str != '#': if token.str != '#':
@ -565,7 +596,11 @@ def misra_20_1(rawTokens):
reportError(token, 20, 1) reportError(token, 20, 1)
def misra_20_2(rawTokens): def misra_20_2(rawTokens):
linenr = -1
for token in rawTokens: for token in rawTokens:
if token.str.startswith('/') or token.linenr == linenr:
continue
linenr = token.linenr
if not simpleMatch(token, '# include'): if not simpleMatch(token, '# include'):
continue continue
header = token.next.next.str header = token.next.next.str
@ -575,13 +610,30 @@ def misra_20_2(rawTokens):
break break
def misra_20_3(rawTokens): def misra_20_3(rawTokens):
linenr = -1
for token in rawTokens: for token in rawTokens:
if token.str.startswith('/') or token.linenr == linenr:
continue
linenr = token.linenr
if not simpleMatch(token, '# include'): if not simpleMatch(token, '# include'):
continue continue
headerToken = token.next.next headerToken = token.next.next
if not headerToken or not (headerToken.str.startswith('<') or headerToken.str.startswith('"')): if not headerToken or not (headerToken.str.startswith('<') or headerToken.str.startswith('"')):
reportError(token, 20, 3) reportError(token, 20, 3)
def misra_20_4(rawTokens):
linenr = -1
for token in rawTokens:
if token.str.startswith('/') or token.linenr == linenr:
continue
linenr = token.linenr
if not simpleMatch(token, '# define'):
continue
macroName = token.next.next.str
if macroName in KEYWORDS:
reportError(token, 20, 4)
if '-verify' in sys.argv[1:]: if '-verify' in sys.argv[1:]:
VERIFY = True VERIFY = True