From patchwork Thu Aug 18 20:19:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 607 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:38f:b0:2d5:3c95:9e21 with SMTP id 15csp504054pxh; Thu, 18 Aug 2022 13:20:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR6ilyfeO9ZI7esMz8aUD6k3Yq5jb4YXdc44uEf2TNdY9P7cz9a+uQ23akO6/cUiZBLm5QVT X-Received: by 2002:a17:907:7fa0:b0:73c:8c93:9357 with SMTP id qk32-20020a1709077fa000b0073c8c939357mr916287ejc.482.1660854029477; Thu, 18 Aug 2022 13:20:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660854029; cv=none; d=google.com; s=arc-20160816; b=x7cwmAbfapwwnVmE3nms2zsy238SlBd9NGsgdhmeeGyGXhTtsuGd/46o1nQ/1vSnSM zKBDhMbhXw6d79PSC+Cw9Oknu3rjbs1WXDeti6tCb6sx3eNjP4sagNN/DeaEMY3AD2Qv 7dpC5exMywbSG9aK//chVZmPJVCUMWacPCn4DH6QwpxPx6TX3o8y0KJPZh0kDUbuUiKk v+l7Al+qECoaqIWybVFkL6b5ZLvfMtpV8vZNl8dRtRHgDpObXfRwPJiVl1+5t9F314Ox AOoi/4n4kJOfn6g8vU56ppRols41RbwnUtMv/CFHUIt5/Qq6yvSCNe56aNjjtNumo1JP uPZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:user-agent:in-reply-to:mime-version:references :message-id:subject:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=MiSq4N5/AIXPGZZuV/9qfXfzpE6L9pWEI4JWZCLWMzo=; b=dMBi03vvUahceAr5YugOvf9t4vV2uiGONkfQUXih2aglflterl4aTEd9Qh6N2HqT4Y 8omfuTUrjl6NzfA/Br53HIMg3Wr6+Jjdjdb90wFqTDafpBOyjFcsQWqhM3mcnXXK+B3v 4udRwg6DtCBpT3oB2gjXSxqiKs6n2zDB4fS2YJAomL00HK7JKBl+oQPc/O9u6/Y5jPNm Flxa5vEn7478zYeSFsf7s5lbfJIrVXetORFXGg26r8qEahOEvSi+CAS9mDR8Ymnb2PO5 URSSfvoEc8CwDQpr9Cyj+GSKDyEvpfAPqHykCgXHUk8p+ROH3w9hgU5vefHVRqEEX3wI FC+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=MCi59muW; 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 nb29-20020a1709071c9d00b007317274bb0dsi1749556ejc.979.2022.08.18.13.20.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 13:20:29 -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=MCi59muW; 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 38E3A3858437 for ; Thu, 18 Aug 2022 20:20:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38E3A3858437 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660854028; bh=MiSq4N5/AIXPGZZuV/9qfXfzpE6L9pWEI4JWZCLWMzo=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=MCi59muW59tmTuElnQvDo4XmghZEvcUK8uKqF/7AMJkogdXbqVVokf2eKC5uLFEpW e51Mxc0hpMACNc9sdOfkjsINWqQnqK6AsKbJPJ6c8Yt4DA8mBHX6PSndL2sovmjE+2 vOqx+pjYqvickVTAvUNFXw0uCLabZPuqxVydLUTI= 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 793E83858CDA for ; Thu, 18 Aug 2022 20:19:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 793E83858CDA Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-298-G2Pdt7REPyOuFSIbyYMmHw-1; Thu, 18 Aug 2022 16:19:42 -0400 X-MC-Unique: G2Pdt7REPyOuFSIbyYMmHw-1 Received: by mail-qt1-f200.google.com with SMTP id i19-20020ac85e53000000b00342f05b902cso1941938qtx.7 for ; Thu, 18 Aug 2022 13:19:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=MiSq4N5/AIXPGZZuV/9qfXfzpE6L9pWEI4JWZCLWMzo=; b=eezgbon2vtCrvM2AkCG8o1wMFUZ2jK09/+7iGmpFtRlwaCePBT6nV9sd4eVPa/o6VI iY7NptGoRKDKyVGqwnVvOxLro/TE0phy3E9//BtqONuPjL/PCF3jT2BvKimof9XYeIC+ YqhYHqlZACPvJDfMqxZypj6/vSxm+bBEfZVqrS8GeuwEBtqh596L5wK9lHSaw+mui/p3 J941fWzq8b7MeBj5Kauk2N8QUndcpTPvJf2NvsAcVQ6fp2i0sPdHR6pSgT3hI8QZ2ak+ jTnO4drs3BGiv8HjZlFGeRBJ7S25jFnl2kqCDg709ArbuW8twoJTW6VXszXI9gBJHMoT mxFQ== X-Gm-Message-State: ACgBeo0lLpj6TRDn4szrVBq2AfytKwJXFeef+MxGAYp/97jN82VNSuJe aiEy4/hRX98+vrTX2eKYcbm7Z65+1C/POse7dwwepc6/yI+QZaEfD9AQttCTNz1n+8UjM6T/O71 JzMkSnCt1hN1/R8/MgQ== X-Received: by 2002:a05:620a:1788:b0:6bb:75c3:ba06 with SMTP id ay8-20020a05620a178800b006bb75c3ba06mr3156691qkb.686.1660853981226; Thu, 18 Aug 2022 13:19:41 -0700 (PDT) X-Received: by 2002:a05:620a:1788:b0:6bb:75c3:ba06 with SMTP id ay8-20020a05620a178800b006bb75c3ba06mr3156681qkb.686.1660853980841; Thu, 18 Aug 2022 13:19:40 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::e531]) by smtp.gmail.com with ESMTPSA id d18-20020a05620a241200b006b919c6749esm2650538qkn.91.2022.08.18.13.19.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 13:19:40 -0700 (PDT) Date: Thu, 18 Aug 2022 16:19:38 -0400 To: Jason Merrill Subject: [PATCH v2] c++: Implement -Wself-move warning [PR81159] Message-ID: References: <20220809163719.381319-1-polacek@redhat.com> <1ca5b35f-0fa6-be28-290f-701c5ac43807@redhat.com> MIME-Version: 1.0 In-Reply-To: <1ca5b35f-0fa6-be28-290f-701c5ac43807@redhat.com> User-Agent: Mutt/2.2.6 (2022-06-05) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-23.2 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 Cc: GCC Patches 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?1740702315618280917?= X-GMAIL-MSGID: =?utf-8?q?1741531675122855056?= On Mon, Aug 15, 2022 at 03:54:05PM -0400, Jason Merrill wrote: > On 8/9/22 09:37, Marek Polacek wrote: > > + /* We're looking for *std::move ((T &) &arg), or > > + *std::move ((T &) (T *) r) if the argument it a reference. */ > > + if (!REFERENCE_REF_P (rhs) > > + || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR) > > + return; > > + tree fn = TREE_OPERAND (rhs, 0); > > + if (!is_std_move_p (fn)) > > + return; > > + tree arg = CALL_EXPR_ARG (fn, 0); > > + if (TREE_CODE (arg) != NOP_EXPR) > > + return; > > + /* Strip the (T &). */ > > + arg = TREE_OPERAND (arg, 0); > > + /* Strip the (T *) or &. */ > > + arg = TREE_OPERAND (arg, 0); > > Are you sure these are the only two expressions that can make it here? What > if the argument to move is *Tptr? Not 100% sure but I couldn't find any other form. For *Tptr we get *std::move ((int * &) &Tptr) so it works as expected as well. I've extended the existing test to test this too. > > @@ -6826,6 +6827,26 @@ of a declaration: > > This warning is enabled by @option{-Wall}. > > +@item -Wno-self-move @r{(C++ and Objective-C++ only)} > > +@opindex Wself-move > > +@opindex Wno-self-move > > +This warning warns when a value is moved to itself with @code{std::move}. > > +Such a @code{std::move} has no effect. > > ...unless it naively breaks the object, like > > T(T&& ot): data(ot.data) { ot.data = nullptr; } // oops "If you try to move me I'll disappear!" I've added the weasel word: "typically has no effect." Or do we want to say more? -- >8 -- About 5 years ago we got a request to implement -Wself-move, which warns about useless moves like this: int x; x = std::move (x); This patch implements that warning. PR c++/81159 gcc/c-family/ChangeLog: * c.opt (Wself-move): New option. gcc/cp/ChangeLog: * typeck.cc (maybe_warn_self_move): New. (cp_build_modify_expr): Call maybe_warn_self_move. gcc/ChangeLog: * doc/invoke.texi: Document -Wself-move. gcc/testsuite/ChangeLog: * g++.dg/warn/Wself-move1.C: New test. --- gcc/c-family/c.opt | 4 + gcc/cp/typeck.cc | 48 ++++++++++- gcc/doc/invoke.texi | 23 +++++- gcc/testsuite/g++.dg/warn/Wself-move1.C | 105 ++++++++++++++++++++++++ 4 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wself-move1.C base-commit: ca170ed9f8a086ca7e1eec841882b6bed9ec1a3a diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index dfdebd596ef..f776efd39d8 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1229,6 +1229,10 @@ Wselector ObjC ObjC++ Var(warn_selector) Warning Warn if a selector has multiple methods. +Wself-move +C++ ObjC++ Var(warn_self_move) Warning LangEnabledBy(C++ ObjC++, Wall) +Warn when a value is moved to itself with std::move. + Wsequence-point C ObjC C++ ObjC++ Var(warn_sequence_point) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall) Warn about possible violations of sequence point rules. diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 992ebfd99fb..cbc32a7c8ca 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -8897,7 +8897,51 @@ cp_build_c_cast (location_t loc, tree type, tree expr, return error_mark_node; } - + +/* Warn when a value is moved to itself with std::move. LHS is the target, + RHS may be the std::move call, and LOC is the location of the whole + assignment. */ + +static void +maybe_warn_self_move (location_t loc, tree lhs, tree rhs) +{ + if (!warn_self_move) + return; + + /* C++98 doesn't know move. */ + if (cxx_dialect < cxx11) + return; + + if (processing_template_decl) + return; + + /* We're looking for *std::move ((T &) &arg), or + *std::move ((T &) (T *) r) if the argument it a reference. */ + if (!REFERENCE_REF_P (rhs) + || TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR) + return; + tree fn = TREE_OPERAND (rhs, 0); + if (!is_std_move_p (fn)) + return; + tree arg = CALL_EXPR_ARG (fn, 0); + if (TREE_CODE (arg) != NOP_EXPR) + return; + /* Strip the (T &). */ + arg = TREE_OPERAND (arg, 0); + /* Strip the (T *) or &. */ + arg = TREE_OPERAND (arg, 0); + arg = convert_from_reference (arg); + /* So that we catch (i) = std::move (i);. */ + lhs = maybe_undo_parenthesized_ref (lhs); + STRIP_ANY_LOCATION_WRAPPER (lhs); + if (cp_tree_equal (lhs, arg)) + { + auto_diagnostic_group d; + if (warning_at (loc, OPT_Wself_move, "moving a variable to itself")) + inform (loc, "remove % call"); + } +} + /* For use from the C common bits. */ tree build_modify_expr (location_t location, @@ -9101,6 +9145,8 @@ cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode, if (modifycode == NOP_EXPR) { + maybe_warn_self_move (loc, lhs, rhs); + if (c_dialect_objc ()) { result = objc_maybe_build_modify_expr (lhs, rhs); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index f65d351a5fc..5dea3fee124 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -264,7 +264,7 @@ in the following sections. -Wreorder -Wregister @gol -Wstrict-null-sentinel -Wno-subobject-linkage -Wtemplates @gol -Wno-non-template-friend -Wold-style-cast @gol --Woverloaded-virtual -Wno-pmf-conversions -Wsign-promo @gol +-Woverloaded-virtual -Wno-pmf-conversions -Wself-move -Wsign-promo @gol -Wsized-deallocation -Wsuggest-final-methods @gol -Wsuggest-final-types -Wsuggest-override @gol -Wno-terminate -Wuseless-cast -Wno-vexing-parse @gol @@ -5843,6 +5843,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}. -Wreorder @gol -Wrestrict @gol -Wreturn-type @gol +-Wself-move @r{(only for C++)} @gol -Wsequence-point @gol -Wsign-compare @r{(only in C++)} @gol -Wsizeof-array-div @gol @@ -6828,6 +6829,26 @@ of a declaration: This warning is enabled by @option{-Wall}. +@item -Wno-self-move @r{(C++ and Objective-C++ only)} +@opindex Wself-move +@opindex Wno-self-move +This warning warns when a value is moved to itself with @code{std::move}. +Such a @code{std::move} typically has no effect. + +@smallexample +struct T @{ +@dots{} +@}; +void fn() +@{ + T t; + @dots{} + t = std::move (t); +@} +@end smallexample + +This warning is enabled by @option{-Wall}. + @item -Wsequence-point @opindex Wsequence-point @opindex Wno-sequence-point diff --git a/gcc/testsuite/g++.dg/warn/Wself-move1.C b/gcc/testsuite/g++.dg/warn/Wself-move1.C new file mode 100644 index 00000000000..b12f0d43dad --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wself-move1.C @@ -0,0 +1,105 @@ +// PR c++/81159 +// { dg-do compile { target c++11 } } +// { dg-options "-Wself-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); } +} + +int g; + +struct S { + int x; + S(S&& o) { + x = std::move (x); // { dg-warning "moving a variable to itself" } + x = std::move (o.x); + o.x = std::move (x); + o.x = std::move (o.x); // { dg-warning "moving a variable to itself" } + } + void foo (int x) { + x = std::move (x); // { dg-warning "moving a variable to itself" } + } +}; + +struct X { + int x; + X(int x) : x(std::move (x)) { } +}; + +struct A {}; +struct B { A a; }; +struct C { C(); ~C(); }; +struct D { D(); D(const D&); D(D&&); D& operator=(const D&); }; + +void +test () +{ + int i = 42; + i = std::move (i); // { dg-warning "moving a variable to itself" } + (i) = std::move (i); // { dg-warning "moving a variable to itself" } + + g = std::move (g); // { dg-warning "moving a variable to itself" } + (g) = std::move (g); // { dg-warning "moving a variable to itself" } + + A a; + a = std::move (a); // { dg-warning "moving a variable to itself" } + + B b; + b = std::move (b); // { dg-warning "moving a variable to itself" } + b.a = std::move (b.a); // { dg-warning "moving a variable to itself" } + + C c; + c = std::move (c); // { dg-warning "moving a variable to itself" } + D d; + d = std::move (d); // { dg-warning "moving a variable to itself" } +} + +template +void ttest () +{ + T t; + t = std::move (t); // { dg-warning "moving a variable to itself" } +} + +template void ttest(); + +void +testref (int &r, int &&rr) +{ + r = std::move (r); // { dg-warning "moving a variable to itself" } + rr = std::move (rr); // { dg-warning "moving a variable to itself" } +} + +// Test various other arguments to std::move. +template +void +testargs (T *Tptr, T& Tref, T&& Trref, const T *Tcptr) +{ + Tptr = std::move (Tptr); // { dg-warning "moving a variable to itself" } + Tref = std::move (Tref); // { dg-warning "moving a variable to itself" } + Trref = std::move (Trref); // { dg-warning "moving a variable to itself" } + Tcptr = std::move (Tcptr); // { dg-warning "moving a variable to itself" } +} + +void +call_testargs () +{ + int i = 42; + testargs(&i, i, 42, &i); +}