Misra: Refactoring (use directives instead of rawTokens)

This commit is contained in:
Daniel Marjamäki 2017-04-15 15:13:18 +02:00
parent 2236034243
commit 20aa099d05
1 changed files with 33 additions and 60 deletions

View File

@ -19,22 +19,11 @@ VERIFY = False
VERIFY_EXPECTED = [] VERIFY_EXPECTED = []
VERIFY_ACTUAL = [] VERIFY_ACTUAL = []
def reportError(token, num1, num2): def reportError(location, num1, num2):
if VERIFY: if VERIFY:
VERIFY_ACTUAL.append(str(token.linenr) + ':' + str(num1) + '.' + str(num2)) VERIFY_ACTUAL.append(str(location.linenr) + ':' + str(num1) + '.' + str(num2))
else: else:
sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] misra ' + str(num1) + '.' + str(num2) + ' violation\n') sys.stderr.write('[' + location.file + ':' + str(location.linenr) + '] misra ' + str(num1) + '.' + str(num2) + ' violation\n')
def reportErrorFileLine(file, linenr, num1, num2):
class Tok:
file = ''
linenr = ''
def __init__(self):
return
token = Tok
token.file = file
token.linenr = linenr
reportError(token, num1, num2)
def simpleMatch(token, pattern): def simpleMatch(token, pattern):
for p in pattern.split(' '): for p in pattern.split(' '):
@ -599,30 +588,24 @@ def misra_19_2(data):
if token.str == 'union': if token.str == 'union':
reportError(token, 19, 2) reportError(token, 19, 2)
def misra_20_1(rawTokens): def misra_20_1(data):
linenr = -1 for directive in data.directives:
state = 1 if not directive.str.startswith('#include'):
for token in rawTokens:
if token.str.startswith('/') or token.linenr == linenr:
continue continue
linenr = token.linenr for token in data.tokenlist:
if token.str != '#': if token.file != directive.file:
state = 2 continue
elif state == 2 and simpleMatch(token, '# include'): if int(token.linenr) < int(directive.linenr):
reportError(token, 20, 1) reportError(directive, 20, 1)
break
def misra_20_2(rawTokens): def misra_20_2(data):
linenr = -1 for directive in data.directives:
for token in rawTokens: if not directive.str.startswith('#include '):
if token.str.startswith('/') or token.linenr == linenr:
continue continue
linenr = token.linenr
if not simpleMatch(token, '# include'):
continue
header = token.next.next.str
for pattern in ['\\', '//', '/*', '\'']: for pattern in ['\\', '//', '/*', '\'']:
if header.find(pattern)>0: if directive.str.find(pattern)>0:
reportError(token, 20, 2) reportError(directive, 20, 2)
break break
def misra_20_3(rawTokens): def misra_20_3(rawTokens):
@ -637,26 +620,16 @@ def misra_20_3(rawTokens):
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): def misra_20_4(data):
linenr = -1 for directive in data.directives:
for token in rawTokens: res = re.search(r'#define ([a-z][a-z0-9_]+)', directive.str)
if token.str.startswith('/') or token.linenr == linenr: if res and (res.group(1) in KEYWORDS):
continue reportError(directive, 20, 4)
linenr = token.linenr
if not simpleMatch(token, '# define'):
continue
macroName = token.next.next.str
if macroName in KEYWORDS:
reportError(token, 20, 4)
def misra_20_5(rawTokens): def misra_20_5(data):
linenr = -1 for directive in data.directives:
for token in rawTokens: if directive.str.startswith('#undef '):
if token.str.startswith('/') or token.linenr == linenr: reportError(directive, 20, 5)
continue
linenr = token.linenr
if simpleMatch(token, '# undef'):
reportError(token, 20, 5)
def misra_21_3(data): def misra_21_3(data):
for token in data.tokenlist: for token in data.tokenlist:
@ -666,12 +639,12 @@ def misra_21_3(data):
def misra_21_4(data): def misra_21_4(data):
directive = findInclude(data.directives, '<setjmp.h>') directive = findInclude(data.directives, '<setjmp.h>')
if directive: if directive:
reportErrorFileLine(directive.file, directive.linenr, 21, 4) reportError(directive, 21, 4)
def misra_21_5(data): def misra_21_5(data):
directive = findInclude(data.directives, '<signal.h>') directive = findInclude(data.directives, '<signal.h>')
if directive: if directive:
reportErrorFileLine(directive.file, directive.linenr, 21, 5) reportError(directive, 21, 5)
def misra_21_7(data): def misra_21_7(data):
for token in data.tokenlist: for token in data.tokenlist:
@ -691,7 +664,7 @@ def misra_21_9(data):
def misra_21_11(data): def misra_21_11(data):
directive = findInclude(data.directives, '<tgmath.h>') directive = findInclude(data.directives, '<tgmath.h>')
if directive: if directive:
reportErrorFileLine(directive.file, directive.linenr, 21, 11) reportError(directive, 21, 11)
if '-verify' in sys.argv[1:]: if '-verify' in sys.argv[1:]:
VERIFY = True VERIFY = True
@ -758,12 +731,12 @@ for arg in sys.argv[1:]:
misra_18_5(cfg) misra_18_5(cfg)
misra_18_8(cfg) misra_18_8(cfg)
misra_19_2(cfg) misra_19_2(cfg)
misra_20_1(cfg)
misra_20_2(cfg)
if cfgNumber == 1: if cfgNumber == 1:
misra_20_1(data.rawTokens)
misra_20_2(data.rawTokens)
misra_20_3(data.rawTokens) misra_20_3(data.rawTokens)
misra_20_4(data.rawTokens) misra_20_4(cfg)
misra_20_5(data.rawTokens) misra_20_5(cfg)
misra_21_3(cfg) misra_21_3(cfg)
misra_21_4(cfg) misra_21_4(cfg)
misra_21_5(cfg) misra_21_5(cfg)