From 754d648b0f67df4285f6aa44c903657a9ce8cb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 11 Jul 2021 07:50:13 +0200 Subject: [PATCH] misra; implement rule 11.1 --- addons/misra.py | 47 ++++++++++++++++++++++++++++++++++ addons/test/misra/misra-test.c | 3 +++ 2 files changed, 50 insertions(+) diff --git a/addons/misra.py b/addons/misra.py index 2786399f0..18e557e32 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -476,6 +476,26 @@ def bitsOfEssentialType(ty): return 0 +def get_function_pointer_type(tok): + ret = '' + par = 0 + while tok and (tok.isName or tok.str == '*'): + ret += ' ' + tok.str + tok = tok.next + if tok is None or tok.str != '(': + return None + tok = tok.link + if not simpleMatch(tok, ') ('): + return None + ret += '(' + tok = tok.next.next + while tok and (tok.str not in '()'): + ret += ' ' + tok.str + tok = tok.next + if (tok is None) or tok.str != ')': + return None + return ret[1:] + ')' + def isCast(expr): if not expr or expr.str != '(' or not expr.astOperand1 or expr.astOperand2: return False @@ -1949,6 +1969,32 @@ class MisraChecker: except ValueError: pass + def misra_11_1(self, data): + for token in data.tokenlist: + if not isCast(token): + continue + vartok = token + while vartok: + if isCast(vartok): + if vartok.astOperand2 is None: + vartok = vartok.astOperand1 + else: + vartok = vartok.astOperand2 + elif vartok.str in ('.', '::'): + vartok = vartok.astOperand2 + elif vartok.str == '[': + vartok = vartok.astOperand1 + else: + break + if (vartok is None) or (not vartok.variable): + continue + var_type = get_function_pointer_type(vartok.variable.typeStartToken) + if var_type is None: + continue + cast_type = get_function_pointer_type(token.next) + if cast_type is None or cast_type != var_type: + self.reportError(token, 11, 1) + def misra_11_3(self, data): for token in data.tokenlist: if not isCast(token): @@ -3438,6 +3484,7 @@ class MisraChecker: self.executeCheck(1004, self.misra_10_4, cfg) self.executeCheck(1006, self.misra_10_6, cfg) self.executeCheck(1008, self.misra_10_8, cfg) + self.executeCheck(1101, self.misra_11_1, cfg) self.executeCheck(1103, self.misra_11_3, cfg) self.executeCheck(1104, self.misra_11_4, cfg) self.executeCheck(1105, self.misra_11_5, cfg) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 7b5f37497..98e403835 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -681,6 +681,9 @@ void misra_10_8(u8 x, s32 a, s32 b) { y = (u16) (a + b) //10.8 } +int (*misra_11_1_p)(void); +void *misra_11_1_bad1 = (void*)misra_11_1_p; // 11.1 + struct Fred {}; struct Wilma {}; void misra_11_3(u8* p, struct Fred *fred) { x = (u64*)p; // 11.3