Repository navigation
Fix #15213 (misra.py: false negative 2.3 and 2.4 depending on the order of the files) - #9053
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no problems in this PR. analyse_ctu_info() now keeps a list of declarations for each name. Declarations at the same file and line are merged, as before. Declarations at different locations are all kept and checked for 2.3 and 2.4. The 5.6 and 5.7 reports are unchanged: each new duplicate is still reported together with the first declaration. With the PR, the example from the description gives the same result in both file orders and with -j2. The new unit tests cover both read orders for typedefs and for tags.
Testing
-
Built
cppcheckfrompr/withmake -j8 cppcheck. -
Ran
PYTHONPATH=addons python3 -m pytest -q addons/test/misra_test.py: 19 passed, including the newtest_ctu_unused_typedefandtest_ctu_unused_tag. WithoutPYTHONPATHthe tests are not collected, because thecppcheckdatamodule is not found. -
Ran the description's example (
a.c/b.c) asb.c a.c, asa.c b.c, and three times as-j2 b.c a.c. Every run reporteda.c:1:13 [misra-c2012-2.3]anda.c:2:10 [misra-c2012-2.4], and nothing inb.c. -
Tried cases that could give false positives:
- A typedef and a struct in a shared
h.h, used in one translation unit only, in both file orders. - The same header included as
../h.hfromsub/b.c. - Two function-local
struct Lwith the same tag in one file.
None of them gave a 2.3 or 2.4 report. Including the header by two different paths did not split it into two declarations.
- A typedef and a struct in a shared
Automated review of 1336af8 by Claude. The pull request was built and tested locally.
1336af8 to
6416745
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. With the PR, analyse_ctu_info() keeps every distinct declaration of a typedef or tag name, so each unused declaration is reported for 2.3 or 2.4. The 5.6 and 5.7 output is the same as on base in every case I tried. When the same declaration is read from several translation units, its used flags are still merged, so this change adds no false positives. Since the earlier automated review, the tests were combined into the parametrized test_ctu_unused_declaration, which covers both read orders for typedefs and for tags.
Testing
- I built
cppcheckin bothpr/andbase/withmake -j8 cppcheck. Both builds succeeded. PYTHONPATH=addons python3 -m pytest -q addons/test/misra_test.py: 21 passed, including the 4 parametrized cases of the newtest_ctu_unused_declaration.- I compared PR and base on these inputs. The command was
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 ...:- Block-scope duplicates in one file: two functions each declare
typedef int T;and two declarestruct L {int a;};. In each pair one is used and one is not. The PR newly reports 2.3 ata.c:2:16and 2.4 ata.c:4:25, which are the unused ones. The 5.6 and 5.7 reports are the same as on base. - Same
typedef int T;ina.c,b.candc.c, used only inc.c: base reports 2.3 fora.conly. The PR reports it fora.candb.c, which is correct. The 5.6 reports are unchanged. - Header with
#ifdef A/#elseversions ofTandstruct S, used in the TU: neither base nor the PR reports anything. - Header in
inc/included from two TUs, with the include path spelled as-Iinc -I./inc/../inc, used in one TU: the paths are normalized to the same file, so nothing is split into two declarations. There is no output on either base or the PR.
- Block-scope duplicates in one file: two functions each declare
Automated review of 6416745 by Claude. The pull request was built and tested locally.
analyse_ctu_info()keeps only the first declaration read for a typedef or tag name. The other declarations of that name are not checked for 2.3 and 2.4, so an unused one is missed when it is not read first.-j2or more and no build dir, it is the order in which the analysis jobs finish, so the result can change from one run to the next.This PR keeps every distinct declaration of the name, so each unused one is reported.
a.cb.cBefore PR: no output.
After PR:
Before PR,
a.c b.creports them, and with-j2they are missing in most runs, in both orders.