From patchwork Wed Sep 6 03:19:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 137552 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp2063499vqo; Tue, 5 Sep 2023 20:20:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHbKhHo6agg47DXh8MS93qKMN0aPG/8P5P3C+P3tDxXFkaUAZeVW3tu20750ZIedWv8TsaH X-Received: by 2002:a17:907:6ea8:b0:9a2:26fa:8752 with SMTP id sh40-20020a1709076ea800b009a226fa8752mr1945497ejc.10.1693970422640; Tue, 05 Sep 2023 20:20:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693970422; cv=none; d=google.com; s=arc-20160816; b=aMCf0t7kJ0QcFi6r+Yq87CeE/w1PRdZ1HHzWqz5kMfc+Vdl0eLlAvqu0Fd+1FZQvP3 gKTU+BM/n6FzOeykzE/s2iU00hKv6/VHxheMA2R6rBPAF6YCJNR+fKcVQj5Kwl6I8snB n4UlplL9rFsnLfy5DHjQOT9YIB/F1EwKGRNuyGF/qgCF7ZzdXDoSqDiQ555QR1/+IbKY g6L5IAf5EH95Msc5JVHN1s28PT1Th7zoxU3ye3e96bmBomAW0J7hAELwuDR7dItzSsGT OZxqXGcBd8Ef5jTKsyk7FM1Ik752GMiz6vTItvYaZnQMime8KlgeGPGxtzog0dpCWNCN R4Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=AquycHtRMStawmGUhVhC2wOF1YRyD2YdrNtcsmCv+pA=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=JjTztA6f8dFV9aIqRYqfdqkgTg/5KahmIrQxmRKDsougemEexW+stfQLcwImdY1G+D 9tcEO/BYcsP/AaiOsttaXns0pdzvOKlmoa0XR298CyyaU/RdSi9LUjsCSwOJrA3OCD1L Ge/Yki9m6iM/hTuuaPYofR4QXCNTSHOYWLDp8YqASeMEe6OLjAGZt0Z6klmSzzAQznHx yZ7bk3wDOjN1Ao5WIKFjLdKjjiZC7yOfR0k3OvQigA4/w0GnhIjp9EJytPzSRbd7NYKg KN0VCcEOxrLEiMdruUIyN0+rciDASk0yYAqwMgHC7XuC5lhIo9e93ea4FiN3DZiFG6/O oGFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=eDySsX+p; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a8-20020a17090682c800b00992f309c359si8415702ejy.430.2023.09.05.20.20.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 20:20:22 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=eDySsX+p; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 53BFD385842A for ; Wed, 6 Sep 2023 03:20:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53BFD385842A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693970421; bh=AquycHtRMStawmGUhVhC2wOF1YRyD2YdrNtcsmCv+pA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=eDySsX+p9CaiukT6LoBYLydaLIVab/CUbV4Z2mCfr8wiCfghTRxxkU0FyS8I6fLXI dgHSVRdVGSmcnMcFKePVF7SsUy7cDgargWR3oAD/jUE701oqVrgOFtWifZjMp3r2BK ybShGN8bMhmcqO6MfBOeqKC50EH9ef+33LBw/m+c= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id C94D63858D28 for ; Wed, 6 Sep 2023 03:19:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C94D63858D28 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-581-TZqAH3SnPe6gN2u-mvi9dg-1; Tue, 05 Sep 2023 23:19:35 -0400 X-MC-Unique: TZqAH3SnPe6gN2u-mvi9dg-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-64bf507a8d4so35158236d6.0 for ; Tue, 05 Sep 2023 20:19:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693970374; x=1694575174; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=AquycHtRMStawmGUhVhC2wOF1YRyD2YdrNtcsmCv+pA=; b=N4MBPEsxDNjsfQqYS74aSesSCcPj3w3nzDToGVRCxrxR9d3jXouBfrxx4N6WnIZpvc +H181ZExBjn9tYeWaUVLtGVPQEnwCcvjjN4FqJnUsm3ryWsZJdIBJJ9VTZ3o2mtPqwno oVjXHyYW74ys0Jhvj74+7peUaqYLPZKpC9Yx1CVUKJV/CQUK4ukPzmS5uSRMjJOUPapT 6ZMVi3HPyaj/9/PZ2BvPDzZ+fXbPG8pX1JHaYzNSCReMF62/TTEU/JWpkwRy9dRiPnjp SZAywW9UWeVKT3ZT+LJLMYlKnfyW4hxT++korZtDQdD7uWY8YET5kPHlv5TxjzNL1qT4 iE1Q== X-Gm-Message-State: AOJu0YwBpRWkik9yEECg1C8ciUvc2sGELEV7cG2Wx8t0Ga2w75qxjCCL WfwN+p6OdboQlBLOglXHbVwQmumhCdDNOi7dp/P6OG71+0SA2MfBJ/shIQSl/3Eo7sjYJfeD4mH r5xD+El6sFNvhwVQUyQgJMwqQwYoabS8tiUxiqfjzwzxXWorA1YwtVLaOZGTWJXz2kmQ4Zkf+og == X-Received: by 2002:a05:622a:13cb:b0:410:60a4:ffbc with SMTP id p11-20020a05622a13cb00b0041060a4ffbcmr21400316qtk.66.1693970374355; Tue, 05 Sep 2023 20:19:34 -0700 (PDT) X-Received: by 2002:a05:622a:13cb:b0:410:60a4:ffbc with SMTP id p11-20020a05622a13cb00b0041060a4ffbcmr21400280qtk.66.1693970373969; Tue, 05 Sep 2023 20:19:33 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id ke18-20020a05622a289200b003f6ac526568sm4919481qtb.39.2023.09.05.20.19.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Sep 2023 20:19:33 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: [[no_unique_address]] and cv-qualified type Date: Tue, 5 Sep 2023 23:19:31 -0400 Message-Id: <20230906031931.622583-1-jason@redhat.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 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_H4, 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: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776256729880533041 X-GMAIL-MSGID: 1776256729880533041 Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- We were checking for overlap using same_type_p and therefore allocating two Empty subobjects at the same offset because one was cv-qualified. This gives the warning at the location of the class name rather than the member declaration, but this should be a rare enough issue that it doesn't seem worth trying to be more precise. gcc/ChangeLog: * common.opt: Update -fabi-version=19. gcc/cp/ChangeLog: * class.cc (check_subobject_offset): Check same_type_ignoring_top_level_qualifiers_p. gcc/testsuite/ChangeLog: * g++.dg/abi/no_unique_address8.C: New test. * g++.dg/abi/no_unique_address8a.C: New test. --- gcc/common.opt | 1 + gcc/cp/class.cc | 28 +++++++++++++++-- gcc/testsuite/g++.dg/abi/no_unique_address8.C | 30 ++++++++++++++++++ .../g++.dg/abi/no_unique_address8a.C | 31 +++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/abi/no_unique_address8.C create mode 100644 gcc/testsuite/g++.dg/abi/no_unique_address8a.C base-commit: 244d1321340116b7780e78096356f69662fd0e18 diff --git a/gcc/common.opt b/gcc/common.opt index 3e1939293e8..f137a1f81ac 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -1011,6 +1011,7 @@ Driver Undocumented ; Default in G++ 13. ; ; 19: Emits ABI tags if needed in structured binding mangled names. +; Ignores cv-quals on [[no_unique_object]] members. ; Default in G++ 14. ; ; Additional positive integers will be assigned as new versions of diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc index 778759237dc..9139a0075ab 100644 --- a/gcc/cp/class.cc +++ b/gcc/cp/class.cc @@ -4053,9 +4053,33 @@ check_subobject_offset (tree type, tree offset, splay_tree offsets) if (!n) return 0; + enum { ignore, fast, slow, warn } + cv_check = (abi_version_crosses (19) ? slow + : abi_version_at_least (19) ? fast + : ignore); for (t = (tree) n->value; t; t = TREE_CHAIN (t)) - if (same_type_p (TREE_VALUE (t), type)) - return 1; + { + tree elt = TREE_VALUE (t); + + if (same_type_p (elt, type)) + return 1; + + if (cv_check != ignore + && same_type_ignoring_top_level_qualifiers_p (elt, type)) + { + if (cv_check == fast) + return 1; + cv_check = warn; + } + } + + if (cv_check == warn) + { + warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs", + "[[no_unique_address]]", type, "-fabi-version=19"); + if (abi_version_at_least (19)) + return 1; + } return 0; } diff --git a/gcc/testsuite/g++.dg/abi/no_unique_address8.C b/gcc/testsuite/g++.dg/abi/no_unique_address8.C new file mode 100644 index 00000000000..6aa2bba7810 --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/no_unique_address8.C @@ -0,0 +1,30 @@ +// { dg-do compile { target c++11 } } +// { dg-options "-fabi-version=19 -Wabi=18" } + +#include + +#define NOUNIQUE [[no_unique_address]] + +struct Empty { }; +#define CHECK_DISTINCT(type, field1, field2) static_assert(offsetof(type, field1) != offsetof(type, field2)) + +struct A1 { + NOUNIQUE Empty a; + Empty b; +}; +CHECK_DISTINCT(A1, a, b); +struct A2 { + NOUNIQUE const Empty a; + const Empty b; +}; +CHECK_DISTINCT(A2, a, b); +struct A3 { // { dg-warning "layout" } + NOUNIQUE const Empty a; + Empty b; +}; +CHECK_DISTINCT(A3, a, b); +struct A4 { // { dg-warning "layout" } + NOUNIQUE Empty a; + const Empty b; +}; +CHECK_DISTINCT(A4, a, b); diff --git a/gcc/testsuite/g++.dg/abi/no_unique_address8a.C b/gcc/testsuite/g++.dg/abi/no_unique_address8a.C new file mode 100644 index 00000000000..c5d48088529 --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/no_unique_address8a.C @@ -0,0 +1,31 @@ +// { dg-do compile { target c++11 } } +// { dg-options "-fabi-version=18 -Wabi=19" } + +#include + +#define NOUNIQUE [[no_unique_address]] + +struct Empty { }; +#define CHECK_DISTINCT(type, field1, field2) static_assert(offsetof(type, field1) != offsetof(type, field2)) +#define CHECK_SAME(type, field1, field2) static_assert(offsetof(type, field1) == offsetof(type, field2)) + +struct A1 { + NOUNIQUE Empty a; + Empty b; +}; +CHECK_DISTINCT(A1, a, b); +struct A2 { + NOUNIQUE const Empty a; + const Empty b; +}; +CHECK_DISTINCT(A2, a, b); +struct A3 { // { dg-warning "layout" } + NOUNIQUE const Empty a; + Empty b; +}; +CHECK_SAME(A3, a, b); +struct A4 { // { dg-warning "layout" } + NOUNIQUE Empty a; + const Empty b; +}; +CHECK_SAME(A4, a, b);