public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-1072] Warn on type punning that toggles scalar storage order
@ 2021-05-26 17:14 Eric Botcazou
  0 siblings, 0 replies; only message in thread
From: Eric Botcazou @ 2021-05-26 17:14 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:401bd4adcfda9965363b1ac3ba7e1580f15d6883

commit r12-1072-g401bd4adcfda9965363b1ac3ba7e1580f15d6883
Author: Eric Botcazou <ebotcazou@adacore.com>
Date:   Wed May 26 19:12:05 2021 +0200

    Warn on type punning that toggles scalar storage order
    
    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.

Diff:
---
 gcc/c/c-decl.c                |  7 +++++++
 gcc/testsuite/gcc.dg/sso-13.c | 24 ++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index 53b2b5b637d..3c7b306809d 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -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
index 00000000000..ddfde00b758
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/sso-13.c
@@ -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;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-05-26 17:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-26 17:14 [gcc r12-1072] Warn on type punning that toggles scalar storage order Eric Botcazou

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).