[OpenCL] NULL redefined as nullptr in C++ mode.
authorAnastasia Stulova <anastasia.stulova@arm.com>
Tue, 27 Jul 2021 15:27:36 +0000 (16:27 +0100)
committerAnastasia Stulova <anastasia.stulova@arm.com>
Tue, 27 Jul 2021 15:33:50 +0000 (16:33 +0100)
Redefines NULL as nullptr instead of ((void*)0)
in C++ for OpenCL.

Such internal representation of NULL provides
compatibility with C++11 and later language
standards.

Patch by Topotuna (Justas Janickas)!

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

clang/lib/Headers/opencl-c-base.h
clang/test/SemaOpenCL/null_literal.cl

index b5029a254b7a800d7ad8065175dd6b25ee82a018..3c5e2c973936824309f8b5491a2568c47d2f0fc2 100644 (file)
@@ -169,7 +169,11 @@ typedef double double8 __attribute__((ext_vector_type(8)));
 typedef double double16 __attribute__((ext_vector_type(16)));
 #endif
 
+#if defined(__OPENCL_CPP_VERSION__)
+#define NULL nullptr
+#elif defined(__OPENCL_C_VERSION__)
 #define NULL ((void*)0)
+#endif
 
 /**
  * Value of maximum non-infinite single-precision floating-point
index f4d55d70aaf89f80e5d9dc11bf97c9be08203aa1..b641c6bd7c6dd54e71ffec96a68600685d42f056 100644 (file)
@@ -2,27 +2,46 @@
 // RUN: %clang_cc1 -cl-std=CL1.1 -fdeclare-opencl-builtins -finclude-default-header -verify %s
 // RUN: %clang_cc1 -cl-std=CL1.2 -fdeclare-opencl-builtins -finclude-default-header -verify %s
 // RUN: %clang_cc1 -cl-std=CL2.0 -fdeclare-opencl-builtins -finclude-default-header -verify %s
+// RUN: %clang_cc1 -cl-std=clc++ -fdeclare-opencl-builtins -finclude-default-header -verify %s
 
 void foo(){
 
 global int* ptr1 = NULL;
 
+#if defined(__OPENCL_CPP_VERSION__)
+// expected-error@+2{{cannot initialize a variable of type '__global int *__private' with an rvalue of type '__global void *'}}
+#endif
 global int* ptr2 = (global void*)0;
 
 constant int* ptr3 = NULL;
 
-constant int* ptr4 = (global void*)0; // expected-error{{initializing '__constant int *__private' with an expression of type '__global void *' changes address space of pointer}}
+#if defined(__OPENCL_CPP_VERSION__)
+// expected-error@+4{{cannot initialize a variable of type '__constant int *__private' with an rvalue of type '__global void *'}}
+#else
+// expected-error@+2{{initializing '__constant int *__private' with an expression of type '__global void *' changes address space of pointer}}
+#endif
+constant int* ptr4 = (global void*)0;
 
 #if __OPENCL_C_VERSION__ == CL_VERSION_2_0
 // Accept explicitly pointer to generic address space in OpenCL v2.0.
 global int* ptr5 = (generic void*)0;
 #endif
 
-global int* ptr6 = (local void*)0; // expected-error{{initializing '__global int *__private' with an expression of type '__local void *' changes address space of pointer}}
+#if defined(__OPENCL_CPP_VERSION__)
+// expected-error@+4{{cannot initialize a variable of type '__global int *__private' with an rvalue of type '__local void *'}}
+#else
+// expected-error@+2{{initializing '__global int *__private' with an expression of type '__local void *' changes address space of pointer}}
+#endif
+global int* ptr6 = (local void*)0;
 
 bool cmp = ptr1 == NULL;
 
-cmp = ptr1 == (local void*)0; // expected-error{{comparison between  ('__global int *' and '__local void *') which are pointers to non-overlapping address spaces}}
+#if defined(__OPENCL_CPP_VERSION__)
+// expected-error@+4{{comparison of distinct pointer types ('__global int *' and '__local void *')}}
+#else
+// expected-error@+2{{comparison between  ('__global int *' and '__local void *') which are pointers to non-overlapping address spaces}}
+#endif
+cmp = ptr1 == (local void*)0;
 
 cmp = ptr3 == NULL;