Skip to content

Commit a658baf

Browse files
Fix #11143 FP: unreadVariable (remove simplifyMathExpressions()) (#4227)
* Fix #11143 FP: unreadVariable (remove simplifyMathExpressions()) * Remove unused function, tests
1 parent 0d4b439 commit a658baf

4 files changed

Lines changed: 9 additions & 134 deletions

File tree

lib/tokenize.cpp

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -4825,9 +4825,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
48254825
// Combine tokens..
48264826
combineOperators();
48274827

4828-
// replace 'sin(0)' to '0' and other similar math expressions
4829-
simplifyMathExpressions();
4830-
48314828
// combine "- %num%"
48324829
concatenateNegativeNumberAndAnyPositive();
48334830

@@ -8518,18 +8515,6 @@ bool Tokenizer::isOneNumber(const std::string &s)
85188515
return isNumberOneOf(s, 1L, "1.0");
85198516
}
85208517

8521-
// ------------------------------------------------------------------------
8522-
// Helper function to check whether number is two (2 or 0.2E+1 or 2E+0) or not?
8523-
// @param s the string to check
8524-
// @return true in case s is two and false otherwise.
8525-
// ------------------------------------------------------------------------
8526-
bool Tokenizer::isTwoNumber(const std::string &s)
8527-
{
8528-
if (!MathLib::isPositive(s))
8529-
return false;
8530-
return isNumberOneOf(s, 2L, "2.0");
8531-
}
8532-
85338518
void Tokenizer::simplifyComma()
85348519
{
85358520
bool inReturn = false;
@@ -10912,96 +10897,6 @@ void Tokenizer::printUnknownTypes() const
1091210897
}
1091310898
}
1091410899

