From 401bd4adcfda9965363b1ac3ba7e1580f15d6883 Mon Sep 17 00:00:00 2001 From: Eric Botcazou Date: Wed, 26 May 2021 19:12:05 +0200 Subject: 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. --- gcc/c/c-decl.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'gcc/c') diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 53b2b5b..3c7b306 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. -- cgit v1.1