From 5bdd4c5d3fc9c143e8edea3b10828e4b75d7a385 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Sun, 19 Apr 2020 12:12:01 -0400 Subject: [PATCH] c++: Fix bogus -Wparentheses warning with fold-expression [PR94505] We issue bogus -Wparentheses warnings (3 of them!) for this fold expression: ((B && true) || ...) Firstly, issuing a warning for a compiler-generated expression is wrong and secondly, B && true must be wrapped in ( ) otherwise you'll get error: binary expression in operand of fold-expression. PR c++/94505 - bogus -Wparentheses warning with fold-expression. * pt.c (fold_expression): Add warning_sentinel for -Wparentheses before calling build_x_binary_op. * g++.dg/cpp1z/fold11.C: New test. --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/pt.c | 1 + gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/cpp1z/fold11.C | 12 ++++++++++++ 4 files changed, 24 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1z/fold11.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 891ec79fcfd..52f199d90bf 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-04-20 Marek Polacek + + PR c++/94505 - bogus -Wparentheses warning with fold-expression. + * pt.c (fold_expression): Add warning_sentinel for -Wparentheses + before calling build_x_binary_op. + 2020-04-20 Marek Polacek * coroutines.cc (captures_temporary): Don't assign the result of diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 4f4c04e7df8..899df9a330c 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -12379,6 +12379,7 @@ fold_expression (tree t, tree left, tree right, tsubst_flags_t complain) if (FOLD_EXPR_MODIFY_P (t)) return build_x_modify_expr (input_location, left, code, right, complain); + warning_sentinel s(warn_parentheses); switch (code) { case COMPOUND_EXPR: diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index ce31ed0cd44..33f008b9af4 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-04-20 Marek Polacek + + PR c++/94505 - bogus -Wparentheses warning with fold-expression. + * g++.dg/cpp1z/fold11.C: New test. + 2020-04-20 Andreas Krebbel * g++.dg/pr94666.C: New test. diff --git a/gcc/testsuite/g++.dg/cpp1z/fold11.C b/gcc/testsuite/g++.dg/cpp1z/fold11.C new file mode 100644 index 00000000000..09a30555eee --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/fold11.C @@ -0,0 +1,12 @@ +// PR c++/94505 - bogus -Wparentheses warning with fold-expression. +// { dg-do compile { target c++17 } } +// { dg-options "-Wparentheses" } + +template +bool foo () { + return ((B && true) || ...); // { dg-bogus "suggest parentheses" } +} + +int main () { + foo (); +} -- 2.30.2