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 6FF553858D1E for ; Wed, 17 Aug 2022 16:11:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6FF553858D1E Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-172-BM4w3ElSM4OVSyISB1SdsQ-1; Wed, 17 Aug 2022 12:11:18 -0400 X-MC-Unique: BM4w3ElSM4OVSyISB1SdsQ-1 Received: by mail-qv1-f70.google.com with SMTP id nk7-20020a056214350700b0047688bd2105so6461164qvb.16 for ; Wed, 17 Aug 2022 09:11:18 -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=FqYOWe3MhCr3GIdwvhv+IoxFVijMoZIK194RI14NT/k=; b=ASpZpKReXuWQIYrVQJ6AadNw77cuaJAw+81STQnFtV0i97gPMCkIyuvzSdE6V4N0Re ++2+RliOLrHwE0CJTwItyyg2V0IjzZO1JIlFBsS0D8D3lp1w53DHPVmfaH5Z/VSlSexi Zn+1ntzAU63R1GogClNaqBtrUvEOjZR+fYz5Z515yMo5fImIk+A72Z/kh+LNGcbNixT4 gAtWkr4C3DCo0OzwoLWjvYey2QHLJxlKiQG6OAh8YqWsF+fDz+A+OePi8XrP3eubU4ST yqzNxeeH9U83qs2nZKimueeXDppas6H8Y8ctK/9CDw72NAFg6DT41n63NrOxBec8fJas py3w== X-Gm-Message-State: ACgBeo1uJIOaM2SV8aHh+FiNW1cu6JXfgl99q4oikM8vnoFyYUwdI2OG a6Lqs1umisGxvJkj5IZ7kQoWO3PK5NkAMDUE/+UKq/zuTd8AjaoNQjd2AshYEztQdMB9CK3EABh 3GUa0SAgkFka6nPSA+Q== X-Received: by 2002:a05:6214:d42:b0:496:a71b:d75 with SMTP id 2-20020a0562140d4200b00496a71b0d75mr3251994qvr.51.1660752678362; Wed, 17 Aug 2022 09:11:18 -0700 (PDT) X-Google-Smtp-Source: AA6agR529s+IdsHppUQa/XRtmZbMcr4rBO+f7CSjLpk5LnloT/G3MBBQ+ubmtl18y2CZWmA1e9gTKw== X-Received: by 2002:a05:6214:d42:b0:496:a71b:d75 with SMTP id 2-20020a0562140d4200b00496a71b0d75mr3251965qvr.51.1660752677938; Wed, 17 Aug 2022 09:11:17 -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 n6-20020a05622a040600b00339b8a5639csm13386535qtx.95.2022.08.17.09.11.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Aug 2022 09:11:17 -0700 (PDT) Message-ID: <98ce1382-bfba-af11-7602-6967d944b154@redhat.com> Date: Wed, 17 Aug 2022 12:11:16 -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] c++: Extend -Wpessimizing-move to other contexts To: Marek Polacek Cc: GCC Patches References: <20220802230447.721163-1-polacek@redhat.com> <9a065efc-c478-8dbf-3251-afd50891e152@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=-12.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, 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: Wed, 17 Aug 2022 16:11:24 -0000 On 8/16/22 14:09, Marek Polacek wrote: > 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. Makes sense. The patch is OK. > But I can move the call into some place in check_initializer if you prefer. > > Marek >