From 5174f7ff5e515cdd4051059fc346320f68ccebad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 14 Jun 2012 21:47:03 +0200 Subject: [PATCH] Fixed #3723 (Preprocessor evaluation order) --- lib/templatesimplifier.cpp | 21 ++++++++++++++++++++- test/testtokenize.cpp | 8 ++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index db7808927..3816925ea 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -748,6 +748,26 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) } if (tok->isNumber()) { + // Remove redundant conditions (0&&x) (1||x) + if (Token::Match(tok->previous(), "[(=,] 0 &&") || + Token::Match(tok->previous(), "[(=,] 1 ||")) { + unsigned int par = 0; + const Token *tok2 = tok; + for (tok2 = tok; tok2; tok2 = tok2->next()) { + if (tok2->str() == "(") + ++par; + else if (tok2->str() == ")") { + if (par == 0) + break; + --par; + } else if (par == 0 && (Token::Match(tok2, "[,;]"))) + break; + } + if (Token::Match(tok2, "[);,]")) + Token::eraseTokens(tok, tok2); + continue; + } + if (tok->str() == "0") { if (Token::Match(tok->previous(), "[+-|] 0")) { tok = tok->previous(); @@ -766,7 +786,6 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) tok->deleteThis(); ret = true; } else if (Token::Match(tok->previous(), "[=[(,] 0 * %any% ,|]|)|;|=|%op%") || - Token::Match(tok->previous(), "[=[(,] 0 && %any% ,|]|)|;|=|%op%") || Token::Match(tok->previous(), "return|case 0 * %any% ,|:|;|=|%op%") || Token::Match(tok->previous(), "return|case 0 && %any% ,|:|;|=|%op%")) { tok->deleteNext(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 20318f75b..50367e51e 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -393,7 +393,7 @@ private: TEST_CASE(simplifyLogicalOperators); - TEST_CASE(simplifyCalculations); // ticket #2870 + TEST_CASE(simplifyCalculations); // foo(p = new char[10]); => p = new char[10]; foo(p); TEST_CASE(simplifyAssignmentInFunctionCall); @@ -6348,7 +6348,11 @@ private: tokenizeAndStringify("int foo ( ) { int i; int j; i = 1 || j; return i; }", true)); ASSERT_EQUALS("int foo ( ) { return 0 ; }", - tokenizeAndStringify("int foo ( ) { int i; int j; i = 0 && j; return i; }", true)); + tokenizeAndStringify("int foo ( ) { int i; int j; i = 0 && j; return i; }", true)); // ticket #3576 - False positives in boolean expressions + + // ticket #3723 - Simplify condition (0 && a < 123) + ASSERT_EQUALS("( 0 )", + tokenizeAndStringify("( 0 && a < 123 )", true)); } void simplifyCompoundAssignment() {