From 5fabe66ff7ccabd785798b0c8d882843cbb242a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 Nov 2015 15:00:41 +0100 Subject: [PATCH] simplifyCalculations: better handling of && and || in conditions (#4931) --- lib/templatesimplifier.cpp | 5 +++-- test/testsimplifytokens.cpp | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 447608281..9fb054663 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -1055,6 +1055,7 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) Token::Match(tok->previous(), "[(=,] 1 %oror%")) { unsigned int par = 0; const Token *tok2 = tok; + bool andAnd = (tok->next()->str() == "&&"); for (; tok2; tok2 = tok2->next()) { if (tok2->str() == "(") ++par; @@ -1062,10 +1063,10 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens) if (par == 0) break; --par; - } else if (par == 0 && (Token::Match(tok2, "[,;?]"))) + } else if (par == 0 && isLowerThanLogicalAnd(tok2) && (andAnd || tok2->str() != "||")) break; } - if (Token::Match(tok2, "[);,?]")) { + if (tok2) { Token::eraseTokens(tok, tok2); ret = true; } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 7498649a3..78769c637 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -2099,6 +2099,7 @@ private: ASSERT_EQUALS("x ( -2 << 6 | 1 )", tok("x(1-3<<6|5/3)")); // #4931 ASSERT_EQUALS("x ( 2 )", tok("x(2|0*0&2>>1+0%2*1)")); // #4931 ASSERT_EQUALS("x ( 0 & 4 != 1 )", tok("x(4%1<<1&4!=1)")); // #4931 (can be simplified further but it's not a problem) + ASSERT_EQUALS("x ( true )", tok("x(0&&4>0==2||4)")); // #4931 // don't remove these spaces.. ASSERT_EQUALS("new ( auto ) ( 4 ) ;", tok("new (auto)(4);"));