From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 D76053858285 for ; Tue, 23 Aug 2022 21:27:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D76053858285 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-103-B_xs1pIOM0O-y7w1UQ3Wxw-1; Tue, 23 Aug 2022 17:27:03 -0400 X-MC-Unique: B_xs1pIOM0O-y7w1UQ3Wxw-1 Received: by mail-qv1-f69.google.com with SMTP id lj8-20020a0562145c8800b00496ceb190d4so5187172qvb.3 for ; Tue, 23 Aug 2022 14:27:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=2YdwfEdjko9xPgkoQ+nvte/Eaq1LYQlY6FeiNPUPiFc=; b=0mwd2mjk/2dVjEOuzqXG/W7jfikBRiOmFK12cVA7PBrdOZQrPu0ounicNm+cVGbJP1 iHNUftu+PkAZb/uDQqC5byYkGQXl8DjAdtKzzEx9T2IvoOL85K62PymgdhP1gqm/ittm tPanHhFt/fV75zwG4eKxw+4ERtpHa7gINJTg+oeWIlwjmoNlY9hRzXw/5ZxshxM+YK6p f57T6qWhFnYGVJiYIizRont0S/wzbwvPW6XqcuCXFPFSRO/XpjmC9V8hIOWruK5cPGOV Q+C0d587l+cxrBTdSPe9FXqHoVKnF2Ay33BPBehX/Ydeu6APiXtTShyEGwDRY8GLZdX5 /gJA== X-Gm-Message-State: ACgBeo3GjPq24AUPKtjRHJd+sxWdOzo98ZvWewxGbkdsgcBKx4SHO8Vh Ze36uhC+wekMA5NlMyXQhtPbeBCXk/HG6w67s2F+JNWGnqQgcHmAqD/OQitcAXXHEzdxeXqzdyA gMpCiHDpyRX1BakSdBw== X-Received: by 2002:ac8:7c44:0:b0:344:5653:df43 with SMTP id o4-20020ac87c44000000b003445653df43mr21693154qtv.359.1661290022616; Tue, 23 Aug 2022 14:27:02 -0700 (PDT) X-Google-Smtp-Source: AA6agR41Lsl2DCMpqdNcfDolA8WaOlHikIWcEmxY3okL/VmRBnpqb67BPWhJQL1uY/0DYUedMBhKzg== X-Received: by 2002:ac8:7c44:0:b0:344:5653:df43 with SMTP id o4-20020ac87c44000000b003445653df43mr21693134qtv.359.1661290022194; Tue, 23 Aug 2022 14:27:02 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id bj25-20020a05620a191900b006b93ef659c3sm13899798qkb.39.2022.08.23.14.27.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 23 Aug 2022 14:27:01 -0700 (PDT) Message-ID: <9e980813-7ab7-25dc-7f74-56faaafafb84@redhat.com> Date: Tue, 23 Aug 2022 17:27:00 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH v4] c++: Implement -Wself-move warning [PR81159] To: Marek Polacek Cc: GCC Patches References: <20220809163719.381319-1-polacek@redhat.com> <1ca5b35f-0fa6-be28-290f-701c5ac43807@redhat.com> <3e9dea44-1e0e-e329-575a-aa621d19deeb@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-25.9 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, 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-List-Received-Date: Tue, 23 Aug 2022 21:27:07 -0000 On 8/23/22 09:39, Marek Polacek wrote: > On Sat, Aug 20, 2022 at 05:31:52PM -0400, Jason Merrill wrote: >> On 8/19/22 15:34, Marek Polacek wrote: >>> On Thu, Aug 18, 2022 at 08:33:47PM -0400, Jason Merrill wrote: >>>> On 8/18/22 13:19, Marek Polacek wrote: >>>>> 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) >>>> >>>> That likes like what you'd get when the argument is Tptr, not when it's >>>> *Tptr. And indeed that's what I see in the testcase: >>>> >>>>> + Tptr = std::move (Tptr); // { dg-warning "moving a variable to itself" } >>>> >>>> is missing the * >>> >>> Duh, sorry. The previous patch didn't handle the *Tptr case. Further poking >>> revealed that we need special care to handle (*(Tptr)) and **Tptr etc. So in >>> this patch I'm stripping all *s and V_C_Es. Sigh. >> >> Ah, I was just thinking that the old patch needed more checking of >> TREE_CODEs. But I suppose it's also good to diagnose these cases as well. > > Probably. I notice that clang++ doesn't detect it, but that doesn't mean > we can't. > >>> + if (cp_tree_equal (lhs, arg)) >>> + { >>> + auto_diagnostic_group d; >>> + if (warning_at (loc, OPT_Wself_move, "moving a variable to itself")) >> >> Maybe only say "variable" when the original operand is in fact a variable? > > Fixed by the print_var_p thingie I introduced. I saw that clang++ points > out the type so I'm printing it too in this patch. > >>> +// Test various other arguments to std::move. >>> +template >>> +void >>> +testargs (T *Tptr, T **Tpptr, T& Tref, T&& Trref, const T *Tcptr) >>> +{ >>> + Tptr = std::move (Tptr); // { dg-warning "moving a variable to itself" } >>> + *Tptr = std::move (*Tptr); // { dg-warning "moving a variable to itself" } >>> + *Tptr = std::move (*(Tptr)); // { dg-warning "moving a variable to itself" } >>> + *(Tptr) = std::move (*Tptr); // { dg-warning "moving a variable to itself" } >>> + *(Tptr + 1) = std::move (*Tptr + 1); >>> + *(Tptr + 1) = std::move (*Tptr + 2); >> >> Do you mean to have *(Tptr + 1) on both sides? The difference in * operand >> is surprising. > > Definitely not intended, good spotting! Fixed (we emit a warning for the > +1 case but not the +2 case -- good). > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >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 | 61 +++++++++++- > gcc/doc/invoke.texi | 23 ++++- > gcc/testsuite/g++.dg/warn/Wself-move1.C | 125 ++++++++++++++++++++++++ > 4 files changed, 211 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/warn/Wself-move1.C > > 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..194c403bd2b 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -8897,7 +8897,64 @@ 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; > + > + 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; > + > + /* Just a little helper to strip * and various NOPs. */ > + auto extract_op = [] (tree &op) { > + STRIP_NOPS (op); > + while (INDIRECT_REF_P (op)) > + op = TREE_OPERAND (op, 0); > + op = maybe_undo_parenthesized_ref (op); > + STRIP_ANY_LOCATION_WRAPPER (op); > + }; > + > + tree arg = CALL_EXPR_ARG (fn, 0); > + extract_op (arg); > + if (TREE_CODE (arg) == ADDR_EXPR) > + arg = TREE_OPERAND (arg, 0); > + tree type = TREE_TYPE (lhs); > + lhs = maybe_undo_parenthesized_ref (lhs); > + STRIP_ANY_LOCATION_WRAPPER (lhs); > + const bool print_var_p = (DECL_P (lhs) > + || REFERENCE_REF_P (lhs) > + || TREE_CODE (lhs) == COMPONENT_REF); Why include REFERENCE_REF_P and COMPONENT_REF? Reference refs should be stripped before this test, member refs aren't variables. > + extract_op (lhs); > + > + if (cp_tree_equal (lhs, arg)) > + { > + auto_diagnostic_group d; > + if (print_var_p > + ? warning_at (loc, OPT_Wself_move, > + "moving a variable of type %qT to itself", type) > + : warning_at (loc, OPT_Wself_move, > + "moving an expression of type %qT to itself", type)) > + inform (loc, "remove % call"); > + } > +} > + > /* For use from the C common bits. */ > tree > build_modify_expr (location_t location, > @@ -9101,6 +9158,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 78eb7ad7e74..dc686da8b9f 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 > @@ -5844,6 +5844,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 > @@ -6829,6 +6830,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..39111f0a2b0 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wself-move1.C > @@ -0,0 +1,125 @@ > +// 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 of type .int. to itself" } > + x = std::move (o.x); > + o.x = std::move (x); > + o.x = std::move (o.x); // { dg-warning "moving a variable of type .int. to itself" } > + } > + void foo (int x) { > + x = std::move (x); // { dg-warning "moving a variable of type .int. 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 of type .int. to itself" } > + (i) = std::move (i); // { dg-warning "moving a variable of type .int. to itself" } > + > + g = std::move (g); // { dg-warning "moving a variable of type .int. to itself" } > + (g) = std::move (g); // { dg-warning "moving a variable of type .int. to itself" } > + > + A a; > + a = std::move (a); // { dg-warning "moving a variable of type .A. to itself" } > + (a) = std::move (a); // { dg-warning "moving a variable of type .A. to itself" } > + > + B b; > + b = std::move (b); // { dg-warning "moving a variable of type .B. to itself" } > + (b) = std::move (b); // { dg-warning "moving a variable of type .B. to itself" } > + b.a = std::move (b.a); // { dg-warning "moving a variable of type .A. to itself" } > + (b.a) = std::move (b.a); // { dg-warning "moving a variable of type .A. to itself" } > + > + C c; > + c = std::move (c); // { dg-warning "moving a variable of type .C. to itself" } > + D d; > + d = std::move (d); // { dg-warning "moving a variable of type .D. to itself" } > +} > + > +template > +void ttest () > +{ > + T t; > + t = std::move (t); // { dg-warning "moving a variable of type .A. to itself" } > +} > + > +template void ttest(); > + > +void > +testref (int &r, int &&rr) > +{ > + r = std::move (r); // { dg-warning "moving a variable of type .int. to itself" } > + rr = std::move (rr); // { dg-warning "moving a variable of type .int. to itself" } > +} > + > +// Test various other arguments to std::move. > +template > +void > +testargs (T *Tptr, T **Tpptr, T& Tref, T&& Trref, const T *Tcptr) > +{ > + Tptr = std::move (Tptr); // { dg-warning "moving a variable of type 'int\\*' to itself" } > + *Tptr = std::move (*Tptr); // { dg-warning "moving an expression of type 'int' to itself" } > + *Tptr = std::move (*(Tptr)); // { dg-warning "moving an expression of type 'int' to itself" } > + *(Tptr) = std::move (*Tptr); // { dg-warning "moving an expression of type 'int' to itself" } > + *(Tptr + 1) = std::move (*(Tptr + 1)); // { dg-warning "moving an expression of type 'int' to itself" } > + *(Tptr + 1) = std::move (*(Tptr + 2)); > + (*(Tptr)) = std::move (*Tptr); // { dg-warning "moving an expression of type 'int' to itself" } > + *Tpptr = std::move (*Tpptr); // { dg-warning "moving an expression of type 'int\\*' to itself" } > + **Tpptr = std::move (**Tpptr); // { dg-warning "moving an expression of type 'int' to itself" } > + Tref = std::move (Tref); // { dg-warning "moving a variable of type 'int' to itself" } > + Trref = std::move (Trref); // { dg-warning "moving a variable of type 'int' to itself" } > + Tcptr = std::move (Tcptr); // { dg-warning "moving a variable of type 'const int\\*' to itself" } > + (Tptr) = std::move (Tptr); // { dg-warning "moving a variable of type 'int\\*' to itself" } > + (*Tptr) = std::move (*Tptr); // { dg-warning "moving an expression of type 'int' to itself" } > + (*Tpptr) = std::move (*Tpptr); // { dg-warning "moving an expression of type 'int\\*' to itself" } > + (**Tpptr) = std::move (**Tpptr); // { dg-warning "moving an expression of type 'int' to itself" } > + (*(*(Tpptr))) = std::move (**Tpptr); // { dg-warning "moving an expression of type 'int' to itself" } > + (Tref) = std::move (Tref); // { dg-warning "moving a variable of type 'int' to itself" } > + (Trref) = std::move (Trref); // { dg-warning "moving a variable of type 'int' to itself" } > + (Tcptr) = std::move (Tcptr); // { dg-warning "moving a variable of type 'const int\\*' to itself" } > +} > + > +void > +call_testargs () > +{ > + int i = 42; > + int *p = &i; > + testargs(&i, &p, i, 42, &i); > +} > > base-commit: baa3ffb19c54fa334ac2884f6acb5d31aa79ac32