[clang] b9f7f05 - [Clang] Fix qualifiers of ext vector components accessed via `->` (#244980)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 22 18:50:20 PST 2026
Author: Shilei Tian
Date: 2026-11-22T21:50:16-05:00
New Revision: b9f7f05497303e6ba2314562a6c2f465746cbf9f
URL: https://github.com/llvm/llvm-project/commit/b9f7f05497303e6ba2314562a6c2f465746cbf9f
DIFF: https://github.com/llvm/llvm-project/commit/b9f7f05497303e6ba2314562a6c2f465746cbf9f.diff
LOG: [Clang] Fix qualifiers of ext vector components accessed via `->` (#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.
Added:
clang/test/SemaCXX/ext-vector-component-qualifiers.cpp
Modified:
clang/lib/Sema/SemaExprMember.cpp
clang/test/Sema/ext_vector_components.c
Removed:
################################################################################
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;
+}
More information about the cfe-commits
mailing list