Skip to content

Commit cfcfe27

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 23fa921 commit cfcfe27

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
@@ -7350,12 +7350,27 @@ struct ValueFlowPassRunner {
73507350
});
73517351
}
73527352

7353-
bool run(std::initializer_list<ValuePtr<ValueFlowPass>> passes) const
7353+
bool run(std::initializer_list<ValuePtr<ValueFlowPass>> passes)
73547354
{
73557355
std::size_t values = 0;
73567356
std::size_t n = state.settings().vfOptions.maxIterations;
73577357
const bool doProgress = state.settings().reportProgress >= 0;
7358+
// A function where no value changed in the previous iteration is skipped in the next iteration
7359+
const std::set<const Scope*> skippedFunctions = state.skippedFunctions;
7360+
std::vector<std::size_t> functionValues = getFunctionValues();
7361+
bool first = true;
73587362
while (n > 0 && values != getTotalValues()) {
7363+
if (!first) {
7364+
std::vector<std::size_t> newFunctionValues = getFunctionValues();
7365+
state.skippedFunctions = skippedFunctions;
7366+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
7367+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7368+
if (newFunctionValues[i] == functionValues[i])
7369+
state.skippedFunctions.insert(functionScopes[i]);
7370+
}
7371+
functionValues = std::move(newFunctionValues);
7372+
}
7373+
first = false;
73597374
values = getTotalValues();
73607375
const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1;
73617376
const std::string passnum_s = std::to_string(passnum);
@@ -7368,6 +7383,7 @@ struct ValueFlowPassRunner {
73687383
return true;
73697384
--n;
73707385
}
7386+
state.skippedFunctions = skippedFunctions;
73717387
if (state.settings().debugwarnings) {
73727388
if (n == 0 && values != getTotalValues()) {
73737389
ErrorMessage::FileLocation loc(state.tokenlist().getFiles()[0], 0, 0);
@@ -7406,6 +7422,21 @@ struct ValueFlowPassRunner {
74067422
return false;
74077423
}
74087424

7425+
// Number of values in each function scope (same order as symboldatabase.functionScopes)
7426+
std::vector<std::size_t> getFunctionValues() const
7427+
{
7428+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
7429+
std::vector<std::size_t> result(functionScopes.size());
7430+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7431+
const Scope* scope = functionScopes[i];
7432+
if (!scope->bodyStart)
7433+
continue;
7434+
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next())
7435+
result[i] += tok->values().size();
7436+
}
7437+
return result;
7438+
}
7439+
74097440
std::size_t getTotalValues() const
74107441
{
74117442
std::size_t n = 1;

0 commit comments

Comments
 (0)