Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / warn-cast-qual.c
blobec140180ffa12fada0742dbd8183e1b90555ce30
1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -Wcast-qual -verify %s
2 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -x c++ -fsyntax-only -Wcast-qual -verify %s
4 #include <stdint.h>
6 void foo(void) {
7 const char *const ptr = 0;
8 const char *const *ptrptr = 0;
9 char *const *ptrcptr = 0;
10 char **ptrptr2 = 0;
11 char *y = (char *)ptr; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
12 char **y1 = (char **)ptrptr; // expected-warning {{cast from 'const char *const *' to 'char **' drops const qualifier}}
13 const char **y2 = (const char **)ptrptr; // expected-warning {{cast from 'const char *const *' to 'const char **' drops const qualifier}}
14 char *const *y3 = (char *const *)ptrptr; // expected-warning {{cast from 'const char *const' to 'char *const' drops const qualifier}}
15 const char **y4 = (const char **)ptrcptr; // expected-warning {{cast from 'char *const *' to 'const char **' drops const qualifier}}
17 char *z = (char *)(uintptr_t)(const void *)ptr; // no warning
18 char *z1 = (char *)(const void *)ptr; // expected-warning {{cast from 'const void *' to 'char *' drops const qualifier}}
20 volatile char *vol = 0;
21 char *vol2 = (char *)vol; // expected-warning {{cast from 'volatile char *' to 'char *' drops volatile qualifier}}
22 const volatile char *volc = 0;
23 char *volc2 = (char *)volc; // expected-warning {{cast from 'const volatile char *' to 'char *' drops const and volatile qualifiers}}
25 int **intptrptr;
26 const int **intptrptrc = (const int **)intptrptr; // expected-warning {{cast from 'int **' to 'const int **' must have all intermediate pointers const qualified}}
27 volatile int **intptrptrv = (volatile int **)intptrptr; // expected-warning {{cast from 'int **' to 'volatile int **' must have all intermediate pointers const qualified}}
29 int *intptr;
30 const int *intptrc = (const int *)intptr; // no warning
32 const char **charptrptrc;
33 char **charptrptr = (char **)charptrptrc; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
35 const char *constcharptr;
36 char *charptr = (char *)constcharptr; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
37 const char *constcharptr2 = (char *)constcharptr; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
38 const char *charptr2 = (char *)charptr; // no warning
40 #ifdef __cplusplus
41 using CharPtr = char *;
42 using CharPtrPtr = char **;
43 using ConstCharPtrPtr = const char **;
44 using CharPtrConstPtr = char *const *;
46 char *fy = CharPtr(ptr); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
47 char **fy1 = CharPtrPtr(ptrptr); // expected-warning {{cast from 'const char *const *' to 'char **' drops const qualifier}}
48 const char **fy2 = ConstCharPtrPtr(ptrptr); // expected-warning {{cast from 'const char *const *' to 'const char **' drops const qualifier}}
49 char *const *fy3 = CharPtrConstPtr(ptrptr); // expected-warning {{cast from 'const char *const' to 'char *const' drops const qualifier}}
50 const char **fy4 = ConstCharPtrPtr(ptrcptr); // expected-warning {{cast from 'char *const *' to 'const char **' drops const qualifier}}
52 using ConstVoidPtr = const void *;
53 char *fz = CharPtr(uintptr_t(ConstVoidPtr(ptr))); // no warning
54 char *fz1 = CharPtr(ConstVoidPtr(ptr)); // expected-warning {{cast from 'const void *' to 'char *' drops const qualifier}}
56 char *fvol2 = CharPtr(vol); // expected-warning {{cast from 'volatile char *' to 'char *' drops volatile qualifier}}
57 char *fvolc2 = CharPtr(volc); // expected-warning {{cast from 'const volatile char *' to 'char *' drops const and volatile qualifiers}}
59 using ConstIntPtrPtr = const int **;
60 using VolitileIntPtrPtr = volatile int **;
61 const int **fintptrptrc = ConstIntPtrPtr(intptrptr); // expected-warning {{cast from 'int **' to 'ConstIntPtrPtr' (aka 'const int **') must have all intermediate pointers const qualified}}
62 volatile int **fintptrptrv = VolitileIntPtrPtr(intptrptr); // expected-warning {{cast from 'int **' to 'VolitileIntPtrPtr' (aka 'volatile int **') must have all intermediate pointers const qualified}}
64 using ConstIntPtr = const int *;
65 const int *fintptrc = ConstIntPtr(intptr); // no warning
67 char **fcharptrptr = CharPtrPtr(charptrptrc); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
69 char *fcharptr = CharPtr(constcharptr); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
70 const char *fcharptr2 = CharPtr(charptr); // no warning
71 #endif
74 void bar_0(void) {
75 struct C {
76 const int a;
77 int b;
80 const struct C S = {0, 0};
82 *(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
83 *(int *)(&S.b) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
85 #ifdef __cplusplus
86 using IntPtr = int *;
87 *(IntPtr(&S.a)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
88 *(IntPtr(&S.b)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
89 #endif
92 void bar_1(void) {
93 struct C {
94 const int a;
95 int b;
98 struct C S = {0, 0};
99 S.b = 0; // no warning
101 *(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
102 *(int *)(&S.b) = 0; // no warning
104 #ifdef __cplusplus
105 using IntPtr = int *;
106 *(IntPtr(&S.a)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
107 *(IntPtr(&S.b)) = 0; // no warning
108 #endif