diff mbox series

[C] Warn on type punning that toggles scalar storage order

Message ID 3368547.iIbC2pHGDl@fomalhaut
State New
Headers show
Series [C] Warn on type punning that toggles scalar storage order | expand

Commit Message

Eric Botcazou May 26, 2021, 2:12 p.m. UTC
Hi,

As documented in the manual, we do not support type punning that toggles the
scalar storage order, so this adds a warning for the simple case of unions.

Tested on x86-64/Linux, OK for the mainline?


2021-05-26  Eric Botcazou  <ebotcazou@adacore.com>

	PR c/100653
	* c-decl.c (finish_struct): Warn for a union containing an aggregate
	field with a differing scalar storage order.


2021-05-26  Eric Botcazou  <ebotcazou@adacore.com>

	* gcc.dg/sso-13.c: New test.

Comments

Jeff Law May 26, 2021, 4:48 p.m. UTC | #1
On 5/26/2021 8:12 AM, Eric Botcazou wrote:
> Hi,
>
> As documented in the manual, we do not support type punning that toggles the
> scalar storage order, so this adds a warning for the simple case of unions.
>
> Tested on x86-64/Linux, OK for the mainline?
>
>
> 2021-05-26  Eric Botcazou  <ebotcazou@adacore.com>
>
> 	PR c/100653
> 	* c-decl.c (finish_struct): Warn for a union containing an aggregate
> 	field with a differing scalar storage order.
>
>
> 2021-05-26  Eric Botcazou  <ebotcazou@adacore.com>
>
> 	* gcc.dg/sso-13.c: New test.
OK
jeff
Eric Botcazou May 26, 2021, 10:26 p.m. UTC | #2
> OK

Thanks.  I have also installed the attached tweak to the documentation.


	PR c/100653
	* doc/extend.texi (scalar_storage_order): Rephrase slightly.
diff mbox series

Patch

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.