[PATCH] D105987: [C++4OpenCL] NULL redefined as nullptr

Justas Janickas via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 14 08:08:28 PDT 2021


Topotuna created this revision.
Herald added subscribers: ldrumm, Anastasia, yaxunl.
Topotuna requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.

Fixes llvm.org/PR48098


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105987

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


Index: clang/test/SemaOpenCL/null_literal.cl
===================================================================
--- clang/test/SemaOpenCL/null_literal.cl
+++ clang/test/SemaOpenCL/null_literal.cl
@@ -1,31 +1,48 @@
-// RUN: %clang_cc1 -verify %s
-// RUN: %clang_cc1 -cl-std=CL1.0 -verify %s
-// RUN: %clang_cc1 -cl-std=CL1.1 -verify %s
-// RUN: %clang_cc1 -cl-std=CL1.2 -verify %s
-// RUN: %clang_cc1 -cl-std=CL2.0 -DCL20 -verify %s
-
-#define NULL ((void*)0)
+// RUN: %clang_cc1 -fdeclare-opencl-builtins -finclude-default-header -verify %s
+// RUN: %clang_cc1 -cl-std=CL1.0 -fdeclare-opencl-builtins -finclude-default-header -verify %s
+// 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 -DCL20 -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 at +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 at +4{{cannot initialize a variable of type '__constant int *__private' with an rvalue of type '__global void *'}}
+#else
+// expected-error at +2{{initializing '__constant int *__private' with an expression of type '__global void *' changes address space of pointer}}
+#endif
+constant int* ptr4 = (global void*)0;
 
 #ifdef CL20
 // 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 at +4{{cannot initialize a variable of type '__global int *__private' with an rvalue of type '__local void *'}}
+#else
+// expected-error at +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 at +4{{comparison of distinct pointer types ('__global int *' and '__local void *')}}
+#else
+// expected-error at +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;
 
Index: clang/lib/Headers/opencl-c-base.h
===================================================================
--- clang/lib/Headers/opencl-c-base.h
+++ clang/lib/Headers/opencl-c-base.h
@@ -164,7 +164,9 @@
 typedef double double16 __attribute__((ext_vector_type(16)));
 #endif
 
-#if defined(__OPENCL_CPP_VERSION__) || defined(__OPENCL_C_VERSION__)
+#if defined(__OPENCL_CPP_VERSION__)
+#define NULL nullptr
+#elif defined(__OPENCL_C_VERSION__)
 #define NULL ((void*)0)
 #endif
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105987.358610.patch
Type: text/x-patch
Size: 3548 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210714/689de97f/attachment.bin>


More information about the cfe-commits mailing list