Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

Before this PR, macros were merged by name, so how the header path was spelled did not matter. Now they are merged by file+line (is_different_location), and the file string differs when TUs reach the same header through different spellings, for example -I$PWD/inc with #include "h.h" in one file and #include "../inc/h.h" in another. The result is two entries, and the TU that does not use the macro gets a 2.5 warning:

/work/scratch/t2/inc/h.h:1:0: style: misra violation ... [misra-c2012-2.5]

The base version reports nothing here. Rules 2.3 and 2.4 already behave like this, and a typedef in the same setup also gets 5.6. Normalising the path in is_different_location (e.g. os.path.realpath(loc['file'])) would avoid the regression and fix the other rules too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

Merging macros by location only works if each TU's summary has every definition with its own used flag. misra_2_5() stores one entry per name in unused_macro[macro_name], and the last directive wins. Inactive #ifndef fallbacks count as well. used is also decided by name. So a TU that uses config.h's macro can record only an inactive fallback in another header:

// config.h
#define BUFSIZE 2
// defaults.h
#ifndef BUFSIZE
#define BUFSIZE 5
#endif
// a.c
#include "config.h"
#include "defaults.h"
int x = BUFSIZE;
// b.c
#include "config.h"
int b1(void);

cppcheck --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 a.c b.c with this PR:

config.h:1:0: style: misra violation ... [misra-c2012-2.5]

The base version reports nothing. The macro-usage entries in the dump (file/line of the definition that was used) could set used per definition, and misra_2_5() should keep every #define directive instead of one per name.


if summary_type == 'MisraExternalIdentifiers':
for s in sorted(summary_data, key=lambda d: "%s %s %s" %(d['file'],d['line'], d['column'] )):
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 15 additions & 1 deletion addons/test/misra_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++")
Expand Down
Loading