From 263fd0402a8a63849154ad4ebfc0472c71a539e8 Mon Sep 17 00:00:00 2001 From: Guillaume Chauvel Date: Tue, 3 Nov 2026 21:52:32 +0100 Subject: [PATCH] Fix #15214 (misra.py: false negative 2.5 when several files define the same macro name) --- addons/misra.py | 17 ++++++----------- addons/test/misra_test.py | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index b337d2dfa5f..c5be73fdeb6 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -4907,18 +4907,18 @@ def analyse_ctu_info(self, ctu_info_files): from cppcheckdata import Location def is_different_location(loc1, loc2): - return loc1['file'] != loc2['file'] or loc1['line'] != loc2['line'] + return is_different_file(loc1, loc2) or loc1['line'] != loc2['line'] def is_different_file(loc1, loc2): - return loc1['file'] != loc2['file'] + return os.path.realpath(loc1['file']) != os.path.realpath(loc2['file']) - def add_declaration(all_info, new_info, num1, num2): + def add_declaration(all_info, new_info, num1=None, num2=None): 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 - if declarations: + if declarations and num1 is not None: self.reportError(Location(declarations[0]), num1, num2) self.reportError(Location(new_info), num1, num2) declarations.append(new_info) @@ -4946,12 +4946,7 @@ def read_lines(filename): if summary_type == 'MisraMacro': for new_macro in summary_data: - key = new_macro['name'] - existing_macro = all_macro_info.get(key, None) - if existing_macro: - existing_macro['used'] = existing_macro['used'] or new_macro['used'] - else: - all_macro_info[key] = new_macro + add_declaration(all_macro_info, new_macro) if summary_type == 'MisraExternalIdentifiers': for s in sorted(summary_data, key=lambda d: "%s %s %s" %(d['file'],d['line'], d['column'] )): @@ -5001,7 +4996,7 @@ def read_lines(filename): for tag in unused_tags: self.reportError(Location(tag), 2, 4) - unused_macros = [m for m in all_macro_info.values() if not m['used']] + unused_macros = [m for ms in all_macro_info.values() for m in ms if not m['used']] for m in unused_macros: self.reportError(Location(m), 2, 5) diff --git a/addons/test/misra_test.py b/addons/test/misra_test.py index 1ff63879efe..9277b7360e7 100644 --- a/addons/test/misra_test.py +++ b/addons/test/misra_test.py @@ -192,7 +192,9 @@ def test_read_ctu_info_line(checker): ('MisraTypedefInfo', 203, False), ('MisraTypedefInfo', 203, True), ('MisraTagName', 204, False), - ('MisraTagName', 204, True) + ('MisraTagName', 204, True), + ('MisraMacro', 205, False), + ('MisraMacro', 205, True) ]) def test_ctu_unused_declaration(checker, tmp_path, summary, rule, used_first): # The same name declared in two files, unused in a.c and used in b.c @@ -206,6 +208,18 @@ 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', ['MisraTypedefInfo', 'MisraTagName', 'MisraMacro']) +def test_ctu_declaration_path_spelling(checker, tmp_path, summary): + # The same declaration in h.h, reached through a relative and an absolute path + unused = {'name': 'X', 'file': 'h.h', 'line': 1, 'column': 13, 'used': False} + used = {'name': 'X', 'file': os.path.abspath('h.h'), 'line': 1, 'column': 13, 'used': True} + ctu_info = tmp_path / 'test.ctu-info' + with open(ctu_info, 'wt') as f: + for declaration in [unused, used]: + f.write(json.dumps({'summary': summary, 'data': [declaration]}) + '\n') + checker.analyse_ctu_info([str(ctu_info)]) + assert not checker.existing_violations + def test_platform(checker): test_file = os.path.join('addons','test','misra','misra-test.c') dump_create(test_file, "--language=c++")