Misra: Added rule 14.2

This commit is contained in:
Daniel Marjamäki 2017-04-13 22:26:12 +02:00
parent 466bbdcf6f
commit b620853b25
2 changed files with 33 additions and 11 deletions

View File

@ -64,6 +64,13 @@ void misra_14_1() {
for (float f=0.1f; f<1.0f; f += 0.1f){} // 14.1
}
void misra_14_2() {
for (dostuff();a<10;a++) {} // 14.2
for (;i++<10;) {} // 14.2
for (;i<10;dostuff()) {} // TODO
// TODO check more variants
}
void misra_14_4() {
if (x+4){} // 14.4
}

View File

@ -70,6 +70,19 @@ def countSideEffects(expr):
ret = 1
return ret + countSideEffects(expr.astOperand1) + countSideEffects(expr.astOperand2)
def getForLoopExpressions(forToken):
if not forToken or forToken.str != 'for':
return None
lpar = forToken.next
if not lpar or lpar.str != '(':
return None
if not lpar.astOperand2 or lpar.astOperand2.str != ';':
return None
if not lpar.astOperand2.astOperand2 or lpar.astOperand2.astOperand2.str != ';':
return None
return [lpar.astOperand2.astOperand1, lpar.astOperand2.astOperand2.astOperand1, lpar.astOperand2.astOperand2.astOperand2]
def hasFloatComparison(expr):
if not expr:
return False
@ -287,19 +300,20 @@ def misra_14_1(data):
for token in data.tokenlist:
if token.str != 'for':
continue
lpar = token.next
if not lpar or lpar.str != '(':
continue
if not lpar.astOperand2 or lpar.astOperand2.str != ';':
continue
if not lpar.astOperand2.astOperand2 or lpar.astOperand2.astOperand2.str != ';':
continue
expr1 = lpar.astOperand2.astOperand1
expr2 = lpar.astOperand2.astOperand2.astOperand1
expr3 = lpar.astOperand2.astOperand2.astOperand2
if hasFloatComparison(expr2):
exprs = getForLoopExpressions(token)
if exprs and hasFloatComparison(exprs[1]):
reportError(token, 14, 1)
def misra_14_2(data):
for token in data.tokenlist:
expressions = getForLoopExpressions(token)
if not expressions:
continue
if expressions[0] and not expressions[0].isAssignmentOp:
reportError(token, 14, 2)
elif hasSideEffectsRecursive(expressions[1]):
reportError(token, 14, 2)
def misra_14_4(data):
for token in data.tokenlist:
@ -343,5 +357,6 @@ for arg in sys.argv[1:]:
misra_13_5(cfg)
misra_13_6(cfg)
misra_14_1(cfg)
misra_14_2(cfg)
misra_14_4(cfg)
misra_15_1(cfg)