https://github.com/shiltian created https://github.com/llvm/llvm-project/pull/244980
For `p->x` where `p` points to an `ext_vector_type` vector, Sema takes the qualifiers from the type of `p`, which is the pointer itself, instead of the vector it points to. As a result, `p->x = 1` is accepted even if `p` points to a `const` vector, while `(*p).x = 1` is rejected. An address space on the vector is lost in the same way. This PR takes the qualifiers from the vector for both `.` and `->`. A swizzle that repeats a component, e.g. `v.xx`, is a computed value, so it doesn't get any qualifiers. This PR was assisted by AI but I reviewed all the changes. >From 24c4b81ae7a6cca386d6015f8b2a073ccc2b1c76 Mon Sep 17 00:00:00 2001 From: Shilei Tian <[email protected]> Date: Sat, 21 Nov 2026 18:43:24 -0500 Subject: [PATCH] [Clang] Fix qualifiers of ext vector components accessed via `->` For `p->x` where `p` points to an `ext_vector_type` vector, Sema takes the qualifiers from the type of `p`, which is the pointer itself, instead of the vector it points to. As a result, `p->x = 1` is accepted even if `p` points to a `const` vector, while `(*p).x = 1` is rejected. An address space on the vector is lost in the same way. This PR takes the qualifiers from the vector for both `.` and `->`. A swizzle that repeats a component, e.g. `v.xx`, is a computed value, so it doesn't get any qualifiers. This PR was assisted by AI but I reviewed all the changes. --- clang/lib/Sema/SemaExprMember.cpp | 6 +++--- clang/test/Sema/ext_vector_components.c | 7 +++++++ .../ext-vector-component-qualifiers.cpp | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 clang/test/SemaCXX/ext-vector-component-qualifiers.cpp diff --git a/clang/lib/Sema/SemaExprMember.cpp b/clang/lib/Sema/SemaExprMember.cpp index 14248802a6c7d1..36f3dd78edd466 100644 --- a/clang/lib/Sema/SemaExprMember.cpp +++ b/clang/lib/Sema/SemaExprMember.cpp @@ -1640,9 +1640,9 @@ static ExprResult LookupMemberExpr(Sema &S, LookupResult &R, Member, MemberLoc); if (ret.isNull()) return ExprError(); - Qualifiers BaseQ = - S.Context.getCanonicalType(BaseExpr.get()->getType()).getQualifiers(); - ret = S.Context.getQualifiedType(ret, BaseQ); + + if (VK != VK_PRValue) + ret = S.Context.getQualifiedType(ret, BaseType.getQualifiers()); return new (S.Context) ExtVectorElementExpr(ret, VK, BaseExpr.get(), *Member, MemberLoc); diff --git a/clang/test/Sema/ext_vector_components.c b/clang/test/Sema/ext_vector_components.c index 0102edb01e7560..94ac1c0fc9c7f1 100644 --- a/clang/test/Sema/ext_vector_components.c +++ b/clang/test/Sema/ext_vector_components.c @@ -72,3 +72,10 @@ float2 lo(float3 x) { return x.lo; } float2 hi(float3 x) { return x.hi; } float2 ev(float3 x) { return x.even; } float2 od(float3 x) { return x.odd; } + +// A component gets the qualifiers of the vector, also when accessed via '->'. +void const_vector(const float4 *p, float2 v) { + (*p).x = 1; // expected-error {{read-only variable is not assignable}} + p->x = 1; // expected-error {{read-only variable is not assignable}} + p->xy = v; // expected-error {{read-only variable is not assignable}} +} diff --git a/clang/test/SemaCXX/ext-vector-component-qualifiers.cpp b/clang/test/SemaCXX/ext-vector-component-qualifiers.cpp new file mode 100644 index 00000000000000..8de81a7d0d5081 --- /dev/null +++ b/clang/test/SemaCXX/ext-vector-component-qualifiers.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +// expected-no-diagnostics + +typedef float float2 __attribute__((ext_vector_type(2))); +typedef __attribute__((address_space(3))) float2 LocalFloat2; + +// A component gets the qualifiers of the vector, also via '->'. A swizzle that +// repeats a component is a computed value, so it doesn't get any qualifiers. +void f(const float2 *p, LocalFloat2 *q, const float2 v) { + static_assert(__is_same(decltype((p->x)), const float &), ""); + static_assert(__is_same(decltype((v.x)), const float &), ""); + static_assert(__is_same(decltype((q->x)), __attribute__((address_space(3))) float &), ""); + static_assert(__is_same(decltype(p->xx), float2), ""); + static_assert(__is_same(decltype(v.xx), float2), ""); + static_assert(__is_same(decltype(q->xx), float2), ""); + const float2 &r = q->xx; + (void)r; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
