diff --git a/addons/misra.py b/addons/misra.py index 89980b43c..061367695 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -2177,6 +2177,32 @@ class MisraChecker: except ValueError: pass + def misra_10_7(self, cfg): + for token in cfg.tokenlist: + if token.astOperand1 is None or token.astOperand2 is None: + continue + if not token.isArithmeticalOp: + continue + parent = token.astParent + if parent is None: + continue + if not parent.isArithmeticalOp: + if not parent.isAssignmentOp: + continue + if parent.str == '=': + continue + token_type = getEssentialType(token) + if token_type is None: + continue + sibling = parent.astOperand1 if (token == parent.astOperand2) else parent.astOperand2 + sibling_type = getEssentialType(sibling) + if sibling_type is None: + continue + b1 = bitsOfEssentialType(token_type) + b2 = bitsOfEssentialType(sibling_type) + if b1 > 0 and b1 < b2: + self.reportError(token, 10, 7) + def misra_10_8(self, data): for token in data.tokenlist: if not isCast(token): @@ -3801,6 +3827,7 @@ class MisraChecker: self.executeCheck(1004, self.misra_10_4, cfg) self.executeCheck(1005, self.misra_10_5, cfg) self.executeCheck(1006, self.misra_10_6, cfg) + self.executeCheck(1007, self.misra_10_7, cfg) self.executeCheck(1008, self.misra_10_8, cfg) self.executeCheck(1101, self.misra_11_1, cfg) self.executeCheck(1102, self.misra_11_2, cfg) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 63e67c0fc..7148c9f3f 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -704,6 +704,15 @@ static void misra_10_6_1(uint32_t *a, uint16_t b, uint16_t c) *a = b + c ; // 10.6 } +static void misra_10_7(uint16_t u16a, uint16_t u16b) { + uint32_t u32a = 100u; + res = u32a * u16a + u16b; // 12.1 no-warning + res = (u32a * u16a) + u16b; // no-warning + res = u32a * ( ( uint32_t ) u16a + u16b ); // no-warning + res = u32a * (u16a + u16b); // 10.7 + u32a *= u16a + u16b; // 10.7 +} + static void misra_10_8(u8 x, s32 a, s32 b) { y = (u16)x; y = (u16)(x+x); // 10.8