From 29feaa5a514540a6a969149602403dc56c15a584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 20 Oct 2018 10:51:50 +0200 Subject: [PATCH] Tokenizer: Avoid constant folding --- lib/checkother.cpp | 11 +++++++---- lib/tokenize.cpp | 5 ----- test/testcondition.cpp | 3 +-- test/testother.cpp | 10 +++++----- test/testtokenize.cpp | 9 +++------ 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 664c5dc7b..3727cc4d8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1716,12 +1716,15 @@ void CheckOther::zerodivError(const Token *tok, const ValueFlow::Value *value) void CheckOther::checkNanInArithmeticExpression() { + if (!mSettings->isEnabled(Settings::STYLE)) + return; for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { - if (Token::Match(tok, "inf.0 +|-") || - Token::Match(tok, "+|- inf.0") || - Token::Match(tok, "+|- %num% / 0.0")) { + if (tok->str() != "/") + continue; + if (!Token::Match(tok->astParent(), "[+-]")) + continue; + if (Token::simpleMatch(tok->astOperand2(), "0.0")) nanInArithmeticExpressionError(tok); - } } } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a8c5998f5..760559e76 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3831,11 +3831,6 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) simplifyRedundantParentheses(); if (!isC()) { - // TODO: Only simplify template parameters - for (Token *tok = list.front(); tok; tok = tok->next()) - while (mTemplateSimplifier->simplifyNumericCalculations(tok)) - ; - // Handle templates.. simplifyTemplates(); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 5f4a0065a..280860ed2 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -1193,8 +1193,7 @@ private: " else\n" " return;\n" "}"); - TODO_ASSERT_EQUALS("", "[test.cpp:3]: (warning) Logical conjunction always evaluates to false: neg < -1.0 && neg > -1.0.\n", errout.str()); - + ASSERT_EQUALS("", errout.str()); } void incorrectLogicOperator8() { // opposite expressions diff --git a/test/testother.cpp b/test/testother.cpp index e7fbd8570..b1d17398b 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -641,7 +641,7 @@ private: void nanInArithmeticExpression() { check("void f()\n" "{\n" - " double x = 3.0 / 0.0 + 1.0\n" + " double x = 3.0 / 0.0 + 1.0;\n" " printf(\"%f\", x);\n" "}"); ASSERT_EQUALS( @@ -649,7 +649,7 @@ private: check("void f()\n" "{\n" - " double x = 3.0 / 0.0 - 1.0\n" + " double x = 3.0 / 0.0 - 1.0;\n" " printf(\"%f\", x);\n" "}"); ASSERT_EQUALS( @@ -657,7 +657,7 @@ private: check("void f()\n" "{\n" - " double x = 1.0 + 3.0 / 0.0\n" + " double x = 1.0 + 3.0 / 0.0;\n" " printf(\"%f\", x);\n" "}"); ASSERT_EQUALS( @@ -665,7 +665,7 @@ private: check("void f()\n" "{\n" - " double x = 1.0 - 3.0 / 0.0\n" + " double x = 1.0 - 3.0 / 0.0;\n" " printf(\"%f\", x);\n" "}"); ASSERT_EQUALS( @@ -673,7 +673,7 @@ private: check("void f()\n" "{\n" - " double x = 3.0 / 0.0\n" + " double x = 3.0 / 0.0;\n" " printf(\"%f\", x);\n" "}"); ASSERT_EQUALS("", errout.str()); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 5540084b8..7924863ad 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -3305,7 +3305,7 @@ private: void removeParentheses15() { ASSERT_EQUALS("a = b ? c : 123 ;", tokenizeAndStringify("a = b ? c : (123);", false)); - ASSERT_EQUALS("a = b ? c : ( 579 ) ;", tokenizeAndStringify("a = b ? c : ((123)+(456));", false)); + ASSERT_EQUALS("a = b ? c : ( 123 + 456 ) ;", tokenizeAndStringify("a = b ? c : ((123)+(456));", false)); ASSERT_EQUALS("a = b ? 123 : c ;", tokenizeAndStringify("a = b ? (123) : c;", false)); // #4316 @@ -3402,7 +3402,7 @@ private: "float b ; b = 4.2f ;\n" "double c ; c = 4.2e+10 ;\n" "double d ; d = 4.2e-10 ;\n" - "int e ; e = 6 ;\n" + "int e ; e = 4 + 2 ;\n" "}", tokenizeAndStringify(code)); } @@ -3722,7 +3722,7 @@ private: // Ticket #4450 const char code[] = "static int large_eeprom_type = (13 | (5)), " "default_flash_type = 42;"; - ASSERT_EQUALS("static int large_eeprom_type = 13 ; static int default_flash_type = 42 ;", + ASSERT_EQUALS("static int large_eeprom_type = 13 | 5 ; static int default_flash_type = 42 ;", tokenizeAndStringify(code)); } @@ -6012,9 +6012,6 @@ private: ASSERT_EQUALS("( 0 ) ;", tokenizeAndStringify("( 0 && a[123] );", true)); - // ticket #3964 - simplify numeric calculations in tokenization - ASSERT_EQUALS("char a [ 10 ] ;", tokenizeAndStringify("char a[9+1];")); - // ticket #4931 ASSERT_EQUALS("dostuff ( 1 ) ;", tokenizeAndStringify("dostuff(9&&8);", true)); }