]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Warn on type punning that toggles scalar storage order
authorEric Botcazou <ebotcazou@adacore.com>
Wed, 26 May 2021 17:12:05 +0000 (19:12 +0200)
committerEric Botcazou <ebotcazou@adacore.com>
Wed, 26 May 2021 17:13:56 +0000 (19:13 +0200)
As documented in the manual, we do not support type punning that toggles
the scalar storage order, so this adds a warning for the case of unions.

gcc/c/
PR c/100653
* c-decl.c (finish_struct): Warn for a union containing an aggregate
field with a differing scalar storage order.
gcc/testsuite/
* gcc.dg/sso-13.c: New test.

gcc/c/c-decl.c
gcc/testsuite/gcc.dg/sso-13.c [new file with mode: 0644]

index 53b2b5b637de20e7cb7e471c0bf0e573684eef66..3c7b306809da4c490a1edd648c3154eec7e4e41d 100644 (file)
@@ -8853,6 +8853,13 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
                = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
            }
        }
+
+      if (TREE_CODE (t) == UNION_TYPE
+         && AGGREGATE_TYPE_P (TREE_TYPE (field))
+         && TYPE_REVERSE_STORAGE_ORDER (t)
+            != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (field)))
+       warning_at (DECL_SOURCE_LOCATION (field), OPT_Wscalar_storage_order,
+                   "type punning toggles scalar storage order");
     }
 
   /* Now we have the truly final field list.
diff --git a/gcc/testsuite/gcc.dg/sso-13.c b/gcc/testsuite/gcc.dg/sso-13.c
new file mode 100644 (file)
index 0000000..ddfde00
--- /dev/null
@@ -0,0 +1,24 @@
+/* Test support of scalar_storage_order attribute */
+
+/* { dg-do compile } */
+
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define REV_ENDIANNESS __attribute__((scalar_storage_order("big-endian")))
+#else
+#define REV_ENDIANNESS __attribute__((scalar_storage_order("little-endian")))
+#endif
+
+typedef struct tIp6Addr
+{
+    unsigned int s6_addr32[4];
+} tIp6Addr;
+
+struct _tBeTimNetAddr
+{
+    unsigned char isIPv4;
+    union
+    {
+        unsigned int addr;
+        tIp6Addr addr6;   /* { dg-warning "type punning toggles" } */
+    } REV_ENDIANNESS u;
+} REV_ENDIANNESS;