From dd5b5ff3b0c8c86aa646632aa3a41b0658a6a551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 5 Nov 2026 09:18:30 +0100 Subject: [PATCH 1/6] 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 --- lib/valueflow.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c8c029aa708..ff5e84f4827 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7377,12 +7377,27 @@ struct ValueFlowPassRunner { }); } - bool run(std::initializer_list> passes) const + bool run(std::initializer_list> passes) { std::size_t values = 0; std::size_t n = state.settings().vfOptions.maxIterations; const bool doProgress = state.settings().reportProgress >= 0; + // A function where no value changed in the previous iteration is skipped in the next iteration + const std::set skippedFunctions = state.skippedFunctions; + std::vector functionValues = getFunctionValues(); + bool first = true; while (n > 0 && values != getTotalValues()) { + if (!first) { + std::vector newFunctionValues = getFunctionValues(); + state.skippedFunctions = skippedFunctions; + const std::vector& functionScopes = state.symboldatabase().functionScopes; + for (std::size_t i = 0; i < functionScopes.size(); ++i) { + if (newFunctionValues[i] == functionValues[i]) + state.skippedFunctions.insert(functionScopes[i]); + } + functionValues = std::move(newFunctionValues); + } + first = false; values = getTotalValues(); const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1; const std::string passnum_s = std::to_string(passnum); @@ -7395,6 +7410,7 @@ struct ValueFlowPassRunner { return true; --n; } + state.skippedFunctions = skippedFunctions; if (state.settings().debugwarnings) { if (n == 0 && values != getTotalValues()) { ErrorMessage::FileLocation loc(state.tokenlist().getFiles()[0], 0, 0); @@ -7433,6 +7449,21 @@ struct ValueFlowPassRunner { return false; } + // Number of values in each function scope (same order as symboldatabase.functionScopes) + std::vector getFunctionValues() const + { + const std::vector& functionScopes = state.symboldatabase().functionScopes; + std::vector result(functionScopes.size()); + for (std::size_t i = 0; i < functionScopes.size(); ++i) { + const Scope* scope = functionScopes[i]; + if (!scope->bodyStart) + continue; + for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) + result[i] += tok->values().size(); + } + return result; + } + std::size_t getTotalValues() const { std::size_t n = 1; From d667325769a50c96157c2a10c119b13d8e025ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 5 Nov 2026 09:44:55 +0100 Subject: [PATCH 2/6] SymbolDatabase: cache getEnumType() during setValueTypeInTokenList() getEnumType() scans the whole enum body each time a token with enum type gets a valuetype. With big enums that are used a lot this is quadratic. Cache the result per enum scope while the valuetypes are set; the result is only cached when every literal it looked at already had a valuetype. crispy-doom: total time 124s => 78s. No changed warnings. Co-Authored-By: Claude Opus 5.5 --- lib/symboldatabase.cpp | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 0baaffad663..80c7d150734 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -6975,7 +6975,7 @@ void SymbolDatabase::setValueType(Token* tok, const Variable& var, const SourceL } } -static ValueType::Type getEnumType(const Scope* scope, const Platform& platform); +static ValueType::Type getEnumTypeCached(const Scope* scope, const Platform& platform); void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, const SourceLocation &loc) { @@ -7005,7 +7005,7 @@ void SymbolDatabase::setValueType(Token* tok, const Enumerator& enumerator, cons setValueType(tok, valuetype); } else { valuetype.sign = ValueType::SIGNED; - valuetype.type = getEnumType(enumerator.scope, mSettings.platform); + valuetype.type = getEnumTypeCached(enumerator.scope, mSettings.platform); setValueType(tok, valuetype); } } @@ -7570,7 +7570,7 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const } } -static ValueType::Type getEnumType(const Scope* scope, const Platform& platform) // TODO: also determine sign? +static ValueType::Type getEnumType(const Scope* scope, const Platform& platform, bool* complete = nullptr) // TODO: also determine sign? { ValueType::Type type = ValueType::Type::INT; for (const Token* tok = scope->bodyStart; tok && tok != scope->bodyEnd; tok = tok->next()) { @@ -7587,6 +7587,9 @@ static ValueType::Type getEnumType(const Scope* scope, const Platform& platform) if (const ValueType* vt = vTok->valueType()) { if ((vt->type > type && (vt->type == ValueType::Type::LONG || vt->type == ValueType::Type::LONGLONG))) type = vt->type; + } else if (complete) { + // the literal does not have a valuetype yet, the result might change later + *complete = false; } continue; } @@ -7600,6 +7603,23 @@ static ValueType::Type getEnumType(const Scope* scope, const Platform& platform) return type; } +// Cache for getEnumType(), only active during SymbolDatabase::setValueTypeInTokenList() +static thread_local std::unordered_map* enumTypeCache = nullptr; + +static ValueType::Type getEnumTypeCached(const Scope* scope, const Platform& platform) +{ + if (!enumTypeCache) + return getEnumType(scope, platform); + const auto it = enumTypeCache->find(scope); + if (it != enumTypeCache->end()) + return it->second; + bool complete = true; + const ValueType::Type type = getEnumType(scope, platform, &complete); + if (complete) + enumTypeCache->emplace(scope, type); + return type; +} + static const Token* parsedecl(const Token* type, ValueType* const valuetype, ValueType::Sign defaultSignedness, @@ -7632,7 +7652,7 @@ static const Token* parsedecl(const Token* type, else if (enum_type->isStandardType()) valuetype->fromLibraryType(enum_type->str(), settings); } else - valuetype->type = getEnumType(valuetype->typeScope, settings.platform); + valuetype->type = getEnumTypeCached(valuetype->typeScope, settings.platform); } else valuetype->type = ValueType::Type::RECORD; const bool cpp = type->isCpp(); @@ -7799,7 +7819,7 @@ static const Token* parsedecl(const Token* type, else if (!valuetype->typeScope && type->type() && type->type()->classScope && valuetype->type != ValueType::Type::SMART_POINTER) { if (type->type()->classScope->type == ScopeType::eEnum) { valuetype->sign = ValueType::Sign::SIGNED; - valuetype->type = getEnumType(type->type()->classScope, settings.platform); + valuetype->type = getEnumTypeCached(type->type()->classScope, settings.platform); } else { valuetype->type = ValueType::Type::RECORD; } @@ -7989,6 +8009,13 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to if (!tokens) tokens = mTokenizer.list.front(); + std::unordered_map enumTypes; + struct EnumTypeCacheGuard { + explicit EnumTypeCacheGuard(std::unordered_map* cache) : prev(enumTypeCache) { enumTypeCache = cache; } + ~EnumTypeCacheGuard() { enumTypeCache = prev; } + std::unordered_map* prev; + } enumTypeCacheGuard(&enumTypes); + for (Token *tok = tokens; tok; tok = tok->next()) tok->setValueType(nullptr); From f7639aba0577658b350ff25e97dd6b060f4e4024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 5 Nov 2026 09:58:21 +0100 Subject: [PATCH 3/6] Tokenizer: avoid quadratic backward search for commas in setVarIdParseDeclaration() For a name after a comma the tokens before it were searched back to ';', '(', '[' or '{'. In a long comma separated list of names this search was repeated from every element. Reuse the result of the search from the previous comma. crispy-doom: total time 78s => 64s. No changed warnings. Co-Authored-By: Claude Opus 5.5 --- lib/tokenize.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 349ca6c0ad1..5ae17a6ecf6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4416,7 +4416,15 @@ void VariableMap::addVariable(const std::string& varname, bool globalNamespace) /** * @throws Token* thrown when closing brackets are missing */ -static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap, bool executableScope, Standards::cstd_t cStandard) +namespace { + // Result of the last backward search from a comma in setVarIdParseDeclaration() + struct CommaSearchCache { + const Token* comma = nullptr; + const Token* result = nullptr; + }; +} + +static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap, bool executableScope, Standards::cstd_t cStandard, CommaSearchCache& commaCache) { const Token* const tok1 = tok; Token* tok2 = tok; @@ -4507,11 +4515,19 @@ static bool setVarIdParseDeclaration(Token*& tok, const VariableMap& variableMap { const Token *tok3 = tok->previous(); if (tok3 && tok3->str() == ",") { + // In a long comma separated list the search from the previous comma gives the same result + const Token* const comma = tok3; while (tok3 && !Token::Match(tok3,";|(|[|{")) { + if (tok3 != comma && tok3 == commaCache.comma) { + tok3 = commaCache.result; + break; + } if (Token::Match(tok3, ")|]")) tok3 = tok3->link(); tok3 = tok3->previous(); } + commaCache.comma = comma; + commaCache.result = tok3; if (tok3 && executableScope && Token::Match(tok3->previous(), "%name% (")) { const Token *fdecl = tok3->previous(); @@ -4829,6 +4845,7 @@ void Tokenizer::setVarIdPass1() VariableMap variableMap; std::map> structMembers; + CommaSearchCache commaCache; std::stack scopeStack; @@ -5029,7 +5046,7 @@ void Tokenizer::setVarIdPass1() } try { /* Ticket #8151 */ - decl = setVarIdParseDeclaration(tok2, variableMap, scopeStack.top().isExecutable, mSettings.standards.c); + decl = setVarIdParseDeclaration(tok2, variableMap, scopeStack.top().isExecutable, mSettings.standards.c, commaCache); } catch (const Token * errTok) { syntaxError(errTok); } From 67076ae7609aabe821cb2a5e18e279064d941928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 5 Nov 2026 20:11:23 +0100 Subject: [PATCH 4/6] ValueFlow: compare a hash of the values when skipping unchanged functions Skipping a function in the next iteration when its number of values did not change missed changes where a value is replaced or changes kind (possible -> known) without changing the count. For example a call whose return value got known in valueFlowFunctionReturn. Compare a hash of all values in the function instead. Co-Authored-By: Claude Opus 5.5 --- lib/valueflow.cpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ff5e84f4827..50ae2bb12f9 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7449,7 +7449,29 @@ struct ValueFlowPassRunner { return false; } - // Number of values in each function scope (same order as symboldatabase.functionScopes) + static std::size_t hashValue(const ValueFlow::Value& v) + { + std::size_t h = std::hash{}(v.intvalue); + const auto combine = [&h](std::size_t x) { + h ^= x + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2); + }; + combine(static_cast(v.valueType)); + combine(static_cast(v.valueKind)); + combine(static_cast(v.bound)); + combine(std::hash{}(v.tokvalue)); + combine(std::hash{}(v.condition)); + combine(std::hash{}(v.floatValue)); + combine(std::hash{}(v.varvalue)); + combine(std::hash{}(v.path)); + combine(v.varId); + combine(static_cast(v.conditional)); + combine(static_cast(v.moveKind)); + combine(static_cast(v.lifetimeKind)); + combine(static_cast(v.lifetimeScope)); + return h; + } + + // Hash of the values in each function scope (same order as symboldatabase.functionScopes) std::vector getFunctionValues() const { const std::vector& functionScopes = state.symboldatabase().functionScopes; @@ -7458,8 +7480,12 @@ struct ValueFlowPassRunner { const Scope* scope = functionScopes[i]; if (!scope->bodyStart) continue; - for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) - result[i] += tok->values().size(); + std::size_t h = 0; + for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { + for (const ValueFlow::Value& v : tok->values()) + h += hashValue(v) * (2 * reinterpret_cast(tok) + 1); + } + result[i] = h; } return result; } From 79c002aaa52ccc4ffa235a8997a90191b63f0a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 8 Nov 2026 13:25:05 +0100 Subject: [PATCH 5/6] Fix clang-tidy, selfcheck and uncrustify findings Co-Authored-By: Claude Opus 5.5 --- lib/symboldatabase.cpp | 10 +++++++--- lib/valueflow.cpp | 7 +++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 80c7d150734..de0e72d3c0b 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -8011,9 +8011,13 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to std::unordered_map enumTypes; struct EnumTypeCacheGuard { - explicit EnumTypeCacheGuard(std::unordered_map* cache) : prev(enumTypeCache) { enumTypeCache = cache; } - ~EnumTypeCacheGuard() { enumTypeCache = prev; } - std::unordered_map* prev; + explicit EnumTypeCacheGuard(std::unordered_map* cache) { + enumTypeCache = cache; + } + ~EnumTypeCacheGuard() { + enumTypeCache = prev; + } + std::unordered_map* prev{enumTypeCache}; } enumTypeCacheGuard(&enumTypes); for (Token *tok = tokens; tok; tok = tok->next()) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 50ae2bb12f9..00e35467dad 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -122,6 +122,7 @@ #include #include #include +#include #include #include #include @@ -7482,8 +7483,10 @@ struct ValueFlowPassRunner { continue; std::size_t h = 0; for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { - for (const ValueFlow::Value& v : tok->values()) - h += hashValue(v) * (2 * reinterpret_cast(tok) + 1); + const std::size_t k = (2 * reinterpret_cast(tok)) + 1; + h = std::accumulate(tok->values().cbegin(), tok->values().cend(), h, [&](std::size_t acc, const ValueFlow::Value& v) { + return acc + (hashValue(v) * k); + }); } result[i] = h; } From cee721f1ab73a114d0f8b962033aa8c033e9df46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 8 Nov 2026 13:31:49 +0100 Subject: [PATCH 6/6] 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 --- lib/valueflow.cpp | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 00e35467dad..325e7b47413 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -7358,6 +7358,8 @@ struct ValueFlowPass { virtual void run(const ValueFlowState& state) const = 0; // Returns true if pass needs C++ virtual bool cpp() const = 0; + // Returns true if pass does not analyse the skipped functions + virtual bool usesSkippedFunctions() const = 0; virtual ~ValueFlowPass() noexcept = default; }; @@ -7383,22 +7385,26 @@ struct ValueFlowPassRunner { std::size_t values = 0; std::size_t n = state.settings().vfOptions.maxIterations; const bool doProgress = state.settings().reportProgress >= 0; - // A function where no value changed in the previous iteration is skipped in the next iteration + // A function is skipped in the next iteration when no value changed in the previous iteration + // and the passes that can skip it did not change it either. Other passes can remove a value that + // a skipping pass adds again; such a function must not be skipped. const std::set skippedFunctions = state.skippedFunctions; + const std::vector& functionScopes = state.symboldatabase().functionScopes; std::vector functionValues = getFunctionValues(); + std::vector changedBySkippingPass(functionScopes.size(), false); bool first = true; while (n > 0 && values != getTotalValues()) { if (!first) { std::vector newFunctionValues = getFunctionValues(); state.skippedFunctions = skippedFunctions; - const std::vector& functionScopes = state.symboldatabase().functionScopes; for (std::size_t i = 0; i < functionScopes.size(); ++i) { - if (newFunctionValues[i] == functionValues[i]) + if (newFunctionValues[i] == functionValues[i] && !changedBySkippingPass[i]) state.skippedFunctions.insert(functionScopes[i]); } functionValues = std::move(newFunctionValues); } first = false; + changedBySkippingPass.assign(functionScopes.size(), false); values = getTotalValues(); const std::size_t passnum = state.settings().vfOptions.maxIterations - n + 1; const std::string passnum_s = std::to_string(passnum); @@ -7406,7 +7412,16 @@ struct ValueFlowPassRunner { // the string concatination is a hot spot in TestIO::testScanfArgument and TestIO::testPrintfArgumentVariables std::string stage = doProgress ? std::string("ValueFlow::") + pass->name() + (' ' + passnum_s) : ""; ProgressReporter progressReporter(state.errorLogger, state.settings().reportProgress, state.tokenlist().getSourceFilePath(), std::move(stage)); - return run(pass, passnum); + if (!pass->usesSkippedFunctions()) + return run(pass, passnum); + const std::vector before = getFunctionValues(true); + const bool ret = run(pass, passnum); + const std::vector after = getFunctionValues(true); + for (std::size_t i = 0; i < functionScopes.size(); ++i) { + if (before[i] != after[i]) + changedBySkippingPass[i] = true; + } + return ret; })) return true; --n; @@ -7473,7 +7488,8 @@ struct ValueFlowPassRunner { } // Hash of the values in each function scope (same order as symboldatabase.functionScopes) - std::vector getFunctionValues() const + // onlyAnalysed: the skipped functions get hash 0 + std::vector getFunctionValues(bool onlyAnalysed = false) const { const std::vector& functionScopes = state.symboldatabase().functionScopes; std::vector result(functionScopes.size()); @@ -7481,6 +7497,8 @@ struct ValueFlowPassRunner { const Scope* scope = functionScopes[i]; if (!scope->bodyStart) continue; + if (onlyAnalysed && state.skippedFunctions.count(scope) != 0) + continue; std::size_t h = 0; for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { const std::size_t k = (2 * reinterpret_cast(tok)) + 1; @@ -7553,8 +7571,10 @@ template struct ValueFlowPassAdaptor : ValueFlowPass { const char* mName = nullptr; bool mCPP = false; + bool mUsesSkippedFunctions = false; F mRun; - ValueFlowPassAdaptor(const char* pname, bool pcpp, F prun) : ValueFlowPass(), mName(pname), mCPP(pcpp), mRun(prun) {} + ValueFlowPassAdaptor(const char* pname, bool pcpp, F prun) + : ValueFlowPass(), mName(pname), mCPP(pcpp), mUsesSkippedFunctions(std::strstr(pname, "skippedFunctions") != nullptr), mRun(prun) {} const char* name() const override { return mName; } @@ -7565,6 +7585,9 @@ struct ValueFlowPassAdaptor : ValueFlowPass { bool cpp() const override { return mCPP; } + bool usesSkippedFunctions() const override { + return mUsesSkippedFunctions; + } }; template