Tokenizer: Removed simplifications of standard functions that should be handled through configuration.

This commit is contained in:
Daniel Marjamäki 2016-10-18 19:25:58 +02:00
parent 466068f55d
commit 9cea2d6dfa
2 changed files with 0 additions and 337 deletions

View File

@ -7728,26 +7728,6 @@ void Tokenizer::simplifyMathFunctions()
// remove ( %num% )
tok->deleteNext(3);
simplifcationMade = true;
} else if (Token::Match(tok, "abs|fabs|labs|llabs ( %num% )")) {
if (Token::simpleMatch(tok->tokAt(-2), "std ::")) {
tok = tok->tokAt(-2);// set token index two steps back
tok->deleteNext(2); // delete "std ::"
}
// get number string
std::string strNumber(tok->strAt(2));
// is the string negative?
if (strNumber[0] == '-') {
strNumber = strNumber.substr(1); // remove '-' sign
}
tok->deleteNext(3); // delete e.g. abs ( 1 )
tok->str(strNumber); // insert result into token list
simplifcationMade = true;
} else if (Token::Match(tok, "fma|fmaf|fmal ( %any% , %any% , %any% )")) {
// Simplify: fma(a,b,c) == > ( a * b + c )
tok->tokAt(3)->str("*");
tok->tokAt(5)->str("+");
tok->deleteThis(); // delete fma call
simplifcationMade = true;
} else if (Token::Match(tok, "sqrt|sqrtf|sqrtl|cbrt|cbrtf|cbrtl ( %num% )")) {
// Simplify: sqrt(0) = 0 and cbrt(0) == 0
// sqrt(1) = 1 and cbrt(1) == 1
@ -7837,66 +7817,6 @@ void Tokenizer::simplifyMathFunctions()
tok->deleteNext(5); // delete e.g. fmax ( 1.0, 0.0 )
simplifcationMade = true;
}
} else if (Token::Match(tok, "isgreater ( %num% , %num% )")) {
// The isgreater(x,y) function is the same as calculating (x)>(y).
// It returns true (1) if x is greater than y and false (0) otherwise.
const std::string& strLeftNumber(tok->strAt(2)); // get left number
const std::string& strRightNumber(tok->strAt(4)); // get right number
const bool isGreater = MathLib::isGreater(strLeftNumber, strRightNumber); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isGreater == true) ? "true": "false"); // insert results
simplifcationMade = true;
} else if (Token::Match(tok, "isgreaterequal ( %num% , %num% )")) {
// The isgreaterequal(x,y) function is the same as calculating (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->strAt(2)); // get left number
const std::string& strRightNumber(tok->strAt(4)); // get right number
const bool isGreaterEqual = MathLib::isGreaterEqual(strLeftNumber, strRightNumber); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isGreaterEqual == true) ? "true": "false"); // insert results
simplifcationMade = true;
} else if (Token::Match(tok, "isless ( %num% , %num% )")) {
// Calling this function is the same as calculating (x)<(y).
// It returns true (1) if x is less than y.
// False (0) is returned otherwise.
const std::string& strLeftNumber(tok->strAt(2)); // get left number
const std::string& strRightNumber(tok->strAt(4)); // get right number
const bool isLess = MathLib::isLess(strLeftNumber, strRightNumber); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isLess == true) ? "true": "false"); // insert results
simplifcationMade = true;
} else if (Token::Match(tok, "islessequal ( %num% , %num% )")) {
// Calling this function is the same as calculating (x)<=(y).
// It returns true (1) if x is less or equal to y.
// False (0) is returned otherwise.
const std::string& strLeftNumber(tok->strAt(2)); // get left number
const std::string& strRightNumber(tok->strAt(4)); // get right number
const bool isLessEqual = MathLib::isLessEqual(strLeftNumber, strRightNumber); // compare numbers
tok->deleteNext(5); // delete tokens
tok->str((isLessEqual == true) ? "true": "false"); // insert results
simplifcationMade = true;
} else if (Token::Match(tok, "islessgreater ( %num% , %num% )")) {
// Calling this function is the same as calculating (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->strAt(2)); // get left number
const std::string& strRightNumber(tok->strAt(4)); // get right number
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
simplifcationMade = true;
} else if (Token::Match(tok, "div|ldiv|lldiv ( %any% , %num% )")) {
// Calling the function 'div(x,y)' is the same as calculating (x)/(y). In case y has the value 1
// (the identity element), the call can be simplified to (x).
const std::string& leftParameter(tok->strAt(2)); // get the left parameter
const std::string& rightNumber(tok->strAt(4)); // get right number
if (isOneNumber(rightNumber)) {
tok->str(leftParameter); // insert simplified result
tok->deleteNext(5); // delete tokens
simplifcationMade = true;
}
} else if (Token::Match(tok, "pow|powf|powl (")) {
if (Token::Match(tok->tokAt(2), "%num% , %num% )")) {
// In case of pow ( 0 , anyNumber > 0): It can be simplified to 0

View File

@ -395,7 +395,6 @@ private:
TEST_CASE(platformWin32WStringCat); // ticket #5015
TEST_CASE(platformWinWithNamespace);
TEST_CASE(simplifyMathFunctions); // ticket #5031
TEST_CASE(simplifyMathFunctions_sqrt);
TEST_CASE(simplifyMathFunctions_cbrt);
TEST_CASE(simplifyMathFunctions_exp);
@ -406,13 +405,7 @@ private:
TEST_CASE(simplifyMathFunctions_log10);
TEST_CASE(simplifyMathFunctions_log);
TEST_CASE(simplifyMathFunctions_log2);
TEST_CASE(simplifyMathFunctions_div);
TEST_CASE(simplifyMathFunctions_pow);
TEST_CASE(simplifyMathFunctions_islessgreater);
TEST_CASE(simplifyMathFunctions_islessequal);
TEST_CASE(simplifyMathFunctions_isless);
TEST_CASE(simplifyMathFunctions_isgreaterequal);
TEST_CASE(simplifyMathFunctions_isgreater);
TEST_CASE(simplifyMathFunctions_fmin);
TEST_CASE(simplifyMathFunctions_fmax);
TEST_CASE(simplifyMathFunctions_acosh);
@ -430,7 +423,6 @@ private:
TEST_CASE(simplifyMathFunctions_atan);
TEST_CASE(simplifyMathFunctions_atanh);
TEST_CASE(simplifyMathFunctions_expm1);
TEST_CASE(simplifyMathFunctions_fma);
TEST_CASE(simplifyMathExpressions); //ticket #1620
TEST_CASE(simplifyStaticConst);
@ -6900,40 +6892,6 @@ private:
ASSERT_EQUALS(expected_atanl, tokenizeAndStringify(code_atanl));
}
void simplifyMathFunctions_fma() {
// verify fma(), fmal(), fmaf() - simplifcation
const char code_fma[] ="int f(int a, int b, int c) { return fma(a,b,c); }";
const char expected_fma[] = "int f ( int a , int b , int c ) { return ( a * b + c ) ; }";
ASSERT_EQUALS(expected_fma, tokenizeAndStringify(code_fma));
const char code_fmaf[] ="float f ( float a , float b , float c ) { return fmaf(a,b,c); }";
const char expected_fmaf[] = "float f ( float a , float b , float c ) { return ( a * b + c ) ; }";
ASSERT_EQUALS(expected_fmaf, tokenizeAndStringify(code_fmaf));
const char code_fmal[] ="long double f ( long double a , long double b , long double c ) { return fmal(a,b,c); }";
const char expected_fmal[] = "long double f ( long double a , long double b , long double c ) { return ( a * b + c ) ; }";
ASSERT_EQUALS(expected_fmal, tokenizeAndStringify(code_fmal));
const char code_fma1[] = "void f() {\n"
" std::cout << \"fma(1,2,3): \" << fma(1,2,3) << std::endl;\n"
" std::cout << \"fmaf(1,2,3): \" << fmaf(1,2,3) << std::endl;\n"
" std::cout << \"fmal(1,2,3): \" << fmal(1,2,3) << std::endl;\n"
"};";
const char expected_fma1[] = "void f() {\n"
"std :: cout << \"fma(1,2,3): \" << 5 << std :: endl ;\n"
"std :: cout << \"fmaf(1,2,3): \" << 5 << std :: endl ;\n"
"std :: cout << \"fmal(1,2,3): \" << 5 << std :: endl ;\n"
"} ;";
const char current_fma1[] = "void f ( ) {\n"
"std :: cout << \"fma(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ;\n"
"std :: cout << \"fmaf(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ;\n"
"std :: cout << \"fmal(1,2,3): \" << ( 1 * 2 + 3 ) << std :: endl ;\n"
"} ;";
TODO_ASSERT_EQUALS(expected_fma1, current_fma1,tokenizeAndStringify(code_fma1));
}
void simplifyMathFunctions_tanh() {
// verify tanh(), tanhf(), tanhl() - simplifcation
const char code_tanh[] ="void f(int x) {\n"
@ -7446,42 +7404,6 @@ private:
ASSERT_EQUALS(expected_log2l, tokenizeAndStringify(code_log2l));
}
void simplifyMathFunctions_div() {
// verify div(), ldiv(), lldiv() - simplifcation
const char code_div[] ="void f(int x) {\n"
" std::cout << div(x,1);\n" //simplify
" std::cout << div(x,-1);\n" // do not simplify
" std::cout << ldiv(10L,1L);\n" // simplify
" std::cout << ldiv(10L,132L);\n" // do not simplify
" std::cout << lldiv(10LL,1LL);\n" // simplify
" std::cout << lldiv(10LL,132LL);\n" // do not simplify
"}";
const char expected_div[] = "void f ( int x ) {\n"
"std :: cout << x ;\n"
"std :: cout << div ( x , -1 ) ;\n"
"std :: cout << 10L ;\n"
"std :: cout << ldiv ( 10L , 132L ) ;\n"
"std :: cout << 10LL ;\n"
"std :: cout << lldiv ( 10LL , 132LL ) ;\n"
"}";
ASSERT_EQUALS(expected_div, tokenizeAndStringify(code_div));
// Do not simplify class members.
// case: div
const char code_div1[] = "int f(const Fred &fred) {return fred.div(12,3);}";
const char expected_div1[] = "int f ( const Fred & fred ) { return fred . div ( 12 , 3 ) ; }";
ASSERT_EQUALS(expected_div1, tokenizeAndStringify(code_div1));
// case: ldiv
const char code_div2[] = "int f(const Fred &fred) {return fred.ldiv(12,3);}";
const char expected_div2[] = "int f ( const Fred & fred ) { return fred . ldiv ( 12 , 3 ) ; }";
ASSERT_EQUALS(expected_div2, tokenizeAndStringify(code_div2));
// case: lldiv
const char code_div3[] = "int f(const Fred &fred) {return fred.lldiv(12,3);}";
const char expected_div3[] = "int f ( const Fred & fred ) { return fred . lldiv ( 12 , 3 ) ; }";
ASSERT_EQUALS(expected_div3, tokenizeAndStringify(code_div3));
}
void simplifyMathFunctions_pow() {
// verify pow(),pow(),powl() - simplifcation
const char code_pow[] ="void f() {\n"
@ -7534,141 +7456,6 @@ private:
ASSERT_EQUALS(expected_pow4, tokenizeAndStringify(code_pow4));
}
void simplifyMathFunctions_islessgreater() {
// 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));
}
void simplifyMathFunctions_islessequal() {
// 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));
}
void simplifyMathFunctions_isless() {
// 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));
}
void simplifyMathFunctions_isgreaterequal() {
// 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));
}
void simplifyMathFunctions_isgreater() {
// 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));
}
void simplifyMathFunctions_fmin() {
// verify fmin,fminl,fminl simplifcation
const char code_fmin[] ="void f() {\n"
@ -7735,50 +7522,6 @@ private:
ASSERT_EQUALS(expected_fmax1, tokenizeAndStringify(code_fmax1));
}
void simplifyMathFunctions() { //#5031
// 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
" std::cout<<abs(0);\n"
" std::cout<<fabs(0.0);\n"
" std::cout<<labs(0);\n"
" std::cout<<llabs(0);\n"
" std::cout<<std::abs(-1);\n" // in std:: namespeace
" std::cout<<std::fabs(-1.0);\n" // in std:: namespeace
" std::cout<<abs(-1);\n"
" std::cout<<fabs(-1.0);\n"
" std::cout<<labs(-1);\n"
" std::cout<<llabs(-1);\n"
" std::cout<<atol(\"1\");\n"
" std::cout<<atol(\"x\");\n"
" std::cout<<abs(atol(\"1\"));\n" // nested calls
"}";
const char expected1[] = "void foo ( ) {\n"
"std :: cout << 0 ;\n"
"std :: cout << 0.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 0.0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 0 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1.0 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1.0 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << atol ( \"x\" ) ;\n"
"std :: cout << 1 ;\n"
"}";
ASSERT_EQUALS(expected1, tokenizeAndStringify(code1));
// testcase from ticket #5031
const char code2[] = "extern int a; void f(){printf(\"%i\", abs(--a));}\n";
const char expected2[] = "extern int a ; void f ( ) { printf ( \"%i\" , abs ( -- a ) ) ; }";
ASSERT_EQUALS(expected2, tokenizeAndStringify(code2));
}
void simplifyMathExpressions() { //#1620
const char code1[] = "void foo() {\n"
" std::cout<<pow(sin(x),2)+pow(cos(x),2);\n"