Fix #7829 (false-positive: knownConditionTrueFalse linked to virtual method)

Return values of virtual functions in valueflow are only possible values
since function might be overloaded.
This commit is contained in:
Frank Zingsheim 2016-12-11 10:42:39 +01:00
parent 87abe1174f
commit e0d4720e19
2 changed files with 47 additions and 1 deletions

View File

@ -2709,7 +2709,10 @@ static void valueFlowFunctionReturn(TokenList *tokenlist, ErrorLogger *errorLogg
&error); &error);
if (!error) { if (!error) {
ValueFlow::Value v(result); ValueFlow::Value v(result);
v.setKnown(); if (function->isVirtual())
v.setPossible();
else
v.setKnown();
setTokenValue(tok, v, tokenlist->getSettings()); setTokenValue(tok, v, tokenlist->getSettings());
} }
} }

View File

@ -1897,6 +1897,25 @@ private:
" f1(0);\n" " f1(0);\n"
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 3U, 0)); ASSERT_EQUALS(false, testValueOfX(code, 3U, 0));
code = "class A\n"
"{\n"
" void f1(int x) { return x; }\n"
" void f2(int x) {\n"
" f1(123);\n"
" }\n"
"};";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
code = "class A\n"
"{\n"
" virtual void f1(int x) { return x; }\n"
" void f2(int x) {\n"
" f1(123);\n"
" }\n"
"};";
ASSERT_EQUALS(true, testValueOfX(code, 3U, 123));
} }
void valueFlowFunctionReturn() { void valueFlowFunctionReturn() {
@ -1941,6 +1960,30 @@ private:
" return 0;\n" " return 0;\n"
"}"; "}";
ASSERT_EQUALS(false, testValueOfX(code, 4U, ValueFlow::Value::MovedVariable)); ASSERT_EQUALS(false, testValueOfX(code, 4U, ValueFlow::Value::MovedVariable));
code = "class A\n"
"{\n"
" int f1(int x) {\n"
" return x+1;\n"
" }\n"
" void f2() {\n"
" x = 10 - f1(2);\n"
" }\n"
"};";
ASSERT_EQUALS(7, valueOfTok(code, "-").intvalue);
ASSERT_EQUALS(true, valueOfTok(code, "-").isKnown());
code = "class A\n"
"{\n"
" virtual int f1(int x) {\n"
" return x+1;\n"
" }\n"
" void f2() {\n"
" x = 10 - f1(2);\n"
" }\n"
"};";
ASSERT_EQUALS(7, valueOfTok(code, "-").intvalue);
ASSERT_EQUALS(false, valueOfTok(code, "-").isKnown());
} }
void valueFlowFunctionDefaultParameter() { void valueFlowFunctionDefaultParameter() {