From patchwork Fri Oct 28 20:42:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 59602 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB6813858D38 for ; Fri, 28 Oct 2022 20:43:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB6813858D38 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666989807; bh=RFAuyAZwt0l/o1mLdQzZ00QK5TPX0Ucx+weR2JYCEuU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=hz5q5UGN1ygzt/1dZ6Vq9nEfyCCUxA9fuVz35VCSFWfyKQj8jO2/4cTYQxi2UHVhi p6wGHIvCBKgLwondH4GBn92qoC2ja6t4ldiHGUm74/jwVYTRdzN2FC7XoFxmWdmmZL KaoH7BCNCSwGZstnaRYkY40AbMZqjmEIsW/tCyoY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 95F643858D28 for ; Fri, 28 Oct 2022 20:42:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 95F643858D28 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-647-PzOLGybqOEui-xu7wBeBhw-1; Fri, 28 Oct 2022 16:42:39 -0400 X-MC-Unique: PzOLGybqOEui-xu7wBeBhw-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3AB55101A528 for ; Fri, 28 Oct 2022 20:42:39 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.17.162]) by smtp.corp.redhat.com (Postfix) with ESMTP id 14A6E492B06; Fri, 28 Oct 2022 20:42:39 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Tweaks for -Wredundant-move [PR107363] Date: Fri, 28 Oct 2022 16:42:33 -0400 Message-Id: <20221028204233.409310-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Two things here: 1) when we're pointing out that std::move on a constant object is redundant, don't say "in return statement" when we aren't in a return statement; 2) suppress the warning when the std::move call was dependent, because removing the std::move may not be correct for a different instantiation of the original template. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/107363 gcc/cp/ChangeLog: * semantics.cc (finish_call_expr): Suppress OPT_Wpessimizing_move. * typeck.cc (maybe_warn_pessimizing_move): Check warn_redundant_move and warning_suppressed_p. Adjust a message depending on return_p. (check_return_expr): Don't suppress OPT_Wpessimizing_move here. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/Wredundant-move13.C: New test. --- gcc/cp/semantics.cc | 4 ++ gcc/cp/typeck.cc | 16 ++--- .../g++.dg/cpp0x/Wredundant-move13.C | 61 +++++++++++++++++++ 3 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wredundant-move13.C base-commit: e583c86f49b9ef6991b25308a0ad60de9697f24a diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 36aa9c4499f..caaa40fde19 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -2738,6 +2738,10 @@ finish_call_expr (tree fn, vec **args, bool disallow_virtual, result = build_min_nt_call_vec (orig_fn, *args); SET_EXPR_LOCATION (result, cp_expr_loc_or_input_loc (fn)); KOENIG_LOOKUP_P (result) = koenig_p; + /* Disable the std::move warnings since this call was dependent + (c++/89780, c++/107363). This also suppresses the + -Wredundant-move warning. */ + suppress_warning (result, OPT_Wpessimizing_move); if (is_overloaded_fn (fn)) fn = get_fns (fn); diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 2e0fd8fbf17..5f5fb2a212b 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10885,7 +10885,9 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) and where the std::move does nothing if T does not have a T(const T&&) constructor, because the argument is const. It will not use T(T&&) because that would mean losing the const. */ - else if (TYPE_REF_P (TREE_TYPE (arg)) + else if (warn_redundant_move + && !warning_suppressed_p (expr, OPT_Wredundant_move) + && TYPE_REF_P (TREE_TYPE (arg)) && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg)))) { tree rtype = TREE_TYPE (TREE_TYPE (arg)); @@ -10901,8 +10903,11 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) return; } auto_diagnostic_group d; - if (warning_at (loc, OPT_Wredundant_move, - "redundant move in return statement")) + if (return_p + ? warning_at (loc, OPT_Wredundant_move, + "redundant move in return statement") + : warning_at (loc, OPT_Wredundant_move, + "redundant move in initialization")) inform (loc, "remove % call"); } } @@ -11126,11 +11131,6 @@ check_return_expr (tree retval, bool *no_warning) /* We don't know if this is an lvalue or rvalue use, but either way we can mark it as read. */ mark_exp_read (retval); - /* Disable our std::move warnings when we're returning - a dependent expression (c++/89780). */ - if (retval && TREE_CODE (retval) == CALL_EXPR) - /* This also suppresses -Wredundant-move. */ - suppress_warning (retval, OPT_Wpessimizing_move); return retval; } diff --git a/gcc/testsuite/g++.dg/cpp0x/Wredundant-move13.C b/gcc/testsuite/g++.dg/cpp0x/Wredundant-move13.C new file mode 100644 index 00000000000..80e7d80cd02 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/Wredundant-move13.C @@ -0,0 +1,61 @@ +// PR c++/107363 +// { dg-do compile { target c++11 } } +// { dg-options "-Wredundant-move" } + +// Define std::move. +namespace std { + template + struct remove_reference + { typedef _Tp type; }; + + template + struct remove_reference<_Tp&> + { typedef _Tp type; }; + + template + struct remove_reference<_Tp&&> + { typedef _Tp type; }; + + template + constexpr typename std::remove_reference<_Tp>::type&& + move(_Tp&& __t) noexcept + { return static_cast::type&&>(__t); } +} + +template +struct Optional { + U &value(); + T release_value() { + T t = std::move (value ()); + return t; + } +}; + +struct Foo {}; +void test(Optional o) { o.release_value(); } + +struct F { + F(const F&); + F(F&&) = delete; +}; + +struct Z { + Z(const Z&) = delete; + Z(Z&&) = delete; + Z(const Z&&); +}; + +const F& constfref(); +const Z& constzref(); + +void +g () +{ + // Will call F::F(const F&) w/ and w/o std::move. So it's redundant. + F f = std::move (constfref()); // { dg-warning "redundant move in initialization" } + (void) f; + // Will call Z::Z(const Z&&) w/ std::move, and Z::Z(const Z&) w/o. + // So it's not redundant. + Z z = std::move (constzref()); + (void) z; +}