Skip to content

Commit 79e72ce

Browse files
committed
Follow up #15213 (misra.py: the CTU step takes quadratic time when a typedef or tag name is declared at many locations)
1 parent 6a4fedd commit 79e72ce

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎addons/misra.py‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4913,15 +4913,16 @@ def is_different_file(loc1, loc2):
49134913
return loc1['file'] != loc2['file']
49144914

49154915
def add_declaration(all_info, new_info, num1, num2):
4916-
declarations = all_info.setdefault(new_info['name'], [])
4917-
for info in declarations:
4918-
if not is_different_location(info, new_info):
4919-
info['used'] = info['used'] or new_info['used']
4920-
return
4916+
declarations = all_info.setdefault(new_info['name'], {})
4917+
key = (new_info['file'], new_info['line'])
4918+
info = declarations.get(key)
4919+
if info:
4920+
info['used'] = info['used'] or new_info['used']
4921+
return
49214922
if declarations:
4922-
self.reportError(Location(declarations[0]), num1, num2)
4923+
self.reportError(Location(next(iter(declarations.values()))), num1, num2)
49234924
self.reportError(Location(new_info), num1, num2)
4924-
declarations.append(new_info)
4925+
declarations[key] = new_info
49254926

49264927
def read_lines(filename):
49274928
with open(filename, 'rt') as f:
@@ -4993,11 +4994,11 @@ def read_lines(filename):
49934994
except FileNotFoundError:
49944995
return
49954996

4996-
unused_typedefs = [tdi for tdis in all_typedef_info.values() for tdi in tdis if not tdi['used']]
4997+
unused_typedefs = [tdi for tdis in all_typedef_info.values() for tdi in tdis.values() if not tdi['used']]
49974998
for tdi in unused_typedefs:
49984999
self.reportError(Location(tdi), 2, 3)
49995000

5000-
unused_tags = [tag for tags in all_tagname_info.values() for tag in tags if not tag['used']]
5001+
unused_tags = [tag for tags in all_tagname_info.values() for tag in tags.values() if not tag['used']]
50015002
for tag in unused_tags:
50025003
self.reportError(Location(tag), 2, 4)
50035004

‎addons/test/misra_test.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ def test_ctu_unused_declaration(checker, tmp_path, summary, rule, used_first):
206206
assert 'a.c-1-13-%d' % rule in checker.existing_violations
207207
assert 'b.c-1-13-%d' % rule not in checker.existing_violations
208208

209+
@pytest.mark.parametrize('summary,rule_unique,rule_unused', [
210+
('MisraTypedefInfo', 506, 203),
211+
('MisraTagName', 507, 204)
212+
])
213+
def test_ctu_declaration_location(checker, tmp_path, summary, rule_unique, rule_unused):
214+
# The same name declared at a.h:1 and b.h:1 (both included by two files) and at b.h:2
215+
ctu_info = tmp_path / 'test.ctu-info'
216+
with open(ctu_info, 'wt') as f:
217+
for declaration in [{'name': 'X', 'file': 'a.h', 'line': 1, 'column': 13, 'used': False},
218+
{'name': 'X', 'file': 'a.h', 'line': 1, 'column': 13, 'used': True},
219+
{'name': 'X', 'file': 'b.h', 'line': 1, 'column': 13, 'used': True},
220+
{'name': 'X', 'file': 'b.h', 'line': 1, 'column': 13, 'used': False},
221+
{'name': 'X', 'file': 'b.h', 'line': 2, 'column': 13, 'used': False}]:
222+
f.write(json.dumps({'summary': summary, 'data': [declaration]}) + '\n')
223+
checker.analyse_ctu_info([str(ctu_info)])
224+
assert checker.existing_violations == {'a.h-1-13-%d' % rule_unique,
225+
'b.h-1-13-%d' % rule_unique,
226+
'b.h-2-13-%d' % rule_unique,
227+
'b.h-2-13-%d' % rule_unused}
228+
209229
def test_platform(checker):
210230
test_file = os.path.join('addons','test','misra','misra-test.c')
211231
dump_create(test_file, "--language=c++")

0 commit comments

Comments
 (0)