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 <ebotca...@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 <ebotca...@adacore.com>
* gcc.dg/sso-13.c: New test.
--
Eric Botcazou
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.
/* 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;