Author: stulova
Date: Thu Nov 29 06:11:15 2018
New Revision: 347865

URL: http://llvm.org/viewvc/llvm-project?rev=347865&view=rev
Log:
[OpenCL] Improve diags for addr spaces in templates

Fix ICEs on template instantiations that were leading to
the creation of invalid code patterns with address spaces.

Incorrect cases are now diagnosed properly.

Differential Revision: https://reviews.llvm.org/D54858


Added:
    cfe/trunk/test/SemaOpenCLCXX/address-space-templates.cl
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/lib/Sema/TreeTransform.h
    cfe/trunk/test/CodeGenOpenCLCXX/template-address-spaces.cl

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=347865&r1=347864&r2=347865&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Nov 29 06:11:15 2018
@@ -15148,22 +15148,6 @@ FieldDecl *Sema::HandleField(Scope *S, R
     }
   }
 
-  // TR 18037 does not allow fields to be declared with address spaces.
-  if (T.getQualifiers().hasAddressSpace() ||
-      T->isDependentAddressSpaceType() ||
-      T->getBaseElementTypeUnsafe()->isDependentAddressSpaceType()) {
-    Diag(Loc, diag::err_field_with_address_space);
-    D.setInvalidType();
-  }
-
-  // OpenCL v1.2 s6.9b,r & OpenCL v2.0 s6.12.5 - The following types cannot be
-  // used as structure or union field: image, sampler, event or block types.
-  if (LangOpts.OpenCL && (T->isEventT() || T->isImageType() ||
-                          T->isSamplerT() || T->isBlockPointerType())) {
-    Diag(Loc, diag::err_opencl_type_struct_or_union_field) << T;
-    D.setInvalidType();
-  }
-
   DiagnoseFunctionSpecifiers(D.getDeclSpec());
 
   if (D.getDeclSpec().isInlineSpecified())
@@ -15275,12 +15259,30 @@ FieldDecl *Sema::CheckFieldDecl(Declarat
     }
   }
 
-  // OpenCL v1.2 s6.9.c: bitfields are not supported.
-  if (BitWidth && getLangOpts().OpenCL) {
-    Diag(Loc, diag::err_opencl_bitfields);
+  // TR 18037 does not allow fields to be declared with address space
+  if (T.getQualifiers().hasAddressSpace() || T->isDependentAddressSpaceType() 
||
+      T->getBaseElementTypeUnsafe()->isDependentAddressSpaceType()) {
+    Diag(Loc, diag::err_field_with_address_space);
+    Record->setInvalidDecl();
     InvalidDecl = true;
   }
 
+  if (LangOpts.OpenCL) {
+    // OpenCL v1.2 s6.9b,r & OpenCL v2.0 s6.12.5 - The following types cannot 
be
+    // used as structure or union field: image, sampler, event or block types.
+    if (T->isEventT() || T->isImageType() || T->isSamplerT() ||
+        T->isBlockPointerType()) {
+      Diag(Loc, diag::err_opencl_type_struct_or_union_field) << T;
+      Record->setInvalidDecl();
+      InvalidDecl = true;
+    }
+    // OpenCL v1.2 s6.9.c: bitfields are not supported.
+    if (BitWidth) {
+      Diag(Loc, diag::err_opencl_bitfields);
+      InvalidDecl = true;
+    }
+  }
+
   // Anonymous bit-fields cannot be cv-qualified (CWG 2229).
   if (!InvalidDecl && getLangOpts().CPlusPlus && !II && BitWidth &&
       T.hasQualifiers()) {

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=347865&r1=347864&r2=347865&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Thu Nov 29 06:11:15 2018
@@ -7226,8 +7226,9 @@ static void deduceOpenCLImplicitAddrSpac
     if (IsPointee) {
       ImpAddr = LangAS::opencl_generic;
     } else {
-      if (D.getContext() == DeclaratorContext::TemplateArgContext) {
-        // Do not deduce address space for non-pointee type in template args
+      if (D.getContext() == DeclaratorContext::TemplateArgContext ||
+          T->isDependentType()) {
+        // Do not deduce address space for non-pointee type in templates.
       } else if (D.getContext() == DeclaratorContext::FileContext) {
         ImpAddr = LangAS::opencl_global;
       } else {

Modified: cfe/trunk/lib/Sema/TreeTransform.h
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/TreeTransform.h?rev=347865&r1=347864&r2=347865&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/TreeTransform.h (original)
+++ cfe/trunk/lib/Sema/TreeTransform.h Thu Nov 29 06:11:15 2018
@@ -5284,6 +5284,13 @@ QualType TreeTransform<Derived>::Transfo
     if (ResultType.isNull())
       return QualType();
 
+    // Return type can not be qualified with an address space.
+    if (ResultType.getAddressSpace() != LangAS::Default) {
+      SemaRef.Diag(TL.getReturnLoc().getBeginLoc(),
+                   diag::err_attribute_address_function_type);
+      return QualType();
+    }
+
     if (getDerived().TransformFunctionTypeParams(
             TL.getBeginLoc(), TL.getParams(),
             TL.getTypePtr()->param_type_begin(),

Modified: cfe/trunk/test/CodeGenOpenCLCXX/template-address-spaces.cl
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenOpenCLCXX/template-address-spaces.cl?rev=347865&r1=347864&r2=347865&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenOpenCLCXX/template-address-spaces.cl (original)
+++ cfe/trunk/test/CodeGenOpenCLCXX/template-address-spaces.cl Thu Nov 29 
06:11:15 2018
@@ -21,11 +21,8 @@ void bar(){
   S<int> sint;
   S<int*> sintptr;
   S<__global int*> sintptrgl;
-  // FIXME: Preserve AS in TreeTransform
-  //S<__global int> sintgl;
 
   sint.foo();
   sintptr.foo();
   sintptrgl.foo();
-  //sintgl.foo();
 }

Added: cfe/trunk/test/SemaOpenCLCXX/address-space-templates.cl
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaOpenCLCXX/address-space-templates.cl?rev=347865&view=auto
==============================================================================
--- cfe/trunk/test/SemaOpenCLCXX/address-space-templates.cl (added)
+++ cfe/trunk/test/SemaOpenCLCXX/address-space-templates.cl Thu Nov 29 06:11:15 
2018
@@ -0,0 +1,12 @@
+//RUN: %clang_cc1 %s -cl-std=c++ -pedantic -verify -fsyntax-only
+
+template <typename T>
+struct S {
+  T a;        // expected-error{{field may not be qualified with an address 
space}}
+  T f1();     // expected-error{{function type may not be qualified with an 
address space}}
+  void f2(T); // expected-error{{parameter may not be qualified with an 
address space}}
+};
+
+void bar() {
+  S<const __global int> sintgl; // expected-note{{in instantiation of template 
class 'S<const __global int>' requested here}}
+}


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to