From 4ebe5208584615e9883decaf492ecfea76b6ae17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 13 Apr 2017 23:02:06 +0200 Subject: [PATCH] Misra: Added rule 15.3 --- addons/misra-test.c | 9 +++++++++ addons/misra.py | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/addons/misra-test.c b/addons/misra-test.c index 80c7d84ef..7419eba03 100644 --- a/addons/misra-test.c +++ b/addons/misra-test.c @@ -84,3 +84,12 @@ void misra_15_2() { label: goto label; // 15.2 15.1 } + +void misra_15_3() { + if (x!=0) { + goto L1; // 15.3 15.1 + if (y!=0) { + L1: + } + } +} diff --git a/addons/misra.py b/addons/misra.py index deb36ca61..c0830b105 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -165,6 +165,17 @@ def noParentheses(tok1, tok2): tok1 = tok1.next return tok1 == tok2 +def findGotoLabel(gotoToken): + label = gotoToken.next.str + tok = gotoToken.next.next + while tok: + if tok.str == '}' and tok.scope.type == 'Function': + break + if tok.str == label and tok.next.str == ':': + return tok + tok = tok.next + return None + def misra_5_1(data): for token in data.tokenlist: if token.isName and len(token.str) > 31: @@ -335,15 +346,23 @@ def misra_15_2(data): continue if (not token.next) or (not token.next.isName): continue - label = token.next.str - tok = token.next.next - while tok: - if tok.str == '}' and tok.scope.type == 'Function': - reportError(token, 15, 2) - break - if tok.str == label: - break - tok = tok.next + if not findGotoLabel(token): + reportError(token, 15, 2) + +def misra_15_3(data): + for token in data.tokenlist: + if token.str != 'goto': + continue + if (not token.next) or (not token.next.isName): + continue + tok = findGotoLabel(token) + if not tok: + continue + scope = token.scope + while scope and scope != tok.scope: + scope = scope.nestedIn + if not scope: + reportError(token, 15, 3) for arg in sys.argv[1:]: print('Checking ' + arg + '...') @@ -375,5 +394,6 @@ for arg in sys.argv[1:]: misra_14_4(cfg) misra_15_1(cfg) misra_15_2(cfg) + misra_15_3(cfg)