Fixed #7546 (Assignment to array element not detected if pointer addition is used)

This commit is contained in:
Daniel Marjamäki 2017-02-28 22:04:05 +01:00
parent 16c06e5714
commit 841e0c2921
2 changed files with 22 additions and 1 deletions

View File

@ -674,6 +674,18 @@ static const Token * skipBracketsAndMembers(const Token *tok)
return tok;
}
static void useFunctionArgs(const Token *tok, Variables& variables)
{
// TODO: Match function args to see if they are const or not. Assume that const data is not written.
if (!tok)
return;
if (Token::Match(tok, "[,+]")) {
useFunctionArgs(tok->astOperand1(), variables);
useFunctionArgs(tok->astOperand2(), variables);
} else if (tok->variable() && tok->variable()->isArray()) {
variables.use(tok->varId(), tok);
}
}
//---------------------------------------------------------------------------
// Usage of function variables
@ -1108,8 +1120,9 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
}
// function
else if (Token::Match(tok, "%var% (")) {
else if (Token::Match(tok, "%name% (")) {
variables.read(tok->varId(), tok);
useFunctionArgs(tok->next()->astOperand2(), variables);
} else if (Token::Match(tok, "std :: ref ( %var% )")) {
variables.eraseAll(tok->tokAt(4)->varId());
}

View File

@ -1060,6 +1060,14 @@ private:
" f(a[0]);\n"
"}");
ASSERT_EQUALS("", errout.str());
functionVariableUsage("void f(int * i);\n"
"void foo()\n"
"{\n"
" int a[10];\n"
" f(a+1);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void localvar3() {