diff options
author | Eric Botcazou <ebotcazou@adacore.com> | 2021-06-06 11:37:45 +0200 |
---|---|---|
committer | Eric Botcazou <ebotcazou@adacore.com> | 2021-06-06 11:41:30 +0200 |
commit | a589877a0036fc2f66b7a957859940c53efdc7c9 (patch) | |
tree | a6bc26893ec860b9b4411e2ef71520937c894b14 /gcc/c/c-decl.c | |
parent | 28c62475050d2ac6c243580e1130a87308e1e907 (diff) | |
download | gcc-a589877a0036fc2f66b7a957859940c53efdc7c9.zip gcc-a589877a0036fc2f66b7a957859940c53efdc7c9.tar.gz gcc-a589877a0036fc2f66b7a957859940c53efdc7c9.tar.bz2 |
Fix thinko in new warning on type punning for storage order purposes
In C, unlike in Ada, the storage order of arrays is that of their component
type, so you need to look at it when deciding to warn. And the PR complains
about a bogus warning on the assignment of a pointer returned by alloca or
malloc, so this also fixes that.
gcc/c
PR c/100920
* c-decl.c (finish_struct): Fix thinko in previous change.
* c-typeck.c (convert_for_assignment): Do not warn on pointer
assignment and initialization for storage order purposes if the
RHS is a call to a DECL_IS_MALLOC function.
gcc/testsuite/
* gcc.dg/sso-14.c: New test.
Diffstat (limited to 'gcc/c/c-decl.c')
-rw-r--r-- | gcc/c/c-decl.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 28f851b..a86792b 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -8854,12 +8854,21 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, } } + /* Warn on problematic type punning for storage order purposes. */ 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"); + && TREE_CODE (field) == FIELD_DECL + && AGGREGATE_TYPE_P (TREE_TYPE (field))) + { + tree ftype = TREE_TYPE (field); + if (TREE_CODE (ftype) == ARRAY_TYPE) + ftype = strip_array_types (ftype); + if (RECORD_OR_UNION_TYPE_P (ftype) + && TYPE_REVERSE_STORAGE_ORDER (ftype) + != TYPE_REVERSE_STORAGE_ORDER (t)) + warning_at (DECL_SOURCE_LOCATION (field), + OPT_Wscalar_storage_order, + "type punning toggles scalar storage order"); + } } /* Now we have the truly final field list. |