[PATCH] D46049: [OpenCL] Add constant address space to __func__ in AST

Anastasia Stulova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 26 10:33:09 PDT 2018


Anastasia updated this revision to Diff 144154.
Anastasia added a comment.

- Renamed test;
- Reformatted;
- Added constant in StringLiteral creation.


https://reviews.llvm.org/D46049

Files:
  lib/AST/Expr.cpp
  lib/Sema/SemaExpr.cpp
  test/SemaOpenCL/predefined-expr.cl


Index: test/SemaOpenCL/predefined-expr.cl
===================================================================
--- /dev/null
+++ test/SemaOpenCL/predefined-expr.cl
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 %s -verify
+
+void f() {
+  char *f1 = __func__;          //expected-error{{initializing 'char *' with an expression of type 'const __constant char *' changes address space of pointer}}
+  constant char *f2 = __func__; //expected-warning{{initializing '__constant char *' with an expression of type 'const __constant char [2]' discards qualifiers}}
+  constant const char *f3 = __func__;
+}
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -3052,6 +3052,8 @@
                                  /*Pascal*/ false, ResTy, Loc);
     } else {
       ResTy = Context.CharTy.withConst();
+      if (LangOpts.OpenCL)
+        ResTy = Context.getAddrSpaceQualType(ResTy, LangAS::opencl_constant);
       ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal,
                                            /*IndexTypeQuals*/ 0);
       SL = StringLiteral::Create(Context, Str, StringLiteral::Ascii,
Index: lib/AST/Expr.cpp
===================================================================
--- lib/AST/Expr.cpp
+++ lib/AST/Expr.cpp
@@ -858,6 +858,17 @@
   assert(C.getAsConstantArrayType(Ty) &&
          "StringLiteral must be of constant array type!");
 
+  // OpenCL v1.1 s6.5.3: a string literal is in the constant address space.
+  assert((!C.getLangOpts().OpenCL ||
+          (C.getLangOpts().OpenCL &&
+           (Ty.getAddressSpace() == LangAS::Default ||
+            Ty.getAddressSpace() == LangAS::opencl_constant))) &&
+         "StringLiteral must either have no address space or constant address "
+         "space!");
+
+  if (C.getLangOpts().OpenCL && Ty.getAddressSpace() == LangAS::Default)
+    Ty = C.getAddrSpaceQualType(Ty, LangAS::opencl_constant);
+
   // Allocate enough space for the StringLiteral plus an array of locations for
   // any concatenated string tokens.
   void *Mem =
@@ -881,7 +892,10 @@
   void *Mem =
       C.Allocate(sizeof(StringLiteral) + sizeof(SourceLocation) * (NumStrs - 1),
                  alignof(StringLiteral));
-  StringLiteral *SL = new (Mem) StringLiteral(QualType());
+  StringLiteral *SL = new (Mem) StringLiteral(
+      C.getLangOpts().OpenCL
+          ? C.getAddrSpaceQualType(QualType(), LangAS::opencl_constant)
+          : QualType());
   SL->CharByteWidth = 0;
   SL->Length = 0;
   SL->NumConcatenated = NumStrs;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46049.144154.patch
Type: text/x-patch
Size: 2595 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180426/a1c0e737/attachment.bin>


More information about the cfe-commits mailing list