From 79e72cec6bc12467bd02935e360f3930e4595060 Mon Sep 17 00:00:00 2001 From: Guillaume Chauvel Date: Mon, 30 Nov 2026 18:58:07 +0100 Subject: [PATCH] Follow up #15213 (misra.py: the CTU step takes quadratic time when a typedef or tag name is declared at many locations) --- addons/misra.py | 19 ++++++++++--------- addons/test/misra_test.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index b337d2dfa5f..c379fa89851 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -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: @@ -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) diff --git a/addons/test/misra_test.py b/addons/test/misra_test.py index 1ff63879efe..78c2405ac84 100644 --- a/addons/test/misra_test.py +++ b/addons/test/misra_test.py @@ -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++")