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 C94D63858D28 for ; Wed, 6 Sep 2023 03:19:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C94D63858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693970376; 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=AquycHtRMStawmGUhVhC2wOF1YRyD2YdrNtcsmCv+pA=; b=dHh8wagXFWxPgLRSst3mPqqhdMRZa9D5l/B5dQIZv/3t7ItTVrTTqmk+pi195/akdfREY7 NS6jANo30dofDcjDYjvACsp8WEjagjcPua4K5jGZZtG6b4r9e/ePKfULKeGAvqBkoNiy2J DBMoIhQ7VSuVtz7510mpC+LC12hgP8U= 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-Google-Smtp-Source: AGHT+IEIcQvPA6a4ZoC47ZmPPQ19isry2SyXjBVzBBc9cpzEG/A/e3ycr9C4LJpHbuR0vS1b3RaakA== 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) From: Jason Merrill 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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); base-commit: 244d1321340116b7780e78096356f69662fd0e18 -- 2.39.3