don't print warning message if --enable=warning is not given.

message was of type:
(warning) Passing value 0 to foo() leads to implementation-defined result.
This commit is contained in:
Matthias Krüger 2015-04-06 13:37:27 +02:00
parent 592177200a
commit 988acf11b4
1 changed files with 43 additions and 40 deletions

View File

@ -1872,6 +1872,7 @@ void CheckOther::nanInArithmeticExpressionError(const Token *tok)
void CheckOther::checkMathFunctions() void CheckOther::checkMathFunctions()
{ {
bool styleC99 = _settings->isEnabled("style") && _settings->standards.c != Standards::C89 && _settings->standards.cpp != Standards::CPP03; bool styleC99 = _settings->isEnabled("style") && _settings->standards.c != Standards::C89 && _settings->standards.cpp != Standards::CPP03;
bool printWarnings = _settings->isEnabled("warning");
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size(); const std::size_t functions = symbolDatabase->functionScopes.size();
@ -1880,48 +1881,50 @@ void CheckOther::checkMathFunctions()
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (tok->varId()) if (tok->varId())
continue; continue;
if (tok->strAt(-1) != "." if (printWarnings) {
&& Token::Match(tok, "log|logf|logl|log10|log10f|log10l ( %num% )")) { if (tok->strAt(-1) != "."
const std::string& number = tok->strAt(2); && Token::Match(tok, "log|logf|logl|log10|log10f|log10l ( %num% )")) {
bool isNegative = MathLib::isNegative(number); const std::string& number = tok->strAt(2);
bool isInt = MathLib::isInt(number); bool isNegative = MathLib::isNegative(number);
bool isFloat = MathLib::isFloat(number); bool isInt = MathLib::isInt(number);
if (isNegative && isInt && MathLib::toLongNumber(number) <= 0) { bool isFloat = MathLib::isFloat(number);
mathfunctionCallWarning(tok); // case log(-2) if (isNegative && isInt && MathLib::toLongNumber(number) <= 0) {
} else if (isNegative && isFloat && MathLib::toDoubleNumber(number) <= 0.) { mathfunctionCallWarning(tok); // case log(-2)
mathfunctionCallWarning(tok); // case log(-2.0) } else if (isNegative && isFloat && MathLib::toDoubleNumber(number) <= 0.) {
} else if (!isNegative && isFloat && MathLib::toDoubleNumber(number) <= 0.) { mathfunctionCallWarning(tok); // case log(-2.0)
mathfunctionCallWarning(tok); // case log(0.0) } else if (!isNegative && isFloat && MathLib::toDoubleNumber(number) <= 0.) {
} else if (!isNegative && isInt && MathLib::toLongNumber(number) <= 0) { mathfunctionCallWarning(tok); // case log(0.0)
mathfunctionCallWarning(tok); // case log(0) } else if (!isNegative && isInt && MathLib::toLongNumber(number) <= 0) {
mathfunctionCallWarning(tok); // case log(0)
}
} }
}
// acos( x ), asin( x ) where x is defined for interval [-1,+1], but not beyond // acos( x ), asin( x ) where x is defined for interval [-1,+1], but not beyond
else if (Token::Match(tok, "acos|acosl|acosf|asin|asinf|asinl ( %num% )")) { else if (Token::Match(tok, "acos|acosl|acosf|asin|asinf|asinl ( %num% )")) {
if (std::fabs(MathLib::toDoubleNumber(tok->strAt(2))) > 1.0) if (std::fabs(MathLib::toDoubleNumber(tok->strAt(2))) > 1.0)
mathfunctionCallWarning(tok); mathfunctionCallWarning(tok);
} }
// sqrt( x ): if x is negative the result is undefined // sqrt( x ): if x is negative the result is undefined
else if (Token::Match(tok, "sqrt|sqrtf|sqrtl ( %num% )")) { else if (Token::Match(tok, "sqrt|sqrtf|sqrtl ( %num% )")) {
if (MathLib::isNegative(tok->strAt(2))) if (MathLib::isNegative(tok->strAt(2)))
mathfunctionCallWarning(tok); mathfunctionCallWarning(tok);
} }
// atan2 ( x , y): x and y can not be zero, because this is mathematically not defined // atan2 ( x , y): x and y can not be zero, because this is mathematically not defined
else if (Token::Match(tok, "atan2|atan2f|atan2l ( %num% , %num% )")) { else if (Token::Match(tok, "atan2|atan2f|atan2l ( %num% , %num% )")) {
if (MathLib::isNullValue(tok->strAt(2)) && MathLib::isNullValue(tok->strAt(4))) if (MathLib::isNullValue(tok->strAt(2)) && MathLib::isNullValue(tok->strAt(4)))
mathfunctionCallWarning(tok, 2); mathfunctionCallWarning(tok, 2);
} }
// fmod ( x , y) If y is zero, then either a range error will occur or the function will return zero (implementation-defined). // fmod ( x , y) If y is zero, then either a range error will occur or the function will return zero (implementation-defined).
else if (Token::Match(tok, "fmod|fmodf|fmodl ( %any%")) { else if (Token::Match(tok, "fmod|fmodf|fmodl ( %any%")) {
const Token* nextArg = tok->tokAt(2)->nextArgument(); const Token* nextArg = tok->tokAt(2)->nextArgument();
if (nextArg && nextArg->isNumber() && MathLib::isNullValue(nextArg->str())) if (nextArg && nextArg->isNumber() && MathLib::isNullValue(nextArg->str()))
mathfunctionCallWarning(tok, 2); mathfunctionCallWarning(tok, 2);
} }
// pow ( x , y) If x is zero, and y is negative --> division by zero // pow ( x , y) If x is zero, and y is negative --> division by zero
else if (Token::Match(tok, "pow|powf|powl ( %num% , %num% )")) { else if (Token::Match(tok, "pow|powf|powl ( %num% , %num% )")) {
if (MathLib::isNullValue(tok->strAt(2)) && MathLib::isNegative(tok->strAt(4))) if (MathLib::isNullValue(tok->strAt(2)) && MathLib::isNegative(tok->strAt(4)))
mathfunctionCallWarning(tok, 2); mathfunctionCallWarning(tok, 2);
}
} }
if (styleC99) { if (styleC99) {