Skip to content

Fix #15213 (misra.py: false negative 2.3 and 2.4 depending on the order of the files) - #9053

Merged
danmar merged 1 commit into
cppcheck-opensource:mainfrom
g-chauvel:misra-ctu-declarations
Nov 22, 2026
Merged

danmar merged 1 commit into
cppcheck-opensource:mainfrom
g-chauvel:misra-ctu-declarations

Conversation

@g-chauvel

Copy link
Copy Markdown
Contributor

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.

  • The order is the order of the files.
  • With -j2 or 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.c

typedef int T;
struct S { int x; };

b.c

typedef int T;
struct S { int x; };
T v;
struct S s;
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-5.* --suppress=misra-c2012-8.4 --suppress=unusedStructMember b.c a.c

Before PR: no output.

After PR:

a.c:1:13: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.3]
a.c:2:10: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.4]

Before PR, a.c b.c reports them, and with -j2 they are missing in most runs, in both orders.

@dmcppcheck dmcppcheck left a comment

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

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 cppcheck from pr/ with make -j8 cppcheck.

  • Ran PYTHONPATH=addons python3 -m pytest -q addons/test/misra_test.py: 19 passed, including the new test_ctu_unused_typedef and test_ctu_unused_tag. Without PYTHONPATH the tests are not collected, because the cppcheckdata module is not found.

  • Ran the description's example (a.c/b.c) as b.c a.c, as a.c b.c, and three times as -j2 b.c a.c. Every run reported a.c:1:13 [misra-c2012-2.3] and a.c:2:10 [misra-c2012-2.4], and nothing in b.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.h from sub/b.c.
    • Two function-local struct L with 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.


Automated review of 1336af8 by Claude. The pull request was built and tested locally.

@g-chauvel
g-chauvel force-pushed the misra-ctu-declarations branch from 1336af8 to 6416745 Compare November 21, 2026 11:01

@dmcppcheck dmcppcheck left a comment

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

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 cppcheck in both pr/ and base/ with make -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 new test_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 declare struct L {int a;};. In each pair one is used and one is not. The PR newly reports 2.3 at a.c:2:16 and 2.4 at a.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; in a.c, b.c and c.c, used only in c.c: base reports 2.3 for a.c only. The PR reports it for a.c and b.c, which is correct. The 5.6 reports are unchanged.
    • Header with #ifdef A / #else versions of T and struct 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.

Automated review of 6416745 by Claude. The pull request was built and tested locally.

@danmar
danmar merged commit 0157282 into cppcheck-opensource:main Nov 22, 2026
74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants