diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index aa67e97bf..da6acbc47 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4825,9 +4825,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) // Combine tokens.. combineOperators(); - // replace 'sin(0)' to '0' and other similar math expressions - simplifyMathExpressions(); - // combine "- %num%" concatenateNegativeNumberAndAnyPositive(); @@ -8518,18 +8515,6 @@ bool Tokenizer::isOneNumber(const std::string &s) return isNumberOneOf(s, 1L, "1.0"); } -// ------------------------------------------------------------------------ -// Helper function to check whether number is two (2 or 0.2E+1 or 2E+0) or not? -// @param s the string to check -// @return true in case s is two and false otherwise. -// ------------------------------------------------------------------------ -bool Tokenizer::isTwoNumber(const std::string &s) -{ - if (!MathLib::isPositive(s)) - return false; - return isNumberOneOf(s, 2L, "2.0"); -} - void Tokenizer::simplifyComma() { bool inReturn = false; @@ -10912,96 +10897,6 @@ void Tokenizer::printUnknownTypes() const } } -void Tokenizer::simplifyMathExpressions() -{ - for (Token *tok = list.front(); tok; tok = tok->next()) { - - //simplify Pythagorean trigonometric identity: pow(sin(x),2)+pow(cos(x),2) = 1 - // pow(cos(x),2)+pow(sin(x),2) = 1 - // @todo: sin(x) * sin(x) + cos(x) * cos(x) = 1 - // cos(x) * cos(x) + sin(x) * sin(x) = 1 - //simplify Hyperbolic identity: pow(sinh(x),2)-pow(cosh(x),2) = -1 - // 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::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|powf|powl ( cos|cosf|cosl (")) - continue; - const std::string& leftExponent = tok2->strAt(2); - if (!isTwoNumber(leftExponent)) - continue; // left exponent is not 2 - const Token * const tok3 = tok2->tokAt(8); - Token * const tok4 = tok3->link(); - if (!Token::Match(tok4, ") , %num% )")) - continue; - const std::string& rightExponent = tok4->strAt(2); - if (!isTwoNumber(rightExponent)) - continue; // right exponent is not 2 - if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok4->next())) { - Token::eraseTokens(tok, tok4->tokAt(4)); - tok->str("1"); - } - } else if (Token::Match(tok->tokAt(2), "cos|cosf|cosl (")) { - Token * const tok2 = tok->linkAt(3); - if (!Token::Match(tok2, ") , %num% ) + pow|powf|powl ( sin|sinf|sinl (")) - continue; - const std::string& leftExponent = tok2->strAt(2); - if (!isTwoNumber(leftExponent)) - continue; // left exponent is not 2 - const Token * const tok3 = tok2->tokAt(8); - Token * const tok4 = tok3->link(); - if (!Token::Match(tok4, ") , %num% )")) - continue; - const std::string& rightExponent = tok4->strAt(2); - if (!isTwoNumber(rightExponent)) - continue; // right exponent is not 2 - if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok4->next())) { - Token::eraseTokens(tok, tok4->tokAt(4)); - tok->str("1"); - } - } else if (Token::Match(tok->tokAt(2), "sinh|sinhf|sinhl (")) { - Token * const tok2 = tok->linkAt(3); - if (!Token::Match(tok2, ") , %num% ) - pow|powf|powl ( cosh|coshf|coshl (")) - continue; - const std::string& leftExponent = tok2->strAt(2); - if (!isTwoNumber(leftExponent)) - continue; // left exponent is not 2 - const Token * const tok3 = tok2->tokAt(8); - Token * const tok4 = tok3->link(); - if (!Token::Match(tok4, ") , %num% )")) - continue; - const std::string& rightExponent = tok4->strAt(2); - if (!isTwoNumber(rightExponent)) - continue; // right exponent is not 2 - if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok4->next())) { - Token::eraseTokens(tok, tok4->tokAt(4)); - tok->str("-1"); - } - } else if (Token::Match(tok->tokAt(2), "cosh|coshf|coshl (")) { - Token * const tok2 = tok->linkAt(3); - if (!Token::Match(tok2, ") , %num% ) - pow|powf|powl ( sinh|sinhf|sinhl (")) - continue; - const std::string& leftExponent = tok2->strAt(2); - if (!isTwoNumber(leftExponent)) - continue; // left exponent is not 2 - const Token * const tok3 = tok2->tokAt(8); - Token * const tok4 = tok3->link(); - if (!Token::Match(tok4, ") , %num% )")) - continue; - const std::string& rightExponent = tok4->strAt(2); - if (!isTwoNumber(rightExponent)) - continue; // right exponent is not 2 - if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok4->next())) { - Token::eraseTokens(tok, tok4->tokAt(4)); - tok->str("-1"); - } - } - } - } -} - void Tokenizer::prepareTernaryOpForAST() { // http://en.cppreference.com/w/cpp/language/operator_precedence says about ternary operator: diff --git a/lib/tokenize.h b/lib/tokenize.h index eb71d4036..8bcc07b30 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -431,11 +431,6 @@ public: void findComplicatedSyntaxErrorsInTemplates(); - /** - * Simplify e.g. 'sin(0)' into '0' - */ - void simplifyMathExpressions(); - /** * Modify strings in the token list by replacing hex and oct * values. E.g. "\x61" -> "a" and "\000" -> "\0" @@ -757,13 +752,6 @@ public: */ static bool isOneNumber(const std::string &s); - /** - * Helper function to check whether number is two (2 or 0.2E+1 or 2E+0) or not? - * @param s the string to check - * @return true in case is is two and false otherwise. - */ - static bool isTwoNumber(const std::string &s); - /** * Helper function to check for start of function execution scope. * Do not use this in checks. Use the symbol database. diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 4c2f667f0..66801f73d 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -146,7 +146,6 @@ private: TEST_CASE(simplifyKeyword); // #5842 - remove C99 static keyword between [] TEST_CASE(isOneNumber); - TEST_CASE(isTwoNumber); TEST_CASE(simplifyFunctionParameters); TEST_CASE(simplifyFunctionParameters1); // #3721 @@ -5643,22 +5642,6 @@ private: ASSERT_EQUALS(false, Tokenizer::isOneNumber("garbage")); } - void isTwoNumber() const { - ASSERT_EQUALS(true, Tokenizer::isTwoNumber("2.0")); - ASSERT_EQUALS(true, Tokenizer::isTwoNumber("+2.0")); - ASSERT_EQUALS(true, Tokenizer::isTwoNumber("2.0e+0")); - ASSERT_EQUALS(true, Tokenizer::isTwoNumber("+2L")); - ASSERT_EQUALS(true, Tokenizer::isTwoNumber("+2")); - ASSERT_EQUALS(true, Tokenizer::isTwoNumber("2")); - ASSERT_EQUALS(true, Tokenizer::isTwoNumber("+2E+0")); - - ASSERT_EQUALS(false, Tokenizer::isTwoNumber("0.0")); - ASSERT_EQUALS(false, Tokenizer::isTwoNumber("+0.0")); - ASSERT_EQUALS(false, Tokenizer::isTwoNumber("-0")); - ASSERT_EQUALS(false, Tokenizer::isTwoNumber("")); - ASSERT_EQUALS(false, Tokenizer::isTwoNumber("garbage")); - } - void simplifyStaticConst() { const char code1[] = "class foo { public: bool const static c ; }"; const char expected1[] = "class foo { public: static const bool c ; }"; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 595762973..8ff2097f1 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -134,6 +134,7 @@ private: TEST_CASE(localvar63); // #6928 TEST_CASE(localvar64); // #9997 TEST_CASE(localvar65); // #9876, #10006 + TEST_CASE(localvar66); // #11143 TEST_CASE(localvarloops); // loops TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 @@ -3560,6 +3561,14 @@ private: ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str()); } + void localvar66() { // #11143 + functionVariableUsage("void f() {\n" + " double phi = 42.0;\n" + " std::cout << pow(sin(phi), 2) + pow(cos(phi), 2) << std::endl;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvarloops() { // loops functionVariableUsage("void fun(int c) {\n"