From: Yaxun Liu Date: Fri, 20 May 2016 17:18:16 +0000 (+0000) Subject: [OpenCL] Allow explicit cast of 0 to event_t. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ad2e1d307d516a7f8a403bddf1baeb28861964a5;p=clang [OpenCL] Allow explicit cast of 0 to event_t. Patch by Aaron Enye Shi. Differential Revision: http://reviews.llvm.org/D17578 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@270238 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 42de37a4be..3692166a2f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -7821,6 +7821,8 @@ def err_sampler_argument_required : Error< "sampler_t variable required - got %0">; def err_wrong_sampler_addressspace: Error< "sampler type cannot be used with the __local and __global address space qualifiers">; +def error_opencl_cast_non_zero_to_event_t : Error< + "cannot cast non-zero value '%0' to 'event_t'">; def err_opencl_global_invalid_addr_space : Error< "%select{program scope|static local|extern}0 variable must reside in %1 address space">; def err_missing_actual_pipe_type : Error< diff --git a/lib/Sema/SemaCast.cpp b/lib/Sema/SemaCast.cpp index 1d95b1f9f1..7239d44ca2 100644 --- a/lib/Sema/SemaCast.cpp +++ b/lib/Sema/SemaCast.cpp @@ -2441,6 +2441,22 @@ void CastOperation::CheckCStyleCast() { return; } + // OpenCL v2.0 s6.13.10 - Allow casts from '0' to event_t type. + if (Self.getLangOpts().OpenCL && DestType->isEventT()) { + llvm::APSInt CastInt; + if (SrcExpr.get()->EvaluateAsInt(CastInt, Self.Context)) { + if (0 == CastInt) { + Kind = CK_ZeroToOCLEvent; + return; + } + Self.Diag(OpRange.getBegin(), + diag::error_opencl_cast_non_zero_to_event_t) + << CastInt.toString(10) << SrcExpr.get()->getSourceRange(); + SrcExpr = ExprError(); + return; + } + } + // Reject any other conversions to non-scalar types. Self.Diag(OpRange.getBegin(), diag::err_typecheck_cond_expect_scalar) << DestType << SrcExpr.get()->getSourceRange(); diff --git a/test/CodeGenOpenCL/event_t.cl b/test/CodeGenOpenCL/event_t.cl index a84d8bb610..aad441f35f 100644 --- a/test/CodeGenOpenCL/event_t.cl +++ b/test/CodeGenOpenCL/event_t.cl @@ -8,5 +8,7 @@ void kernel ker() { foo(e); // CHECK: call {{.*}}void @foo(%opencl.event_t* % foo(0); +// CHECK: call {{.*}}void @foo(%opencl.event_t* null) + foo((event_t)0); // CHECK: call {{.*}}void @foo(%opencl.event_t* null) } diff --git a/test/SemaOpenCL/event_t.cl b/test/SemaOpenCL/event_t.cl index 3c4b45b923..990c063409 100644 --- a/test/SemaOpenCL/event_t.cl +++ b/test/SemaOpenCL/event_t.cl @@ -14,5 +14,6 @@ void kernel ker(event_t argevt) { // expected-error {{'event_t' cannot be used a foo(e); foo(0); foo(5); // expected-error {{passing 'int' to parameter of incompatible type 'event_t'}} + foo((event_t)1); // expected-error {{cannot cast non-zero value '1' to 'event_t'}} }