public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-10724] c++: Treat unnamed bitfields as padding for __has_unique_object_representations [PR109096]
Date: Tue,  2 May 2023 20:15:47 +0000 (GMT)	[thread overview]
Message-ID: <20230502201547.7E91A385415A@sourceware.org> (raw)

https://gcc.gnu.org/g:66d35a0cb88f83f085b2ba4c46d0429ba0cfe574

commit r11-10724-g66d35a0cb88f83f085b2ba4c46d0429ba0cfe574
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Mar 14 16:17:32 2023 +0100

    c++: Treat unnamed bitfields as padding for __has_unique_object_representations [PR109096]
    
    As reported in the PR, for __has_unique_object_representations we
    were treating unnamed bitfields as named ones, which is wrong, they
    are actually padding.
    
    THe following patch fixes that.
    
    2023-03-14  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/109096
            * tree.c (record_has_unique_obj_representations): Ignore unnamed
            bitfields.
    
            * g++.dg/cpp1z/has-unique-obj-representations3.C: New test.
    
    (cherry picked from commit c35cf160a0ed81570cff6600dba465cf95fa80fa)

Diff:
---
 gcc/cp/tree.c                                                |  4 ++--
 gcc/testsuite/g++.dg/cpp1z/has-unique-obj-representations3.C | 10 ++++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index b499f38be15..e414d8cfd18 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -4641,7 +4641,7 @@ record_has_unique_obj_representations (const_tree t, const_tree sz)
 						    DECL_SIZE (field)))
 	  return false;
       }
-    else if (DECL_C_BIT_FIELD (field))
+    else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field))
       {
 	tree btype = DECL_BIT_FIELD_TYPE (field);
 	if (!type_has_unique_obj_representations (btype))
@@ -4652,7 +4652,7 @@ record_has_unique_obj_representations (const_tree t, const_tree sz)
 
   offset_int cur = 0;
   for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
-    if (TREE_CODE (field) == FIELD_DECL)
+    if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field))
       {
 	offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
 	offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
diff --git a/gcc/testsuite/g++.dg/cpp1z/has-unique-obj-representations3.C b/gcc/testsuite/g++.dg/cpp1z/has-unique-obj-representations3.C
new file mode 100644
index 00000000000..349c93e3c2b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/has-unique-obj-representations3.C
@@ -0,0 +1,10 @@
+// PR c++/109096
+// { dg-do compile { target c++11 } }
+
+#define INTB (__SIZEOF_INT__ * __CHAR_BIT__)
+struct U { int i : INTB * 3 / 4; int : INTB / 4; };
+struct V { int : INTB * 3 / 4; int j : INTB / 4; };
+struct W { int i; int : 0; int j; };
+static_assert (__has_unique_object_representations (U) == false, "");
+static_assert (__has_unique_object_representations (V) == false, "");
+static_assert (sizeof (W) != 2 * sizeof (int) || __has_unique_object_representations (W) == true, "");

                 reply	other threads:[~2023-05-02 20:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230502201547.7E91A385415A@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).