1 #define	__nocast	__attribute__((nocast))
2 typedef unsigned long __nocast ulong_nc_t;
3 
4 extern void use_val(ulong_nc_t);
5 extern void use_ptr(ulong_nc_t *);
6 
7 /* use address */
good_use_address(void)8 static void good_use_address(void)
9 {
10 	ulong_nc_t t;
11 
12 	use_ptr(&t);
13 }
14 
good_ret_address(void)15 static ulong_nc_t *good_ret_address(void)
16 {
17 	static ulong_nc_t t;
18 
19 	return &t;
20 }
21 
good_deref(ulong_nc_t * t)22 static ulong_nc_t good_deref(ulong_nc_t *t)
23 {
24 	return *t;
25 }
26 
27 /* assign value */
28 static ulong_nc_t t;
29 static ulong_nc_t good_assign_self = t;
30 static unsigned long good_assign_sametype = t;
31 
32 /* assign pointer */
33 static ulong_nc_t *good_ptr = &t;
34 static ulong_nc_t *bad_ptr_to = 1UL;
35 static unsigned long *bad_ptr_from = &t;
36 
37 /* arithmetic operation */
good_arith(ulong_nc_t t,unsigned int n)38 static ulong_nc_t good_arith(ulong_nc_t t, unsigned int n)
39 {
40 	return t + n;
41 }
42 
43 /* implicit cast to other types */
good_ret_samecast(ulong_nc_t t)44 static unsigned long good_ret_samecast(ulong_nc_t t)
45 {
46 	return t;
47 }
bad_ret_biggercast(ulong_nc_t t)48 static unsigned long long bad_ret_biggercast(ulong_nc_t t)
49 {
50 	return t;
51 }
bad_ret_signcast(ulong_nc_t t)52 static long bad_ret_signcast(ulong_nc_t t)
53 {
54 	return t;
55 }
bad_ret_smallercast(ulong_nc_t t)56 static short bad_ret_smallercast(ulong_nc_t t)
57 {
58 	return t;
59 }
60 
assign_val(ulong_nc_t t)61 static void assign_val(ulong_nc_t t)
62 {
63 	ulong_nc_t good_c = t;
64 	unsigned long good_ul = t;
65 	unsigned long long bad_ull = t;
66 	long bad_l = t;
67 	short bad_i = t;
68 }
69 
assign_via_ptr(ulong_nc_t * t)70 static void assign_via_ptr(ulong_nc_t *t)
71 {
72 	ulong_nc_t good_c = *t;
73 	unsigned long good_ul = *t;
74 	unsigned long long bad_ull = *t;
75 	long bad_l = *t;
76 	short bad_i = *t;
77 }
78 
assign_ptr(ulong_nc_t * t)79 static void assign_ptr(ulong_nc_t *t)
80 {
81 	ulong_nc_t *good_same_type = t;
82 	unsigned long *bad_mod = t;
83 	unsigned long long __nocast *bad_size = t;
84 	short __nocast *bad_i = t;
85 	long __nocast *bad_l = t;
86 }
87 
88 /* implicit cast to nocast */
implicit_assign_to(void)89 static void implicit_assign_to(void)
90 {
91 	ulong_nc_t t;
92 	unsigned long ul = 1;
93 	unsigned short us = 1;
94 	unsigned long long ull = 1;
95 	long l = 1;
96 
97 	t = ul;		/* implicit to nocast from same type: OK? */
98 	t = us;
99 	t = ull;
100 	t = l;
101 }
102 
bad_implicit_arg_to(void)103 static void bad_implicit_arg_to(void)
104 {
105 	unsigned long ul = 1;
106 	unsigned short us = 1;
107 	unsigned long long ull = 1;
108 	long l = 1;
109 
110 	use_val(ul);	/* implicit to nocast from same type: OK? */
111 	use_val(us);
112 	use_val(ull);
113 	use_val(l);
114 }
115 
116 /* implicit cast from nocast */
good_implicit_ret_ul(ulong_nc_t t)117 static unsigned long good_implicit_ret_ul(ulong_nc_t t)
118 {
119 	return t;	/* implicit to nocast from same type: OK? */
120 }
121 
bad_implicit_ret_us(ulong_nc_t t)122 static unsigned short bad_implicit_ret_us(ulong_nc_t t)
123 {
124 	return t;
125 }
126 
bad_implicit_ret_ull(ulong_nc_t t)127 static unsigned long long bad_implicit_ret_ull(ulong_nc_t t)
128 {
129 	return t;
130 }
131 
bad_implicit_ret_l(ulong_nc_t t)132 static long bad_implicit_ret_l(ulong_nc_t t)
133 {
134 	return t;
135 }
136 
137 /* FIXME: explicit cast: should we complain? */
good_samecast(ulong_nc_t v)138 static ulong_nc_t good_samecast(ulong_nc_t v)
139 {
140 	return (ulong_nc_t) v;
141 }
142 
bad_tocast(unsigned long v)143 static ulong_nc_t bad_tocast(unsigned long v)
144 {
145 	return (ulong_nc_t) v;
146 }
147 
bad_fromcast(ulong_nc_t v)148 static unsigned long bad_fromcast(ulong_nc_t v)
149 {
150 	return (unsigned long) v;
151 }
152 
153 /*
154  * check-name: nocast.c
155  *
156  * check-error-start
157 nocast.c:34:33: warning: incorrect type in initializer (different base types)
158 nocast.c:34:33:    expected unsigned long [nocast] [usertype] *static [toplevel] bad_ptr_to
159 nocast.c:34:33:    got unsigned long
160 nocast.c:34:33: warning: implicit cast to nocast type
161 nocast.c:35:39: warning: incorrect type in initializer (different modifiers)
162 nocast.c:35:39:    expected unsigned long *static [toplevel] bad_ptr_from
163 nocast.c:35:39:    got unsigned long [nocast] *
164 nocast.c:35:39: warning: implicit cast from nocast type
165 nocast.c:50:16: warning: implicit cast from nocast type
166 nocast.c:54:16: warning: implicit cast from nocast type
167 nocast.c:58:16: warning: implicit cast from nocast type
168 nocast.c:65:38: warning: implicit cast from nocast type
169 nocast.c:66:22: warning: implicit cast from nocast type
170 nocast.c:67:23: warning: implicit cast from nocast type
171 nocast.c:74:38: warning: implicit cast from nocast type
172 nocast.c:75:22: warning: implicit cast from nocast type
173 nocast.c:76:23: warning: implicit cast from nocast type
174 nocast.c:82:34: warning: incorrect type in initializer (different modifiers)
175 nocast.c:82:34:    expected unsigned long *bad_mod
176 nocast.c:82:34:    got unsigned long [nocast] [usertype] *t
177 nocast.c:82:34: warning: implicit cast from nocast type
178 nocast.c:83:49: warning: incorrect type in initializer (different type sizes)
179 nocast.c:83:49:    expected unsigned long long [nocast] *bad_size
180 nocast.c:83:49:    got unsigned long [nocast] [usertype] *t
181 nocast.c:83:49: warning: implicit cast to/from nocast type
182 nocast.c:84:33: warning: incorrect type in initializer (different type sizes)
183 nocast.c:84:33:    expected short [nocast] *bad_i
184 nocast.c:84:33:    got unsigned long [nocast] [usertype] *t
185 nocast.c:84:33: warning: implicit cast to/from nocast type
186 nocast.c:85:32: warning: implicit cast to/from nocast type
187 nocast.c:98:13: warning: implicit cast to nocast type
188 nocast.c:99:13: warning: implicit cast to nocast type
189 nocast.c:100:13: warning: implicit cast to nocast type
190 nocast.c:111:17: warning: implicit cast to nocast type
191 nocast.c:112:17: warning: implicit cast to nocast type
192 nocast.c:113:17: warning: implicit cast to nocast type
193 nocast.c:124:16: warning: implicit cast from nocast type
194 nocast.c:129:16: warning: implicit cast from nocast type
195 nocast.c:134:16: warning: implicit cast from nocast type
196  * check-error-end
197  */
198