isVariableChangedByFunctionCall: Fix FN when constructor argument is const reference

This commit is contained in:
Daniel Marjamäki 2017-09-20 14:03:56 +02:00
parent 3e231a9325
commit 5f4b06c0f4
2 changed files with 14 additions and 3 deletions

View File

@ -418,10 +418,10 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
const ::Scope *typeScope = tok->variable()->typeScope();
if (typeScope) {
for (std::list<Function>::const_iterator it = typeScope->functionList.begin(); it != typeScope->functionList.end(); ++it) {
if (!it->isConstructor() || it->argCount() != argCount)
if (!it->isConstructor() || it->argCount() < argCount)
continue;
const Variable *arg = it->getArgumentVar(argnr);
if (arg && arg->isReference())
if (arg && arg->isReference() && !arg->isConst())
return true;
}
return false;

View File

@ -1804,7 +1804,7 @@ private:
code = "class C {\n"
"public:\n"
" C(int &i);\n"
" C(int &i);\n" // non-const argument => might be changed
"};\n"
"int f() {\n"
" int x=1;\n"
@ -1812,6 +1812,17 @@ private:
" return x;\n"
"}";
ASSERT_EQUALS(false, testValueOfX(code, 8U, 1));
code = "class C {\n"
"public:\n"
" C(const int &i);\n" // const argument => is not changed
"};\n"
"int f() {\n"
" int x=1;\n"
" C c(x);\n"
" return x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 8U, 1));
}
void valueFlowForwardLambda() {