Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {'+', '-', '*', '/', '%', '&', '|', '^', '+=', '-=', ':'}
Expand Down
2 changes: 2 additions & 0 deletions addons/test/misra/misra-test-c11.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading