]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Treat unnamed bitfields as padding for __has_unique_object_representations ...
authorJakub Jelinek <jakub@redhat.com>
Tue, 14 Mar 2023 15:17:32 +0000 (16:17 +0100)
committerJakub Jelinek <jakub@redhat.com>
Wed, 3 May 2023 13:02:06 +0000 (15:02 +0200)
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)

gcc/cp/tree.c
gcc/testsuite/g++.dg/cpp1z/has-unique-obj-representations3.C [new file with mode: 0644]

index 6b9765fb71a4916fa895a2c9394cf8824d9c2c7a..399c4435293cfbc2b2be1599bcf9f1ddfaaa2df7 100644 (file)
@@ -4466,7 +4466,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))
@@ -4477,7 +4477,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 (file)
index 0000000..349c93e
--- /dev/null
@@ -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, "");