diff --git a/addons/cppcheckdata.py b/addons/cppcheckdata.py index ffe830579..bee9a71a0 100644 --- a/addons/cppcheckdata.py +++ b/addons/cppcheckdata.py @@ -69,6 +69,11 @@ class ValueType: def setId(self, IdMap): self.typeScope = IdMap[self.typeScopeId] + def isIntegral(self): + return self.type == 'bool' or self.type == 'char' or self.type == 'short' or self.type == 'int' or self.type == 'long' or self.type == 'long long' + + def isFloat(self): + return self.type == 'float' or self.type == 'double' or self.type == 'long double' class Token: """ diff --git a/addons/misra-test.c b/addons/misra-test.c index 21c5bbc4c..ebea52c7c 100644 --- a/addons/misra-test.c +++ b/addons/misra-test.c @@ -24,6 +24,10 @@ void misra_7_3() { int x = 12lu; // 7.3 } +void misra_11_3(u8* p) { + x = (u64*)p; // 11.3 +} + void misra_11_4(u8*p) { u64 y = (u64)p; // 11.4 } diff --git a/addons/misra.py b/addons/misra.py index 462f8a170..a422b8f37 100644 --- a/addons/misra.py +++ b/addons/misra.py @@ -247,24 +247,16 @@ def misra_7_3(rawTokens): if re.match(r'^[0-9]+l', tok.str): reportError(tok, 7, 3) - -def misra_12_1_sizeof(rawTokens): - state = 0 - for tok in rawTokens: - if tok.str.startswith('//') or tok.str.startswith('/*'): - continue - if tok.str == 'sizeof': - state = 1 - elif state == 1: - if re.match(r'^[a-zA-Z_]',tok.str): - state = 2 - else: - state = 0 - elif state == 2: - if tok.str in ['+','-','*','/','%']: - reportError(tok, 12, 1) - else: - state = 0 +def misra_11_3(data): + for token in data.tokenlist: + if not isCast(token): + continue + vt1 = token.valueType + vt2 = token.astOperand1.valueType + if not vt1 or not vt2: + continue + if vt1.pointer==vt2.pointer and vt1.pointer>0 and vt1.type != vt2.type and vt1.isIntegral() and vt2.isIntegral(): + reportError(token, 11, 3) def misra_11_4(data): for token in data.tokenlist: @@ -335,6 +327,24 @@ def misra_11_9(data): if value == '((void*)0)': reportError(directive, 11, 9) +def misra_12_1_sizeof(rawTokens): + state = 0 + for tok in rawTokens: + if tok.str.startswith('//') or tok.str.startswith('/*'): + continue + if tok.str == 'sizeof': + state = 1 + elif state == 1: + if re.match(r'^[a-zA-Z_]',tok.str): + state = 2 + else: + state = 0 + elif state == 2: + if tok.str in ['+','-','*','/','%']: + reportError(tok, 12, 1) + else: + state = 0 + def misra_12_1(data): for token in data.tokenlist: p = getPrecedence(token) @@ -776,6 +786,7 @@ for arg in sys.argv[1:]: if cfgNumber == 1: misra_7_1(data.rawTokens) misra_7_3(data.rawTokens) + misra_11_3(cfg) misra_11_4(cfg) misra_11_5(cfg) misra_11_6(cfg)