diff options
author | Andrew Pinski <quic_apinski@quicinc.com> | 2024-02-20 13:38:28 -0800 |
---|---|---|
committer | Andrew Pinski <quic_apinski@quicinc.com> | 2024-05-07 15:42:36 -0700 |
commit | 4421d35167b3083e0f2e4c84c91fded09a30cf22 (patch) | |
tree | 6fd98bb87e60125555279c426457cdccfb7fc63a | |
parent | c9dd853680b12d9c9def5de61abde5d057c526ba (diff) | |
download | gcc-4421d35167b3083e0f2e4c84c91fded09a30cf22.zip gcc-4421d35167b3083e0f2e4c84c91fded09a30cf22.tar.gz gcc-4421d35167b3083e0f2e4c84c91fded09a30cf22.tar.bz2 |
c++/c-common: Fix convert_vector_to_array_for_subscript for qualified vector types [PR89224]
After r7-987-gf17a223de829cb, the access for the elements of a vector type would lose the qualifiers.
So if we had `constvector[0]`, the type of the element of the array would not have const on it.
This was due to a missing build_qualified_type for the inner type of the vector when building the array type.
We need to add back the call to build_qualified_type and now the access has the correct qualifiers. So the
overloads and even if it is a lvalue or rvalue is correctly done.
Note we correctly now reject the testcase gcc.dg/pr83415.c which was incorrectly accepted after r7-987-gf17a223de829cb.
Built and tested for aarch64-linux-gnu.
PR c++/89224
gcc/c-family/ChangeLog:
* c-common.cc (convert_vector_to_array_for_subscript): Call build_qualified_type
for the inner type.
gcc/cp/ChangeLog:
* constexpr.cc (cxx_eval_array_reference): Compare main variants
for the vector/array types instead of the types directly.
gcc/testsuite/ChangeLog:
* g++.dg/torture/vector-subaccess-1.C: New test.
* gcc.dg/pr83415.c: Change warning to error.
Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
-rw-r--r-- | gcc/c-family/c-common.cc | 7 | ||||
-rw-r--r-- | gcc/cp/constexpr.cc | 3 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/torture/vector-subaccess-1.C | 23 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/pr83415.c | 2 |
4 files changed, 32 insertions, 3 deletions
diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 032dcb4..aae998d 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -8964,6 +8964,7 @@ convert_vector_to_array_for_subscript (location_t loc, if (gnu_vector_type_p (TREE_TYPE (*vecp))) { tree type = TREE_TYPE (*vecp); + tree newitype; ret = !lvalue_p (*vecp); @@ -8978,8 +8979,12 @@ convert_vector_to_array_for_subscript (location_t loc, for function parameters. */ c_common_mark_addressable_vec (*vecp); + /* Make sure qualifiers are copied from the vector type to the new element + of the array type. */ + newitype = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); + *vecp = build1 (VIEW_CONVERT_EXPR, - build_array_type_nelts (TREE_TYPE (type), + build_array_type_nelts (newitype, TYPE_VECTOR_SUBPARTS (type)), *vecp); } diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 50f799d..bd72533 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -4424,7 +4424,8 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, if (!lval && TREE_CODE (ary) == VIEW_CONVERT_EXPR && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0))) - && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))) + && (TYPE_MAIN_VARIANT (TREE_TYPE (t)) + == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0)))))) ary = TREE_OPERAND (ary, 0); tree oldidx = TREE_OPERAND (t, 1); diff --git a/gcc/testsuite/g++.dg/torture/vector-subaccess-1.C b/gcc/testsuite/g++.dg/torture/vector-subaccess-1.C new file mode 100644 index 0000000..0c8958a --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/vector-subaccess-1.C @@ -0,0 +1,23 @@ +/* PR c++/89224 */ + +/* The access of `vector[i]` has the same qualifiers as the original + vector which was missing. */ + +typedef __attribute__((vector_size(16))) unsigned char Int8x8_t; + +template <class T> +void g(T &x) { + __builtin_abort(); +} +template <class T> +void g(const T &x) { + __builtin_exit(0); +} +void f(const Int8x8_t x) { + g(x[0]); +} +int main(void) +{ + Int8x8_t x ={}; + f(x); +} diff --git a/gcc/testsuite/gcc.dg/pr83415.c b/gcc/testsuite/gcc.dg/pr83415.c index 5934c16..2fc8503 100644 --- a/gcc/testsuite/gcc.dg/pr83415.c +++ b/gcc/testsuite/gcc.dg/pr83415.c @@ -7,6 +7,6 @@ int main (int argc, short *argv[]) { int i = argc; - y[i] = 7 - i; /* { dg-warning "read-only" } */ + y[i] = 7 - i; /* { dg-error "read-only" } */ return 0; } |