Fixed #3633 (False positive: struct array not assigned a value)

This commit is contained in:
Daniel Marjamäki 2012-02-26 08:29:02 +01:00
parent 4d3013d43d
commit 385c9d341d
2 changed files with 27 additions and 2 deletions

View File

@ -579,6 +579,21 @@ static const Token * skipBrackets(const Token *tok)
}
// Skip [ .. ] . x
static const Token * skipBracketsAndMembers(const Token *tok)
{
while (tok) {
if (tok->str() == "[")
tok = tok->link()->next();
else if (Token::Match(tok, ". %var%"))
tok = tok->tokAt(2);
else
break;
}
return tok;
}
//---------------------------------------------------------------------------
// Usage of function variables
//---------------------------------------------------------------------------
@ -804,7 +819,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
}
}
const Token *equal = skipBrackets(tok->next());
const Token * const equal = skipBracketsAndMembers(tok->next());
// checked for chained assignments
if (tok != start && equal && equal->str() == "=") {
@ -818,7 +833,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
}
// assignment
else if (Token::Match(tok, "%var% [") && Token::simpleMatch(skipBrackets(tok->next()), "=")) {
else if (Token::Match(tok, "%var% [") && Token::simpleMatch(skipBracketsAndMembers(tok->next()), "=")) {
unsigned int varid = tok->varId();
const Variables::VariableUsage *var = variables.find(varid);

View File

@ -117,6 +117,7 @@ private:
TEST_CASE(localvarStruct4); // Ticket #31: sigsegv on incomplete struct
TEST_CASE(localvarStruct5);
TEST_CASE(localvarStruct6);
TEST_CASE(localvarStructArray);
TEST_CASE(localvarOp); // Usage with arithmetic operators
TEST_CASE(localvarInvert); // Usage with inverted variable
@ -2462,6 +2463,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvarStructArray() {
// #3633 - detect that struct array is assigned a value
functionVariableUsage("void f() {\n"
" struct X x[10];\n"
" x[0].a = 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' is assigned a value that is never used\n", errout.str());
}
void localvarOp() {
const char op[] = "+-*/%&|^";
for (const char *p = op; *p; ++p) {