Misra: Added rule 16.4

This commit is contained in:
Daniel Marjamäki 2017-04-14 12:57:07 +02:00
parent 0ba8885d89
commit e840d67f03
2 changed files with 38 additions and 1 deletions

View File

@ -113,6 +113,8 @@ void misra_15_7() {
void misra_16_2() {
switch (x) {
default:
break;
case 1:
while (y>4) {
case 2: break; // 16.2
@ -134,3 +136,12 @@ void misra_16_3() {
default: break;
}
}
void misra_16_4() {
switch (x) { // 16.4
case 1:
break;
case 2:
break;
}
}

View File

@ -25,6 +25,13 @@ def reportError(token, num1, num2):
else:
sys.stderr.write('[' + token.file + ':' + str(token.linenr) + '] misra ' + str(num1) + '.' + str(num2) + ' violation\n')
def simpleMatch(token, pattern):
for p in pattern.split(' '):
if not token or token.str != p:
return False
token = token.next
return True
# Platform
# TODO get this from dump
CHAR_BITS = 8
@ -425,7 +432,6 @@ def misra_16_2(data):
if token.str == 'case' and token.scope.type != 'Switch':
reportError(token, 16, 2)
# The goal is handle comments/annotations by other tools
def misra_16_3(rawTokens):
# state: 0=no, 1=break is seen but not its ';', 2=after 'break;', 'comment', '{'
state = 0
@ -445,6 +451,25 @@ def misra_16_3(rawTokens):
elif token.str == 'case' and state != 2:
reportError(token, 16, 3)
def misra_16_4(data):
for token in data.tokenlist:
if token.str != 'switch':
continue
if not simpleMatch(token, 'switch ('):
continue
if not simpleMatch(token.next.link, ') {'):
continue
startTok = token.next.link.next
tok = startTok.next
while tok and tok.str != '}':
if tok.str == '{':
tok = tok.link
elif tok.str == 'default':
break
tok = tok.next
if tok and tok.str != 'default':
reportError(token, 16, 4)
if '-verify' in sys.argv[1:]:
VERIFY = True
@ -499,6 +524,7 @@ for arg in sys.argv[1:]:
misra_16_2(cfg)
if cfgNumber == 1:
misra_16_3(data.rawTokens)
misra_16_4(cfg)
if VERIFY:
for expected in VERIFY_EXPECTED: