Skip to content

Commit bd55888

Browse files
committed
Fix #15214 (misra.py: false negative 2.5 when several files define the same macro name)
1 parent 64cee51 commit bd55888

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

‎addons/misra.py‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4912,13 +4912,13 @@ def is_different_location(loc1, loc2):
49124912
def is_different_file(loc1, loc2):
49134913
return loc1['file'] != loc2['file']
49144914

4915-
def add_declaration(all_info, new_info, num1, num2):
4915+
def add_declaration(all_info, new_info, num1=None, num2=None):
49164916
declarations = all_info.setdefault(new_info['name'], [])
49174917
for info in declarations:
49184918
if not is_different_location(info, new_info):
49194919
info['used'] = info['used'] or new_info['used']
49204920
return
4921-
if declarations:
4921+
if declarations and num1 is not None:
49224922
self.reportError(Location(declarations[0]), num1, num2)
49234923
self.reportError(Location(new_info), num1, num2)
49244924
declarations.append(new_info)
@@ -4946,12 +4946,7 @@ def read_lines(filename):
49464946

49474947
if summary_type == 'MisraMacro':
49484948
for new_macro in summary_data:
4949-
key = new_macro['name']
4950-
existing_macro = all_macro_info.get(key, None)
4951-
if existing_macro:
4952-
existing_macro['used'] = existing_macro['used'] or new_macro['used']
4953-
else:
4954-
all_macro_info[key] = new_macro
4949+
add_declaration(all_macro_info, new_macro)
49554950

49564951
if summary_type == 'MisraExternalIdentifiers':
49574952
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):
50014996
for tag in unused_tags:
50024997
self.reportError(Location(tag), 2, 4)
50034998

5004-
unused_macros = [m for m in all_macro_info.values() if not m['used']]
4999+
unused_macros = [m for ms in all_macro_info.values() for m in ms if not m['used']]
50055000
for m in unused_macros:
50065001
self.reportError(Location(m), 2, 5)
50075002

‎addons/test/misra_test.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ def test_read_ctu_info_line(checker):
192192
('MisraTypedefInfo', 203, False),
193193
('MisraTypedefInfo', 203, True),
194194
('MisraTagName', 204, False),
195-
('MisraTagName', 204, True)
195+
('MisraTagName', 204, True),
196+
('MisraMacro', 205, False),
197+
('MisraMacro', 205, True)
196198
])
197199
def test_ctu_unused_declaration(checker, tmp_path, summary, rule, used_first):
198200
# The same name declared in two files, unused in a.c and used in b.c

0 commit comments

Comments
 (0)