add support in CheckOther::functionVariableUsage() for checking structures that are declared with struct keyword

This commit is contained in:
Robert Reif 2011-07-01 07:42:20 -04:00
parent 3225c9dd9b
commit 18e6509c5d
2 changed files with 22 additions and 3 deletions

View File

@ -1766,6 +1766,24 @@ void CheckOther::functionVariableUsage()
tok = tok->next();
}
// standard struct type declaration with possible initialization
// struct S s; struct S s = { 0 }; static struct S s;
else if (Token::Match(tok, "[;{}] static| struct %type% %var% ;|=") &&
(isRecordTypeWithoutSideEffects(tok->strAt(1) == "static" ? tok->tokAt(4) : tok->tokAt(3))))
{
tok = tok->next();
bool isStatic = tok->str() == "static";
if (isStatic)
tok = tok->next();
tok = tok->next();
variables.addVar(tok->next(), Variables::standard, info,
tok->tokAt(2)->str() == "=" || isStatic);
tok = tok->next();
}
// standard type declaration and initialization using constructor
// int i(0); static int j(0);
else if (Token::Match(tok, "[;{}] static| %type% %var% ( %any% ) ;") &&
@ -2162,7 +2180,7 @@ void CheckOther::functionVariableUsage()
variables.read(tok->varId());
}
else if (tok->varId() != varid1 && Token::Match(tok, "%var% ."))
variables.use(tok->varId());
variables.read(tok->varId());
else if (tok->varId() != varid1 &&
var2->_type == Variables::standard &&
tok->strAt(-1) != "&")

View File

@ -1887,7 +1887,8 @@ private:
" struct AB ab;\n"
" int * a = &ab.a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'ab' is not assigned a value\n"
"[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("struct AB { int a; int b; };\n"
"void foo()\n"
@ -2322,7 +2323,7 @@ private:
" struct ABC { int a, b, c; };\n"
" struct ABC abc = { 1, 2, 3 };\n"
"}\n");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'abc' is assigned a value that is never used\n", errout.str());
}
void localvarStruct3()