From 0052ef743751a579d3ab42d889ba70978e858fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 14 Apr 2017 08:05:14 +0200 Subject: [PATCH] Misra: Add rule 15.7 --- addons/misra-test.c | 16 ++++++++++++---- addons/misra.py | 23 ++++++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/addons/misra-test.c b/addons/misra-test.c index 068e7fe4c..b908bb2b6 100644 --- a/addons/misra-test.c +++ b/addons/misra-test.c @@ -50,10 +50,12 @@ void misra_13_3() { void misra_13_4() { if (x != (y = z)) {} // 13.4 + else {} } void misra_13_5() { if (x && (y++ < 123)){} // 13.5 + else {} } void misra_13_6() { @@ -73,6 +75,7 @@ void misra_14_2() { void misra_14_4() { if (x+4){} // 14.4 + else {} } void misra_15_1() { @@ -90,18 +93,23 @@ void misra_15_3() { goto L1; // 15.3 15.1 if (y!=0) { L1: - } - } + } else {} + } else {} } int misra_15_5() { if (x!=0) { return 1; // 15.5 - } + } else {} return 2; } - void misra_15_6() { if (x!=0); // 15.6 + else{} } + +void misra_15_7() { + if (x!=0){} // 15.7 +} + diff --git a/addons/misra.py b/addons/misra.py index 772a1e1ca..c7c0a373c 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -392,6 +392,27 @@ def misra_15_6(rawTokens): if token.str != '{': reportError(token, 15, 6) +def misra_15_7(data): + for token in data.tokenlist: + if token.str != 'if': + continue + lpar = token.next + if not lpar or lpar.str != '(': + continue + rpar = lpar.link + if not rpar or rpar.str != ')': + continue + brace1 = rpar.next + if not brace1 or brace1.str != '{': + continue + brace2 = brace1.link + if not brace2 or brace2.str != '}': + continue + else_ = brace2.next + if not else_ or else_.str != 'else': + reportError(token, 15, 7) + + for arg in sys.argv[1:]: print('Checking ' + arg + '...') data = cppcheckdata.parsedump(arg) @@ -426,5 +447,5 @@ for arg in sys.argv[1:]: misra_15_5(cfg) if cfgNumber == 1: misra_15_6(data.rawTokens) - + misra_15_7(cfg)