Repository navigation
Fix #15214 (misra.py: false negative 2.5 when several files define the same macro name) #9059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
The base version reports nothing. The macro-usage entries in the dump ( |
||
|
|
||
| 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) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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/incwith#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: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.