Skip to content

Commit 263fd04

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

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

‎addons/misra.py‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4907,18 +4907,18 @@ def analyse_ctu_info(self, ctu_info_files):
49074907
from cppcheckdata import Location
49084908

49094909
def is_different_location(loc1, loc2):
4910-
return loc1['file'] != loc2['file'] or loc1['line'] != loc2['line']
4910+
return is_different_file(loc1, loc2) or loc1['line'] != loc2['line']
49114911

49124912
def is_different_file(loc1, loc2):
4913-
return loc1['file'] != loc2['file']
4913+
return os.path.realpath(loc1['file']) != os.path.realpath(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: 15 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
@@ -206,6 +208,18 @@ def test_ctu_unused_declaration(checker, tmp_path, summary, rule, used_first):
206208
assert 'a.c-1-13-%d' % rule in checker.existing_violations
207209
assert 'b.c-1-13-%d' % rule not in checker.existing_violations
208210

211+
@pytest.mark.parametrize('summary', ['MisraTypedefInfo', 'MisraTagName', 'MisraMacro'])
212+
def test_ctu_declaration_path_spelling(checker, tmp_path, summary):
213+
# The same declaration in h.h, reached through a relative and an absolute path
214+
unused = {'name': 'X', 'file': 'h.h', 'line': 1, 'column': 13, 'used': False}
215+
used = {'name': 'X', 'file': os.path.abspath('h.h'), 'line': 1, 'column': 13, 'used': True}
216+
ctu_info = tmp_path / 'test.ctu-info'
217+
with open(ctu_info, 'wt') as f:
218+
for declaration in [unused, used]:
219+
f.write(json.dumps({'summary': summary, 'data': [declaration]}) + '\n')
220+
checker.analyse_ctu_info([str(ctu_info)])
221+
assert not checker.existing_violations
222+
209223
def test_platform(checker):
210224
test_file = os.path.join('addons','test','misra','misra-test.c')
211225
dump_create(test_file, "--language=c++")

0 commit comments

Comments
 (0)