Fix #810 (false positive: Division with signed and unsigned operators)
http://sourceforge.net/apps/trac/cppcheck/ticket/810
This commit is contained in:
parent
cbe9fcad9f
commit
1401e3f668
|
@ -308,27 +308,25 @@ void CheckOther::checkUnsignedDivision()
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Check for "ivar / uvar" and "uvar / ivar"
|
// Check for "ivar / uvar" and "uvar / ivar"
|
||||||
std::map<std::string, char> varsign;
|
std::map<unsigned int, char> varsign;
|
||||||
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
|
||||||
{
|
{
|
||||||
if (Token::Match(tok, "[{};(,] %type% %var% [;=,)]"))
|
if (Token::Match(tok, "[{};(,] %type% %var% [;=,)]"))
|
||||||
{
|
{
|
||||||
const char *type = tok->strAt(1);
|
const char *type = tok->strAt(1);
|
||||||
if (strcmp(type, "char") == 0 || strcmp(type, "short") == 0 || strcmp(type, "int") == 0)
|
if (strcmp(type, "char") == 0 || strcmp(type, "short") == 0 || strcmp(type, "int") == 0)
|
||||||
varsign[tok->strAt(2)] = 's';
|
varsign[tok->tokAt(2)->varId()] = 's';
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (Token::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]"))
|
else if (Token::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]"))
|
||||||
varsign[tok->strAt(3)] = 'u';
|
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%"))
|
||||||
{
|
{
|
||||||
if (ErrorLogger::udivWarning(*_settings))
|
if (ErrorLogger::udivWarning(*_settings))
|
||||||
{
|
{
|
||||||
const char *varname1 = tok->strAt(1);
|
char sign1 = varsign[tok->tokAt(1)->varId()];
|
||||||
const char *varname2 = tok->strAt(3);
|
char sign2 = varsign[tok->tokAt(3)->varId()];
|
||||||
char sign1 = varsign[varname1];
|
|
||||||
char sign2 = varsign[varname2];
|
|
||||||
|
|
||||||
if (sign1 && sign2 && sign1 != sign2)
|
if (sign1 && sign2 && sign1 != sign2)
|
||||||
{
|
{
|
||||||
|
@ -342,8 +340,7 @@ void CheckOther::checkUnsignedDivision()
|
||||||
{
|
{
|
||||||
if (tok->strAt(3)[0] == '-' && ErrorLogger::udivError())
|
if (tok->strAt(3)[0] == '-' && ErrorLogger::udivError())
|
||||||
{
|
{
|
||||||
const char *varname1 = tok->strAt(1);
|
char sign1 = varsign[tok->tokAt(1)->varId()];
|
||||||
char sign1 = varsign[varname1];
|
|
||||||
if (sign1 == 'u')
|
if (sign1 == 'u')
|
||||||
{
|
{
|
||||||
udivError(tok->next());
|
udivError(tok->next());
|
||||||
|
@ -355,8 +352,7 @@ void CheckOther::checkUnsignedDivision()
|
||||||
{
|
{
|
||||||
if (tok->strAt(1)[0] == '-' && ErrorLogger::udivError())
|
if (tok->strAt(1)[0] == '-' && ErrorLogger::udivError())
|
||||||
{
|
{
|
||||||
const char *varname2 = tok->strAt(3);
|
char sign2 = varsign[tok->tokAt(3)->varId()];
|
||||||
char sign2 = varsign[varname2];
|
|
||||||
if (sign2 == 'u')
|
if (sign2 == 'u')
|
||||||
{
|
{
|
||||||
udivError(tok->next());
|
udivError(tok->next());
|
||||||
|
|
|
@ -179,6 +179,14 @@ private:
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n", true, true);
|
"}\n", true, true);
|
||||||
ASSERT_EQUALS("[test.cpp:6]: (possible style) Division with signed and unsigned operators\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:6]: (possible style) Division with signed and unsigned operators\n", errout.str());
|
||||||
|
|
||||||
|
check("void a(int i) { }\n"
|
||||||
|
"int foo( unsigned int sz )\n"
|
||||||
|
"{\n"
|
||||||
|
" register unsigned int i=1;\n"
|
||||||
|
" return i/sz;\n"
|
||||||
|
"}\n", true, true);
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue