Tokenizer:simplifyMathExpressions: fixed false negative patterns for simplification.

This commit is contained in:
orbitcowboy 2013-10-11 11:58:37 -07:00
parent b6c405ef21
commit 9debeab47c
2 changed files with 65 additions and 9 deletions

View File

@ -10330,10 +10330,10 @@ void Tokenizer::simplifyMathExpressions()
// pow(cosh(x),2)-pow(sinh(x),2) = -1
// @todo: sinh(x) * sinh(x) - cosh(x) * cosh(x) = -1
// cosh(x) * cosh(x) - sinh(x) * sinh(x) = -1
if (Token::simpleMatch(tok, "pow (")) {
if (Token::simpleMatch(tok->tokAt(2), "sin (")) {
if (Token::Match(tok, "pow|powf|powl (")) {
if (Token::Match(tok->tokAt(2), "sin|sinf|sinl (")) {
Token * const tok2 = tok->linkAt(3);
if (!Token::Match(tok2, ") , %num% ) + pow ( cos ("))
if (!Token::Match(tok2, ") , %num% ) + pow|powf|powl ( cos|cosf|cosl ("))
continue;
const std::string leftExponent = tok2->tokAt(2)->str();
if (!isTwoNumber(leftExponent))
@ -10349,9 +10349,9 @@ void Tokenizer::simplifyMathExpressions()
Token::eraseTokens(tok, tok3->link()->tokAt(4));
tok->str("1");
}
} else if (Token::simpleMatch(tok->tokAt(2), "cos (")) {
} else if (Token::Match(tok->tokAt(2), "cos|cosf|cosl (")) {
Token * const tok2 = tok->linkAt(3);
if (!Token::Match(tok2, ") , %num% ) + pow ( sin ("))
if (!Token::Match(tok2, ") , %num% ) + pow|powf|powl ( sin|sinf|sinl ("))
continue;
const std::string leftExponent = tok2->tokAt(2)->str();
if (!isTwoNumber(leftExponent))
@ -10367,9 +10367,9 @@ void Tokenizer::simplifyMathExpressions()
Token::eraseTokens(tok, tok3->link()->tokAt(4));
tok->str("1");
}
} else if (Token::simpleMatch(tok->tokAt(2), "sinh (")) {
} else if (Token::Match(tok->tokAt(2), "sinh|sinhf|sinhl (")) {
Token * const tok2 = tok->linkAt(3);
if (!Token::Match(tok2, ") , %num% ) - pow ( cosh ("))
if (!Token::Match(tok2, ") , %num% ) - pow|powf|powl ( cosh|coshf|coshl ("))
continue;
const std::string leftExponent = tok2->tokAt(2)->str();
if (!isTwoNumber(leftExponent))
@ -10385,9 +10385,9 @@ void Tokenizer::simplifyMathExpressions()
Token::eraseTokens(tok, tok3->link()->tokAt(4));
tok->str("-1");
}
} else if (Token::simpleMatch(tok->tokAt(2), "cosh (")) {
} else if (Token::Match(tok->tokAt(2), "cosh|coshf|coshl (")) {
Token * const tok2 = tok->linkAt(3);
if (!Token::Match(tok2, ") , %num% ) - pow ( sinh ("))
if (!Token::Match(tok2, ") , %num% ) - pow|powf|powl ( sinh|sinhf|sinhl ("))
continue;
const std::string leftExponent = tok2->tokAt(2)->str();
if (!isTwoNumber(leftExponent))

View File

@ -9735,6 +9735,62 @@ private:
"h = pow ( cosh ( x ) , 2.0 ) - pow ( sinh ( y ) , 2.0 ) ;\n"
"}";
ASSERT_EQUALS(code2, tokenizeAndStringify(code2));
const char code3[] = "void foo() {\n"
" std::cout<<powf(sinf(x),2)+powf(cosf(x),2);\n"
" std::cout<<powf(sinf(powf(sinf(y),2)+powf(cosf(y),2)),2)+powf(cosf(powf(sinf(y),2)+powf(cosf(y),2)),2);\n"
" std::cout<<powf(sinf(x),2.0)+powf(cosf(x),2.0);\n"
" std::cout<<powf(sinf(x*y+z),2.0)+powf(cosf(x*y+z),2.0);\n"
" std::cout<<powf(sinf(x*y+z),2)+powf(cosf(x*y+z),2);\n"
" std::cout<<powf(cosf(x),2)+powf(sinf(x),2);\n"
" std::cout<<powf(cosf(x),2.0)+powf(sinf(x),2.0);\n"
" std::cout<<powf(cosf(x*y+z),2.0)+powf(sinf(x*y+z),2.0);\n"
" std::cout<<powf(cosf(x*y+z),2)+powf(sinf(x*y+z),2);\n"
" std::cout<<powf(sinhf(x*y+z),2)-powf(coshf(x*y+z),2);\n"
" std::cout<<powf(sinhf(x),2)-powf(coshf(x),2);\n"
" std::cout<<powf(sinhf(x*y+z),2.0)-powf(coshf(x*y+z),2.0);\n"
" std::cout<<powf(sinhf(x),2.0)-powf(coshf(x),2.0);\n"
" std::cout<<powf(coshf(x*y+z),2)-powf(sinhf(x*y+z),2);\n"
" std::cout<<powf(coshf(x),2)-powf(sinhf(x),2);\n"
" std::cout<<powf(coshf(x*y+z),2.0)-powf(sinhf(x*y+z),2.0);\n"
" std::cout<<powf(coshf(x),2.0)-powf(sinhf(x),2.0);\n"
" std::cout<<powf(coshf(powf(x,1)),2.0)-powf(sinhf(powf(x,1)),2.0);\n"
"}";
const char expected3[] = "void foo ( ) {\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << 1 ;\n"
"std :: cout << -1 ;\n"
"std :: cout << -1 ;\n"
"std :: cout << -1 ;\n"
"std :: cout << -1 ;\n"
"std :: cout << -1 ;\n"
"std :: cout << -1 ;\n"
"std :: cout << -1 ;\n"
"std :: cout << -1 ;\n"
"std :: cout << -1 ;\n"
"}";
ASSERT_EQUALS(expected3, tokenizeAndStringify(code3));
const char code4[] = "void f ( ) {\n"
"a = powf ( sinf ( x ) , 2 ) + powf ( cosf ( y ) , 2 ) ;\n"
"b = powf ( sinhf ( x ) , 2 ) - powf ( coshf ( y ) , 2 ) ;\n"
"c = powf ( sinf ( x ) , 2.0 ) + powf ( cosf ( y ) , 2.0 ) ;\n"
"d = powf ( sinhf ( x ) , 2.0 ) - powf ( coshf ( y ) , 2.0 ) ;\n"
"e = powf ( cosf ( x ) , 2 ) + powf ( sinf ( y ) , 2 ) ;\n"
"f = powf ( coshf ( x ) , 2 ) - powf ( sinhf ( y ) , 2 ) ;\n"
"g = powf ( cosf ( x ) , 2.0 ) + powf ( sinf ( y ) , 2.0 ) ;\n"
"h = powf ( coshf ( x ) , 2.0 ) - powf ( sinhf ( y ) , 2.0 ) ;\n"
"}";
ASSERT_EQUALS(code4, tokenizeAndStringify(code4));
}
static std::string testAst(const char code[]) {