array-bounds: Fix up ICE on overaligned variables [PR99109]
authorJakub Jelinek <jakub@redhat.com>
Thu, 18 Feb 2021 08:45:19 +0000 (09:45 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 18 Feb 2021 08:45:19 +0000 (09:45 +0100)
commitf72e3d8c9ffb81d25d4fdba10056cd2197e22d64
treea62c5274d39fbcae0077291f40e1553e72da3989
parentdecd8fb0128870d0d768ba53dae626913d6d9c54
array-bounds: Fix up ICE on overaligned variables [PR99109]

check_mem_ref builds artificial arrays for variables that don't have
array type.
The C standard says:
"For the purposes of these operators, a pointer to an object that is not an element of an
array behaves the same as a pointer to the first element of an array of length one with the
type of the object as its element type."
so it isn't completely wrong and does simplify the function.
But, layout_type can fail if the size of the element type is not a multiple
of its alignment (i.e. overaligned types) and we then ICE because of that.

The following patch uses TYPE_MAIN_VARIANT in those cases instead, but only
for the types that need it, as for the diagnostics it is better to use the
typedef names etc. that were really used in the source if possible.

2021-02-18  Jakub Jelinek  <jakub@redhat.com>

PR middle-end/99109
* gimple-array-bounds.cc (build_zero_elt_array_type): Rename to ...
(build_printable_array_type): ... this.  Add nelts argument.  For
overaligned eltype, use TYPE_MAIN_VARIANT (eltype) instead.  If
nelts, call build_array_type_nelts.
(array_bounds_checker::check_mem_ref): Use build_printable_array_type
instead of build_zero_elt_array_type and build_array_type_nelts.

* g++.dg/warn/Warray-bounds-17.C: New test.
gcc/gimple-array-bounds.cc
gcc/testsuite/g++.dg/warn/Warray-bounds-17.C [new file with mode: 0644]