Skip to content

Fix #15292 Whole program analysis hangs with --cppcheck-build-dir when a header with nested calls is included by many files - #9093

Open
autoantwort wants to merge 2 commits into
cppcheck-opensource:mainfrom
autoantwort:fix-ctu-findpath-memo
Open

autoantwort wants to merge 2 commits into
cppcheck-opensource:mainfrom
autoantwort:fix-ctu-findpath-memo

Conversation

@autoantwort

@autoantwort autoantwort commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

https://trac.cppcheck.net/ticket/15292

Since #9024 nested calls are loaded from the build dir. With --cppcheck-build-dir, the whole program analysis can now hang when a header with nested calls is included by many files.

Reproducer:

cat > header.h <<'H'
inline int digit(const char *s, int i) { return s[i] - '0'; }
inline int two(const char *s, int i) { return digit(s, i) * 10 + digit(s, i + 1); }
inline int four(const char *s, int i) { return two(s, i) * 100 + two(s, i + 2); }
H
for i in $(seq 1 1000); do printf '#include "header.h"\nint f%d(void) { return four("2026", 0); }\n' $i > test$i.cpp; done
mkdir b
time cppcheck -q -j16 --cppcheck-build-dir=b test*.cpp

Cause: with a build dir every file brings its own copy of the calls in the header, so each header function has N entries in the calls map. findPath() tries all of them on every level and does not remember searches that failed. One unsafe usage costs about N² steps with the default --max-ctu-depth=2, and there is one unsafe usage per file.

Fix: findPath() remembers the (callId, callArgNr, index) states it has visited during one search. Within one search the result of a state only depends on these three values, and a success ends the whole search, so a repeated state has failed before and can be skipped. The paths that are found and reported do not change.

N files before #9024 main this PR
250 0.1 s 0.5 s 0.1 s
500 0.2 s 3.5 s 0.2 s
1000 0.3 s 27.3 s 0.4 s
4000 3.3 s

On the real project from the ticket (5676 files, one header included by 4434 files) the whole program analysis did not finish within 2.5 hours on main. With this PR it takes 38 s (6.6 s before #9024) and reports the same results as before #9024, see the comment below.

Test: test_slow_ctu_header_included_by_many_files in test/cli/performance_test.py (100 files, a header with a chain of 5 functions, --max-ctu-depth=4): 0.13 s with this PR, hits the 20 s timeout without it.

Limitations: the remaining cost is still quadratic in the number of files that include the header (N unsafe usages × N entries in the calls map). Deduplicating identical calls and unsafe usages from different files would remove that, but is not part of this PR.

This PR was created by Claude Code (an AI coding agent) on behalf of @autoantwort.

🤖 Generated with Claude Code

autoantwort and others added 2 commits November 30, 2026 17:56
…en a header with nested calls is included by many files)

With a build dir every file brings its own copy of the calls in a
header, so findPath() tried each of them on every level and searched
the same failed states again and again. Remember the visited states
during one search.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test with 2000 files hit the timeout on slow CI runners even with
the fix. 100 files with --max-ctu-depth=4 take 0.1 s with the fix and
more than a minute without it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@autoantwort

Copy link
Copy Markdown
Contributor Author

Result on the real project from the ticket (5676 files, Debug|Win32, --enable=all --check-level=exhaustive, --cppcheck-build-dir):

before #9024 (b3d8256^) main this PR
whole program analysis 6.6 s did not finish within 2.5 h 38 s
ctuOneDefinitionRuleViolation 138 138
ctunullpointer / ctuuninitvar / ctuArrayIndex 0 0

The remaining difference to before #9024 comes from the nested calls that are now loaded and the quadratic part mentioned in the description. The other differences in the results (missingReturn, uninitStructMember) come from other commits between b3d8256^ and main.

The first version of the performance test (2000 files) hit the timeout on the slower CI runners even with the fix. It now uses 100 files with a deeper call chain and --max-ctu-depth=4: 0.13 s with this PR, timeout without it.

(Written by Claude Code on behalf of @autoantwort.)

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.

1 participant