10915-
void Tokenizer::simplifyMathExpressions()
10916-
{
10917-
for (Token *tok = list.front(); tok; tok = tok->next()) {
10918-
10919-
//simplify Pythagorean trigonometric identity: pow(sin(x),2)+pow(cos(x),2) = 1
10920-
// pow(cos(x),2)+pow(sin(x),2) = 1
10921-
// @todo: sin(x) * sin(x) + cos(x) * cos(x) = 1
10922-
// cos(x) * cos(x) + sin(x) * sin(x) = 1
10923-
//simplify Hyperbolic identity: pow(sinh(x),2)-pow(cosh(x),2) = -1
10924-
// pow(cosh(x),2)-pow(sinh(x),2) = -1
10925-
// @todo: sinh(x) * sinh(x) - cosh(x) * cosh(x) = -1
10926-
// cosh(x) * cosh(x) - sinh(x) * sinh(x) = -1
10927-
if (Token::Match(tok, "pow|powf|powl (")) {
10928-
if (Token::Match(tok->tokAt(2), "sin|sinf|sinl (")) {
10929-
Token * const tok2 = tok->linkAt(3);
10930-
if (!Token::Match(tok2, ") , %num% ) + pow|powf|powl ( cos|cosf|cosl ("))
10931-
continue;
10932-
const std::string& leftExponent = tok2->strAt(2);
10933-
if (!isTwoNumber(leftExponent))
10934-
continue; // left exponent is not 2
10935-
const Token * const tok3 = tok2->tokAt(8);
10936-
Token * const tok4 = tok3->link();
10937-
if (!Token::Match(tok4, ") , %num% )"))
10938-
continue;
10939-
const std::string& rightExponent = tok4->strAt(2);
10940-
if (!isTwoNumber(rightExponent))
10941-
continue; // right exponent is not 2
10942-
if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok4->next())) {
10943-
Token::eraseTokens(tok, tok4->tokAt(4));
10944-
tok->str("1");
10945-
}
10946-
} else if (Token::Match(tok->tokAt(2), "cos|cosf|cosl (")) {
10947-
Token * const tok2 = tok->linkAt(3);
10948-
if (!Token::Match(tok2, ") , %num% ) + pow|powf|powl ( sin|sinf|sinl ("))
10949-
continue;
10950-
const std::string& leftExponent = tok2->strAt(2);
10951-
if (!isTwoNumber(leftExponent))
10952-
continue; // left exponent is not 2
10953-
const Token * const tok3 = tok2->tokAt(8);
10954-
Token * const tok4 = tok3->link();
10955-
if (!Token::Match(tok4, ") , %num% )"))
10956-
continue;
10957-
const std::string& rightExponent = tok4->strAt(2);
10958-
if (!isTwoNumber(rightExponent))
10959-
continue; // right exponent is not 2
10960-
if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok4->next())) {
10961-
Token::eraseTokens(tok, tok4->tokAt(4));
10962-
tok->str("1");
10963-
}
10964-
} else if (Token::Match(tok->tokAt(2), "sinh|sinhf|sinhl (")) {
10965-
Token * const tok2 = tok->linkAt(3);
10966-
if (!Token::Match(tok2, ") , %num% ) - pow|powf|powl ( cosh|coshf|coshl ("))
10967-
continue;
10968-
const std::string& leftExponent = tok2->strAt(2);
10969-
if (!isTwoNumber(leftExponent))
10970-
continue; // left exponent is not 2
10971-
const Token * const tok3 = tok2->tokAt(8);
10972-
Token * const tok4 = tok3->link();
10973-
if (!Token::Match(tok4, ") , %num% )"))
10974-
continue;
10975-
const std::string& rightExponent = tok4->strAt(2);
10976-
if (!isTwoNumber(rightExponent))
10977-
continue; // right exponent is not 2
10978-
if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok4->next())) {
10979-
Token::eraseTokens(tok, tok4->tokAt(4));
10980-
tok->str("-1");
10981-
}
10982-
} else if (Token::Match(tok->tokAt(2), "cosh|coshf|coshl (")) {
10983-
Token * const tok2 = tok->linkAt(3);
10984-
if (!Token::Match(tok2, ") , %num% ) - pow|powf|powl ( sinh|sinhf|sinhl ("))
10985-
continue;
10986-
const std::string& leftExponent = tok2->strAt(2);
10987-
if (!isTwoNumber(leftExponent))
10988-
continue; // left exponent is not 2
10989-
const Token * const tok3 = tok2->tokAt(8);
10990-
Token * const tok4 = tok3->link();
10991-
if (!Token::Match(tok4, ") , %num% )"))
10992-
continue;
10993-
const std::string& rightExponent = tok4->strAt(2);
10994-
if (!isTwoNumber(rightExponent))
10995-
continue; // right exponent is not 2
10996-
if (tok->tokAt(3)->stringifyList(tok2->next()) == tok3->stringifyList(tok4->next())) {
10997-
Token::eraseTokens(tok, tok4->tokAt(4));
10998-
tok->str("-1");
10999-
}
11000-
}
11001-
}
11002-
}
11003-
}
11004-
1100510900
void Tokenizer::prepareTernaryOpForAST()
1100610901
{
1100710902
// http://en.cppreference.com/w/cpp/language/operator_precedence says about ternary operator:

lib/tokenize.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,6 @@ class CPPCHECKLIB Tokenizer {
431431

432432
void findComplicatedSyntaxErrorsInTemplates();
433433

434-
/**
435-
* Simplify e.g. 'sin(0)' into '0'
436-
*/
437-
void simplifyMathExpressions();
438-
439434
/**
440435
* Modify strings in the token list by replacing hex and oct
441436
* values. E.g. "\x61" -> "a" and "\000" -> "\0"
@@ -757,13 +752,6 @@ class CPPCHECKLIB Tokenizer {
757752
*/
758753
static bool isOneNumber(const std::string &s);
759754

760-
/**
761-
* Helper function to check whether number is two (2 or 0.2E+1 or 2E+0) or not?
762-
* @param s the string to check
763-
* @return true in case is is two and false otherwise.
764-
*/
765-
static bool isTwoNumber(const std::string &s);
766-
767755
/**
768756
* Helper function to check for start of function execution scope.
769757
* Do not use this in checks. Use the symbol database.

test/testtokenize.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ class TestTokenizer : public TestFixture {
146146
TEST_CASE(simplifyKeyword); // #5842 - remove C99 static keyword between []
147147

148148
TEST_CASE(isOneNumber);
149-
TEST_CASE(isTwoNumber);
150149

151150
TEST_CASE(simplifyFunctionParameters);
152151
TEST_CASE(simplifyFunctionParameters1); // #3721
@@ -5643,22 +5642,6 @@ class TestTokenizer : public TestFixture {
56435642
ASSERT_EQUALS(false, Tokenizer::isOneNumber("garbage"));
56445643
}
56455644

5646-
void isTwoNumber() const {
5647-
ASSERT_EQUALS(true, Tokenizer::isTwoNumber("2.0"));
5648-
ASSERT_EQUALS(true, Tokenizer::isTwoNumber("+2.0"));
5649-
ASSERT_EQUALS(true, Tokenizer::isTwoNumber("2.0e+0"));
5650-
ASSERT_EQUALS(true, Tokenizer::isTwoNumber("+2L"));
5651-
ASSERT_EQUALS(true, Tokenizer::isTwoNumber("+2"));
5652-
ASSERT_EQUALS(true, Tokenizer::isTwoNumber("2"));
5653-
ASSERT_EQUALS(true, Tokenizer::isTwoNumber("+2E+0"));
5654-
5655-
ASSERT_EQUALS(false, Tokenizer::isTwoNumber("0.0"));
5656-
ASSERT_EQUALS(false, Tokenizer::isTwoNumber("+0.0"));
5657-
ASSERT_EQUALS(false, Tokenizer::isTwoNumber("-0"));
5658-
ASSERT_EQUALS(false, Tokenizer::isTwoNumber(""));
5659-
ASSERT_EQUALS(false, Tokenizer::isTwoNumber("garbage"));
5660-
}
5661-
56625645
void simplifyStaticConst() {
56635646
const char code1[] = "class foo { public: bool const static c ; }";
56645647
const char expected1[] = "class foo { public: static const bool c ; }";

test/testunusedvar.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class TestUnusedVar : public TestFixture {
134134
TEST_CASE(localvar63); // #6928
135135
TEST_CASE(localvar64); // #9997
136136
TEST_CASE(localvar65); // #9876, #10006
137+
TEST_CASE(localvar66); // #11143
137138
TEST_CASE(localvarloops); // loops
138139
TEST_CASE(localvaralias1);
139140
TEST_CASE(localvaralias2); // ticket #1637
@@ -3560,6 +3561,14 @@ class TestUnusedVar : public TestFixture {
35603561
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
35613562
}
35623563

3564+
void localvar66() { // #11143
3565+
functionVariableUsage("void f() {\n"
3566+
" double phi = 42.0;\n"
3567+
" std::cout << pow(sin(phi), 2) + pow(cos(phi), 2) << std::endl;\n"
3568+
"}\n");
3569+
ASSERT_EQUALS("", errout.str());
3570+
}
3571+
35633572
void localvarloops() {
35643573
// loops
35653574
functionVariableUsage("void fun(int c) {\n"

0 commit comments

Comments
 (0)