Fixed #6743 (valueFlowAfterCondition: wrong value when variable is changed in conditional code)
This commit is contained in:
parent
c5bbea2994
commit
e759710198
|
@ -1558,12 +1558,19 @@ static void valueFlowAfterCondition(TokenList *tokenlist, SymbolDatabase* symbol
|
||||||
startToken = top->link()->linkAt(1)->tokAt(2);
|
startToken = top->link()->linkAt(1)->tokAt(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok = true;
|
if (startToken) {
|
||||||
if (startToken)
|
if (!valueFlowForward(startToken->next(), startToken->link(), var, varid, values, true, tokenlist, errorLogger, settings))
|
||||||
ok = valueFlowForward(startToken->next(), startToken->link(), var, varid, values, true, tokenlist, errorLogger, settings);
|
continue;
|
||||||
|
if (isVariableChanged(startToken, startToken->link(), varid)) {
|
||||||
|
// TODO: The endToken should not be startToken->link() in the valueFlowForward call
|
||||||
|
if (settings->debugwarnings)
|
||||||
|
bailout(tokenlist, errorLogger, startToken->link(), "valueFlowAfterCondition: " + var->name() + " is changed in conditional block");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// After conditional code..
|
// After conditional code..
|
||||||
if (ok && Token::simpleMatch(top->link(), ") {")) {
|
if (Token::simpleMatch(top->link(), ") {")) {
|
||||||
Token *after = top->link()->linkAt(1);
|
Token *after = top->link()->linkAt(1);
|
||||||
std::string unknownFunction;
|
std::string unknownFunction;
|
||||||
if (settings->library.isScopeNoReturn(after, &unknownFunction)) {
|
if (settings->library.isScopeNoReturn(after, &unknownFunction)) {
|
||||||
|
|
|
@ -1067,7 +1067,7 @@ private:
|
||||||
void valueFlowAfterCondition() {
|
void valueFlowAfterCondition() {
|
||||||
const char *code;
|
const char *code;
|
||||||
|
|
||||||
// if
|
// in if
|
||||||
code = "void f(int x) {\n"
|
code = "void f(int x) {\n"
|
||||||
" if (x == 123) {\n"
|
" if (x == 123) {\n"
|
||||||
" a = x;\n"
|
" a = x;\n"
|
||||||
|
@ -1082,7 +1082,7 @@ private:
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(false, testValueOfX(code, 3U, 123));
|
ASSERT_EQUALS(false, testValueOfX(code, 3U, 123));
|
||||||
|
|
||||||
// else
|
// in else
|
||||||
code = "void f(int x) {\n"
|
code = "void f(int x) {\n"
|
||||||
" if (x == 123) {}\n"
|
" if (x == 123) {}\n"
|
||||||
" else a = x;\n"
|
" else a = x;\n"
|
||||||
|
@ -1095,6 +1095,16 @@ private:
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
|
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
|
||||||
|
|
||||||
|
// after if
|
||||||
|
code = "void f(int x) {\n"
|
||||||
|
" if (x == 10) {\n"
|
||||||
|
" x++;\n"
|
||||||
|
" }\n"
|
||||||
|
" a = x;\n"
|
||||||
|
"}";
|
||||||
|
ASSERT_EQUALS(false, testValueOfX(code, 5U, 10));
|
||||||
|
TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 5U, 11));
|
||||||
|
|
||||||
// !
|
// !
|
||||||
code = "void f(int x) {\n"
|
code = "void f(int x) {\n"
|
||||||
" if (!x) { a = x; }\n"
|
" if (!x) { a = x; }\n"
|
||||||
|
|
Loading…
Reference in New Issue