Skip to content

Commit 036545d

Browse files
misra: avoid false positives for true/false after #14607
1 parent b99ce96 commit 036545d

2 files changed

Lines changed: 32 additions & 19 deletions

File tree

addons/misra.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,27 +2460,38 @@ def get_category(essential_type):
24602460
if essential_type.split(' ')[0] in ('unsigned', 'signed'):
24612461
return essential_type.split(' ')[0]
24622462
return None
2463+
24632464
for tok in cfg.tokenlist:
2464-
if tok.isAssignmentOp:
2465-
lhs = getEssentialType(tok.astOperand1)
2466-
rhs = getEssentialType(tok.astOperand2)
2467-
#print(lhs)
2468-
#print(rhs)
2469-
if lhs is None or rhs is None:
2465+
if not tok.isAssignmentOp:
2466+
continue
2467+
2468+
lhs = getEssentialType(tok.astOperand1)
2469+
rhs = getEssentialType(tok.astOperand2)
2470+
if lhs is None or rhs is None:
2471+
continue
2472+
2473+
find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion
2474+
2475+
rhs_tok = tok.astOperand2
2476+
rhs_macro_name = rhs_tok.macroName if rhs_tok else None
2477+
rhs_spelling = rhs_macro_name if rhs_macro_name in ('true', 'false') else rhs_tok.str
2478+
2479+
rhs_is_source_bool_literal = rhs_spelling in ('true', 'false')
2480+
rhs_is_source_int_literal_0_1 = rhs_spelling in ('0', '1')
2481+
2482+
if lhs == 'bool':
2483+
if rhs_is_source_bool_literal:
24702484
continue
2471-
lhs_category = get_category(lhs)
2472-
rhs_category = get_category(rhs)
2473-
if lhs_category and rhs_category and lhs_category != rhs_category and rhs_category not in ('signed','unsigned'):
2474-
self.reportError(tok, 10, 3)
2475-
find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion
2476-
allow_bool_literal_0_1 = (
2477-
find_std == "c89" and
2478-
lhs == "bool" and
2479-
tok.astOperand2 and
2480-
tok.astOperand2.str in ('0', '1')
2481-
)
2482-
if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs) and not allow_bool_literal_0_1:
2483-
self.reportError(tok, 10, 3)
2485+
if find_std == 'c89' and rhs_is_source_int_literal_0_1:
2486+
continue
2487+
2488+
lhs_category = get_category(lhs)
2489+
rhs_category = get_category(rhs)
2490+
if lhs_category and rhs_category and lhs_category != rhs_category and rhs_category not in ('signed', 'unsigned'):
2491+
self.reportError(tok, 10, 3)
2492+
2493+
if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs):
2494+
self.reportError(tok, 10, 3)
24842495

24852496
def misra_10_4(self, data):
24862497
op = {'+', '-', '*', '/', '%', '&', '|', '^', '+=', '-=', ':'}

addons/test/misra/misra-test-c11.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ static void misra_10_3_c11(void) {
2929
bool b = false;
3030
bool b0 = 0; // 10.3
3131
bool b1 = 1; // 10.3
32+
bool bf = false; // no-warning
33+
bool bt = true; // no-warning
3234
b = 0; // 10.3
3335
b = 1; // 10.3
3436
b = false; // no-warning

0 commit comments

Comments
 (0)