fix #2786 (new check: Using sizeof with numeric constant)

Add unit test and improve check with negative constant because in cppcheck %num%
means 'integer'
This commit is contained in:
seb777 2011-05-27 02:04:47 +08:00 committed by Sébastien Debrard
parent a99aded1a4
commit e06b2419a4
2 changed files with 29 additions and 1 deletions

View File

@ -214,7 +214,11 @@ void CheckOther::checkSizeofForNumericParameter()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "sizeof ( %num% )") || Token::Match(tok, "sizeof %num%"))
if (Token::Match(tok, "sizeof ( %num% )")
|| Token::Match(tok, "sizeof ( - %num% )")
|| Token::Match(tok, "sizeof %num%")
|| Token::Match(tok, "sizeof - %num%")
)
{
sizeofForNumericParameterError(tok);
}
@ -960,6 +964,17 @@ void CheckOther::checkUnsignedDivision()
}
}
}
else if (Token::Match(tok, "|[|=|return|%op% %var% / %var%"))
{
//std::cout << "cicicicic" << std::endl;
char sign1 = varsign[tok->tokAt(1)->varId()];
char sign2 = varsign[tok->tokAt(3)->varId()];
if ((sign1 == 'u' && sign2 == 's') || (sign1 == 's' && sign2 == 'u'))
{
//udivError(tok);
}
}
}
}

View File

@ -2313,12 +2313,25 @@ private:
);
ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof with a numeric constant as function argument might not be what you intended.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof(-10) << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof with a numeric constant as function argument might not be what you intended.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof 10 << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof with a numeric constant as function argument might not be what you intended.\n", errout.str());
check("void f() {\n"
" std::cout << sizeof -10 << std::endl;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:2]: (error) Using sizeof with a numeric constant as function argument might not be what you intended.\n", errout.str());
}
void clarifyCalculation()