Skip to content

Commit 6494daa

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 e418933 commit 6494daa

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
@@ -7377,12 +7377,27 @@ struct ValueFlowPassRunner {
73777377
});
73787378
}
73797379

7380-
bool run(std::initializer_list<ValuePtr<ValueFlowPass>> passes) const
7380+
bool run(std::initializer_list<ValuePtr<ValueFlowPass>> passes)
73817381
{
73827382
std::size_t values = 0;
73837383
std::size_t n = state.settings().vfOptions.maxIterations;
73847384
const bool doProgress = state.settings().reportProgress >= 0;
7385+
// A function where no value changed in the previous iteration is skipped in the next iteration
7386+
const std::set<const Scope*> skippedFunctions = state.skippedFunctions;
7387+
std::vector<std::size_t> functionValues = getFunctionValues();
7388+
bool first = true;
73857389
while (n > 0 && values != getTotalValues()) {
7390+
if (!first) {
7391+
std::vector<std::size_t> newFunctionValues = getFunctionValues();
7392+
state.skippedFunctions = skippedFunctions;
7393+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
7394+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7395+
if (newFunctionValues[i] == functionValues[i])
7396+
state.skippedFunctions.insert(functionScopes[i]);
7397+
}
7398+
functionValues = std::move(newFunctionValues);
7399+
}
7400+
first = false;
73867401
values = getTotalValues();
73877402
const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1;
73887403
const std::string passnum_s = std::to_string(passnum);
@@ -7395,6 +7410,7 @@ struct ValueFlowPassRunner {
73957410
return true;
73967411
--n;
73977412
}
7413+
state.skippedFunctions = skippedFunctions;
73987414
if (state.settings().debugwarnings) {
73997415
if (n == 0 && values != getTotalValues()) {
74007416
ErrorMessage::FileLocation loc(state.tokenlist().getFiles()[0], 0, 0);
@@ -7433,6 +7449,21 @@ struct ValueFlowPassRunner {
74337449
return false;
74347450
}
74357451

7452+
// Number of values in each function scope (same order as symboldatabase.functionScopes)
7453+
std::vector<std::size_t> getFunctionValues() const
7454+
{
7455+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
7456+
std::vector<std::size_t> result(functionScopes.size());
7457+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7458+
const Scope* scope = functionScopes[i];
7459+
if (!scope->bodyStart)
7460+
continue;
7461+
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next())
7462+
result[i] += tok->values().size();
7463+
}
7464+
return result;
7465+
}
7466+
74367467
std::size_t getTotalValues() const
74377468
{
74387469
std::size_t n = 1;

0 commit comments

Comments
 (0)