[PATCH] D42530: Clang permits assignment to vector/extvector elements in a const method

Andrew V. Tischenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 05:14:10 PST 2018


avt77 created this revision.
avt77 added reviewers: RKSimon, spatel, rjmccall, dtemirbulatov, rnk.

This patch should close Bug 35951.


https://reviews.llvm.org/D42530

Files:
  lib/AST/ExprClassification.cpp
  lib/Sema/SemaExpr.cpp
  test/SemaCXX/err_typecheck_assign_const.cpp


Index: test/SemaCXX/err_typecheck_assign_const.cpp
===================================================================
--- test/SemaCXX/err_typecheck_assign_const.cpp
+++ test/SemaCXX/err_typecheck_assign_const.cpp
@@ -129,3 +129,21 @@
   Func &bar();
   bar()() = 0; // expected-error {{read-only variable is not assignable}}
 }
+
+typedef float float4 __attribute__((ext_vector_type(4)));
+struct OhNo {
+  float4 v;
+  void AssignMe() const { v.x = 1; } // expected-error {{read-only variable is not assignable}}
+};
+
+typedef float float4_2 __attribute__((__vector_size__(16)));
+struct OhNo2 {
+  float4_2 v;
+  void AssignMe() const { v[0] = 1; } // expected-error {{read-only variable is not assignable}}
+};
+
+struct OhNo3 {
+  float v[4];
+  void AssignMe() const { v[0] = 1; } // expected-error {{read-only variable is not assignable}}
+};
+
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -10321,7 +10321,7 @@
 /// 'const' due to being captured within a block?
 enum NonConstCaptureKind { NCCK_None, NCCK_Block, NCCK_Lambda };
 static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) {
-  assert(E->isLValue() && E->getType().isConstQualified());
+  assert(E->isLValue());
   E = E->IgnoreParens();
 
   // Must be a reference to a declaration from an enclosing scope.
Index: lib/AST/ExprClassification.cpp
===================================================================
--- lib/AST/ExprClassification.cpp
+++ lib/AST/ExprClassification.cpp
@@ -643,6 +643,14 @@
     if (R->hasConstFields())
       return Cl::CM_ConstQualifiedField;
 
+  if (const ExtVectorElementExpr *VE = dyn_cast<ExtVectorElementExpr>(E))
+    if (Ctx.getCanonicalType(VE->getBase()->getType()).isConstQualified())
+      return Cl::CM_ConstQualified;
+
+  if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
+    if (Ctx.getCanonicalType(ASE->getBase()->getType()).isConstQualified())
+      return Cl::CM_ConstQualified;
+
   return Cl::CM_Modifiable;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42530.131422.patch
Type: text/x-patch
Size: 2087 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180125/03c2c2dd/attachment.bin>


More information about the llvm-commits mailing list