Skip to content

Commit cee721f

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 79c002a commit cee721f

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
@@ -7358,6 +7358,8 @@ struct ValueFlowPass {
73587358
virtual void run(const ValueFlowState& state) const = 0;
73597359
// Returns true if pass needs C++
73607360
virtual bool cpp() const = 0;
7361+
// Returns true if pass does not analyse the skipped functions
7362+
virtual bool usesSkippedFunctions() const = 0;
73617363
virtual ~ValueFlowPass() noexcept = default;
73627364
};
73637365

@@ -7383,30 +7385,43 @@ struct ValueFlowPassRunner {
73837385
std::size_t values = 0;
73847386
std::size_t n = state.settings().vfOptions.maxIterations;
73857387
const bool doProgress = state.settings().reportProgress >= 0;
7386-
// A function where no value changed in the previous iteration is skipped in the next iteration
7388+
// A function is skipped in the next iteration when no value changed in the previous iteration
7389+
// and the passes that can skip it did not change it either. Other passes can remove a value that
7390+
// a skipping pass adds again; such a function must not be skipped.
73877391
const std::set<const Scope*> skippedFunctions = state.skippedFunctions;
7392+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
73887393
std::vector<std::size_t> functionValues = getFunctionValues();
7394+
std::vector<bool> changedBySkippingPass(functionScopes.size(), false);
73897395
bool first = true;
73907396
while (n > 0 && values != getTotalValues()) {
73917397
if (!first) {
73927398
std::vector<std::size_t> newFunctionValues = getFunctionValues();
73937399
state.skippedFunctions = skippedFunctions;
7394-
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
73957400
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7396-
if (newFunctionValues[i] == functionValues[i])
7401+
if (newFunctionValues[i] == functionValues[i] && !changedBySkippingPass[i])
73977402
state.skippedFunctions.insert(functionScopes[i]);
73987403
}
73997404
functionValues = std::move(newFunctionValues);
74007405
}
74017406
first = false;
7407+
changedBySkippingPass.assign(functionScopes.size(), false);
74027408
values = getTotalValues();
74037409
const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1;
74047410
const std::string passnum_s = std::to_string(passnum);
74057411
if (std::any_of(passes.begin(), passes.end(), [&](const ValuePtr<ValueFlowPass>& pass) {
74067412
// the string concatination is a hot spot in TestIO::testScanfArgument and TestIO::testPrintfArgumentVariables
74077413
std::string stage = doProgress ? std::string("ValueFlow::") + pass->name() + (' ' + passnum_s) : "";
74087414
ProgressReporter progressReporter(state.errorLogger, state.settings().reportProgress, state.tokenlist().getSourceFilePath(), std::move(stage));
7409-
return run(pass, passnum);
7415+
if (!pass->usesSkippedFunctions())
7416+
return run(pass, passnum);
7417+
const std::vector<std::size_t> before = getFunctionValues(true);
7418+
const bool ret = run(pass, passnum);
7419+
const std::vector<std::size_t> after = getFunctionValues(true);
7420+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7421+
if (before[i] != after[i])
7422+
changedBySkippingPass[i] = true;
7423+
}
7424+
return ret;
74107425
}))
74117426
return true;
74127427
--n;
@@ -7473,14 +7488,17 @@ struct ValueFlowPassRunner {
74737488
}
74747489

74757490
// Hash of the values in each function scope (same order as symboldatabase.functionScopes)
7476-
std::vector<std::size_t> getFunctionValues() const
7491+
// onlyAnalysed: the skipped functions get hash 0
7492+
std::vector<std::size_t> getFunctionValues(bool onlyAnalysed = false) const
74777493
{
74787494
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
74797495
std::vector<std::size_t> result(functionScopes.size());
74807496
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
74817497
const Scope* scope = functionScopes[i];
74827498
if (!scope->bodyStart)
74837499
continue;
7500+
if (onlyAnalysed && state.skippedFunctions.count(scope) != 0)
7501+
continue;
74847502
std::size_t h = 0;
74857503
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
74867504
const std::size_t k = (2 * reinterpret_cast<std::uintptr_t>(tok)) + 1;
@@ -7553,8 +7571,10 @@ template<class F>
75537571
struct ValueFlowPassAdaptor : ValueFlowPass {
75547572
const char* mName = nullptr;
75557573
bool mCPP = false;
7574+
bool mUsesSkippedFunctions = false;
75567575
F mRun;
7557-
ValueFlowPassAdaptor(const char* pname, bool pcpp, F prun) : ValueFlowPass(), mName(pname), mCPP(pcpp), mRun(prun) {}
7576+
ValueFlowPassAdaptor(const char* pname, bool pcpp, F prun)
7577+
: ValueFlowPass(), mName(pname), mCPP(pcpp), mUsesSkippedFunctions(std::strstr(pname, "skippedFunctions") != nullptr), mRun(prun) {}
75587578
const char* name() const override {
75597579
return mName;
75607580
}
@@ -7565,6 +7585,9 @@ struct ValueFlowPassAdaptor : ValueFlowPass {
75657585
bool cpp() const override {
75667586
return mCPP;
75677587
}
7588+
bool usesSkippedFunctions() const override {
7589+
return mUsesSkippedFunctions;
7590+
}
75687591
};
75697592

75707593
template<class F>

0 commit comments

Comments
 (0)