Fixed #5040 - simplify more math functions.

This commit is contained in:
Martin Ettl 2013-09-26 00:44:16 +02:00
parent 445d08d082
commit ea5bcfaa35
2 changed files with 358 additions and 38 deletions

View File

@ -8250,55 +8250,151 @@ void Tokenizer::cppcheckError(const Token *tok) const
// ------------------------------------------------------
// Simplify math functions.
// It simplifies following functions: atol(),abs(),fabs()
// labs(),llabs() in the tokenlist.
// It simplifies following functions: atol(), abs(), fabs()
// labs(), llabs(), fmin(), fminl(), fminf(), fmax(), fmaxl()
// fmaxf(), isgreater(), isgreaterequal(), isless()
// islessgreater(), islessequal(), pow(), powf(), powl()
// in the tokenlist.
//
// Reference:
// - http://www.cplusplus.com/reference/cmath/
// ------------------------------------------------------
void Tokenizer::simplifyMathFunctions()
{
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "atol ( %str% )")) {
if (!MathLib::isInt(tok->tokAt(2)->strValue())) {
if (Token::Match(tok, "atol ( %str% )")) { //@todo Add support for atoll()
if (tok->previous() &&
Token::simpleMatch(tok->tokAt(-2), "std ::")) {
tok = tok->tokAt(-2);// set token index two steps back
tok->deleteNext(2); // delete "std ::"
}
const std::string strNumber = tok->tokAt(2)->strValue(); // get number
const bool isNotAnInteger = (!MathLib::isInt(strNumber));// check: is not an integer
if (!strNumber.empty() && isNotAnInteger) {
// Ignore strings which we can't convert
continue;
}
if (tok->previous() &&
Token::simpleMatch(tok->tokAt(-2), "std ::")) {
// Delete "std ::"
tok = tok->tokAt(-2);
tok->deleteNext();
tok->deleteThis();
}
// Delete atol(
tok->deleteNext();
tok->deleteThis();
// Convert string into a number
tok->str(MathLib::toString(MathLib::toLongNumber(tok->strValue())));
// Delete remaining )
tok->deleteNext();
// remove atol ( %num%
tok->deleteNext(3);
// Convert string into a number and insert into token list
tok->str(MathLib::toString(MathLib::toLongNumber(strNumber)));
} else if (Token::Match(tok, "abs|fabs|labs|llabs ( %num% )")) {
if (tok->previous() &&
Token::simpleMatch(tok->tokAt(-2), "std ::")) {
// Delete "std ::"
tok = tok->tokAt(-2);
tok->deleteNext();
tok->deleteThis();
tok = tok->tokAt(-2);// set token index two steps back
tok->deleteNext(2); // delete "std ::"
}
// get number string
std::string strNumber(tok->tokAt(2)->str());
// is the string negative?
if (!strNumber.empty() && strNumber[0] == '-') {
strNumber = strNumber.substr(1); // remove '-' sign
}
tok->deleteNext(3); // delete e.g. abs ( 1 )
tok->str(strNumber); // insert result into token list
} else if (Token::Match(tok, "fmin|fminl|fminf ( %num% , %num% )")) {
// @todo if one of the parameters is NaN the other is returned
// e.g. printf ("fmin (NaN, -1.0) = %f\n", fmin(NaN,-1.0));
// e.g. printf ("fmin (-1.0, NaN) = %f\n", fmin(-1.0,NaN));
const std::string strLeftNumber(tok->tokAt(2)->str());
const std::string strRightNumber(tok->tokAt(4)->str());
const bool isLessEqual = MathLib::isLessEqual(strLeftNumber, strRightNumber);
// case: left <= right ==> insert left
if (!strLeftNumber.empty() && !strRightNumber.empty() && isLessEqual) {
tok->deleteNext(5); // delete e.g. fmin ( -1.0, 1.0 )
tok->str(strLeftNumber); // insert e.g. -1.0
} else { // case left > right ==> insert right
tok->deleteNext(5); // delete e.g. fmin ( 1.0, 0.0 )
tok->str(strRightNumber); // insert e.g. 0.0
}
} else if (Token::Match(tok, "fmax|fmaxl|fmaxf ( %num% , %num% )")) {
// @todo if one of the parameters is NaN the other is returned
// e.g. printf ("fmax (NaN, -1.0) = %f\n", fmax(NaN,-1.0));
// e.g. printf ("fmax (-1.0, NaN) = %f\n", fmax(-1.0,NaN));
const std::string strLeftNumber(tok->tokAt(2)->str());
const std::string strRightNumber(tok->tokAt(4)->str());
const bool isLessEqual = MathLib::isLessEqual(strLeftNumber, strRightNumber);
// case: left <= right ==> insert right
if (!strLeftNumber.empty() && !strRightNumber.empty() && isLessEqual) {
tok->deleteNext(5); // delete e.g. fmax ( -1.0, 1.0 )
tok->str(strRightNumber);// insert e.g. 1.0
} else { // case left > right ==> insert left
tok->deleteNext(5); // delete e.g. fmax ( 1.0, 0.0 )
tok->str(strLeftNumber); // insert e.g. 1.0
}
} else if (Token::Match(tok, "isgreater ( %num% , %num% )")) {
// The isgreater(x,y) function is the same as writing (x)>(y).
// It returns true (1) if x is greater than y and false (0) otherwise.
const std::string strLeftNumber(tok->tokAt(2)->str()); // get left number
const std::string strRightNumber(tok->tokAt(4)->str()); // get right number
if (!strRightNumber.empty() && !strLeftNumber.empty()) {
const bool isGreater = MathLib::isGreater(strLeftNumber, strRightNumber); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isGreater == true) ? "true": "false"); // insert results
}
} else if (Token::Match(tok, "isgreaterequal ( %num% , %num% )")) {
// The isgreaterequal(x,y) function is the same as writing (x)>=(y).
// It returns true (1) if x is greater than or equal to y.
// False (0) is returned otherwise.
const std::string strLeftNumber(tok->tokAt(2)->str()); // get left number
const std::string strRightNumber(tok->tokAt(4)->str()); // get right number
if (!strRightNumber.empty() && !strLeftNumber.empty()) {
const bool isGreaterEqual = MathLib::isGreaterEqual(strLeftNumber, strRightNumber); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isGreaterEqual == true) ? "true": "false"); // insert results
}
} else if (Token::Match(tok, "isless ( %num% , %num% )")) {
// The is (x,y) function is the same as writing (x)<(y).
// It returns true (1) if x is less than y.
// False (0) is returned otherwise.
const std::string strLeftNumber(tok->tokAt(2)->str()); // get left number
const std::string strRightNumber(tok->tokAt(4)->str()); // get right number
if (!strRightNumber.empty() && !strLeftNumber.empty()) {
const bool isLess = MathLib::isLess(strLeftNumber, strRightNumber); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isLess == true) ? "true": "false"); // insert results
}
} else if (Token::Match(tok, "islessequal ( %num% , %num% )")) {
// The is (x,y) function is the same as writing (x)<=(y).
// It returns true (1) if x is less or equal to y.
// False (0) is returned otherwise.
const std::string strLeftNumber(tok->tokAt(2)->str()); // get left number
const std::string strRightNumber(tok->tokAt(4)->str()); // get right number
if (!strRightNumber.empty() && !strLeftNumber.empty()) {
const bool isLessEqual = MathLib::isLessEqual(strLeftNumber, strRightNumber); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isLessEqual == true) ? "true": "false"); // insert results
}
} else if (Token::Match(tok, "islessgreater ( %num% , %num% )")) {
// The is (x,y) function is the same as writing (x)<(y) || (x)>(y).
// It returns true (1) if x is less than y or x is greater than y.
// False (0) is returned otherwise.
const std::string strLeftNumber(tok->tokAt(2)->str()); // get left number
const std::string strRightNumber(tok->tokAt(4)->str()); // get right number
if (!strRightNumber.empty() && !strLeftNumber.empty()) {
const bool isLessOrGreater(MathLib::isLess(strLeftNumber, strRightNumber) ||
MathLib::isGreater(strLeftNumber, strRightNumber)); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isLessOrGreater == true) ? "true": "false"); // insert results
}
}
// Delete abs (
tok->deleteNext();
tok->deleteThis();
std::string strNumber(tok->str());
if (!strNumber.empty() && strNumber[0] == '-')
strNumber = strNumber.substr(1);
// insert result into token list
tok->str(strNumber);
// Delete remaining )
tok->deleteNext();
else if (Token::Match(tok, "pow|powf|powl ( %any% , %num% )")) {
// pow( anyNumber,1 ) --> can be simplified to anynumber
const std::string base = (tok->tokAt(2)->str()); // get the base
const std::string exponent(tok->tokAt(4)->str()); // get exponent number
if (!exponent.empty()) {
const bool isNegative = MathLib::isNegative(exponent);
const bool isInteger = MathLib::isInt(exponent);
const bool isFloat = MathLib::isFloat(exponent);
if (!isNegative && isInteger && (MathLib::toLongNumber(exponent) == 1L)) {
tok->deleteNext(5); // delete tokens
tok->str(base); // insert results
} else if (!isNegative && isFloat && (MathLib::toDoubleNumber(exponent)-1.0) <= 0.) {
tok->deleteNext(5); // delete tokens
tok->str(base); // insert results
}
}
}
}
}

