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
3
4 #include <stdint.h>
5
foo()6 void foo() {
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}}
16
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}}
19
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}}
24
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}}
28
29 int *intptr;
30 const int *intptrc = (const int *)intptr; // no warning
31
32 const char **charptrptrc;
33 char **charptrptr = (char **)charptrptrc; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
34
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
39 }
40
bar_0()41 void bar_0() {
42 struct C {
43 const int a;
44 int b;
45 };
46
47 const struct C S = {0, 0};
48
49 *(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
50 *(int *)(&S.b) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
51 }
52
bar_1()53 void bar_1() {
54 struct C {
55 const int a;
56 int b;
57 };
58
59 struct C S = {0, 0};
60 S.b = 0; // no warning
61
62 *(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
63 *(int *)(&S.b) = 0; // no warning
64 }
65