public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Eric Botcazou <ebotcazou@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-1072] Warn on type punning that toggles scalar storage order
Date: Wed, 26 May 2021 17:14:05 +0000 (GMT)	[thread overview]
Message-ID: <20210526171405.7BDF23858004@sourceware.org> (raw)

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;


                 reply	other threads:[~2021-05-26 17:14 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=20210526171405.7BDF23858004@sourceware.org \
    --to=ebotcazou@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).