llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Shilei Tian (shiltian)

<details>
<summary>Changes</summary>

For `p-&gt;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-&gt;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 `-&gt;`. 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.


---
Full diff: https://github.com/llvm/llvm-project/pull/244980.diff


3 Files Affected:

- (modified) clang/lib/Sema/SemaExprMember.cpp (+3-3) 
- (modified) clang/test/Sema/ext_vector_components.c (+7) 
- (added) clang/test/SemaCXX/ext-vector-component-qualifiers.cpp (+18) 


``````````diff
diff --git a/clang/lib/Sema/SemaExprMember.cpp 
b/clang/lib/Sema/SemaExprMember.cpp
index 14248802a6c7d..36f3dd78edd46 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 0102edb01e756..94ac1c0fc9c7f 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 0000000000000..8de81a7d0d508
--- /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;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/244980
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to