Fixed #1620 (tokenizer: simplify well known math functions)
This commit is contained in:
parent
b156c727b0
commit
bf11248a09
|
@ -1646,6 +1646,9 @@ bool Tokenizer::tokenize(std::istream &code,
|
||||||
// replace 'NULL' and similar '0'-defined macros with '0'
|
// replace 'NULL' and similar '0'-defined macros with '0'
|
||||||
simplifyNull();
|
simplifyNull();
|
||||||
|
|
||||||
|
// replace 'sin(0)' to '0' and other similar math expressions
|
||||||
|
simplifyMathExpressions();
|
||||||
|
|
||||||
// combine "- %num%"
|
// combine "- %num%"
|
||||||
concatenateNegativeNumber();
|
concatenateNegativeNumber();
|
||||||
|
|
||||||
|
@ -9218,6 +9221,32 @@ void Tokenizer::printUnknownTypes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Tokenizer::simplifyMathExpressions()
|
||||||
|
{
|
||||||
|
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
||||||
|
|
||||||
|
if (Token::Match(tok,"exp ( 0 )") || Token::Match(tok,"cosh ( 0 )") || Token::Match(tok,"cos ( 0 )") || Token::Match(tok,"sqrt ( 1 )")) {
|
||||||
|
tok->deleteNext(3);
|
||||||
|
tok->str("1");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Token::Match(tok,"sinh ( 0 )") || Token::Match(tok,"sin ( 0 )") || Token::Match(tok,"sqrt ( 0 )") || Token::Match(tok,"ln ( 1 )")) {
|
||||||
|
tok->deleteNext(3);
|
||||||
|
tok->str("0");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Token::Match(tok,"pow ( sin ( %var% ) , 2 ) + pow ( cos ( %var% ) , 2 )")) {
|
||||||
|
tok->deleteNext(18);
|
||||||
|
tok->str("1");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Token::Match(tok,"pow ( sinh ( %var% ) , 2 ) - pow ( cosh ( %var% ) , 2 )")) {
|
||||||
|
tok->deleteNext(18);
|
||||||
|
tok->str("-1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const std::string& Tokenizer::getSourceFilePath() const
|
const std::string& Tokenizer::getSourceFilePath() const
|
||||||
{
|
{
|
||||||
if (list.getFiles().empty()) {
|
if (list.getFiles().empty()) {
|
||||||
|
|
|
@ -461,6 +461,11 @@ public:
|
||||||
*/
|
*/
|
||||||
void simplifyMathFunctions();
|
void simplifyMathFunctions();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simplify e.g. 'sin(0)' into '0'
|
||||||
|
*/
|
||||||
|
void simplifyMathExpressions();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modify strings in the token list by replacing hex and oct
|
* Modify strings in the token list by replacing hex and oct
|
||||||
* values. E.g. "\x61" -> "a" and "\000" -> "\0"
|
* values. E.g. "\x61" -> "a" and "\000" -> "\0"
|
||||||
|
|
|
@ -439,6 +439,8 @@ private:
|
||||||
TEST_CASE(platformWin64);
|
TEST_CASE(platformWin64);
|
||||||
TEST_CASE(platformUnix32);
|
TEST_CASE(platformUnix32);
|
||||||
TEST_CASE(platformUnix64);
|
TEST_CASE(platformUnix64);
|
||||||
|
|
||||||
|
TEST_CASE(simplifyMathExpressions); //ticket #1620
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Unspecified, const char* filename = "test.cpp", bool cpp11 = true) {
|
std::string tokenizeAndStringify(const char code[], bool simplify = false, bool expand = true, Settings::PlatformType platform = Settings::Unspecified, const char* filename = "test.cpp", bool cpp11 = true) {
|
||||||
|
@ -7188,6 +7190,36 @@ private:
|
||||||
|
|
||||||
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unix64));
|
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unix64));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void simplifyMathExpressions() {//#1620
|
||||||
|
const char *code1 ="void foo() {\n"
|
||||||
|
"std::cout<<sin(0);\n"
|
||||||
|
"std::cout<<cos(0);\n"
|
||||||
|
"std::cout<<sinh(0);\n"
|
||||||
|
"std::cout<<cosh(0);\n"
|
||||||
|
"std::cout<<exp(0);\n"
|
||||||
|
"std::cout<<sqrt(0);\n"
|
||||||
|
"std::cout<<sqrt(1);\n"
|
||||||
|
"std::cout<<ln(1);\n"
|
||||||
|
"std::cout<<pow(sin(x),2)+pow(cos(x),2);\n"
|
||||||
|
"std::cout<<pow(sinh(x),2)-pow(cosh(x),2);\n"
|
||||||
|
"}";
|
||||||
|
|
||||||
|
const char *expected1 ="void foo ( ) {\n"
|
||||||
|
"std :: cout << 0 ;\n"
|
||||||
|
"std :: cout << 1 ;\n"
|
||||||
|
"std :: cout << 0 ;\n"
|
||||||
|
"std :: cout << 1 ;\n"
|
||||||
|
"std :: cout << 1 ;\n"
|
||||||
|
"std :: cout << 0 ;\n"
|
||||||
|
"std :: cout << 1 ;\n"
|
||||||
|
"std :: cout << 0 ;\n"
|
||||||
|
"std :: cout << 1 ;\n"
|
||||||
|
"std :: cout << -1 ;\n"
|
||||||
|
"}";
|
||||||
|
ASSERT_EQUALS(expected1, tokenizeAndStringify(code1));
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_TEST(TestTokenizer)
|
REGISTER_TEST(TestTokenizer)
|
||||||
|
|
Loading…
Reference in New Issue