value flow: pass result of bool operator to function

This commit is contained in:
Daniel Marjamäki 2014-01-08 16:17:47 +01:00
parent 6d22c9deaa
commit a2fe1ebe92
2 changed files with 21 additions and 2 deletions

View File

@ -192,6 +192,9 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger,
{ {
std::list<ValueFlow::Value> argvalues; std::list<ValueFlow::Value> argvalues;
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) { for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
if (!Token::Match(tok, "[(,]"))
continue;
// passing value(s) to function // passing value(s) to function
if (Token::Match(tok, "[(,] %var% [,)]") && !tok->next()->values.empty()) if (Token::Match(tok, "[(,] %var% [,)]") && !tok->next()->values.empty())
argvalues = tok->next()->values; argvalues = tok->next()->values;
@ -199,7 +202,18 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger,
argvalues.clear(); argvalues.clear();
argvalues.push_back(ValueFlow::Value(MathLib::toLongNumber(tok->next()->str()))); argvalues.push_back(ValueFlow::Value(MathLib::toLongNumber(tok->next()->str())));
} else { } else {
continue; // bool operator => values 1/0 are passed to function..
const Token *op = tok->next();
while (op && op->astParent() && !Token::Match(op->astParent(), "[(,]"))
op = op->astParent();
if (op && (op->isComparisonOp() || op->str() == "!")) {
argvalues.clear();
argvalues.push_back(ValueFlow::Value(0));
argvalues.push_back(ValueFlow::Value(1));
} else {
// possible values are unknown..
continue;
}
} }
const Token * const argumentToken = tok->next(); const Token * const argumentToken = tok->next();
@ -230,7 +244,7 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger,
// Set value in function scope.. // Set value in function scope..
const unsigned int varid2 = arg->nameToken()->varId(); const unsigned int varid2 = arg->nameToken()->varId();
for (const Token *tok2 = functionScope->classStart->next(); tok2 != functionScope->classEnd; tok2 = tok2->next()) { for (const Token *tok2 = functionScope->classStart->next(); tok2 != functionScope->classEnd; tok2 = tok2->next()) {
if (Token::Match(tok2, "%cop%|return %varid%", varid2)) { if (Token::Match(tok2, "%cop%|return %varid%", varid2) || Token::Match(tok2, "= %varid% %cop%|;", varid2)) {
tok2 = tok2->next(); tok2 = tok2->next();
std::list<ValueFlow::Value> &values = const_cast<Token*>(tok2)->values; std::list<ValueFlow::Value> &values = const_cast<Token*>(tok2)->values;
values.insert(values.begin(), argvalues.begin(), argvalues.end()); values.insert(values.begin(), argvalues.begin(), argvalues.end());

View File

@ -168,6 +168,11 @@ private:
" f1(0);\n" " f1(0);\n"
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
code = "void f1(int x) { a=x; }\n"
"void f2(int y) { f1(y<123); }\n";
ASSERT_EQUALS(true, testValueOfX(code, 1U, 0));
ASSERT_EQUALS(true, testValueOfX(code, 1U, 1));
} }
}; };