From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 4CE4E386101C; Tue, 25 May 2021 15:25:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CE4E386101C MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-1043] c++: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 1d3707a52d6b8a3054248b4291719150937db309 X-Git-Newrev: ad52d89808a947264397e920d7483090d4108f7b Message-Id: <20210525152541.4CE4E386101C@sourceware.org> Date: Tue, 25 May 2021 15:25:41 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 May 2021 15:25:41 -0000 https://gcc.gnu.org/g:ad52d89808a947264397e920d7483090d4108f7b commit r12-1043-gad52d89808a947264397e920d7483090d4108f7b Author: Jakub Jelinek Date: Tue May 25 17:24:38 2021 +0200 c++: Avoid -Wunused-value false positives on nullptr passed to ellipsis [PR100666] When passing expressions with decltype(nullptr) type with side-effects to ellipsis, we pass (void *)0 instead, but for the side-effects evaluate them on the lhs of a COMPOUND_EXPR. Unfortunately that means we warn about it if the expression is a call to nodiscard marked function, even when the result is really used, just needs to be transformed. Fixed by adding a warning_sentinel. 2021-05-25 Jakub Jelinek PR c++/100666 * call.c (convert_arg_to_ellipsis): For expressions with NULLPTR_TYPE and side-effects, temporarily disable -Wunused-result warning when building COMPOUND_EXPR. * g++.dg/cpp1z/nodiscard8.C: New test. * g++.dg/cpp1z/nodiscard9.C: New test. Diff: --- gcc/cp/call.c | 5 ++++- gcc/testsuite/g++.dg/cpp1z/nodiscard8.C | 15 +++++++++++++++ gcc/testsuite/g++.dg/cpp1z/nodiscard9.C | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/gcc/cp/call.c b/gcc/cp/call.c index cfccf273632..3076fe67d5c 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -8178,7 +8178,10 @@ convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain) { arg = mark_rvalue_use (arg); if (TREE_SIDE_EFFECTS (arg)) - arg = cp_build_compound_expr (arg, null_pointer_node, complain); + { + warning_sentinel w(warn_unused_result); + arg = cp_build_compound_expr (arg, null_pointer_node, complain); + } else arg = null_pointer_node; } diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard8.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard8.C new file mode 100644 index 00000000000..b5096acbea1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/nodiscard8.C @@ -0,0 +1,15 @@ +// PR c++/100666 +// { dg-do compile { target c++11 } } + +[[nodiscard]] decltype(nullptr) bar (); +extern void foo (...); +template void qux (T); + +void +baz () +{ + foo (bar ()); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + bar (); // { dg-warning "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + auto x = bar (); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + qux (bar ()); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } +} diff --git a/gcc/testsuite/g++.dg/cpp1z/nodiscard9.C b/gcc/testsuite/g++.dg/cpp1z/nodiscard9.C new file mode 100644 index 00000000000..1315ccdbf7c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/nodiscard9.C @@ -0,0 +1,22 @@ +// PR c++/100666 +// { dg-do compile { target c++11 } } + +struct S {}; +[[nodiscard]] S bar (); +struct U { S s; }; +[[nodiscard]] U corge (); +extern void foo (...); +template void qux (T); + +void +baz () +{ + foo (bar ()); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + bar (); // { dg-warning "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + auto x = bar (); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + qux (bar ()); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + foo (corge ()); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + corge (); // { dg-warning "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + auto y = corge (); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } + qux (corge ()); // { dg-bogus "ignoring return value of '\[^\n\r]*', declared with attribute 'nodiscard'" } +}