Debian Codegraph @

clang/test/Sema/cast.c

raw
1 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown %s -verify -Wvla
2
3 int array[(long)(char *)0]; // expected-warning {{variable length array used}} \
4 // expected-warning {{variable length array folded to constant array as an extension}} \
5 // expected-note {{this conversion is not allowed in a constant expression}}
6
7 typedef struct { unsigned long bits[(((1) + (64) - 1) / (64))]; } cpumask_t;
8 cpumask_t x;
9 void foo(void) {
10 (void)x;
11 }
12 void bar(void) {
13 char* a;
14 double b;
15 b = (double)a; // expected-error {{pointer cannot be cast to type}}
16 a = (char*)b; // expected-error {{cannot be cast to a pointer type}}
17 }
18
19 long bar1(long *next) {
20 return (long)(*next)++;
21 }
22
23 typedef _Bool Bool;
24 typedef int Int;
25 typedef long Long;
26 typedef float Float;
27 typedef double Double;
28 typedef _Complex int CInt;
29 typedef _Complex long CLong;
30 typedef _Complex float CFloat;
31 typedef _Complex double CDouble;
32 typedef void *VoidPtr;
33 typedef char *CharPtr;
34
35 void testBool(Bool v) {
36 (void) (Bool) v;
37 (void) (Int) v;
38 (void) (Long) v;
39 (void) (Float) v;
40 (void) (Double) v;
41 (void) (CInt) v;
42 (void) (CLong) v;
43 (void) (CFloat) v;
44 (void) (CDouble) v;
45 (void) (VoidPtr) v;
46 (void) (CharPtr) v;
47 }
48
49 void testInt(Int v) {
50 (void) (Bool) v;
51 (void) (Int) v;
52 (void) (Long) v;
53 (void) (Float) v;
54 (void) (Double) v;
55 (void) (CInt) v;
56 (void) (CLong) v;
57 (void) (CFloat) v;
58 (void) (CDouble) v;
59 (void) (VoidPtr) v; // expected-warning{{cast to 'VoidPtr' (aka 'void *') from smaller integer type 'Int' (aka 'int')}}
60 (void) (CharPtr) v; // expected-warning{{cast to 'CharPtr' (aka 'char *') from smaller integer type 'Int' (aka 'int')}}
61
62 // Test that casts to void* can be controlled separately
63 // from other -Wint-to-pointer-cast warnings.
64 #pragma clang diagnostic push
65 #pragma clang diagnostic ignored "-Wint-to-void-pointer-cast"
66 (void) (VoidPtr) v; // no-warning
67 (void) (CharPtr) v; // expected-warning{{cast to 'CharPtr' (aka 'char *') from smaller integer type 'Int' (aka 'int')}}
68 #pragma clang diagnostic pop
69 }
70
71 void testLong(Long v) {
72 (void) (Bool) v;
73 (void) (Int) v;
74 (void) (Long) v;
75 (void) (Float) v;
76 (void) (Double) v;
77 (void) (CInt) v;
78 (void) (CLong) v;
79 (void) (CFloat) v;
80 (void) (CDouble) v;
81 (void) (VoidPtr) v;
82 (void) (CharPtr) v;
83 }
84
85 void testFloat(Float v) {
86 (void) (Bool) v;
87 (void) (Int) v;
88 (void) (Long) v;
89 (void) (Float) v;
90 (void) (Double) v;
91 (void) (CInt) v;
92 (void) (CLong) v;
93 (void) (CFloat) v;
94 (void) (CDouble) v;
95 }
96
97 void testDouble(Double v) {
98 (void) (Bool) v;
99 (void) (Int) v;
100 (void) (Long) v;
101 (void) (Float) v;
102 (void) (Double) v;
103 (void) (CInt) v;
104 (void) (CLong) v;
105 (void) (CFloat) v;
106 (void) (CDouble) v;
107 }
108
109 void testCI(CInt v) {
110 (void) (Bool) v;
111 (void) (Int) v;
112 (void) (Long) v;
113 (void) (Float) v;
114 (void) (Double) v;
115 (void) (CInt) v;
116 (void) (CLong) v;
117 (void) (CFloat) v;
118 (void) (CDouble) v;
119 }
120
121 void testCLong(CLong v) {
122 (void) (Bool) v;
123 (void) (Int) v;
124 (void) (Long) v;
125 (void) (Float) v;
126 (void) (Double) v;
127 (void) (CInt) v;
128 (void) (CLong) v;
129 (void) (CFloat) v;
130 (void) (CDouble) v;
131 }
132
133 void testCFloat(CFloat v) {
134 (void) (Bool) v;
135 (void) (Int) v;
136 (void) (Long) v;
137 (void) (Float) v;
138 (void) (Double) v;
139 (void) (CInt) v;
140 (void) (CLong) v;
141 (void) (CFloat) v;
142 (void) (CDouble) v;
143 }
144
145 void testCDouble(CDouble v) {
146 (void) (Bool) v;
147 (void) (Int) v;
148 (void) (Long) v;
149 (void) (Float) v;
150 (void) (Double) v;
151 (void) (CInt) v;
152 (void) (CLong) v;
153 (void) (CFloat) v;
154 (void) (CDouble) v;
155 }
156
157 void testVoidPtr(VoidPtr v) {
158 (void)(Bool) v;
159 (void) (Int) v; // expected-warning{{cast to smaller integer type 'Int' (aka 'int') from 'VoidPtr' (aka 'void *')}}
160 (void) (Long) v;
161 (void) (VoidPtr) v;
162 (void) (CharPtr) v;
163 // Test that casts to void* can be controlled separately
164 // from other -Wpointer-to-int-cast warnings.
165 #pragma clang diagnostic push
166 #pragma clang diagnostic ignored "-Wvoid-pointer-to-int-cast"
167 (void)(Int) v; // no-warning
168 #pragma clang diagnostic pop
169 }
170
171 void testCharPtr(CharPtr v) {
172 (void)(Bool) v;
173 (void) (Int) v; // expected-warning{{cast to smaller integer type 'Int' (aka 'int') from 'CharPtr' (aka 'char *')}}
174 (void) (Long) v;
175 (void) (VoidPtr) v;
176 (void) (CharPtr) v;
177 // Test that casts to void* can be controlled separately
178 // from other -Wpointer-to-int-cast warnings.
179 #pragma clang diagnostic push
180 #pragma clang diagnostic ignored "-Wvoid-pointer-to-int-cast"
181 (void)(Int) v; // expected-warning{{cast to smaller integer type 'Int' (aka 'int') from 'CharPtr' (aka 'char *')}}
182 #pragma clang diagnostic pop
183 }
184
185 typedef enum { x_a, x_b } X;
186 void *intToPointerCast2(X x) {
187 return (void*)x;
188 }
189
190 void *intToPointerCast3(void) {
191 return (void*)(1 + 3);
192 }
193
194 void voidPointerToEnumCast(VoidPtr v) {
195 (void)(X) v; // expected-warning{{cast to smaller integer type 'X' from 'VoidPtr' (aka 'void *')}}
196 // Test that casts to void* can be controlled separately
197 // from other -Wpointer-to-enum-cast warnings.
198 #pragma clang diagnostic push
199 #pragma clang diagnostic ignored "-Wvoid-pointer-to-enum-cast"
200 (void)(X) v; // no-warning
201 #pragma clang diagnostic pop
202 }
203
204 void pointerToEnumCast(CharPtr v) {
205 (void)(X) v; // expected-warning{{cast to smaller integer type 'X' from 'CharPtr' (aka 'char *')}}
206 // Test that casts to void* can be controlled separately
207 // from other -Wpointer-to-enum-cast warnings.
208 #pragma clang diagnostic push
209 #pragma clang diagnostic ignored "-Wvoid-pointer-to-enum-cast"
210 (void)(X) v; // expected-warning{{cast to smaller integer type 'X' from 'CharPtr' (aka 'char *')}}
211 #pragma clang diagnostic pop
212 }