Fixed #1792 (false positive: Variable 'test' is assigned a value that is never used)

This commit is contained in:
Robert Reif 2010-06-14 15:46:57 +02:00 committed by Daniel Marjamäki
parent cb7e9fbec1
commit 98ae660260
5 changed files with 40 additions and 0 deletions

View File

@ -1378,6 +1378,9 @@ void CheckOther::functionVariableUsage()
const Variables::VariableUsage &usage = it->second; const Variables::VariableUsage &usage = it->second;
const std::string &varname = usage._name->str(); const std::string &varname = usage._name->str();
if (usage._name->isUnused())
continue;
if (usage.unused() && !usage._modified) if (usage.unused() && !usage._modified)
unusedVariableError(usage._name, varname); unusedVariableError(usage._name, varname);

View File

@ -35,6 +35,7 @@ Token::Token(Token **t) :
_isUnsigned(false), _isUnsigned(false),
_isSigned(false), _isSigned(false),
_isLong(false), _isLong(false),
_isUnused(false),
_varId(0), _varId(0),
_next(0), _next(0),
_previous(0), _previous(0),

View File

@ -172,6 +172,14 @@ public:
{ {
_isLong = size; _isLong = size;
} }
bool isUnused() const
{
return _isUnused;
}
void isUnused(bool used)
{
_isUnused = used;
}
bool isStandardType() const; bool isStandardType() const;
bool isIntegerType() const; bool isIntegerType() const;
@ -368,6 +376,7 @@ private:
bool _isUnsigned; bool _isUnsigned;
bool _isSigned; bool _isSigned;
bool _isLong; bool _isLong;
bool _isUnused;
unsigned int _varId; unsigned int _varId;
Token *_next; Token *_next;
Token *_previous; Token *_previous;

View File

@ -172,6 +172,7 @@ void Tokenizer::addtoken(const Token * tok, const unsigned int lineno, const uns
_tokensBack->isUnsigned(tok->isUnsigned()); _tokensBack->isUnsigned(tok->isUnsigned());
_tokensBack->isSigned(tok->isSigned()); _tokensBack->isSigned(tok->isSigned());
_tokensBack->isLong(tok->isLong()); _tokensBack->isLong(tok->isLong());
_tokensBack->isUnused(tok->isUnused());
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -7678,6 +7679,16 @@ void Tokenizer::simplifyAttribute()
{ {
if (Token::simpleMatch(tok, "__attribute__ (") && tok->next()->link() && tok->next()->link()->next()) if (Token::simpleMatch(tok, "__attribute__ (") && tok->next()->link() && tok->next()->link()->next())
{ {
if (Token::simpleMatch(tok->tokAt(2), "( unused )"))
{
// check if after variable name
if (Token::Match(tok->next()->link()->next(), ";|="))
{
if (Token::Match(tok->previous(), "%type%"))
tok->previous()->isUnused(true);
}
}
Token::eraseTokens(tok, tok->next()->link()->next()); Token::eraseTokens(tok, tok->next()->link()->next());
tok->deleteThis(); tok->deleteThis();
} }

View File

@ -105,6 +105,7 @@ private:
TEST_CASE(localvarShift); // 1 >> var TEST_CASE(localvarShift); // 1 >> var
TEST_CASE(localvarCast); TEST_CASE(localvarCast);
TEST_CASE(localvarClass); TEST_CASE(localvarClass);
TEST_CASE(localvarUnused);
} }
void structmember1() void structmember1()
@ -1893,6 +1894,21 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS(std::string(""), errout.str()); ASSERT_EQUALS(std::string(""), errout.str());
} }
void localvarUnused()
{
functionVariableUsage("int foo()\n"
"{\n"
" bool test __attribute__((unused));\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("int foo()\n"
"{\n"
" bool test __attribute__((unused)) = true;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
}; };
REGISTER_TEST(TestUnusedVar) REGISTER_TEST(TestUnusedVar)