ValueFlow: In evaluate handle correlated values

This commit is contained in:
Daniel Marjamäki 2018-11-05 18:07:35 +01:00
parent 458f1ae510
commit ca19894a04
2 changed files with 10 additions and 12 deletions

View File

@ -3375,6 +3375,11 @@ static bool evaluate(const Token *expr, const std::vector<std::list<ValueFlow::V
if (!val2.isIntValue())
continue;
if (val1.varId != 0 && val2.varId != 0) {
if (val1.varId != val2.varId || val1.varvalue != val2.varvalue)
continue;
}
if (expr->str() == "+")
result->emplace_back(ValueFlow::Value(val1.intvalue + val2.intvalue));
else if (expr->str() == "-")
@ -3436,19 +3441,11 @@ static bool evaluate(const Token *expr, const std::vector<std::list<ValueFlow::V
static void valueFlowLibraryFunction(Token *tok, const std::string &returnValue, const Settings *settings)
{
unsigned int varId = 0;
std::vector<std::list<ValueFlow::Value>> argValues;
for (const Token *argtok : getArguments(tok->previous())) {
argValues.emplace_back(argtok->values());
if (argValues.back().empty())
return;
for (const ValueFlow::Value &argval : argValues.back()) {
if (argval.varId) {
if (varId && varId != argval.varId)
return;
varId = argval.varId;
}
}
}
TokenList tokenList(settings);

View File

@ -2708,11 +2708,12 @@ private:
" a = 1;\n"
" else\n"
" a = 2;\n"
" int x = add(3, a);\n"
" return x;\n"
" add(a, a);\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 8U, 4));
ASSERT_EQUALS(true, testValueOfX(code, 8U, 5));
std::list<ValueFlow::Value> values = tokenValues(code, "( a , a )");
ASSERT_EQUALS(2, values.size());
ASSERT_EQUALS(2, values.front().intvalue);
ASSERT_EQUALS(4, values.back().intvalue);
}
void valueFlowFunctionReturn() {