Debian Codegraph @

clang/test/Sema/implicit-void-ptr-cast.c

raw
1 // RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -std=c23 -verify=c -Wimplicit-void-ptr-cast %s
2 // RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -std=c23 -verify=c -Wc++-compat %s
3 // RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -verify=cxx -x c++ %s
4 // RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -std=c23 -verify=good %s
5 // RUN: %clang_cc1 -fsyntax-only -Wno-alloc-size -std=c23 -verify=good -Wc++-compat -Wno-implicit-void-ptr-cast %s
6 // good-no-diagnostics
7
8 typedef __typeof__(sizeof(int)) size_t;
9 extern void *malloc(size_t);
10
11 void func(int *); // #func-param
12
13 void 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
35 int *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
40 void 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 macro
64 // expansion. This allows for things like NULL expanding to different token
65 // sequences depending on language mode, but applies to any macro that
66 // expands to a valid null pointer constant.
67 #if defined(__cplusplus)
68 #define NULL 0
69 #else
70 #define NULL ((void *)0)
71 #endif
72 #define SOMETHING_NOT_SPELLED_NULL nullptr
73 #define SOMETHING_THAT_IS_NOT_NULL (void *)12
74
75 char *ptr1 = NULL; // Ok
76 char *ptr2 = SOMETHING_NOT_SPELLED_NULL; // Ok
77 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; // Ok
81 ptr2 = SOMETHING_NOT_SPELLED_NULL; // Ok
82 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
86 void 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 }