Diagnose loads of 'half' l-values in OpenCL.
authorJohn McCall <rjmccall@apple.com>
Tue, 12 Feb 2013 01:29:43 +0000 (01:29 +0000)
committerJohn McCall <rjmccall@apple.com>
Tue, 12 Feb 2013 01:29:43 +0000 (01:29 +0000)
Patch by Joey Gouly!

llvm-svn: 174928

clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaCast.cpp
clang/lib/Sema/SemaExpr.cpp
clang/lib/Sema/SemaType.cpp
clang/test/SemaOpenCL/half.cl

index b2f28d5..44bcf63 100644 (file)
@@ -420,12 +420,9 @@ def err_object_cannot_be_passed_returned_by_value : Error<
   "; did you forget * in %1?">;
 def err_parameters_retval_cannot_have_fp16_type : Error<
   "%select{parameters|function return value}0 cannot have __fp16 type; did you forget * ?">;
-def err_opencl_half_dereferencing : Error<
-  "dereferencing pointer of type %0 is not allowed">;
-def err_opencl_half_subscript : Error<
-  "subscript to array of type %0 is not allowed">;
+def err_opencl_half_load_store : Error<
+  "%select{loading directly from|assigning directly to}0 pointer to type %1 is not allowed">;
 def err_opencl_cast_to_half : Error<"casting to type %0 is not allowed">;
-def err_opencl_cast_from_half : Error<"casting from type %0 is not allowed">;
 def err_opencl_half_declaration : Error<
   "declaring variable of type %0 is not allowed">;
 def err_opencl_half_argument : Error<
index d1e95d7..e6dc0bd 100644 (file)
@@ -2111,12 +2111,6 @@ void CastOperation::CheckCStyleCast() {
       SrcExpr = ExprError();
       return;
     }
-    if (SrcExpr.get()->getType()->isHalfType()) {
-      Self.Diag(SrcExpr.get()->getLocStart(), diag::err_opencl_cast_from_half)
-        << SrcType << SrcExpr.get()->getSourceRange();
-      SrcExpr = ExprError();
-      return;
-    }
   }
 
   // ARC imposes extra restrictions on casts.
index d0aa697..689c149 100644 (file)
@@ -482,6 +482,14 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) {
   if (T->isVoidType())
     return Owned(E);
 
+  // OpenCL usually rejects direct accesses to values of 'half' type.
+  if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp16 &&
+      T->isHalfType()) {
+    Diag(E->getExprLoc(), diag::err_opencl_half_load_store)
+      << 0 << T;
+    return ExprError();
+  }
+
   CheckForNullPointerDereference(*this, E);
 
   // C++ [conv.lval]p1:
@@ -3487,13 +3495,6 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc,
                           diag::err_subscript_incomplete_type, BaseExpr))
     return ExprError();
 
-  if (ResultType->isHalfType() && getLangOpts().OpenCL &&
-      !getOpenCLOptions().cl_khr_fp16) {
-    Diag(BaseExpr->getLocStart(), diag::err_opencl_half_subscript) << ResultType
-      << BaseExpr->getType() << BaseExpr->getSourceRange();
-    return ExprError();
-  }
-
   assert(VK == VK_RValue || LangOpts.CPlusPlus ||
          !ResultType.isCForbiddenLValueType());
 
@@ -5663,7 +5664,6 @@ Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS,
   LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType();
   RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType();
 
-
   // Common case: no conversion required.
   if (LHSType == RHSType) {
     Kind = CK_NoOp;
@@ -8242,13 +8242,6 @@ static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK,
     return QualType();
   }
 
-  if (Result->isHalfType() && S.getLangOpts().OpenCL &&
-      !S.getOpenCLOptions().cl_khr_fp16) {
-    S.Diag(OpLoc, diag::err_opencl_half_dereferencing)
-      << OpTy << Op->getSourceRange();
-    return QualType();
-  }
-
   // Dereferences are usually l-values...
   VK = VK_LValue;
 
index b257a59..109c8e4 100644 (file)
@@ -2642,6 +2642,7 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
                 S.Diag(Param->getLocation(),
                   diag::err_opencl_half_argument) << ArgTy;
                 D.setInvalidType();
+                Param->setInvalidDecl();
               }
             } else {
               S.Diag(Param->getLocation(),
index 830d178..0e6acb7 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only
+// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -Wno-unused-value
 
 #pragma OPENCL EXTENSION cl_khr_fp16 : disable
 
@@ -7,16 +7,15 @@ half half_disabled(half *p, // expected-error{{declaring function return value o
 {
   half a[2]; // expected-error{{declaring variable of type 'half [2]' is not allowed}}
   half b;    // expected-error{{declaring variable of type 'half' is not allowed}}
-
-  b = *p;    // expected-error{{dereferencing pointer of type 'half *' is not allowed}}
-  *p = b;    // expected-error{{dereferencing pointer of type 'half *' is not allowed}}
-
-  b = p[1];  // expected-error {{subscript to array of type 'half' is not allowed}}
-  p[1] = b;  // expected-error {{subscript to array of type 'half' is not allowed}}
+  *p; // expected-error{{loading directly from pointer to type 'half' is not allowed}}
+  p[1]; // expected-error{{loading directly from pointer to type 'half' is not allowed}}
 
   float c = 1.0f;
   b = (half) c;  // expected-error{{casting to type 'half' is not allowed}}
-  c = (float) h; // expected-error{{casting from type 'half' is not allowed}}
+
+  half *allowed = &p[1];
+  half *allowed2 = &*p;
+  half *allowed3 = p + 1;
 
   return h;
 }
@@ -27,16 +26,15 @@ half half_enabled(half *p, half h)
 {
   half a[2];
   half b;
-
-  b = *p;
-  *p = b;
-
-  b = p[1];
-  p[1] = b;
+  *p;
+  p[1];
 
   float c = 1.0f;
   b = (half) c;
-  c = (float) h;
+
+  half *allowed = &p[1];
+  half *allowed2 = &*p;
+  half *allowed3 = p + 1;
 
   return h;
 }