From 8846077caa2ff5f6e218aa0ed20d3c4c23b0e9ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 8 Apr 2017 19:00:50 +0200 Subject: [PATCH] Fix and test MISRA --- addons/misra-test.c | 43 +++++++++++++++++++++++++++++++++++++++++++ addons/misra.py | 15 ++++++++------- 2 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 addons/misra-test.c diff --git a/addons/misra-test.c b/addons/misra-test.c new file mode 100644 index 000000000..a12bdfb53 --- /dev/null +++ b/addons/misra-test.c @@ -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 +} diff --git a/addons/misra.py b/addons/misra.py index e53d658b4..07670c168 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -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