Skip to content

Commit 4cc4ad2

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 c60290f commit 4cc4ad2

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
@@ -7278,6 +7278,8 @@ struct ValueFlowPass {
72787278
virtual void run(const ValueFlowState& state) const = 0;
72797279
// Returns true if pass needs C++
72807280
virtual bool cpp() const = 0;
7281+
// Returns true if pass does not analyse the skipped functions
7282+
virtual bool usesSkippedFunctions() const = 0;
72817283
virtual ~ValueFlowPass() noexcept = default;
72827284
};
72837285

@@ -7303,30 +7305,43 @@ struct ValueFlowPassRunner {
73037305
std::size_t values = 0;
73047306
std::size_t n = state.settings().vfOptions.maxIterations;
73057307
const bool doProgress = state.settings().reportProgress >= 0;
7306-
// A function where no value changed in the previous iteration is skipped in the next iteration
7308+
// A function is skipped in the next iteration when no value changed in the previous iteration
7309+
// and the passes that can skip it did not change it either. Other passes can remove a value that
7310+
// a skipping pass adds again; such a function must not be skipped.
73077311
const std::set<const Scope*> skippedFunctions = state.skippedFunctions;
7312+
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
73087313
std::vector<std::size_t> functionValues = getFunctionValues();
7314+
std::vector<bool> changedBySkippingPass(functionScopes.size(), false);
73097315
bool first = true;
73107316
while (n > 0 && values != getTotalValues()) {
73117317
if (!first) {
73127318
std::vector<std::size_t> newFunctionValues = getFunctionValues();
73137319
state.skippedFunctions = skippedFunctions;
7314-
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
73157320
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7316-
if (newFunctionValues[i] == functionValues[i])
7321+
if (newFunctionValues[i] == functionValues[i] && !changedBySkippingPass[i])
73177322
state.skippedFunctions.insert(functionScopes[i]);
73187323
}
73197324
functionValues = std::move(newFunctionValues);
73207325
}
73217326
first = false;
7327+
changedBySkippingPass.assign(functionScopes.size(), false);
73227328
values = getTotalValues();
73237329
const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1;
73247330
const std::string passnum_s = std::to_string(passnum);
73257331
if (std::any_of(passes.begin(), passes.end(), [&](const ValuePtr<ValueFlowPass>& pass) {
73267332
// the string concatination is a hot spot in TestIO::testScanfArgument and TestIO::testPrintfArgumentVariables
73277333
std::string stage = doProgress ? std::string("ValueFlow::") + pass->name() + (' ' + passnum_s) : "";
73287334
ProgressReporter progressReporter(state.errorLogger, state.settings().reportProgress, state.tokenlist().getSourceFilePath(), std::move(stage));
7329-
return run(pass, passnum);
7335+
if (!pass->usesSkippedFunctions())
7336+
return run(pass, passnum);
7337+
const std::vector<std::size_t> before = getFunctionValues(true);
7338+
const bool ret = run(pass, passnum);
7339+
const std::vector<std::size_t> after = getFunctionValues(true);
7340+
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
7341+
if (before[i] != after[i])
7342+
changedBySkippingPass[i] = true;
7343+
}
7344+
return ret;
73307345
}))
73317346
return true;
73327347
--n;
@@ -7393,14 +7408,17 @@ struct ValueFlowPassRunner {
73937408
}
73947409

73957410
// Hash of the values in each function scope (same order as symboldatabase.functionScopes)
7396-
std::vector<std::size_t> getFunctionValues() const
7411+
// onlyAnalysed: the skipped functions get hash 0
7412+
std::vector<std::size_t> getFunctionValues(bool onlyAnalysed = false) const
73977413
{
73987414
const std::vector<const Scope*>& functionScopes = state.symboldatabase().functionScopes;
73997415
std::vector<std::size_t> result(functionScopes.size());
74007416
for (std::size_t i = 0; i < functionScopes.size(); ++i) {
74017417
const Scope* scope = functionScopes[i];
74027418
if (!scope->bodyStart)
74037419
continue;
7420+
if (onlyAnalysed && state.skippedFunctions.count(scope) != 0)
7421+
continue;
74047422
std::size_t h = 0;
74057423
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
74067424
const std::size_t k = (2 * reinterpret_cast<std::uintptr_t>(tok)) + 1;
@@ -7473,8 +7491,10 @@ template<class F>
74737491
struct ValueFlowPassAdaptor : ValueFlowPass {
74747492
const char* mName = nullptr;
74757493
bool mCPP = false;
7494+
bool mUsesSkippedFunctions = false;
74767495
F mRun;
7477-
ValueFlowPassAdaptor(const char* pname, bool pcpp, F prun) : ValueFlowPass(), mName(pname), mCPP(pcpp), mRun(prun) {}
7496+
ValueFlowPassAdaptor(const char* pname, bool pcpp, F prun)
7497+
: ValueFlowPass(), mName(pname), mCPP(pcpp), mUsesSkippedFunctions(std::strstr(pname, "skippedFunctions") != nullptr), mRun(prun) {}
74787498
const char* name() const override {
74797499
return mName;
74807500
}
@@ -7485,6 +7505,9 @@ struct ValueFlowPassAdaptor : ValueFlowPass {
74857505
bool cpp() const override {
74867506
return mCPP;
74877507
}
7508+
bool usesSkippedFunctions() const override {
7509+
return mUsesSkippedFunctions;
7510+
}
74887511
};
74897512

74907513
template<class F>

0 commit comments

Comments
 (0)