struct member usage: fixed false positives

This commit is contained in:
Daniel Marjamäki 2009-01-04 14:55:18 +00:00
parent be2a10bfb6
commit a4d7ce1620
2 changed files with 11 additions and 15 deletions

View File

@ -625,28 +625,24 @@ void CheckOther::CheckStructMemberUsage()
if (structname && Token::Match(tok, "[{;]")) if (structname && Token::Match(tok, "[{;]"))
{ {
const Token *variableToken = 0; const char *varname = 0;
if (Token::Match(tok->next(), "%type% %var% [;[]")) if (Token::Match(tok->next(), "%type% %var% [;[]"))
variableToken = tok->tokAt( 2 ); varname = tok->strAt( 2 );
else if (Token::Match(tok->next(), "%type% %type% %var% [;[]")) else if (Token::Match(tok->next(), "%type% %type% %var% [;[]"))
variableToken = tok->tokAt( 2 ); varname = tok->strAt( 2 );
else if (Token::Match(tok->next(), "%type% * %var% [;[]")) else if (Token::Match(tok->next(), "%type% * %var% [;[]"))
variableToken = tok->tokAt( 3 ); varname = tok->strAt( 3 );
else if (Token::Match(tok->next(), "%type% %type% * %var% [;[]")) else if (Token::Match(tok->next(), "%type% %type% * %var% [;[]"))
variableToken = tok->tokAt( 4 ); varname = tok->strAt( 4 );
else else
continue; continue;
const std::string usagePattern( ". " + std::string(varname) );
bool used = false; bool used = false;
for ( const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next() ) for ( const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next() )
{ {
if ( tok->fileIndex() != 0 ) if (Token::simpleMatch(tok2, usagePattern.c_str() ))
continue;
if (Token::Match(tok2, ". %varid%", variableToken->varId() ))
{ {
if ( Token::simpleMatch(tok2->tokAt(2), "=") )
continue;
used = true; used = true;
break; break;
} }
@ -655,7 +651,7 @@ void CheckOther::CheckStructMemberUsage()
if ( ! used ) if ( ! used )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok) << ": struct member '" << structname << "::" << variableToken->str() << "' is never read"; errmsg << _tokenizer->fileLine(tok->next()) << ": struct member '" << structname << "::" << varname << "' is never used";
_errorLogger->reportErr(errmsg.str()); _errorLogger->reportErr(errmsg.str());
} }
} }

View File

@ -73,9 +73,9 @@ private:
" int b;\n" " int b;\n"
" int c;\n" " int c;\n"
"};\n" ); "};\n" );
ASSERT_EQUALS( std::string("[test.cpp:2]: struct member 'abc::a' is never read\n" ASSERT_EQUALS( std::string("[test.cpp:3]: struct member 'abc::a' is never used\n"
"[test.cpp:3]: struct member 'abc::b' is never read\n" "[test.cpp:4]: struct member 'abc::b' is never used\n"
"[test.cpp:4]: struct member 'abc::c' is never read\n"), errout.str() ); "[test.cpp:5]: struct member 'abc::c' is never used\n"), errout.str() );
} }
void structmember2() void structmember2()