Fix and test MISRA

This commit is contained in:
Daniel Marjamäki 2017-04-08 19:00:50 +02:00
parent 33107ea64c
commit 8846077caa
2 changed files with 51 additions and 7 deletions

43
addons/misra-test.c Normal file
View File

@ -0,0 +1,43 @@
/*
~/cppcheck/cppcheck --dump misra-test.c
python misra.py misra-test.c.dump
Expected output:
[misra-test.c:5] (style) misra: 11 Identifier is longer than 31 characters
[misra-test.c:9] (style) misra: 14 The type char shall always be declared as unsigned char or signed char
[misra-test.c:3] (style) misra: 15 Make sure the floating point implementation comply with a defined floating point standard
[misra-test.c:17] (style) misra: 33 The right hand of a && or || shall not have side effects
[misra-test.c:21] (style) misra: 34 The operands of a && or || shall be primary expressions
[misra-test.c:3] (style) misra: 41 The implementation of integer division in the chosen compiler should be determined, documented and taken into account.
[misra-test.c:25] (style) misra: 56 The goto statement shall not be used
[misra-test.c:29] (style) misra: 57 The continue statement shall not be used
*/
void misra11() {
int a123456789012345678901234567890; // no-warning
int a1234567890123456789012345678901; // 11
}
void misra14() {
char c; // 14
}
void misra28() {
register int x = 3; // 28
}
void misra33() {
if (x && (y++ < 123)){} // 33
}
void misra34() {
if (x+3 && y){} // 34
}
void misra56() {
goto a1; // 56
}
void misra57() {
continue; // 57
}

View File

@ -19,14 +19,13 @@ def reportError(token, severity, msg):
def hasSideEffects(expr):
if not expr:
return False
if expr in ['++', '--', '=']:
if expr.str in ['++', '--', '=']:
return True
# Todo: Check function calls
return hasSideEffects(expr.astOperand1) or hasSideEffects(expr.astOperand2)
def isPrimaryExpression(expr):
return expr and (token.isName or token.isNumber or (token.str in [expr.str, '!', '==', '!=', '<', '<=', '>', '>=']))
return expr and (expr.isName or expr.isNumber or (expr.str in ['!', '==', '!=', '<', '<=', '>', '>=', '&&', '||']))
# Environment
# -----------
@ -270,7 +269,9 @@ def misra33(data):
# STATUS: Done
def misra34(data):
for token in data.tokenlist:
if token.isLogicalOp and not isPrimaryExpression(token):
if not token.isLogicalOp:
continue
if not isPrimaryExpression(token.astOperand1) or not isPrimaryExpression(token.astOperand2):
reportError(
token, 'style', '34 The operands of a && or || shall be primary expressions')
@ -395,15 +396,15 @@ def misra55(data):
# STATUS: Done.
def misra56(data):
for token in data.tokenlist:
if (token.str == "goto":
if token.str == "goto":
reportError(token, 'style', '56 The goto statement shall not be used')
# 57 The continue statement shall not be used
# STATUS: Done.
def misra57(data):
for token in data.tokenlist:
if (token.str == "continue":
reportError(token, 'style', '56 The continue statement shall not be used')
if token.str == "continue":
reportError(token, 'style', '57 The continue statement shall not be used')
# 58 The break statement shall not be used, except to terminate the cases of a switch statement
# STATUS: TODO