Skip to content

Commit 38ea799

Browse files
dmcppcheckclaude
andcommitted
ValueFlow: do not skip a function that a skipping pass changed
Only some passes skip the unchanged functions. Another pass can remove a value that a skipping pass then adds again, so the function's values look unchanged after the iteration. When such a function was skipped only the removal happened, and the values kept changing in the next iterations. In selfcheck lib/mathlib.cpp then needed 6 iterations instead of 4 and gave 'ValueFlow maximum iterations exceeded'. Now a function is skipped only if its values are unchanged and the skipping passes did not change it in the previous iteration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a2f1735 commit 38ea799

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7331,6 +7331,8 @@ struct ValueFlowPass {
73317331
virtual void run(const ValueFlowState& state) const = 0;
73327332
// Returns true if pass needs C++
73337333
virtual bool cpp() const = 0;
7334+
// Returns true if pass does not analyse the skipped functions
7335+
virtual bool usesSkippedFunctions() const = 0;
73347336
virtual ~ValueFlowPass() noexcept = default;
73357337
};
73367338

@@ -7356,30 +7358,43 @@ struct ValueFlowPassRunner {
73567358
std::size_t values = 0;
73577359
std::size_t n = state.settings().vfOptions.maxIterations;
73587360
const bool doProgress = state.settings().reportProgress >= 0;
7359-
// A function where no value changed in the previous iteration is skipped in the next iteration
7361+
// A function is skipped in the next iteration when no value changed in the previous iteration
7362+
// and the passes that can skip it did not change it either. Other passes can remove a value that
7363+
// a skipping pass adds again; such a function must not be skipped.
73607364
const std::set<const Scope*> skippedFunctions = state.skippedFunctions;
7365+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
73617366
std::vector<std::size_t> functionValues = getFunctionValues();
7367+
std::vector<bool> changedBySkippingPass(functionScopes.size(), false);
73627368
bool first = true;
73637369
while (n > 0 && values != getTotalValues()) {
73647370
if (!first) {
73657371
std::vector<std::size_t> newFunctionValues = getFunctionValues();
73667372
state.skippedFunctions = skippedFunctions;
7367-
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
73687373
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7369-
if (newFunctionValues[i] == functionValues[i])
7374+
if (newFunctionValues[i] == functionValues[i] && !changedBySkippingPass[i])
73707375
state.skippedFunctions.insert(functionScopes[i]);
73717376
}
73727377
functionValues = std::move(newFunctionValues);
73737378
}
73747379
first = false;
7380+
changedBySkippingPass.assign(functionScopes.size(), false);
73757381
values = getTotalValues();
73767382
const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1;
73777383
const std::string passnum_s = std::to_string(passnum);
73787384
if (std::any_of(passes.begin(), passes.end(), [&](const ValuePtr<ValueFlowPass>& pass) {
73797385
// the string concatination is a hot spot in TestIO::testScanfArgument and TestIO::testPrintfArgumentVariables
73807386
std::string stage = doProgress ? std::string("ValueFlow::") + pass->name() + (' ' + passnum_s) : "";
73817387
ProgressReporter progressReporter(state.errorLogger, state.settings().reportProgress, state.tokenlist().getSourceFilePath(), std::move(stage));
7382-
return run(pass, passnum);
7388+
if (!pass->usesSkippedFunctions())
7389+
return run(pass, passnum);
7390+
const std::vector<std::size_t> before = getFunctionValues(true);
7391+
const bool ret = run(pass, passnum);
7392+
const std::vector<std::size_t> after = getFunctionValues(true);
7393+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7394+
if (before[i] != after[i])
7395+
changedBySkippingPass[i] = true;
7396+
}
7397+
return ret;
73837398
}))
73847399
return true;
73857400
--n;
@@ -7446,14 +7461,17 @@ struct ValueFlowPassRunner {
74467461
}
74477462

74487463
// Hash of the values in each function scope (same order as symboldatabase.functionScopes)
7449-
std::vector<std::size_t> getFunctionValues() const
7464+
// onlyAnalysed: the skipped functions get hash 0
7465+
std::vector<std::size_t> getFunctionValues(bool onlyAnalysed = false) const
74507466
{
74517467
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
74527468
std::vector<std::size_t> result(functionScopes.size());
74537469
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
74547470
const Scope* scope = functionScopes[i];
74557471
if (!scope->bodyStart)
74567472
continue;
7473+
if (onlyAnalysed && state.skippedFunctions.count(scope) != 0)
7474+
continue;
74577475
std::size_t h = 0;
74587476
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
74597477
const std::size_t k = (2 * reinterpret_cast<std::uintptr_t>(tok)) + 1;
@@ -7526,8 +7544,10 @@ template<class F>
75267544
struct ValueFlowPassAdaptor : ValueFlowPass {
75277545
const char* mName = nullptr;
75287546
bool mCPP = false;
7547+
bool mUsesSkippedFunctions = false;
75297548
F mRun;
7530-
ValueFlowPassAdaptor(const char* pname, bool pcpp, F prun) : ValueFlowPass(), mName(pname), mCPP(pcpp), mRun(prun) {}
7549+
ValueFlowPassAdaptor(const char* pname, bool pcpp, F prun)
7550+
: ValueFlowPass(), mName(pname), mCPP(pcpp), mUsesSkippedFunctions(std::strstr(pname, "skippedFunctions") != nullptr), mRun(prun) {}
75317551
const char* name() const override {
75327552
return mName;
75337553
}
@@ -7538,6 +7558,9 @@ struct ValueFlowPassAdaptor : ValueFlowPass {
75387558
bool cpp() const override {
75397559
return mCPP;
75407560
}
7561+
bool usesSkippedFunctions() const override {
7562+
return mUsesSkippedFunctions;
7563+
}
75417564
};
75427565

75437566
template<class F>

0 commit comments

Comments
 (0)