[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / warn-cast-qual.c
blob609c1b8d31fa3a8e7722972cf2e9b302f5cd8781
1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -Wcast-qual -verify %s
2 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -Wcast-qual -verify %s -fexperimental-new-constant-interpreter
3 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -x c++ -fsyntax-only -Wcast-qual -verify %s -fexperimental-new-constant-interpreter
5 #include <stdint.h>
7 void foo(void) {
8 const char *const ptr = 0;
9 const char *const *ptrptr = 0;
10 char *const *ptrcptr = 0;
11 char **ptrptr2 = 0;
12 char *y = (char *)ptr; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
13 char **y1 = (char **)ptrptr; // expected-warning {{cast from 'const char *const *' to 'char **' drops const qualifier}}
14 const char **y2 = (const char **)ptrptr; // expected-warning {{cast from 'const char *const *' to 'const char **' drops const qualifier}}
15 char *const *y3 = (char *const *)ptrptr; // expected-warning {{cast from 'const char *const' to 'char *const' drops const qualifier}}
16 const char **y4 = (const char **)ptrcptr; // expected-warning {{cast from 'char *const *' to 'const char **' drops const qualifier}}
18 char *z = (char *)(uintptr_t)(const void *)ptr; // no warning
19 char *z1 = (char *)(const void *)ptr; // expected-warning {{cast from 'const void *' to 'char *' drops const qualifier}}
21 volatile char *vol = 0;
22 char *vol2 = (char *)vol; // expected-warning {{cast from 'volatile char *' to 'char *' drops volatile qualifier}}
23 const volatile char *volc = 0;
24 char *volc2 = (char *)volc; // expected-warning {{cast from 'const volatile char *' to 'char *' drops const and volatile qualifiers}}
26 int **intptrptr;
27 const int **intptrptrc = (const int **)intptrptr; // expected-warning {{cast from 'int **' to 'const int **' must have all intermediate pointers const qualified}}
28 volatile int **intptrptrv = (volatile int **)intptrptr; // expected-warning {{cast from 'int **' to 'volatile int **' must have all intermediate pointers const qualified}}
30 int *intptr;
31 const int *intptrc = (const int *)intptr; // no warning
33 const char **charptrptrc;
34 char **charptrptr = (char **)charptrptrc; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
36 const char *constcharptr;
37 char *charptr = (char *)constcharptr; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
38 const char *constcharptr2 = (char *)constcharptr; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
39 const char *charptr2 = (char *)charptr; // no warning
41 #ifdef __cplusplus
42 using CharPtr = char *;
43 using CharPtrPtr = char **;
44 using ConstCharPtrPtr = const char **;
45 using CharPtrConstPtr = char *const *;
47 char *fy = CharPtr(ptr); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
48 char **fy1 = CharPtrPtr(ptrptr); // expected-warning {{cast from 'const char *const *' to 'char **' drops const qualifier}}
49 const char **fy2 = ConstCharPtrPtr(ptrptr); // expected-warning {{cast from 'const char *const *' to 'const char **' drops const qualifier}}
50 char *const *fy3 = CharPtrConstPtr(ptrptr); // expected-warning {{cast from 'const char *const' to 'char *const' drops const qualifier}}
51 const char **fy4 = ConstCharPtrPtr(ptrcptr); // expected-warning {{cast from 'char *const *' to 'const char **' drops const qualifier}}
53 using ConstVoidPtr = const void *;
54 char *fz = CharPtr(uintptr_t(ConstVoidPtr(ptr))); // no warning
55 char *fz1 = CharPtr(ConstVoidPtr(ptr)); // expected-warning {{cast from 'const void *' to 'char *' drops const qualifier}}
57 char *fvol2 = CharPtr(vol); // expected-warning {{cast from 'volatile char *' to 'char *' drops volatile qualifier}}
58 char *fvolc2 = CharPtr(volc); // expected-warning {{cast from 'const volatile char *' to 'char *' drops const and volatile qualifiers}}
60 using ConstIntPtrPtr = const int **;
61 using VolitileIntPtrPtr = volatile int **;
62 const int **fintptrptrc = ConstIntPtrPtr(intptrptr); // expected-warning {{cast from 'int **' to 'ConstIntPtrPtr' (aka 'const int **') must have all intermediate pointers const qualified}}
63 volatile int **fintptrptrv = VolitileIntPtrPtr(intptrptr); // expected-warning {{cast from 'int **' to 'VolitileIntPtrPtr' (aka 'volatile int **') must have all intermediate pointers const qualified}}
65 using ConstIntPtr = const int *;
66 const int *fintptrc = ConstIntPtr(intptr); // no warning
68 char **fcharptrptr = CharPtrPtr(charptrptrc); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
70 char *fcharptr = CharPtr(constcharptr); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
71 const char *fcharptr2 = CharPtr(charptr); // no warning
72 #endif
75 void bar_0(void) {
76 struct C {
77 const int a;
78 int b;
81 const struct C S = {0, 0};
83 *(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
84 *(int *)(&S.b) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
86 #ifdef __cplusplus
87 using IntPtr = int *;
88 *(IntPtr(&S.a)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
89 *(IntPtr(&S.b)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
90 #endif
93 void bar_1(void) {
94 struct C {
95 const int a;
96 int b;
99 struct C S = {0, 0};
100 S.b = 0; // no warning
102 *(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
103 *(int *)(&S.b) = 0; // no warning
105 #ifdef __cplusplus
106 using IntPtr = int *;
107 *(IntPtr(&S.a)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
108 *(IntPtr(&S.b)) = 0; // no warning
109 #endif