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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -4913,15 +4913,16 @@ def is_different_file(loc1, loc2):
return loc1['file'] != loc2['file']

def add_declaration(all_info, new_info, num1, num2):
declarations = all_info.setdefault(new_info['name'], [])
for info in declarations:
if not is_different_location(info, new_info):
info['used'] = info['used'] or new_info['used']
return
declarations = all_info.setdefault(new_info['name'], {})
key = (new_info['file'], new_info['line'])
info = declarations.get(key)
if info:
info['used'] = info['used'] or new_info['used']
return
if declarations:
self.reportError(Location(declarations[0]), num1, num2)
self.reportError(Location(next(iter(declarations.values()))), num1, num2)
self.reportError(Location(new_info), num1, num2)
declarations.append(new_info)
declarations[key] = new_info

def read_lines(filename):
with open(filename, 'rt') as f:
Expand Down Expand Up @@ -4993,11 +4994,11 @@ def read_lines(filename):
except FileNotFoundError:
return

unused_typedefs = [tdi for tdis in all_typedef_info.values() for tdi in tdis if not tdi['used']]
unused_typedefs = [tdi for tdis in all_typedef_info.values() for tdi in tdis.values() if not tdi['used']]
for tdi in unused_typedefs:
self.reportError(Location(tdi), 2, 3)

unused_tags = [tag for tags in all_tagname_info.values() for tag in tags if not tag['used']]
unused_tags = [tag for tags in all_tagname_info.values() for tag in tags.values() if not tag['used']]
for tag in unused_tags:
self.reportError(Location(tag), 2, 4)

Expand Down
20 changes: 20 additions & 0 deletions addons/test/misra_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ def test_ctu_unused_declaration(checker, tmp_path, summary, rule, used_first):
assert 'a.c-1-13-%d' % rule in checker.existing_violations
assert 'b.c-1-13-%d' % rule not in checker.existing_violations

@pytest.mark.parametrize('summary,rule_unique,rule_unused', [
('MisraTypedefInfo', 506, 203),
('MisraTagName', 507, 204)
])
def test_ctu_declaration_location(checker, tmp_path, summary, rule_unique, rule_unused):
# The same name declared at a.h:1 and b.h:1 (both included by two files) and at b.h:2
ctu_info = tmp_path / 'test.ctu-info'
with open(ctu_info, 'wt') as f:
for declaration in [{'name': 'X', 'file': 'a.h', 'line': 1, 'column': 13, 'used': False},
{'name': 'X', 'file': 'a.h', 'line': 1, 'column': 13, 'used': True},
{'name': 'X', 'file': 'b.h', 'line': 1, 'column': 13, 'used': True},
{'name': 'X', 'file': 'b.h', 'line': 1, 'column': 13, 'used': False},
{'name': 'X', 'file': 'b.h', 'line': 2, 'column': 13, 'used': False}]:
f.write(json.dumps({'summary': summary, 'data': [declaration]}) + '\n')
checker.analyse_ctu_info([str(ctu_info)])
assert checker.existing_violations == {'a.h-1-13-%d' % rule_unique,
'b.h-1-13-%d' % rule_unique,
'b.h-2-13-%d' % rule_unique,
'b.h-2-13-%d' % rule_unused}

def test_platform(checker):
test_file = os.path.join('addons','test','misra','misra-test.c')
dump_create(test_file, "--language=c++")
Expand Down
Loading