From patchwork Wed Sep 18 18:35:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1986935 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Y734A0d7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4X86m844GBz1y1g for ; Thu, 19 Sep 2024 04:35:52 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5FA373858283 for ; Wed, 18 Sep 2024 18:35:50 +0000 (GMT) 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 ESMTP id CB1D33858C52 for ; Wed, 18 Sep 2024 18:35:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB1D33858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CB1D33858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726684530; cv=none; b=sIRgf1rUSqouy8tNRkO1jFV3QKXXuyXY2fwlU5WHXtHL9FfhtjJFFXlWisQsF4V0YtOCnYFkt/iMroCvj0GJK979nueP544G3clWXoFCeBedvlG9PyUHveO7euPEIALhfviqkB764UPXPC6CIe65GfjEBXCy/ZLtnK3Fk5PnaR8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726684530; c=relaxed/simple; bh=PFesAhtTfuKpWeQ4NkksH/B8JT4DqD76Fo3klNhfSpI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=S+PVvNE6lsHHar8sErAL3aCDfNTzONXEPxEFTNkvb1uBJAkyiRYCds7ZPLbNOzZg07YBU/qdN9OpBk1nHr6SJpE5zkEEatE/ITzQtsfOkIKsUWJFJO6TV11CDZGA9tfBjnOVWZzGePmT6J3o4PlbYhP5nYug1i8w1hnLWgbFmy0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726684527; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=t2OpPUgFfk1vMNs26rtudv07d94kkc5p7qXpt5g08ZM=; b=Y734A0d79PXJZJR8/t9olYDv39oIiKZoPdTb7qH4ujnCY6nQXTEA7mYXTyiCnKcz1RT3uZ Z0ENP4POiy79lyN854UG3FsqG3LFmKi79uT74mCTLrwS0KlUc1DWS9SLGl1hJ1Trj8qpTZ YYTviY5jYNTNSpeN+9wyW47MZHG16Ao= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-214-D4plMLyONGCVerDhHsao5Q-1; Wed, 18 Sep 2024 14:35:26 -0400 X-MC-Unique: D4plMLyONGCVerDhHsao5Q-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 04371195FE15 for ; Wed, 18 Sep 2024 18:35:23 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.65.73]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ECAFD1956086; Wed, 18 Sep 2024 18:35:21 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: tweak for -Wrange-loop-construct [PR116731] Date: Wed, 18 Sep 2024 14:35:17 -0400 Message-ID: <20240918183517.517366-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 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, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- This PR reports that the warning would be better off using a check for trivially constructible rather than trivially copyable. LLVM accepted a similar fix: https://github.com/llvm/llvm-project/issues/47355 PR c++/116731 gcc/cp/ChangeLog: * parser.cc (warn_for_range_copy): Check if TYPE is trivially constructible, not copyable. gcc/testsuite/ChangeLog: * g++.dg/warn/Wrange-loop-construct3.C: New test. --- gcc/cp/parser.cc | 8 ++- .../g++.dg/warn/Wrange-loop-construct3.C | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C base-commit: cc62b2c3da118f08f71d2ae9c08bafb55b35767a diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 4dd9474cf60..140457ba224 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -14394,11 +14394,13 @@ warn_for_range_copy (tree decl, tree expr) else if (!CP_TYPE_CONST_P (type)) return; - /* Since small trivially copyable types are cheap to copy, we suppress the - warning for them. 64B is a common size of a cache line. */ + /* Since small trivially constructible types are cheap to construct, we + suppress the warning for them. 64B is a common size of a cache line. */ + tree vec = make_tree_vec (1); + TREE_VEC_ELT (vec, 0) = TREE_TYPE (expr); if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64 - && trivially_copyable_p (type))) + && is_trivially_xible (INIT_EXPR, type, vec))) return; /* If we can initialize a reference directly, suggest that to avoid the diff --git a/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C b/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C new file mode 100644 index 00000000000..3d9d0c9088e --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wrange-loop-construct3.C @@ -0,0 +1,57 @@ +// PR c++/116731 +// { dg-do compile { target c++11 } } +// { dg-options "-Wrange-loop-construct" } + +void +f0 () +{ + struct S { + char a[64]; + S& operator=(const S&) { return *this; }; + }; + + S arr[8]; + for (const auto r : arr) + (void) r; +} + +void +f1 () +{ + struct S { + char a[65]; + S& operator=(const S&) { return *this; }; + }; + + S arr[8]; + for (const auto r : arr) // { dg-warning "creates a copy" } + (void) r; +} + +void +f2 () +{ + struct S { + char a[64]; + S& operator=(const S&) { return *this; }; + ~S() { } + }; + + S arr[8]; + for (const auto r : arr) // { dg-warning "creates a copy" } + (void) r; +} + +void +f3 () +{ + struct S { + char a[65]; + S& operator=(const S&) { return *this; }; + ~S() { } + }; + + S arr[8]; + for (const auto r : arr) // { dg-warning "creates a copy" } + (void) r; +}