ValueFlow: value that is changed in do while loop must be possible instead of known.

This commit is contained in:
Daniel Marjamäki 2015-07-17 20:30:34 +02:00
parent 69c1a7ecb1
commit f1e410a878
2 changed files with 34 additions and 15 deletions

View File

@ -850,6 +850,25 @@ static void valueFlowAST(Token *tok, unsigned int varid, const ValueFlow::Value
valueFlowAST(const_cast<Token*>(tok->astOperand2()), varid, value); valueFlowAST(const_cast<Token*>(tok->astOperand2()), varid, value);
} }
/** if known variable is changed in loop body, change it to a possible value */
static void handleKnownValuesInLoop(const Token *loopBodyStart,
std::list<ValueFlow::Value> *values,
unsigned int varid)
{
bool isChanged = false;
for (std::list<ValueFlow::Value>::iterator it = values->begin(); it != values->end(); ++it) {
if (it->valueKind == ValueFlow::Value::Known) {
if (!isChanged) {
if (!isVariableChanged(loopBodyStart, loopBodyStart->link(), varid))
break;
isChanged = true;
}
it->valueKind = ValueFlow::Value::Possible;
}
}
}
static bool valueFlowForward(Token * const startToken, static bool valueFlowForward(Token * const startToken,
const Token * const endToken, const Token * const endToken,
const Variable * const var, const Variable * const var,
@ -926,6 +945,10 @@ static bool valueFlowForward(Token * const startToken,
} }
} }
else if (Token::simpleMatch(tok2, "do {")) {
handleKnownValuesInLoop(tok2->next(), &values, varid);
}
// conditional block of code that assigns variable.. // conditional block of code that assigns variable..
else if (Token::Match(tok2, "%name% (") && Token::simpleMatch(tok2->linkAt(1), ") {")) { else if (Token::Match(tok2, "%name% (") && Token::simpleMatch(tok2->linkAt(1), ") {")) {
// is variable changed in condition? // is variable changed in condition?
@ -936,21 +959,7 @@ static bool valueFlowForward(Token * const startToken,
} }
// if known variable is changed in loop body, change it to a possible value.. // if known variable is changed in loop body, change it to a possible value..
if (tok2->str() != "if") { handleKnownValuesInLoop(tok2->linkAt(1)->next(), &values, varid);
bool isChanged = false;
for (std::list<ValueFlow::Value>::iterator it = values.begin(); it != values.end(); ++it) {
if (it->valueKind == ValueFlow::Value::Known) {
if (!isChanged) {
const Token *bodyStart = tok2->linkAt(1)->next();
if (!isVariableChanged(bodyStart, bodyStart->link(), varid))
break;
isChanged = true;
}
it->valueKind = ValueFlow::Value::Possible;
}
}
}
// Set values in condition // Set values in condition
for (Token* tok3 = tok2->tokAt(2); tok3 != tok2->next()->link(); tok3 = tok3->next()) { for (Token* tok3 = tok2->tokAt(2); tok3 != tok2->next()->link(); tok3 = tok3->next()) {

View File

@ -1576,6 +1576,16 @@ private:
ASSERT_EQUALS(1, value.intvalue); ASSERT_EQUALS(1, value.intvalue);
ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind); ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind);
code = "void f() {\n"
" int x = 0;\n"
" do {\n"
" if (!x) { x = y; }\n" // <- possible value
" } while (count < 10);\n"
"}";
value = valueOfTok(code, "!");
ASSERT_EQUALS(1, value.intvalue);
ASSERT_EQUALS(ValueFlow::Value::ValueKind::Possible, value.valueKind);
// after condition // after condition
code = "int f(int x) {\n" code = "int f(int x) {\n"
" if (x == 4) {}\n" " if (x == 4) {}\n"