From 8f432880fa9918cb0fb646bc4555fa3c66c5bed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 8 Nov 2023 18:54:40 +0100 Subject: [PATCH] Fix #12164 (False positive: operator precedence warning even though there are parentheses) (#5640) --- lib/tokenize.cpp | 12 ------------ test/testtokenize.cpp | 9 ++++++++- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d6571b68d..cbf316587 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7835,18 +7835,6 @@ bool Tokenizer::simplifyRedundantParentheses() ret = true; } - if (Token::simpleMatch(tok->previous(), "? (") && Token::simpleMatch(tok->link(), ") :")) { - const Token *tok2 = tok->next(); - while (tok2 && (Token::Match(tok2,"%bool%|%num%|%name%") || tok2->isArithmeticalOp())) - tok2 = tok2->next(); - if (tok2 && tok2->str() == ")") { - tok->link()->deleteThis(); - tok->deleteThis(); - ret = true; - continue; - } - } - while (Token::Match(tok->previous(), "[{([,] ( !!{") && Token::Match(tok->link(), ") [;,])]") && !Token::simpleMatch(tok->tokAt(-2), "operator ,") && // Ticket #5709 diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 0b753f571..791e56c3b 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -171,6 +171,7 @@ private: TEST_CASE(removeParentheses25); // daca@home - a=(b,c) TEST_CASE(removeParentheses26); // Ticket #8875 a[0](0) TEST_CASE(removeParentheses27); + TEST_CASE(removeParentheses28); // #12164 - don't remove parentheses in '(expr1) ? (expr2) : (expr3);' TEST_CASE(tokenize_double); TEST_CASE(tokenize_strings); @@ -1809,7 +1810,7 @@ private: void removeParentheses15() { ASSERT_EQUALS("a = b ? c : 123 ;", tokenizeAndStringify("a = b ? c : (123);")); ASSERT_EQUALS("a = b ? c : ( 123 + 456 ) ;", tokenizeAndStringify("a = b ? c : ((123)+(456));")); - ASSERT_EQUALS("a = b ? 123 : c ;", tokenizeAndStringify("a = b ? (123) : c;")); + ASSERT_EQUALS("a = b ? ( 123 ) : c ;", tokenizeAndStringify("a = b ? (123) : c;")); // #4316 ASSERT_EQUALS("a = b ? c : ( d = 1 , 0 ) ;", tokenizeAndStringify("a = b ? c : (d=1,0);")); @@ -1918,6 +1919,12 @@ private: ASSERT_EQUALS(exp, tokenizeAndStringify(code)); } + void removeParentheses28() { // Ticket #12164 + static char code[] = "temp1 = (value > 100U) ? (value+100U) : (value-50U);"; + static const char exp[] = "temp1 = ( value > 100U ) ? ( value + 100U ) : ( value - 50U ) ;"; + ASSERT_EQUALS(exp, tokenizeAndStringify(code)); + } + void tokenize_double() { const char code[] = "void f() {\n" " double a = 4.2;\n"