Fix #1385 (False positive: unsigned division)

http://sourceforge.net/apps/trac/cppcheck/ticket/1385
This also fixes a bug in setVarId(). "unsigned int a" didn't get varid,
untill later when unsigned was simplified away.
This commit is contained in:
Reijo Tomperi 2010-02-12 22:24:06 +02:00
parent 669fe1b23d
commit b52fa9451f
3 changed files with 19 additions and 2 deletions

View File

@ -323,7 +323,10 @@ void CheckOther::checkUnsignedDivision()
else if (Token::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]"))
varsign[tok->tokAt(3)->varId()] = 'u';
else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / %var%"))
else if (!Token::Match(tok, "[).]") &&
Token::Match(tok->next(), "%var% / %var%") &&
tok->tokAt(1)->varId() != 0 &&
tok->tokAt(3)->varId() != 0)
{
if (ErrorLogger::udivWarning(*_settings))
{

View File

@ -1830,9 +1830,12 @@ void Tokenizer::setVarId()
if (Token::Match(tok, "[,;{}(] %type%"))
tok = tok->next();
if (tok->str() == "new" || tok->str() == "unsigned")
if (tok->str() == "new")
continue;
if (tok->str() == "unsigned")
tok = tok->next();
if (Token::Match(tok, "class|struct %type% :|{|;"))
continue;

View File

@ -115,6 +115,17 @@ private:
" result = i2 / i1;}\n"
);
ASSERT_EQUALS("", errout.str());
check("void f1()\n"
"{\n"
" unsigned int num = 0;\n"
"}\n"
"\n"
"void f2(int X)\n"
"{\n"
" X = X / z;}\n"
);
ASSERT_EQUALS("", errout.str());
}
void division5()