From 6494daafc07b9376cbf303ec551d145ee83a2e35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 5 Nov 2026 09:18:30 +0100 Subject: [PATCH] ValueFlow: skip functions without new values in the next iteration The iterated ValueFlow passes are run again for all functions until no new values are found. A function where no value changed in the previous iteration will not get new values from the intra-function passes, so skip it. Running the passes again on an unchanged function can add a possible value to a comparison, which then keeps a known value from being set later. In 10000 Debian packages (--check-level=exhaustive) this adds 16 correct knownConditionTrueFalse warnings and loses none. Co-Authored-By: Claude Opus 5.5 --- lib/valueflow.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c8c029aa708..ff5e84f4827 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7377,12 +7377,27 @@ struct ValueFlowPassRunner { }); } - bool run(std::initializer_list> passes) const + bool run(std::initializer_list> passes) { std::size_t values = 0; std::size_t n = state.settings().vfOptions.maxIterations; const bool doProgress = state.settings().reportProgress >= 0; + // A function where no value changed in the previous iteration is skipped in the next iteration + const std::set skippedFunctions = state.skippedFunctions; + std::vector functionValues = getFunctionValues(); + bool first = true; while (n > 0 && values != getTotalValues()) { + if (!first) { + std::vector newFunctionValues = getFunctionValues(); + state.skippedFunctions = skippedFunctions; + const std::vector& functionScopes = state.symboldatabase().functionScopes; + for (std::size_t i = 0; i < functionScopes.size(); ++i) { + if (newFunctionValues[i] == functionValues[i]) + state.skippedFunctions.insert(functionScopes[i]); + } + functionValues = std::move(newFunctionValues); + } + first = false; values = getTotalValues(); const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1; const std::string passnum_s = std::to_string(passnum); @@ -7395,6 +7410,7 @@ struct ValueFlowPassRunner { return true; --n; } + state.skippedFunctions = skippedFunctions; if (state.settings().debugwarnings) { if (n == 0 && values != getTotalValues()) { ErrorMessage::FileLocation loc(state.tokenlist().getFiles()[0], 0, 0); @@ -7433,6 +7449,21 @@ struct ValueFlowPassRunner { return false; } + // Number of values in each function scope (same order as symboldatabase.functionScopes) + std::vector getFunctionValues() const + { + const std::vector& functionScopes = state.symboldatabase().functionScopes; + std::vector result(functionScopes.size()); + for (std::size_t i = 0; i < functionScopes.size(); ++i) { + const Scope* scope = functionScopes[i]; + if (!scope->bodyStart) + continue; + for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) + result[i] += tok->values().size(); + } + return result; + } + std::size_t getTotalValues() const { std::size_t n = 1;