Don't warn about division by zero if zero if floating point number.

Fix bug from mathlib isInt() and add test case from which few are false positives
This commit is contained in:
Reijo Tomperi 2009-09-01 22:06:46 +03:00
parent 93659bbe08
commit 8ab26e85cf
4 changed files with 28 additions and 8 deletions

View File

@ -1218,15 +1218,16 @@ void CheckOther::checkZeroDivision()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (Token::Match(tok, "/ %num%") &&
MathLib::toLongNumber(tok->next()->str()) == 0L &&
MathLib::toDoubleNumber(tok->next()->str()) == 0.0)
MathLib::isInt(tok->next()->str()) &&
MathLib::toLongNumber(tok->next()->str()) == 0L)
{
zerodivError(tok);
}
else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , %num% )")
&& MathLib::toLongNumber(tok->tokAt(4)->str()) == 0L
&& MathLib::toDoubleNumber(tok->tokAt(4)->str()) == 0.0)
else if (Token::Match(tok, "div|ldiv|lldiv|imaxdiv ( %num% , %num% )") &&
MathLib::isInt(tok->tokAt(4)->str()) &&
MathLib::toLongNumber(tok->tokAt(4)->str()) == 0L)
{
zerodivError(tok);
}

View File

@ -143,7 +143,7 @@ bool MathLib::isInt(const std::string & s)
{
while (std::isdigit(s[n])) ++n;
// unsigned or long
if (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n;
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n;
}
// eat up whitespace
while (std::isspace(s[n]))

View File

@ -33,6 +33,7 @@ private:
{
TEST_CASE(calculate);
TEST_CASE(convert);
TEST_CASE(isint);
}
void calculate()
@ -52,6 +53,21 @@ private:
ASSERT_EQUALS(8, MathLib::toLongNumber("010"));
ASSERT_EQUALS(10, MathLib::toLongNumber("10"));
}
void isint()
{
ASSERT_EQUALS(true, MathLib::isInt("0xa"));
ASSERT_EQUALS(true, MathLib::isInt("0l"));
ASSERT_EQUALS(true, MathLib::isInt("0L"));
ASSERT_EQUALS(true, MathLib::isInt("0ul"));
ASSERT_EQUALS(true, MathLib::isInt("0ull"));
ASSERT_EQUALS(true, MathLib::isInt("0llu"));
ASSERT_EQUALS(true, MathLib::isInt("333L"));
TODO_ASSERT_EQUALS(true, MathLib::isInt("330L"));
TODO_ASSERT_EQUALS(true, MathLib::isInt("330llu"));
ASSERT_EQUALS(false, MathLib::isInt("0.4"));
ASSERT_EQUALS(false, MathLib::isInt("2352.3f"));
}
};
REGISTER_TEST(TestMathLib)

View File

@ -174,11 +174,13 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
// Don't warn about floating points (gcc doesn't warn either)
// and floating points are handled differently than integers.
check("void f()\n"
"{\n"
" long a = b / 0.0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
@ -186,11 +188,12 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
// Don't warn about 0.0
check("void f()\n"
"{\n"
" div_t divresult = div (1,0.0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Division by zero\n", errout.str());
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"