Skip to content

Follow up #15213 (misra.py: the CTU step takes quadratic time when a typedef or tag name is declared at many locations) - #9095

Open
g-chauvel wants to merge 1 commit into
cppcheck-opensource:mainfrom
g-chauvel:misra-ctu-declarations-perf
Open

g-chauvel wants to merge 1 commit into
cppcheck-opensource:mainfrom
g-chauvel:misra-ctu-declarations-perf

Conversation

@g-chauvel

@g-chauvel g-chauvel commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Since #9053, add_declaration() compares each declaration with all the previous ones of the same name, so a name declared at many locations makes the CTU step quadratic for 2.3, 2.4, 5.6 and 5.7. This PR keeps the declarations of a name in a dict keyed by (file, line), the same location test as is_different_location().

With the same typedef name in every source file, on Linux:

mkdir src bd
for i in $(seq 1 20000); do printf 'typedef struct { int x; } ctx_t;\nint f%d(ctx_t *c) { return c->x; }\n' $i > src/m$i.c; done
cppcheck -q -j16 --enable=style --cppcheck-build-dir=bd --addon=misra src > /dev/null 2>&1
ls bd/*.ctu-info > files.txt
time python3 addons/misra.py --file-list files.txt > /dev/null 2>&1
Files Before this PR With this PR
5000 real 0m1.698s real 0m0.193s
10000 real 0m6.362s real 0m0.347s
20000 real 0m24.614s real 0m0.598s

…typedef or tag name is declared at many locations)

@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. The new add_declaration() produces the same results as the old one. It uses the same (file, line) key as is_different_location(). On a duplicate it still reports the first declaration that was stored, because Python dicts keep insertion order, so next(iter(values())) is the old declarations[0]. The used flags are merged the same way as before. The two unused-declaration comprehensions were updated to match the dict. A differential test on random inputs gave the same violations and the same output on base and on the PR. Nobody had commented on the PR before this review.

Testing

  • Built cppcheck in /work/pr with make -j8 cppcheck; the build succeeded. I did not build testrunner, because the PR changes only Python files.
  • PYTHONPATH=addons python3 -m pytest -q addons/test/misra_test.py: 23 passed, including the new test_ctu_declaration_location cases.
  • Differential test (/work/scratch/diff.py):
    • It loads base/addons/misra.py and pr/addons/misra.py side by side.
    • For 3000 seeds it writes a random .ctu-info file with 1 to 8 MisraTypedefInfo/MisraTagName records. The records use random names (X/Y), files (a.h/b.h), lines and columns (1–2) and used flags.
    • It runs analyse_ctu_info with each version and compares the sorted existing_violations and the stdout/stderr.
    • Result: all same, with no difference in any case.
  • I did not time the 20000-file scenario from the description. A dict lookup instead of the linear scan removes the quadratic behaviour.

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

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.

2 participants