diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 83c30a245..6d984f591 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -286,8 +286,8 @@ static bool isVariableChanged(const Token *start, const Token *end, const unsign return false; } -/** set ValueFlow value and perform calculations if possible */ -static void setTokenValue(Token* tok, const ValueFlow::Value &value) +/** Add token value. Return true if value is added. */ +static bool addValue(Token *tok, const ValueFlow::Value &value) { // if value already exists, don't add it again std::list::iterator it; @@ -305,21 +305,32 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value) // same value, but old value is inconclusive so replace it if (it->inconclusive && !value.inconclusive) { *it = value; + if (it->varId == 0) + it->varId = tok->varId(); break; } // Same value already exists, don't add new value - return; + return false; } + // Add value if (it == tok->values.end()) { - tok->values.push_back(value); - it = tok->values.end(); - --it; - if (it->varId == 0) - it->varId = tok->varId(); + ValueFlow::Value v(value); + if (v.varId == 0) + v.varId = tok->varId(); + tok->values.push_back(v); } + return true; +} + +/** set ValueFlow value and perform calculations if possible */ +static void setTokenValue(Token* tok, const ValueFlow::Value &value) +{ + if (!addValue(tok,value)) + return; + Token *parent = const_cast(tok->astParent()); if (!parent) return;