From 1b8881cd80e1e4f9e0bbd00135ea5da4dd69ca62 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Mon, 30 Nov 2026 17:56:26 +0100 Subject: [PATCH 1/2] Fix #15292 (Whole program analysis hangs with --cppcheck-build-dir when 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 --- lib/ctu.cpp | 17 ++++++++++++++--- test/cli/performance_test.py | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/ctu.cpp b/lib/ctu.cpp index 3346329dc65..498d501cfb3 100644 --- a/lib/ctu.cpp +++ b/lib/ctu.cpp @@ -32,7 +32,9 @@ #include #include #include // back_inserter +#include #include +#include #include #include "xml.h" @@ -507,11 +509,19 @@ static bool findPath(const std::string &callId, const CTU::FileInfo::CallBase *path[10], int index, bool warning, - int maxCtuDepth) + int maxCtuDepth, + std::set> &visited) { if (index >= maxCtuDepth) return false; // TODO: add bailout message? + // Within one search the result only depends on callId, callArgNr and index. + // A repeated state has failed before (a success ends the whole search). + // Without this the search is exponential when the same calls are reachable + // on many paths, for example calls in a header that many files include. + if (!visited.emplace(callId, callArgNr, index).second) + return false; + const auto it = callsMap.find(callId); if (it == callsMap.end()) return false; @@ -550,7 +560,7 @@ static bool findPath(const std::string &callId, if (!nestedCall) continue; - if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth)) { + if (findPath(nestedCall->myId, nestedCall->myArgNr, unsafeValue, invalidValue, callsMap, path, index + 1, warning, maxCtuDepth, visited)) { path[index] = nestedCall; return true; } @@ -584,7 +594,8 @@ std::list CTU::FileInfo::getErrorPath(InvalidValueTy { const CTU::FileInfo::CallBase *path[10] = {nullptr}; - if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth)) + std::set> visited; + if (!findPath(unsafeUsage.myId, unsafeUsage.myArgNr, unsafeUsage.value, invalidValue, callsMap, path, 0, warning, maxCtuDepth, visited)) return {}; if (unknownFunctionReturn && path[0]) { diff --git a/test/cli/performance_test.py b/test/cli/performance_test.py index ea1ad8bd5ff..d63e6fb30df 100644 --- a/test/cli/performance_test.py +++ b/test/cli/performance_test.py @@ -478,3 +478,25 @@ def test_large_number_of_violations_and_suppressions(tmpdir): f.write(f'misra-c2012-21.1:**/{i}/{header_name}\n') cppcheck([filename_main] + ['--addon=misra.py', '--check-level=exhaustive', '--enable=all', f'--suppressions-list={suppressions_file}']) + + +@pytest.mark.timeout(20) +def test_slow_ctu_header_included_by_many_files(tmpdir): + # whole program analysis was very slow when a header with nested calls is included by many files + with open(os.path.join(tmpdir, 'header.h'), 'wt') as f: + f.write("inline int digit(const char *s, int i) { return s[i] - '0'; }\n" + "inline int two(const char *s, int i) { return digit(s, i) * 10 + digit(s, i + 1); }\n" + "inline int four(const char *s, int i) { return two(s, i) * 100 + two(s, i + 2); }\n") + files = [] + for i in range(2000): + filename = f'test{i}.cpp' + with open(os.path.join(tmpdir, filename), 'wt') as f: + f.write('#include "header.h"\n' + f'int f{i}(void) {{ return four("2026", 0); }}\n') + files.append(filename) + build_dir = os.path.join(tmpdir, 'b1') + os.mkdir(build_dir) + ret, stdout, stderr = cppcheck(['-q', '-j2', f'--cppcheck-build-dir={build_dir}'] + files, cwd=tmpdir) # should not take more than ~3 seconds + assert ret == 0, stdout + assert stdout == '' + assert stderr == '' From 68812d62e945eeac86c19df9cf77d464e910dca5 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Mon, 30 Nov 2026 19:06:55 +0100 Subject: [PATCH 2/2] Use a deeper call chain with fewer files in the performance test 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 --- test/cli/performance_test.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/test/cli/performance_test.py b/test/cli/performance_test.py index d63e6fb30df..e45f43a8cdf 100644 --- a/test/cli/performance_test.py +++ b/test/cli/performance_test.py @@ -482,21 +482,22 @@ def test_large_number_of_violations_and_suppressions(tmpdir): @pytest.mark.timeout(20) def test_slow_ctu_header_included_by_many_files(tmpdir): - # whole program analysis was very slow when a header with nested calls is included by many files + # 15292: whole program analysis was very slow when a header with nested calls is included by many files with open(os.path.join(tmpdir, 'header.h'), 'wt') as f: - f.write("inline int digit(const char *s, int i) { return s[i] - '0'; }\n" - "inline int two(const char *s, int i) { return digit(s, i) * 10 + digit(s, i + 1); }\n" - "inline int four(const char *s, int i) { return two(s, i) * 100 + two(s, i + 2); }\n") + f.write("inline int f0(const char *s, int i) { return s[i] - '0'; }\n") + for level in range(1, 5): + f.write(f'inline int f{level}(const char *s, int i) {{ return f{level - 1}(s, i) + f{level - 1}(s, i + 1); }}\n') files = [] - for i in range(2000): + for i in range(100): filename = f'test{i}.cpp' with open(os.path.join(tmpdir, filename), 'wt') as f: f.write('#include "header.h"\n' - f'int f{i}(void) {{ return four("2026", 0); }}\n') + f'int g{i}(void) {{ return f4("0123456789abcdef", 0); }}\n') files.append(filename) build_dir = os.path.join(tmpdir, 'b1') os.mkdir(build_dir) - ret, stdout, stderr = cppcheck(['-q', '-j2', f'--cppcheck-build-dir={build_dir}'] + files, cwd=tmpdir) # should not take more than ~3 seconds + args = ['-q', '-j2', '--max-ctu-depth=4', f'--cppcheck-build-dir={build_dir}'] + files + ret, stdout, stderr = cppcheck(args, cwd=tmpdir) # should not take more than ~1 second assert ret == 0, stdout assert stdout == '' assert stderr == ''