diff --git a/addons/misra.py b/addons/misra.py index 34dc6ef4c..89980b43c 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -2111,6 +2111,47 @@ class MisraChecker: if e1 and e2 and e1 != e2: self.reportError(token, 10, 4) + def misra_10_5(self, cfg): + def _get_essential_category(token): + essential_type = getEssentialType(token) + #print(essential_type) + if essential_type: + if essential_type in ('bool', 'char'): + return essential_type + if essential_type.split(' ')[-1] in ('float', 'double'): + return 'floating' + if essential_type.split(' ')[0] in ('unsigned', 'signed'): + return essential_type.split(' ')[0] + return None + for token in cfg.tokenlist: + if not isCast(token): + continue + to_type = _get_essential_category(token) + #print(to_type) + if to_type is None: + continue + from_type = _get_essential_category(token.astOperand1) + #print(from_type) + if from_type is None: + continue + if to_type == from_type: + continue + if to_type == 'bool' or from_type == 'bool': + if token.astOperand1.isInt and token.astOperand1.getKnownIntValue() == 1: + # Exception + continue + self.reportError(token, 10, 5) + continue + if to_type == 'enum': + self.reportError(token, 10, 5) + continue + if from_type == 'float' and to_type == 'char': + self.reportError(token, 10, 5) + continue + if from_type == 'char' and to_type == 'float': + self.reportError(token, 10, 5) + continue + def misra_10_6(self, data): for token in data.tokenlist: if token.str != '=' or not token.astOperand1 or not token.astOperand2: @@ -3758,6 +3799,7 @@ class MisraChecker: self.executeCheck(1002, self.misra_10_2, cfg) self.executeCheck(1003, self.misra_10_3, cfg) 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(1008, self.misra_10_8, cfg) self.executeCheck(1101, self.misra_11_1, cfg) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 26ce67129..63e67c0fc 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -683,6 +683,16 @@ static void misra_10_4(u32 x, s32 y) { z = (a == misra_10_4_A3) ? y : y; // no-warning } +static void misra_10_5(uint16_t x) { + // bool + res = (uint16_t) (x > 10u); // 10.5 + res = (bool) 1u; // no-warning + + // char <=> float + res = (char) 0.1f; + res = (float) 'x'; +} + static void misra_10_6(u8 x, u32 a, u32 b, char c1, char c2) { u16 y = x+x; // 10.6 u16 z = ~u8 x ;//10.6