diff --git a/addons/misra.py b/addons/misra.py index 45332d58681..43b92b51fb0 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -2460,27 +2460,38 @@ def get_category(essential_type): if essential_type.split(' ')[0] in ('unsigned', 'signed'): return essential_type.split(' ')[0] return None + for tok in cfg.tokenlist: - if tok.isAssignmentOp: - lhs = getEssentialType(tok.astOperand1) - rhs = getEssentialType(tok.astOperand2) - #print(lhs) - #print(rhs) - if lhs is None or rhs is None: + if not tok.isAssignmentOp: + continue + + lhs = getEssentialType(tok.astOperand1) + rhs = getEssentialType(tok.astOperand2) + if lhs is None or rhs is None: + continue + + find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion + + rhs_tok = tok.astOperand2 + rhs_macro_name = rhs_tok.macroName if rhs_tok else None + rhs_spelling = rhs_macro_name if rhs_macro_name in ('true', 'false') else rhs_tok.str + + rhs_is_source_bool_literal = rhs_spelling in ('true', 'false') + rhs_is_source_int_literal_0_1 = rhs_spelling in ('0', '1') + + if lhs == 'bool': + if rhs_is_source_bool_literal: + continue + if find_std == 'c89' and rhs_is_source_int_literal_0_1: continue - lhs_category = get_category(lhs) - rhs_category = get_category(rhs) - if lhs_category and rhs_category and lhs_category != rhs_category and rhs_category not in ('signed','unsigned'): - self.reportError(tok, 10, 3) - find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion - allow_bool_literal_0_1 = ( - find_std == "c89" and - lhs == "bool" and - tok.astOperand2 and - tok.astOperand2.str in ('0', '1') - ) - if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs) and not allow_bool_literal_0_1: - self.reportError(tok, 10, 3) + + lhs_category = get_category(lhs) + rhs_category = get_category(rhs) + if lhs_category and rhs_category and lhs_category != rhs_category and rhs_category not in ('signed', 'unsigned'): + self.reportError(tok, 10, 3) + + if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs): + self.reportError(tok, 10, 3) def misra_10_4(self, data): op = {'+', '-', '*', '/', '%', '&', '|', '^', '+=', '-=', ':'} diff --git a/addons/test/misra/misra-test-c11.c b/addons/test/misra/misra-test-c11.c index 77679d71547..aa00b2f0b42 100644 --- a/addons/test/misra/misra-test-c11.c +++ b/addons/test/misra/misra-test-c11.c @@ -29,6 +29,8 @@ static void misra_10_3_c11(void) { bool b = false; bool b0 = 0; // 10.3 bool b1 = 1; // 10.3 + bool bf = false; // no-warning + bool bt = true; // no-warning b = 0; // 10.3 b = 1; // 10.3 b = false; // no-warning