View File

@ -8279,6 +8279,229 @@ private:
}
void simplifyMathFunctions() { //#5031
// verify pow(),pow(),powl() simplifcation
const char code_pow[] ="void f() {\n"
" std::cout << pow(-1.0,1);\n"
" std::cout << pow(1.0,1);\n"
" std::cout << pow(0,1);\n"
" std::cout << powf(-1.0,1.0f);\n"
" std::cout << powf(1.0,1.0f);\n"
" std::cout << powf(0,1.0f);\n"
" std::cout << powl(-1.0,1.0);\n"
" std::cout << powl(1.0,1.0);\n"
" std::cout << powl(0,1.0);\n"
"}";
const char expected_pow[] = "void f ( ) {\n"
"std :: cout << -1.0 ;\n"
"std :: cout << 1.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << -1.0 ;\n"
"std :: cout << 1.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << -1.0 ;\n"
"std :: cout << 1.0 ;\n"
"std :: cout << 0 ;\n"
"}";
ASSERT_EQUALS(expected_pow, tokenizeAndStringify(code_pow));
// verify if code is simplified correctly.
// Do not simplify class members.
const char code_pow1[] = "int f(const Fred &fred) {return fred.pow(12,3);}";
const char expected_pow1[] = "int f ( const Fred & fred ) { return fred . pow ( 12 , 3 ) ; }";
ASSERT_EQUALS(expected_pow1, tokenizeAndStringify(code_pow1));
// verify islessgreater() simplification
const char code_islessgreater[] = "bool f(){\n"
"return islessgreater(1,0);\n" // (1 < 0) or (1 > 0) --> true
"}";
const char expected_islessgreater[] = "bool f ( ) {\nreturn true ;\n}";
ASSERT_EQUALS(expected_islessgreater, tokenizeAndStringify(code_islessgreater));
const char code_islessgreater1[] = "bool f(){\n"
"return islessgreater(0,1);\n" // (0 < 1) or (0 > 1) --> true
"}";
const char expected_islessgreater1[] = "bool f ( ) {\nreturn true ;\n}";
ASSERT_EQUALS(expected_islessgreater1, tokenizeAndStringify(code_islessgreater1));
const char code_islessgreater2[] = "bool f(){\n"
"return islessgreater(0,0);\n" // (0 < 0) or (0 > 0) --> false
"}";
const char expected_islessgreater2[] = "bool f ( ) {\nreturn false ;\n}";
ASSERT_EQUALS(expected_islessgreater2, tokenizeAndStringify(code_islessgreater2));
const char code_islessgreater3[] = "bool f(int i){\n"
"return islessgreater(i,0);\n" // <-- Do not simplify this
"}";
const char expected_islessgreater3[] = "bool f ( int i ) {\nreturn islessgreater ( i , 0 ) ;\n}";
ASSERT_EQUALS(expected_islessgreater3, tokenizeAndStringify(code_islessgreater3));
// verify islessequal() simplification
const char code_islessequal[] = "bool f(){\n"
"return islessequal(1,0);\n" // (1 <= 0) --> false
"}";
const char expected_islessequal[] = "bool f ( ) {\nreturn false ;\n}";
ASSERT_EQUALS(expected_islessequal, tokenizeAndStringify(code_islessequal));
const char code_islessequal1[] = "bool f(){\n"
"return islessequal(0,1);\n" // (0 <= 1) --> true
"}";
const char expected_islessequal1[] = "bool f ( ) {\nreturn true ;\n}";
ASSERT_EQUALS(expected_islessequal1, tokenizeAndStringify(code_islessequal1));
const char code_islessequal2[] = "bool f(){\n"
"return islessequal(0,0);\n" // (0 <= 0) --> true
"}";
const char expected_islessequal2[] = "bool f ( ) {\nreturn true ;\n}";
ASSERT_EQUALS(expected_islessequal2, tokenizeAndStringify(code_islessequal2));
const char code_islessequal3[] = "bool f(int i){\n"
"return islessequal(i,0);\n" // <-- Do not simplify this
"}";
const char expected_islessequal3[] = "bool f ( int i ) {\nreturn islessequal ( i , 0 ) ;\n}";
ASSERT_EQUALS(expected_islessequal3, tokenizeAndStringify(code_islessequal3));
// verify isless() simplification
const char code_isless[] = "bool f(){\n"
"return isless(1,0);\n" // (1 < 0) --> false
"}";
const char expected_isless[] = "bool f ( ) {\nreturn false ;\n}";
ASSERT_EQUALS(expected_isless, tokenizeAndStringify(code_isless));
const char code_isless1[] = "bool f(){\n"
"return isless(0,1);\n" // (0 < 1) --> true
"}";
const char expected_isless1[] = "bool f ( ) {\nreturn true ;\n}";
ASSERT_EQUALS(expected_isless1, tokenizeAndStringify(code_isless1));
const char code_isless2[] = "bool f(){\n"
"return isless(0,0);\n" // (0 < 0) --> false
"}";
const char expected_isless2[] = "bool f ( ) {\nreturn false ;\n}";
ASSERT_EQUALS(expected_isless2, tokenizeAndStringify(code_isless2));
const char code_isless3[] = "bool f(int i){\n"
"return isless(i,0);\n" // <-- Do not simplify this
"}";
const char expected_isless3[] = "bool f ( int i ) {\nreturn isless ( i , 0 ) ;\n}";
ASSERT_EQUALS(expected_isless3, tokenizeAndStringify(code_isless3));
// verify isgreaterequal() simplification
const char code_isgreaterequal[] = "bool f(){\n"
"return isgreaterequal(1,0);\n" // (1 >= 0) --> true
"}";
const char expected_isgreaterequal[] = "bool f ( ) {\nreturn true ;\n}";
ASSERT_EQUALS(expected_isgreaterequal, tokenizeAndStringify(code_isgreaterequal));
const char code_isgreaterequal1[] = "bool f(){\n"
"return isgreaterequal(0,1);\n" // (0 >= 1) --> false
"}";
const char expected_isgreaterequal1[] = "bool f ( ) {\nreturn false ;\n}";
ASSERT_EQUALS(expected_isgreaterequal1, tokenizeAndStringify(code_isgreaterequal1));
const char code_isgreaterequal2[] = "bool f(){\n"
"return isgreaterequal(0,0);\n" // (0 >= 0) --> true
"}";
const char expected_isgreaterequal2[] = "bool f ( ) {\nreturn true ;\n}";
ASSERT_EQUALS(expected_isgreaterequal2, tokenizeAndStringify(code_isgreaterequal2));
const char code_isgreaterequal3[] = "bool f(int i){\n"
"return isgreaterequal(i,0);\n" // <-- Do not simplify this
"}";
const char expected_isgreaterequal3[] = "bool f ( int i ) {\nreturn isgreaterequal ( i , 0 ) ;\n}";
ASSERT_EQUALS(expected_isgreaterequal3, tokenizeAndStringify(code_isgreaterequal3));
// verify isgreater() simplification
const char code_isgreater[] = "bool f(){\n"
"return isgreater(1,0);\n" // (1 > 0) --> true
"}";
const char expected_isgreater[] = "bool f ( ) {\nreturn true ;\n}";
ASSERT_EQUALS(expected_isgreater, tokenizeAndStringify(code_isgreater));
const char code_isgreater1[] = "bool f(){\n"
"return isgreater(0,1);\n" // (0 > 1) --> false
"}";
const char expected_isgreater1[] = "bool f ( ) {\nreturn false ;\n}";
ASSERT_EQUALS(expected_isgreater1, tokenizeAndStringify(code_isgreater1));
const char code_isgreater2[] = "bool f(){\n"
"return isgreater(0,0);\n" // (0 > 0) --> false
"}";
const char expected_isgreater2[] = "bool f ( ) {\nreturn false ;\n}";
ASSERT_EQUALS(expected_isgreater2, tokenizeAndStringify(code_isgreater2));
const char code_isgreater3[] = "bool f(int i){\n"
"return isgreater(i,0);\n" // <-- Do not simplify this
"}";
const char expected_isgreater3[] = "bool f ( int i ) {\nreturn isgreater ( i , 0 ) ;\n}";
ASSERT_EQUALS(expected_isgreater3, tokenizeAndStringify(code_isgreater3));
// verify fmax(),fmax(),fmaxl() simplifcation
const char code_fmax[] ="void f() {\n"
" std::cout << fmax(-1.0,0);\n"
" std::cout << fmax(1.0,0);\n"
" std::cout << fmax(0,0);\n"
" std::cout << fmaxf(-1.0,0);\n"
" std::cout << fmaxf(1.0,0);\n"
" std::cout << fmaxf(0,0);\n"
" std::cout << fmaxl(-1.0,0);\n"
" std::cout << fmaxl(1.0,0);\n"
" std::cout << fmaxl(0,0);\n"
"}";
const char expected_fmax[] = "void f ( ) {\n"
"std :: cout << 0 ;\n"
"std :: cout << 1.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 1.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 1.0 ;\n"
"std :: cout << 0 ;\n"
"}";
ASSERT_EQUALS(expected_fmax, tokenizeAndStringify(code_fmax));
// do not simplify this case
const char code_fmax1[] = "float f(float f) { return fmax(f,0);}";
const char expected_fmax1[] = "float f ( float f ) { return fmax ( f , 0 ) ; }";
ASSERT_EQUALS(expected_fmax1, tokenizeAndStringify(code_fmax1));
// verify fmin,fminl,fminl simplifcation
const char code_fmin[] ="void f() {\n"
" std::cout << fmin(-1.0,0);\n"
" std::cout << fmin(1.0,0);\n"
" std::cout << fmin(0,0);\n"
" std::cout << fminf(-1.0,0);\n"
" std::cout << fminf(1.0,0);\n"
" std::cout << fminf(0,0);\n"
" std::cout << fminl(-1.0,0);\n"
" std::cout << fminl(1.0,0);\n"
" std::cout << fminl(0,0);\n"
"}";
const char expected_fmin[] = "void f ( ) {\n"
"std :: cout << -1.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << -1.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << -1.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 0 ;\n"
"}";
ASSERT_EQUALS(expected_fmin, tokenizeAndStringify(code_fmin));
// do not simplify this case
const char code_fmin1[] = "float f(float f) { return fmin(f,0);}";
const char expected_fmin1[] = "float f ( float f ) { return fmin ( f , 0 ) ; }";
ASSERT_EQUALS(expected_fmin1, tokenizeAndStringify(code_fmin1));
// verify abs,fabs,labs,llabs,atol simplifcation
const char code1[] = "void foo() {\n"
" std::cout<<std::abs(0);\n" // in std:: namespeace
" std::cout<<std::fabs(0.0);\n" // in std:: namespeace
@ -8293,6 +8516,7 @@ private:
" std::cout<<labs(-1);\n"
" std::cout<<llabs(-1);\n"
" std::cout<<atol(\"1\");\n"
" std::cout<<atol(\"x\");\n"
"}";
const char expected1[] = "void foo ( ) {\n"
"std :: cout << 0 ;\n"
@ -8308,6 +8532,7 @@ private:
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << atol ( \"x\" ) ;\n"
"}";
ASSERT_EQUALS(expected1, tokenizeAndStringify(code1));
@ -8317,7 +8542,6 @@ private:
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2));
}
void simplifyMathExpressions() {//#1620
const char code1[] = "void foo() {\n"
" std::cout<<sin(0);\n"