Repository navigation
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix works for the case in the description, and with -j2 the output is now the same on every run. However, the PR adds a false positive for misra 2.5. Merging is now by file+line instead of by macro name. When two translation units reach the same header through different path spellings, that header's macro gets two entries. One example is an absolute -I path, which is common with compile_commands.json, in one file and a relative #include "../inc/h.h" in another. If only one of those TUs uses the macro, it is reported as unused. The base version reports nothing. Rules 2.3 and 2.4 already have this problem: a typedef in the same setup gets a 2.3 and also 5.6 on the base version. With this PR, 2.5 has it too. Normalising the path (for example os.path.realpath) in is_different_location would fix all three rules.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Macro merge by file+line gives 2.5 false positive when a header path is spelled differently | AI review | addons/misra.py:4949 |
🆕 New |
@g-chauvel, please fix, resolve or reply to the open and new comments: #1.
Testing
- Built
cppcheckfrompr/withmake -j8 cppcheck. The PR only changes Python, sotestrunnerwas not needed. PYTHONPATH=addons python3 -m pytest addons/test/misra_test.py: all 23 pass, including the newMisraMacrocases.pytest -k misra test/cli: 10 pass.- PR example (
m1.c/m2.c):m1.c:1:0 ... [misra-c2012-2.5], as the description says. Ifm3.cdefinesMand does not use it,-j2 m1.c m3.creports bothm1.c:1andm3.c:1, the same on 3 runs. - A header included through different relative spellings (
h.h,../h.h,../sub/../h.h) and used in only one TU: no 2.5 warning, which is correct, because simplecpp normalises those paths.
False positive (regression):
mkdir -p t2/inc t2/src && cd t2
printf '#define HM 3\n' > inc/h.h
printf '#include "h.h"\nint a1(void);\n' > src/a.c
printf '#include "../inc/h.h"\nint b1 = HM;\n' > src/b.c
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 --suppress=misra-c2012-20.1 -I$PWD/inc src/a.c src/b.c
PR:
/work/scratch/t2/inc/h.h:1:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]
Base: no output. HM is used in src/b.c.
Passing one source file as an absolute path and the other as a relative one ($PWD/a.c sub/b.c, both including h.h) gives the same false positive, again only with the PR. The same setup with a typedef header already gives 2.3 and 5.6 on the base version, so the path comparison is a known weakness that this PR extends to 2.5.
Automated review of bd55888 by Claude. The pull request was built and tested locally.
| existing_macro['used'] = existing_macro['used'] or new_macro['used'] | ||
| else: | ||
| all_macro_info[key] = new_macro | ||
| add_declaration(all_macro_info, new_macro) |
There was a problem hiding this comment.
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.
…e same macro name)
bd55888 to
263fd04
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The PR adds a new false positive for misra 2.5. For each macro name, misra_2_5() keeps only the last #define directive of a TU, and it decides used by name. Inactive directives count too. Before this PR the CTU merge also went by name, so this did not matter. Now the merge goes by location. Suppose a TU uses a macro from config.h while a later #ifndef X / #define X fallback in another header also defines it. That TU then records only the fallback's location as used, and a second TU that includes config.h without using the macro gets a 2.5 warning on config.h. The base version reports nothing. The path-spelling problem from the earlier review is fixed.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Macro merge by file+line gives 2.5 false positive when a header path is spelled differently | AI review | addons/misra.py:4949 |
✅ Fixed |
| 2 | 2.5 false positive: per-TU summary keeps only the last #define per name, so the macro that is really used can look unused | AI review | addons/misra.py:4949 |
🆕 New |
@g-chauvel, please fix, resolve or reply to the open and new comments: #2.
Testing
- Built
cppcheckinpr/withmake -j8 cppcheck. The PR only changes Python, sotestrunnerwas not needed. To compare with the base version I ran the same binary withbase/addons/misra.py. PYTHONPATH=addons python3 -m pytest addons/test/misra_test.py: all 26 pass.- I re-ran the repro from the earlier review (absolute
-Ipath in one TU,#include "../inc/h.h"in the other): no 2.5 warning any more.
False positive (regression):
printf '#define BUFSIZE 2\n' > config.h
printf '#ifndef BUFSIZE\n#define BUFSIZE 5\n#endif\n' > defaults.h
printf '#include "config.h"\n#include "defaults.h"\nint x = BUFSIZE;\n' > a.c
printf '#include "config.h"\nint b1(void);\n' > b.c
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 a.c b.c
PR, with both -j1 and -j2:
config.h:1:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]
Base addon: no output. a.c uses BUFSIZE from config.h:1: the dump's macro-usage points there. But a.c's summary only has defaults.h:2, which is inactive.
Automated review of 263fd04 by Claude. The pull request was built and tested locally.
| existing_macro['used'] = existing_macro['used'] or new_macro['used'] | ||
| else: | ||
| all_macro_info[key] = new_macro | ||
| add_declaration(all_macro_info, new_macro) |
There was a problem hiding this comment.
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.
Follow-up of #9053 for 2.5:
analyse_ctu_info()merges the definitions of a macro name into the first one read.This PR uses the same
add_declaration()as 2.3 and 2.4, so each unused definition is reported.m1.cm2.cBefore PR: no output.
After PR:
When
m2.cdoes not useM, only the definition of the first file is reported, and with-j2it changes from one run to the next.