97 lines · c
1// RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -std=c23 -verify=c -Wimplicit-void-ptr-cast %s2// RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -std=c23 -verify=c -Wc++-compat %s3// RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -verify=cxx -x c++ %s4// RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -std=c23 -verify=good %s5// RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -std=c23 -verify=good -Wc++-compat -Wno-implicit-void-ptr-cast %s6// good-no-diagnostics7 8typedef __typeof__(sizeof(int)) size_t;9extern void *malloc(size_t);10 11void func(int *); // #func-param12 13void test(void) {14 int *x = malloc(sizeof(char)); // c-warning {{implicit conversion when initializing 'int *' with an expression of type 'void *' is not permitted in C++}} \15 cxx-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'void *'}}16 x = malloc(sizeof(char)); // c-warning {{implicit conversion when assigning to 'int *' from type 'void *' is not permitted in C++}} \17 cxx-error {{assigning to 'int *' from incompatible type 'void *'}}18 func(malloc(sizeof(char))); // c-warning {{implicit conversion when passing 'void *' to parameter of type 'int *' is not permitted in C++}} \19 c-note@#func-param {{passing argument to parameter here}} \20 cxx-error {{no matching function for call to 'func'}} \21 cxx-note@#func-param {{candidate function not viable: cannot convert argument of incomplete type 'void *' to 'int *' for 1st argument}}22 x = (int *)malloc(sizeof(char));23 24 void *vp = 0;25 x = vp; // c-warning {{implicit conversion when assigning to 'int *' from type 'void *' is not permitted in C++}} \26 cxx-error {{assigning to 'int *' from incompatible type 'void *'}}27 vp = vp;28 29 x = (void *)malloc(sizeof(char)); // c-warning {{implicit conversion when assigning to 'int *' from type 'void *' is not permitted in C++}} \30 cxx-error {{assigning to 'int *' from incompatible type 'void *'}}31 const int *y = vp; // c-warning {{implicit conversion when initializing 'const int *' with an expression of type 'void *' is not permitted in C++}} \32 cxx-error {{cannot initialize a variable of type 'const int *' with an lvalue of type 'void *'}}33}34 35int *other_func(void *ptr) {36 return ptr; // c-warning {{implicit conversion when returning 'void *' from a function with result type 'int *' is not permitted in C++}} \37 cxx-error {{cannot initialize return object of type 'int *' with an lvalue of type 'void *'}}38}39 40void more(void) {41 __attribute__((address_space(0))) char *b1 = (void *)0; // c-warning {{implicit conversion when initializing '__attribute__((address_space(0))) char *' with an expression of type 'void *' is not permitted in C++}} \42 cxx-error {{cannot initialize a variable of type '__attribute__((address_space(0))) char *' with an rvalue of type 'void *'}}43 __attribute__((address_space(0))) void *b2 = (void *)0; // c-warning {{implicit conversion when initializing '__attribute__((address_space(0))) void *' with an expression of type 'void *' is not permitted in C++}} \44 cxx-error {{cannot initialize a variable of type '__attribute__((address_space(0))) void *' with an rvalue of type 'void *'}}45 char *b3 = (void *)0; // c-warning {{implicit conversion when initializing 'char *' with an expression of type 'void *' is not permitted in C++}} \46 cxx-error {{cannot initialize a variable of type 'char *' with an rvalue of type 'void *'}}47 48 b1 = (void*)0; // c-warning {{implicit conversion when assigning to '__attribute__((address_space(0))) char *' from type 'void *' is not permitted in C++}} \49 cxx-error {{assigning 'void *' to '__attribute__((address_space(0))) char *' changes address space of pointer}}50 51 b2 = (void*)0; // c-warning {{implicit conversion when assigning to '__attribute__((address_space(0))) void *' from type 'void *' is not permitted in C++}} \52 cxx-error {{assigning 'void *' to '__attribute__((address_space(0))) void *' changes address space of pointer}}53 b2 = (__attribute__((address_space(0))) void *)0;54 b2 = nullptr;55 b2 = 0;56 57 b3 = (void*)0; // c-warning {{implicit conversion when assigning to 'char *' from type 'void *' is not permitted in C++}} \58 cxx-error {{assigning to 'char *' from incompatible type 'void *'}}59 b3 = (char *)0;60 b3 = nullptr;61 b3 = 0;62 63 // Note that we explicitly silence the diagnostic if the RHS is from a macro64 // expansion. This allows for things like NULL expanding to different token65 // sequences depending on language mode, but applies to any macro that66 // expands to a valid null pointer constant.67#if defined(__cplusplus)68 #define NULL 069#else70 #define NULL ((void *)0)71#endif72 #define SOMETHING_NOT_SPELLED_NULL nullptr73 #define SOMETHING_THAT_IS_NOT_NULL (void *)1274 75 char *ptr1 = NULL; // Ok76 char *ptr2 = SOMETHING_NOT_SPELLED_NULL; // Ok77 char *ptr3 = SOMETHING_THAT_IS_NOT_NULL; // c-warning {{implicit conversion when initializing 'char *' with an expression of type 'void *' is not permitted in C++}} \78 cxx-error {{cannot initialize a variable of type 'char *' with an rvalue of type 'void *'}}79 80 ptr1 = NULL; // Ok81 ptr2 = SOMETHING_NOT_SPELLED_NULL; // Ok82 ptr3 = SOMETHING_THAT_IS_NOT_NULL; // c-warning {{implicit conversion when assigning to 'char *' from type 'void *' is not permitted in C++}} \83 cxx-error {{assigning to 'char *' from incompatible type 'void *'}}84}85 86void gh154157(void) {87 #define ATOMIC_VAR_INIT(value) (value)88 89 typedef const struct T * T_Ref;90 static T_Ref _Atomic x = ATOMIC_VAR_INIT((void*)NULL); // c-warning {{implicit conversion when initializing '_Atomic(T_Ref)' with an expression of type 'void *' is not permitted in C++}} \91 cxx-error {{cannot initialize a variable of type '_Atomic(T_Ref)' with an rvalue of type 'void *'}}92 static T_Ref const y = ATOMIC_VAR_INIT((void*)NULL); // c-warning {{implicit conversion when initializing 'const T_Ref' (aka 'const struct T *const') with an expression of type 'void *' is not permitted in C++}} \93 cxx-error {{cannot initialize a variable of type 'const T_Ref' (aka 'const struct T *const') with an rvalue of type 'void *'}}94 static T_Ref z = ATOMIC_VAR_INIT((void*)NULL); // c-warning {{implicit conversion when initializing 'T_Ref' (aka 'const struct T *') with an expression of type 'void *' is not permitted in C++}} \95 cxx-error {{cannot initialize a variable of type 'T_Ref' (aka 'const struct T *') with an rvalue of type 'void *'}}96}97