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.133.124]) by sourceware.org (Postfix) with ESMTPS id DC95D3858CDA for ; Tue, 16 Aug 2022 21:09:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DC95D3858CDA Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-318-6QZpQ5hzORCYLHNBP8qd1Q-1; Tue, 16 Aug 2022 17:09:17 -0400 X-MC-Unique: 6QZpQ5hzORCYLHNBP8qd1Q-1 Received: by mail-qk1-f199.google.com with SMTP id x22-20020a05620a259600b006b552a69231so10164122qko.18 for ; Tue, 16 Aug 2022 14:09:17 -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=2rIHDM7m0BSIqQT334yKHhlvZpp5a+yNezA42SGnlAg=; b=uOKCBaIYAz5gtEY1grWQD9RyiqLFLLpXdncGTSlp+UDBZ07Ekb75qkOxjO6XrPQubI 9syMsnqvxx4P72uO2KNgljdSR+2RpaIhIHct6MKlV/+njv7ER/4/wzi5D3hzOyAJNGUw uGAkJTJXO64iQA22ha1zyLPdDv+9u+Obd54MnTijilVxnMiGq3JvwmYAOCboNUwWECKT s9ulntu+beu1YTFAK6APYoW99+H36sfXy0y/iaVQdSWTikKk0dbgZnaPqklZTLEWRruS RPKJiqthieZSA+8hbYME3gnp5355g+LYPpLDuZAMERO0oPzTfXb7G6ensM44OYcF/cir QGGQ== X-Gm-Message-State: ACgBeo3Jzqpr10XkjAkZf9PNuLcBJLs22UQGLC1spq0JTt4F1OfRn+jl j6KlWRnErMleZHxBm0KAeAVialrK5sMu7dmftfexDODPKhkdRZHknsNLzOktpU5YY+dO58x4X82 4qlLgMg4dfI9/NX8MGw== X-Received: by 2002:ad4:5ecb:0:b0:474:7449:e897 with SMTP id jm11-20020ad45ecb000000b004747449e897mr19090902qvb.112.1660684157323; Tue, 16 Aug 2022 14:09:17 -0700 (PDT) X-Google-Smtp-Source: AA6agR6xm7uMwAOyxePL1ABFoHxJGndt4BfGRjngPb4wAOpOZl12C4wFAQSJJ0a4B6OGsWOb/vletQ== X-Received: by 2002:ad4:5ecb:0:b0:474:7449:e897 with SMTP id jm11-20020ad45ecb000000b004747449e897mr19090887qvb.112.1660684157086; Tue, 16 Aug 2022 14:09:17 -0700 (PDT) Received: from redhat.com ([2601:184:4780:4310::e531]) by smtp.gmail.com with ESMTPSA id 190-20020a3703c7000000b006bb2bca5741sm6349663qkd.93.2022.08.16.14.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Aug 2022 14:09:16 -0700 (PDT) Date: Tue, 16 Aug 2022 17:09:14 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts Message-ID: References: <20220802230447.721163-1-polacek@redhat.com> <9a065efc-c478-8dbf-3251-afd50891e152@redhat.com> MIME-Version: 1.0 In-Reply-To: <9a065efc-c478-8dbf-3251-afd50891e152@redhat.com> User-Agent: Mutt/2.2.6 (2022-06-05) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.8 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, 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, 16 Aug 2022 21:09:23 -0000 On Tue, Aug 16, 2022 at 03:23:18PM -0400, Jason Merrill wrote: > On 8/2/22 16:04, Marek Polacek wrote: > > In my recent patch which enhanced -Wpessimizing-move so that it warns > > about class prvalues too I said that I'd like to extend it so that it > > warns in more contexts where a std::move can prevent copy elision, such > > as: > > > > T t = std::move(T()); > > T t(std::move(T())); > > T t{std::move(T())}; > > T t = {std::move(T())}; > > void foo (T); > > foo (std::move(T())); > > > > This patch does that by adding two maybe_warn_pessimizing_move calls. > > These must happen before we've converted the initializers otherwise the > > std::move will be buried in a TARGET_EXPR. > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > PR c++/106276 > > > > gcc/cp/ChangeLog: > > > > * call.cc (build_over_call): Call maybe_warn_pessimizing_move. > > * cp-tree.h (maybe_warn_pessimizing_move): Declare. > > * decl.cc (build_aggr_init_full_exprs): Call > > maybe_warn_pessimizing_move. > > * typeck.cc (maybe_warn_pessimizing_move): Handle TREE_LIST and > > CONSTRUCTOR. Add a bool parameter and use it. Adjust a diagnostic > > message. > > (check_return_expr): Adjust the call to maybe_warn_pessimizing_move. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp0x/Wpessimizing-move7.C: Add dg-warning. > > * g++.dg/cpp0x/Wpessimizing-move8.C: New test. > > --- > > gcc/cp/call.cc | 5 +- > > gcc/cp/cp-tree.h | 1 + > > gcc/cp/decl.cc | 3 +- > > gcc/cp/typeck.cc | 58 ++++++++++++----- > > .../g++.dg/cpp0x/Wpessimizing-move7.C | 16 ++--- > > .../g++.dg/cpp0x/Wpessimizing-move8.C | 65 +++++++++++++++++++ > > 6 files changed, 120 insertions(+), 28 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move8.C > > > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > > index 01a7be10077..370137ebd6d 100644 > > --- a/gcc/cp/call.cc > > +++ b/gcc/cp/call.cc > > @@ -9627,10 +9627,13 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) > > if (!conversion_warning) > > arg_complain &= ~tf_warning; > > + if (arg_complain & tf_warning) > > + maybe_warn_pessimizing_move (arg, type, /*return_p*/false); > > + > > val = convert_like_with_context (conv, arg, fn, i - is_method, > > arg_complain); > > val = convert_for_arg_passing (type, val, arg_complain); > > - > > + > > if (val == error_mark_node) > > return error_mark_node; > > else > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > > index 3278b4114bd..5a8af22b509 100644 > > --- a/gcc/cp/cp-tree.h > > +++ b/gcc/cp/cp-tree.h > > @@ -8101,6 +8101,7 @@ extern tree finish_right_unary_fold_expr (tree, int); > > extern tree finish_binary_fold_expr (tree, tree, int); > > extern tree treat_lvalue_as_rvalue_p (tree, bool); > > extern bool decl_in_std_namespace_p (tree); > > +extern void maybe_warn_pessimizing_move (tree, tree, bool); > > /* in typeck2.cc */ > > extern void require_complete_eh_spec_types (tree, tree); > > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > > index 70ad681467e..dc6853a7de1 100644 > > --- a/gcc/cp/decl.cc > > +++ b/gcc/cp/decl.cc > > @@ -7220,9 +7220,10 @@ check_array_initializer (tree decl, tree type, tree init) > > static tree > > build_aggr_init_full_exprs (tree decl, tree init, int flags) > > - > > { > > gcc_assert (stmts_are_full_exprs_p ()); > > + if (init) > > + maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false); > > This is a surprising place to add this. Why here rather than in > build_aggr_init or check_initializer? IIRC it just felt appropriate since we only want to invoke maybe_warn_ on the full expression, not any subexpressions -- we're looking to see if the outermost expr is a std::move. Also, we want to warn for all types, not just classes. But I can move the call into some place in check_initializer if you prefer. Marek