From patchwork Thu Jul 28 00:14:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 262 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:b5d6:b0:2b9:3548:2db5 with SMTP id v22csp716948pxt; Wed, 27 Jul 2022 17:15:17 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t+W5CTXY2JZGi/ZwIoiwjKCNtbt2FxHz4egjLgOp1AqQL+qQJqIdO3AWzei5fRF8hkCOD8 X-Received: by 2002:a17:907:6d8f:b0:72b:6b87:81f1 with SMTP id sb15-20020a1709076d8f00b0072b6b8781f1mr19554829ejc.674.1658967317279; Wed, 27 Jul 2022 17:15:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658967317; cv=none; d=google.com; s=arc-20160816; b=raIsZtNJoeLG3a/gdswgCuPmQXWmtHfuaZ3mZZ50iKOrkQszQkvZ1JA9DeOE3IGxUM ee8pzXdZHrOC05x+bz8tLepPa+I5ykddFKvyHAi/oRd54LSRHCSpSeWuyemV+crKW9lN aKgdkr1lPfBWTu44Vki5KHQaQuwFIuBY6OL8sK0rrHXq9WH1iwohc+uZE0FXeYKnR117 oDfi3ONVi0b2RHfchAvIEqORBcJ6tmtgYH87m9sDHU6RPoG4KDZXfE+CQdeb7kr5t3cC YHmzkhBqM3TJc6dU4OSW+MMySHWsDUX8N01nwJE7Vo+noUy4rRCxC4fcF7DBpKTEiksT vSSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=rD3XjlUdgd+Lf/4Nstfz//zJEKcaF2k4YUW6PhTdWIg=; b=zQpKY39Ryxrcp9zXIH9kUNGsohfszMgKPyA0badNbpYJ4obIf9mRekH6dCui7eEGs+ e7jktJCf7BOxixNXyMVws10/JPaEnmOJhXLP5ZbxlTxS+9ODBwvigVmo3+prG+YVtkmu WIi+YYVJ5o52O1ckbnFw9BotKcGv6zpH+blmIlkjLEFZUlKMzmGXTkh/hX6IEz6cjOtH pM6FoSDJE8xCRtX2AAzqBiWI1Uf5J8DJvASi7vkuCd6h1WgjybiWtvW+o9SSVj4CjeTM Kohzd2S5/D104oFvwJ2AL+2uOY53cY/u9NavUiK3hCHzcAdIjmajDKFd1Cns4ARm41mx /qzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=PAUBLShb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id qk12-20020a1709077f8c00b0072b6e49abc7si18111452ejc.795.2022.07.27.17.15.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Jul 2022 17:15:17 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=PAUBLShb; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E4ED7385AE42 for ; Thu, 28 Jul 2022 00:15:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E4ED7385AE42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1658967315; bh=rD3XjlUdgd+Lf/4Nstfz//zJEKcaF2k4YUW6PhTdWIg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=PAUBLShbdAwqm3KOb/484AYxiy1178ZdGODMnsYgPCH/VZDnzO+IMBP9ASZn/l8WV ln7PVBRAwjMzOqo+QTIWAeBTtMjsHBh5HAwrhy127MjpnWQPwPwTYSWnCq5YXJxrGb 6sOklZQ7CvL9TzJzZShshGknqLGU5tiOtr1CHbFQ= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 75575385829F for ; Thu, 28 Jul 2022 00:14:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 75575385829F Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-538-e_ssxq6NOIGuFyHkWcZuyQ-1; Wed, 27 Jul 2022 20:14:30 -0400 X-MC-Unique: e_ssxq6NOIGuFyHkWcZuyQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E13133C0D84F for ; Thu, 28 Jul 2022 00:14:29 +0000 (UTC) Received: from pdp-11.hsd1.ma.comcast.net (unknown [10.22.32.210]) by smtp.corp.redhat.com (Postfix) with ESMTP id C24E6400DEF8; Thu, 28 Jul 2022 00:14:29 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: Extend -Wpessimizing-move for class prvalues [PR106276] Date: Wed, 27 Jul 2022 20:14:24 -0400 Message-Id: <20220728001424.396849-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1739553313704824355?= X-GMAIL-MSGID: =?utf-8?q?1739553313704824355?= We already have a warning that warns about pessimizing std::move in a return statement, when it prevents the NRVO: T fn() { T t; return std::move (t); // warning \o/ } However, the warning doesn't warn when what we are returning is a class prvalue, that is, when std::move prevents the RVO: T fn() { T t; return std::move (T{}); // no warning :-( } This came up recently in GCC: . This patch fixes that. I would like to extend the warning further, so that it warns in more contexts, e.g.: T t = std::move(T()); or void foo (T); foo (std::move(T())); Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/106276 gcc/cp/ChangeLog: * typeck.cc (can_do_rvo_p): New. (maybe_warn_pessimizing_move): Warn when moving a temporary object in a return statement prevents copy elision. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/Wpessimizing-move7.C: New test. --- gcc/cp/typeck.cc | 31 ++++++++- .../g++.dg/cpp0x/Wpessimizing-move7.C | 63 +++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C base-commit: 219f86495791fd27b012678f13e10cada211daab diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 6e4f23af982..9500c4e2fe8 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10287,12 +10287,29 @@ can_do_nrvo_p (tree retval, tree functype) /* The cv-unqualified type of the returned value must be the same as the cv-unqualified return type of the function. */ - && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))), - (TYPE_MAIN_VARIANT (functype))) + && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)), + TYPE_MAIN_VARIANT (functype)) /* And the returned value must be non-volatile. */ && !TYPE_VOLATILE (TREE_TYPE (retval))); } +/* Like can_do_nrvo_p, but we check if we're trying to move a class + prvalue. */ + +static bool +can_do_rvo_p (tree retval, tree functype) +{ + if (functype == error_mark_node) + return false; + if (retval) + STRIP_ANY_LOCATION_WRAPPER (retval); + return (retval != NULL_TREE + && TREE_CODE (retval) == TARGET_EXPR + && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)), + TYPE_MAIN_VARIANT (functype)) + && !TYPE_VOLATILE (TREE_TYPE (retval))); +} + /* If we should treat RETVAL, an expression being returned, as if it were designated by an rvalue, returns it adjusted accordingly; otherwise, returns NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return @@ -10401,12 +10418,20 @@ maybe_warn_pessimizing_move (tree retval, tree functype) "prevents copy elision")) inform (loc, "remove % call"); } + else if (can_do_rvo_p (arg, functype)) + { + auto_diagnostic_group d; + if (warning_at (loc, OPT_Wpessimizing_move, + "moving a temporary object in a return statement " + "prevents copy elision")) + inform (loc, "remove % call"); + } /* Warn if the move is redundant. It is redundant when we would do maybe-rvalue overload resolution even without std::move. */ else if (warn_redundant_move && (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true))) { - /* Make sure that the overload resolution would actually succeed + /* Make sure that overload resolution would actually succeed if we removed the std::move call. */ tree t = convert_for_initialization (NULL_TREE, functype, moved, diff --git a/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C b/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C new file mode 100644 index 00000000000..cd4eaa09aae --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move7.C @@ -0,0 +1,63 @@ +// PR c++/106276 +// { dg-do compile { target c++11 } } +// { dg-options "-Wpessimizing-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); } +} + +struct A { A(); A(const A&) = delete; A(A&&); }; +struct B { B(A); }; + +static A foo (); + +A +fn1 () +{ + return std::move (A{}); // { dg-warning "moving a temporary object in a return statement prevents copy elision" } + return std::move (A()); // { dg-warning "moving a temporary object in a return statement prevents copy elision" } + return std::move (foo ()); // { dg-warning "moving a temporary object in a return statement prevents copy elision" } +} + +B fn2 () +{ + return std::move (A()); + return std::move (A{}); + return std::move (foo ()); +} + +template +T1 +fn3 () +{ + return std::move (T2{}); // { dg-warning "moving a temporary object in a return statement prevents copy elision" } +} + +void +do_fn3 () +{ + fn3(); + fn3(); +} + +char take_buffer; +struct label_text { + label_text take() { return std::move(label_text(&take_buffer)); } // { dg-warning "moving a temporary object in a return statement prevents copy elision" } + label_text(char *); +};