From a1c30b834cb318a463f5448846b81e2d73f21156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 11 Apr 2019 10:50:07 +0200 Subject: [PATCH] misra.py: Added rule 20.10 --- addons/misra.py | 48 ++++++++++++++++++++++++---------------- addons/test/misra-test.c | 7 ++++-- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index cfdbae694..4c56371f1 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1631,27 +1631,36 @@ class MisraChecker: d = Define(directive) exp = '(' + d.expansionList + ')' for arg in d.args: - pos = exp.find(arg) - if pos < 0: - continue - pos1 = pos - 1 - pos2 = pos + len(arg) - if isalnum(exp[pos1]) or exp[pos1]=='_': - continue - if isalnum(exp[pos2]) or exp[pos2]=='_': - continue - while exp[pos1] == ' ': - pos1 -= 1 - if exp[pos1] != '(' and exp[pos1] != '[': - self.reportError(directive, 20, 7); - break - while exp[pos2] == ' ': - pos2 += 1 - if exp[pos2] != ')' and exp[pos2] != ']': - self.reportError(directive, 20, 7); - break + pos = 0 + while pos < len(exp): + pos = exp.find(arg, pos) + if pos < 0: + break + pos1 = pos - 1 + pos2 = pos + len(arg) + pos = pos2 + if isalnum(exp[pos1]) or exp[pos1]=='_': + continue + if isalnum(exp[pos2]) or exp[pos2]=='_': + continue + while exp[pos1] == ' ': + pos1 -= 1 + if exp[pos1] != '(' and exp[pos1] != '[': + self.reportError(directive, 20, 7); + break + while exp[pos2] == ' ': + pos2 += 1 + if exp[pos2] != ')' and exp[pos2] != ']': + self.reportError(directive, 20, 7); + break + def misra_20_10(self, data): + for directive in data.directives: + d = Define(directive) + if d.expansionList.find('#') >= 0: + self.reportError(directive, 20, 10); + def misra_20_13(self, data): dir_pattern = re.compile(r'#[ ]*([^ (<]*)') for directive in data.directives: @@ -2185,6 +2194,7 @@ class MisraChecker: self.misra_20_4(cfg) self.misra_20_5(cfg) self.misra_20_7(cfg) + self.misra_20_10(cfg) self.misra_20_13(cfg) self.misra_20_14(cfg) self.misra_21_3(cfg) diff --git a/addons/test/misra-test.c b/addons/test/misra-test.c index 7f807e95f..02b568ff9 100644 --- a/addons/test/misra-test.c +++ b/addons/test/misra-test.c @@ -498,8 +498,11 @@ union misra_19_2 { }; // 19.2 #define int short // 20.4 #undef X // 20.5 -#define M1(A) (A+1) // 20.7 -#define M2(A,B) (1+AB+2) +#define M_20_7_1(A) (A+1) // 20.7 +#define M_20_7_2(A,B) (1+AB+2) // no warning +#define M_20_7_3(A) ((A)+A) // 20.7 + +#define STRINGIFY(a) (#a) // 20.7 20.10 #else1 // 20.13