Tokenizer:simplifyMathFunctions: fixed false positives when simplifying special pow() cases.

This commit is contained in:
Martin Ettl 2013-10-03 22:54:45 +02:00
parent f8bd33f2bc
commit a8cf8f0984
2 changed files with 26 additions and 19 deletions

View File

@ -8526,22 +8526,7 @@ bool Tokenizer::simplifyMathFunctions()
} }
} }
} else if (Token::Match(tok, "pow|powf|powl (")) { } else if (Token::Match(tok, "pow|powf|powl (")) {
if (tok && Token::Match(tok->tokAt(2), " %any% , %num% )")) { if (tok && Token::Match(tok->tokAt(2), "%num% , %num% )")) {
// In case of pow( x , 1 ): It can be simplified to x.
const std::string leftParameter(tok->tokAt(2)->str()); // get the left parameter
const std::string rightNumber(tok->tokAt(4)->str()); // get right number
if (!rightNumber.empty() && !leftParameter.empty()) {
if (isOneNumber(rightNumber)) { // case: x^(1) = x
tok->deleteNext(5); // delete tokens
tok->str(leftParameter); // insert simplified result
simplifcationMade = true;
} else if (isZeroNumber(rightNumber)) { // case: x^(0) = 1
tok->deleteNext(5); // delete tokens
tok->str("1"); // insert simplified result
simplifcationMade = true;
}
}
} else if (tok && Token::Match(tok->tokAt(2), " %num% , %num% )")) {
// In case of pow ( 0 , anyNumber > 0): It can be simplified to 0 // In case of pow ( 0 , anyNumber > 0): It can be simplified to 0
// In case of pow ( 0 , 0 ): It simplified to 1 // In case of pow ( 0 , 0 ): It simplified to 1
// In case of pow ( 1 , anyNumber ): It simplified to 1 // In case of pow ( 1 , anyNumber ): It simplified to 1
@ -8566,6 +8551,22 @@ bool Tokenizer::simplifyMathFunctions()
} }
} }
} }
if (tok && Token::Match(tok->tokAt(2), "%any% , %num% )")) {
// In case of pow( x , 1 ): It can be simplified to x.
const std::string leftParameter(tok->tokAt(2)->str()); // get the left parameter
const std::string rightNumber(tok->tokAt(4)->str()); // get right number
if (!rightNumber.empty() && !leftParameter.empty()) {
if (isOneNumber(rightNumber)) { // case: x^(1) = x
tok->deleteNext(5); // delete tokens
tok->str(leftParameter); // insert simplified result
simplifcationMade = true;
} else if (isZeroNumber(rightNumber)) { // case: x^(0) = 1
tok->deleteNext(5); // delete tokens
tok->str("1"); // insert simplified result
simplifcationMade = true;
}
}
}
} }
} }
// returns true if a simplifcation was performed and false otherwise. // returns true if a simplifcation was performed and false otherwise.

View File

@ -8730,24 +8730,30 @@ private:
" std::cout << pow(-1.0,1);\n" " std::cout << pow(-1.0,1);\n"
" std::cout << pow(1.0,1);\n" " std::cout << pow(1.0,1);\n"
" std::cout << pow(0,1);\n" " std::cout << pow(0,1);\n"
" std::cout << pow(1,-6);\n"
" std::cout << powf(-1.0,1.0f);\n" " std::cout << powf(-1.0,1.0f);\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 << powf(0,1.0f);\n"
" std::cout << powf(1.0,-6.0f);\n"
" std::cout << powl(-1.0,1.0);\n" " std::cout << powl(-1.0,1.0);\n"
" std::cout << powl(1.0,1.0);\n" " std::cout << powl(1.0,1.0);\n"
" std::cout << powl(0,1.0);\n" " std::cout << powl(0,1.0);\n"
" std::cout << powl(1.0,-6.0d);\n"
"}"; "}";
const char expected_pow[] = "void f ( ) {\n" const char expected_pow[] = "void f ( ) {\n"
"std :: cout << -1.0 ;\n" "std :: cout << -1.0 ;\n"
"std :: cout << 1.0 ;\n" "std :: cout << 1 ;\n"
"std :: cout << 0 ;\n" "std :: cout << 0 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << -1.0 ;\n" "std :: cout << -1.0 ;\n"
"std :: cout << 1.0 ;\n" "std :: cout << 1 ;\n"
"std :: cout << 0 ;\n" "std :: cout << 0 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << -1.0 ;\n" "std :: cout << -1.0 ;\n"
"std :: cout << 1.0 ;\n" "std :: cout << 1 ;\n"
"std :: cout << 0 ;\n" "std :: cout << 0 ;\n"
"std :: cout << 1 ;\n"
"}"; "}";
ASSERT_EQUALS(expected_pow, tokenizeAndStringify(code_pow)); ASSERT_EQUALS(expected_pow, tokenizeAndStringify(code_pow));