From 9305394abd7f7d7e0845684188295e52ad8ddc0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 17 Apr 2017 13:17:37 +0200 Subject: [PATCH] Misra: Add rule 5.3 --- addons/misra-test.c | 7 +++++++ addons/misra.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/addons/misra-test.c b/addons/misra-test.c index 1e85dde82..f067b6c0d 100644 --- a/addons/misra-test.c +++ b/addons/misra-test.c @@ -17,6 +17,13 @@ void misra_5_1() { int a1234567890123456789012345678901; // 5.1 } +void misra_5_3() { + u8 x=1; + if (y!=0) { + u8 x=2; // 5.3 + } else {} +} + #define m54_123456789012345678901234567890123456789012345678901234567890 1 // 5.4 #define m54_1234567890123456789012345678901234567890123456789012345678901 2 // 5.4 diff --git a/addons/misra.py b/addons/misra.py index 030489d53..8e9a53cdf 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -273,6 +273,35 @@ def misra_5_1(data): if token.isName and len(token.str) > 31: reportError(token, 5, 1) +def misra_5_3(data): + scopeVars = {} + for var in data.variables: + if var.isArgument: + continue + if not var.nameToken.scopeId in scopeVars: + scopeVars[var.nameToken.scope] = [] + scopeVars[var.nameToken.scope].append(var) + + for innerScope in data.scopes: + if innerScope.type == 'Global': + continue + if not innerScope in scopeVars: + continue + for innerVar in scopeVars[innerScope]: + outerScope = innerScope.nestedIn + while outerScope: + if not outerScope in scopeVars: + continue + found = False + for outerVar in scopeVars[outerScope]: + if innerVar.nameToken.str == outerVar.nameToken.str: + found = True + break + if found: + reportError(innerVar.nameToken, 5, 3) + break + outerScope = outerScope.nestedIn + def misra_5_4(data): for dir in data.directives: if re.match(r'#define [a-zA-Z0-9_]{64,}', dir.str): @@ -964,6 +993,7 @@ for arg in sys.argv[1:]: print('Checking ' + arg + ', config "' + cfg.name + '"...') misra_5_1(cfg) + misra_5_3(cfg) misra_5_4(cfg) misra_5_5(cfg) if cfgNumber == 1: