Skip to content

Commit 128d874

Browse files
dmcppcheckclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 9c1d989 commit 128d874

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

‎lib/valueflow.cpp‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7297,12 +7297,27 @@ struct ValueFlowPassRunner {
72977297
});
72987298
}
72997299

7300-
bool run(std::initializer_list<ValuePtr<ValueFlowPass>> passes) const
7300+
bool run(std::initializer_list<ValuePtr<ValueFlowPass>> passes)
73017301
{
73027302
std::size_t values = 0;
73037303
std::size_t n = state.settings().vfOptions.maxIterations;
73047304
const bool doProgress = state.settings().reportProgress >= 0;
7305+
// A function where no value changed in the previous iteration is skipped in the next iteration
7306+
const std::set<const Scope*> skippedFunctions = state.skippedFunctions;
7307+
std::vector<std::size_t> functionValues = getFunctionValues();
7308+
bool first = true;
73057309
while (n > 0 && values != getTotalValues()) {
7310+
if (!first) {
7311+
std::vector<std::size_t> newFunctionValues = getFunctionValues();
7312+
state.skippedFunctions = skippedFunctions;
7313+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
7314+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7315+
if (newFunctionValues[i] == functionValues[i])
7316+
state.skippedFunctions.insert(functionScopes[i]);
7317+
}
7318+
functionValues = std::move(newFunctionValues);
7319+
}
7320+
first = false;
73067321
values = getTotalValues();
73077322
const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1;
73087323
const std::string passnum_s = std::to_string(passnum);
@@ -7315,6 +7330,7 @@ struct ValueFlowPassRunner {
73157330
return true;
73167331
--n;
73177332
}
7333+
state.skippedFunctions = skippedFunctions;
73187334
if (state.settings().debugwarnings) {
73197335
if (n == 0 && values != getTotalValues()) {
73207336
ErrorMessage::FileLocation loc(state.tokenlist().getFiles()[0], 0, 0);
@@ -7353,6 +7369,21 @@ struct ValueFlowPassRunner {
73537369
return false;
73547370
}
73557371

7372+
// Number of values in each function scope (same order as symboldatabase.functionScopes)
7373+
std::vector<std::size_t> getFunctionValues() const
7374+
{
7375+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
7376+
std::vector<std::size_t> result(functionScopes.size());
7377+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7378+
const Scope* scope = functionScopes[i];
7379+
if (!scope->bodyStart)
7380+
continue;
7381+
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next())
7382+
result[i] += tok->values().size();
7383+
}
7384+
return result;
7385+
}
7386+
73567387
std::size_t getTotalValues() const
73577388
{
73587389
std::size_t n = 1;

0 commit comments

Comments
 (0)