fix #2730 (The same expression on both sides of != is OK when checking for NaN)

This commit is contained in:
Robert Reif 2011-04-18 19:20:27 -04:00
parent 1b7183a294
commit c238b1bba6
2 changed files with 36 additions and 3 deletions

View File

@ -2908,7 +2908,7 @@ void CheckOther::checkMathFunctions()
}
// sqrt( x ): if x is negative the result is undefined
else if (tok->varId() == 0 &&
Token::Match(tok, "sqrt ( %num% )") &&
Token::Match(tok, "sqrt|sqrtf|sqrtl ( %num% )") &&
MathLib::isNegative(tok->tokAt(2)->str()))
{
mathfunctionCallError(tok);
@ -3257,6 +3257,17 @@ void CheckOther::checkDuplicateExpression()
if (Token::Match(tok, "(|&&|%oror% %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% )|&&|%oror%") &&
tok->strAt(1) == tok->strAt(3))
{
// float == float and float != float are valid NaN checks
if (Token::Match(tok->tokAt(2), "==|!=") && tok->next()->varId())
{
const Variable * var = symbolDatabase->getVariableFromVarId(tok->next()->varId());
if (var && var->typeStartToken() == var->typeEndToken())
{
if (Token::Match(var->typeStartToken(), "float|double"))
continue;
}
}
duplicateExpressionError(tok->next(), tok->tokAt(3), tok->strAt(2));
}
else if (Token::Match(tok, "(|&&|%oror% %var% . %var% &&|%oror%|==|!=|<=|>=|<|>|-|%or% %var% . %var% )|&&|%oror%") &&

View File

@ -115,7 +115,8 @@ private:
TEST_CASE(duplicateIf);
TEST_CASE(duplicateBranch);
TEST_CASE(duplicateExpression);
TEST_CASE(duplicateExpression1);
TEST_CASE(duplicateExpression2); // ticket #2730
}
void check(const char code[], const char *filename = NULL)
@ -2502,7 +2503,7 @@ private:
ASSERT_EQUALS("", errout.str());
}
void duplicateExpression()
void duplicateExpression1()
{
check("voif foo() {\n"
" if (a == a) { }\n"
@ -2523,6 +2524,27 @@ private:
"[test.cpp:5] -> [test.cpp:5]: (style) Same expression on both sides of '>'.\n"
"[test.cpp:6] -> [test.cpp:6]: (style) Same expression on both sides of '<'.\n", errout.str());
}
void duplicateExpression2() // ticket #2730
{
check("int main()\n"
"{\n"
" long double ldbl;\n"
" double dbl, in;\n"
" float flt;\n"
" int have_nan = 0;\n"
" ldbl = sqrtl(-1.0);\n"
" dbl = sqrt(-1.0);\n"
" flt = sqrtf(-1.0);\n"
" if (ldbl != ldbl) have_nan = 1;\n"
" if (!(dbl == dbl)) have_nan = 1;\n"
" if (flt != flt) have_nan = 1;\n"
" return have_nan;\n"
"}");
ASSERT_EQUALS("[test.cpp:7]: (error) Passing value -1.0 to sqrtl() leads to undefined result\n"
"[test.cpp:8]: (error) Passing value -1.0 to sqrt() leads to undefined result\n"
"[test.cpp:9]: (error) Passing value -1.0 to sqrtf() leads to undefined result\n", errout.str());
}
};
REGISTER_TEST(TestOther)