xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/cast.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown %s -verify
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc typedef struct { unsigned long bits[(((1) + (64) - 1) / (64))]; } cpumask_t;
4*f4a2713aSLionel Sambuc cpumask_t x;
foo()5*f4a2713aSLionel Sambuc void foo() {
6*f4a2713aSLionel Sambuc   (void)x;
7*f4a2713aSLionel Sambuc }
bar()8*f4a2713aSLionel Sambuc void bar() {
9*f4a2713aSLionel Sambuc   char* a;
10*f4a2713aSLionel Sambuc   double b;
11*f4a2713aSLionel Sambuc   b = (double)a; // expected-error {{pointer cannot be cast to type}}
12*f4a2713aSLionel Sambuc   a = (char*)b; // expected-error {{cannot be cast to a pointer type}}
13*f4a2713aSLionel Sambuc }
14*f4a2713aSLionel Sambuc 
bar1(long * next)15*f4a2713aSLionel Sambuc long bar1(long *next) {
16*f4a2713aSLionel Sambuc         return (long)(*next)++;
17*f4a2713aSLionel Sambuc }
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc typedef _Bool Bool;
20*f4a2713aSLionel Sambuc typedef int Int;
21*f4a2713aSLionel Sambuc typedef long Long;
22*f4a2713aSLionel Sambuc typedef float Float;
23*f4a2713aSLionel Sambuc typedef double Double;
24*f4a2713aSLionel Sambuc typedef _Complex int CInt;
25*f4a2713aSLionel Sambuc typedef _Complex long CLong;
26*f4a2713aSLionel Sambuc typedef _Complex float CFloat;
27*f4a2713aSLionel Sambuc typedef _Complex double CDouble;
28*f4a2713aSLionel Sambuc typedef void *VoidPtr;
29*f4a2713aSLionel Sambuc typedef char *CharPtr;
30*f4a2713aSLionel Sambuc 
testBool(Bool v)31*f4a2713aSLionel Sambuc void testBool(Bool v) {
32*f4a2713aSLionel Sambuc   (void) (Bool) v;
33*f4a2713aSLionel Sambuc   (void) (Int) v;
34*f4a2713aSLionel Sambuc   (void) (Long) v;
35*f4a2713aSLionel Sambuc   (void) (Float) v;
36*f4a2713aSLionel Sambuc   (void) (Double) v;
37*f4a2713aSLionel Sambuc   (void) (CInt) v;
38*f4a2713aSLionel Sambuc   (void) (CLong) v;
39*f4a2713aSLionel Sambuc   (void) (CFloat) v;
40*f4a2713aSLionel Sambuc   (void) (CDouble) v;
41*f4a2713aSLionel Sambuc   (void) (VoidPtr) v;
42*f4a2713aSLionel Sambuc   (void) (CharPtr) v;
43*f4a2713aSLionel Sambuc }
44*f4a2713aSLionel Sambuc 
testInt(Int v)45*f4a2713aSLionel Sambuc void testInt(Int v) {
46*f4a2713aSLionel Sambuc   (void) (Bool) v;
47*f4a2713aSLionel Sambuc   (void) (Int) v;
48*f4a2713aSLionel Sambuc   (void) (Long) v;
49*f4a2713aSLionel Sambuc   (void) (Float) v;
50*f4a2713aSLionel Sambuc   (void) (Double) v;
51*f4a2713aSLionel Sambuc   (void) (CInt) v;
52*f4a2713aSLionel Sambuc   (void) (CLong) v;
53*f4a2713aSLionel Sambuc   (void) (CFloat) v;
54*f4a2713aSLionel Sambuc   (void) (CDouble) v;
55*f4a2713aSLionel Sambuc   (void) (VoidPtr) v; // expected-warning{{cast to 'VoidPtr' (aka 'void *') from smaller integer type 'Int' (aka 'int')}}
56*f4a2713aSLionel Sambuc   (void) (CharPtr) v; // expected-warning{{cast to 'CharPtr' (aka 'char *') from smaller integer type 'Int' (aka 'int')}}
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   // Test that casts to void* can be controlled separately
59*f4a2713aSLionel Sambuc   // from other -Wint-to-pointer-cast warnings.
60*f4a2713aSLionel Sambuc #pragma clang diagnostic push
61*f4a2713aSLionel Sambuc #pragma clang diagnostic ignored "-Wint-to-void-pointer-cast"
62*f4a2713aSLionel Sambuc   (void) (VoidPtr) v; // no-warning
63*f4a2713aSLionel Sambuc   (void) (CharPtr) v; // expected-warning{{cast to 'CharPtr' (aka 'char *') from smaller integer type 'Int' (aka 'int')}}
64*f4a2713aSLionel Sambuc #pragma clang diagnostic pop
65*f4a2713aSLionel Sambuc }
66*f4a2713aSLionel Sambuc 
testLong(Long v)67*f4a2713aSLionel Sambuc void testLong(Long v) {
68*f4a2713aSLionel Sambuc   (void) (Bool) v;
69*f4a2713aSLionel Sambuc   (void) (Int) v;
70*f4a2713aSLionel Sambuc   (void) (Long) v;
71*f4a2713aSLionel Sambuc   (void) (Float) v;
72*f4a2713aSLionel Sambuc   (void) (Double) v;
73*f4a2713aSLionel Sambuc   (void) (CInt) v;
74*f4a2713aSLionel Sambuc   (void) (CLong) v;
75*f4a2713aSLionel Sambuc   (void) (CFloat) v;
76*f4a2713aSLionel Sambuc   (void) (CDouble) v;
77*f4a2713aSLionel Sambuc   (void) (VoidPtr) v;
78*f4a2713aSLionel Sambuc   (void) (CharPtr) v;
79*f4a2713aSLionel Sambuc }
80*f4a2713aSLionel Sambuc 
testFloat(Float v)81*f4a2713aSLionel Sambuc void testFloat(Float v) {
82*f4a2713aSLionel Sambuc   (void) (Bool) v;
83*f4a2713aSLionel Sambuc   (void) (Int) v;
84*f4a2713aSLionel Sambuc   (void) (Long) v;
85*f4a2713aSLionel Sambuc   (void) (Float) v;
86*f4a2713aSLionel Sambuc   (void) (Double) v;
87*f4a2713aSLionel Sambuc   (void) (CInt) v;
88*f4a2713aSLionel Sambuc   (void) (CLong) v;
89*f4a2713aSLionel Sambuc   (void) (CFloat) v;
90*f4a2713aSLionel Sambuc   (void) (CDouble) v;
91*f4a2713aSLionel Sambuc }
92*f4a2713aSLionel Sambuc 
testDouble(Double v)93*f4a2713aSLionel Sambuc void testDouble(Double v) {
94*f4a2713aSLionel Sambuc   (void) (Bool) v;
95*f4a2713aSLionel Sambuc   (void) (Int) v;
96*f4a2713aSLionel Sambuc   (void) (Long) v;
97*f4a2713aSLionel Sambuc   (void) (Float) v;
98*f4a2713aSLionel Sambuc   (void) (Double) v;
99*f4a2713aSLionel Sambuc   (void) (CInt) v;
100*f4a2713aSLionel Sambuc   (void) (CLong) v;
101*f4a2713aSLionel Sambuc   (void) (CFloat) v;
102*f4a2713aSLionel Sambuc   (void) (CDouble) v;
103*f4a2713aSLionel Sambuc }
104*f4a2713aSLionel Sambuc 
testCI(CInt v)105*f4a2713aSLionel Sambuc void testCI(CInt v) {
106*f4a2713aSLionel Sambuc   (void) (Bool) v;
107*f4a2713aSLionel Sambuc   (void) (Int) v;
108*f4a2713aSLionel Sambuc   (void) (Long) v;
109*f4a2713aSLionel Sambuc   (void) (Float) v;
110*f4a2713aSLionel Sambuc   (void) (Double) v;
111*f4a2713aSLionel Sambuc   (void) (CInt) v;
112*f4a2713aSLionel Sambuc   (void) (CLong) v;
113*f4a2713aSLionel Sambuc   (void) (CFloat) v;
114*f4a2713aSLionel Sambuc   (void) (CDouble) v;
115*f4a2713aSLionel Sambuc }
116*f4a2713aSLionel Sambuc 
testCLong(CLong v)117*f4a2713aSLionel Sambuc void testCLong(CLong v) {
118*f4a2713aSLionel Sambuc   (void) (Bool) v;
119*f4a2713aSLionel Sambuc   (void) (Int) v;
120*f4a2713aSLionel Sambuc   (void) (Long) v;
121*f4a2713aSLionel Sambuc   (void) (Float) v;
122*f4a2713aSLionel Sambuc   (void) (Double) v;
123*f4a2713aSLionel Sambuc   (void) (CInt) v;
124*f4a2713aSLionel Sambuc   (void) (CLong) v;
125*f4a2713aSLionel Sambuc   (void) (CFloat) v;
126*f4a2713aSLionel Sambuc   (void) (CDouble) v;
127*f4a2713aSLionel Sambuc }
128*f4a2713aSLionel Sambuc 
testCFloat(CFloat v)129*f4a2713aSLionel Sambuc void testCFloat(CFloat v) {
130*f4a2713aSLionel Sambuc   (void) (Bool) v;
131*f4a2713aSLionel Sambuc   (void) (Int) v;
132*f4a2713aSLionel Sambuc   (void) (Long) v;
133*f4a2713aSLionel Sambuc   (void) (Float) v;
134*f4a2713aSLionel Sambuc   (void) (Double) v;
135*f4a2713aSLionel Sambuc   (void) (CInt) v;
136*f4a2713aSLionel Sambuc   (void) (CLong) v;
137*f4a2713aSLionel Sambuc   (void) (CFloat) v;
138*f4a2713aSLionel Sambuc   (void) (CDouble) v;
139*f4a2713aSLionel Sambuc }
140*f4a2713aSLionel Sambuc 
testCDouble(CDouble v)141*f4a2713aSLionel Sambuc void testCDouble(CDouble v) {
142*f4a2713aSLionel Sambuc   (void) (Bool) v;
143*f4a2713aSLionel Sambuc   (void) (Int) v;
144*f4a2713aSLionel Sambuc   (void) (Long) v;
145*f4a2713aSLionel Sambuc   (void) (Float) v;
146*f4a2713aSLionel Sambuc   (void) (Double) v;
147*f4a2713aSLionel Sambuc   (void) (CInt) v;
148*f4a2713aSLionel Sambuc   (void) (CLong) v;
149*f4a2713aSLionel Sambuc   (void) (CFloat) v;
150*f4a2713aSLionel Sambuc   (void) (CDouble) v;
151*f4a2713aSLionel Sambuc }
152*f4a2713aSLionel Sambuc 
testVoidPtr(VoidPtr v)153*f4a2713aSLionel Sambuc void testVoidPtr(VoidPtr v) {
154*f4a2713aSLionel Sambuc   (void) (Bool) v;
155*f4a2713aSLionel Sambuc   (void) (Int) v;
156*f4a2713aSLionel Sambuc   (void) (Long) v;
157*f4a2713aSLionel Sambuc   (void) (VoidPtr) v;
158*f4a2713aSLionel Sambuc   (void) (CharPtr) v;
159*f4a2713aSLionel Sambuc }
160*f4a2713aSLionel Sambuc 
testCharPtr(CharPtr v)161*f4a2713aSLionel Sambuc void testCharPtr(CharPtr v) {
162*f4a2713aSLionel Sambuc   (void) (Bool) v;
163*f4a2713aSLionel Sambuc   (void) (Int) v;
164*f4a2713aSLionel Sambuc   (void) (Long) v;
165*f4a2713aSLionel Sambuc   (void) (VoidPtr) v;
166*f4a2713aSLionel Sambuc   (void) (CharPtr) v;
167*f4a2713aSLionel Sambuc }
168*f4a2713aSLionel Sambuc 
169*f4a2713aSLionel Sambuc typedef enum { x_a, x_b } X;
intToPointerCast2(X x)170*f4a2713aSLionel Sambuc void *intToPointerCast2(X x) {
171*f4a2713aSLionel Sambuc   return (void*)x;
172*f4a2713aSLionel Sambuc }
173*f4a2713aSLionel Sambuc 
intToPointerCast3()174*f4a2713aSLionel Sambuc void *intToPointerCast3() {
175*f4a2713aSLionel Sambuc   return (void*)(1 + 3);
176*f4a2713aSLionel Sambuc }
177