[PATCH] Fix assertion failure with constant lvalues.

John Garvin jgarvin at apple.com
Fri May 15 17:22:38 PDT 2015


(Resubmitted with Phabricator for easier reviewing.)

This fixes an assertion failure in assignment if an lvalue is constant but its
type is not const-qualified.

An expression may be MLV_ConstQualified either because the type is actually
“const” or because it’s in the OpenCL constant address space (see
ExprClassification.cpp:608). Unfortunately, isReferenceToConstCapture asserts
that the type must actually be “const”, which is not necessarily true. This
patch changes the assertion to what I think is intended.

http://reviews.llvm.org/D9805

Files:
  lib/Sema/SemaExpr.cpp
  test/Sema/invalid-assignment-constant-address-space.c

Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -8954,7 +8954,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->isModifiableLvalue(S.getASTContext()) == Expr::MLV_ConstQualified);
   E = E->IgnoreParens();
 
   // Must be a reference to a declaration from an enclosing scope.
Index: test/Sema/invalid-assignment-constant-address-space.c
===================================================================
--- /dev/null
+++ test/Sema/invalid-assignment-constant-address-space.c
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
+
+#define OPENCL_CONSTANT 16776962
+int __attribute__((address_space(OPENCL_CONSTANT))) c[3] = {0};
+
+void foo() {
+  c[0] = 1; //expected-error{{read-only variable is not assignable}}
+}

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9805.25908.patch
Type: text/x-patch
Size: 1065 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150516/78b239a2/attachment.bin>


More information about the cfe-commits mailing list