xref: /llvm-project/clang/test/Sema/overloadable.c (revision 874217f99b99ab3c9026dc3b7bd84cd2beebde6e)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -Wincompatible-pointer-types -Wno-strict-prototypes
2 
3 int var __attribute__((overloadable)); // expected-error{{'overloadable' attribute only applies to functions}}
4 void bad_attr_target(int) [[clang::overloadable]]; // expected-error{{'overloadable' attribute cannot be applied to types}}
5 void params(void) __attribute__((overloadable(12))); // expected-error {{'overloadable' attribute takes no arguments}}
6 
7 int *f(int) __attribute__((overloadable)); // expected-note{{previous overload of function is here}}
8 float *f(float);
9 int *f(int); // expected-error{{redeclaration of 'f' must have the 'overloadable' attribute}} \
10              // expected-note{{previous declaration is here}}
11 double *f(double) __attribute__((overloadable)); // okay, new
12 
13 // Ensure we don't complain about overloadable on implicitly declared functions.
14 int isdigit(int) __attribute__((overloadable));
15 
test_f(int iv,float fv,double dv)16 void test_f(int iv, float fv, double dv) {
17   int *ip = f(iv);
18   float *fp = f(fv);
19   double *dp = f(dv);
20 }
21 
22 int *accept_funcptr(int (*)()) __attribute__((overloadable)); //         \
23   // expected-note{{candidate function}}
24 float *accept_funcptr(int (*)(int, double)) __attribute__((overloadable)); //  \
25   // expected-note{{candidate function}}
26 
test_funcptr(int (* f1)(int,double),int (* f2)(int,float))27 void test_funcptr(int (*f1)(int, double),
28                   int (*f2)(int, float)) {
29   float *fp = accept_funcptr(f1);
30   accept_funcptr(f2); // expected-error{{no matching function for call to 'accept_funcptr'}}
31 }
32 
33 struct X { int x; float y; };
34 struct Y { int x; float y; };
35 int* accept_struct(struct X x) __attribute__((__overloadable__));
36 float* accept_struct(struct Y y) __attribute__((overloadable));
37 
test_struct(struct X x,struct Y y)38 void test_struct(struct X x, struct Y y) {
39   int *ip = accept_struct(x);
40   float *fp = accept_struct(y);
41 }
42 
43 double *f(int) __attribute__((overloadable)); // expected-error{{conflicting types for 'f'}}
44 
45 double promote(float) __attribute__((__overloadable__));
46 double promote(double) __attribute__((__overloadable__));
47 long double promote(long double) __attribute__((__overloadable__));
48 
49 void promote(...) __attribute__((__overloadable__, __unavailable__)); // expected-note {{marked unavailable here}}
50 
test_promote(short * sp)51 void test_promote(short* sp) {
52   promote(1.0);
53   promote(sp); // expected-error{{'promote' is unavailable}}
54 }
55 
56 // PR6600
57 typedef double Double;
58 typedef Double DoubleVec __attribute__((vector_size(16)));
59 typedef int Int;
60 typedef Int IntVec __attribute__((vector_size(16)));
61 double magnitude(DoubleVec) __attribute__((__overloadable__));
62 double magnitude(IntVec) __attribute__((__overloadable__));
test_p6600(DoubleVec d)63 double test_p6600(DoubleVec d) {
64   return magnitude(d) * magnitude(d);
65 }
66 
67 // PR7738
68 extern int __attribute__((overloadable)) f0(); // expected-error{{'overloadable' function 'f0' must have a prototype}}
69 typedef int f1_type();
70 f1_type __attribute__((overloadable)) f1; // expected-error{{'overloadable' function 'f1' must have a prototype}}
71 
test()72 void test() {
73   f0();
74   f1();
75 }
76 
77 // Validate that the invalid function doesn't stay overloadable.
78 int __attribute__((overloadable)) invalid(); // expected-error{{'overloadable' function 'invalid' must have a prototype}}
79 int __attribute__((overloadable)) invalid(int); // expected-error{{redeclaration of 'invalid' must not have the 'overloadable' attribute}}
80                                                 // expected-note@-2{{previous unmarked overload of function is here}}
use_invalid(void)81 void use_invalid(void) {
82   invalid(); // expected-error{{too few arguments to function call, expected 1, have 0}}
83              // expected-note@-4{{'invalid' declared here}}
84   invalid(1);
85 }
86 
87 void before_local_1(int) __attribute__((overloadable));
88 void before_local_2(int); // expected-note {{here}}
89 void before_local_3(int) __attribute__((overloadable));
local()90 void local() {
91   void before_local_1(char);
92   void before_local_2(char); // expected-error {{conflicting types}}
93   void before_local_3(char) __attribute__((overloadable));
94   void after_local_1(char);
95   void after_local_2(char) __attribute__((overloadable));
96   void after_local_3(char) __attribute__((overloadable));
97 }
98 void after_local_1(int) __attribute__((overloadable));
99 void after_local_2(int);
100 void after_local_3(int) __attribute__((overloadable));
101 
102 // Make sure we allow C-specific conversions in C.
conversions()103 void conversions() {
104   void foo(char *c) __attribute__((overloadable));
105   void foo(char *c) __attribute__((overloadable, enable_if(c, "nope.jpg")));
106 
107   void *ptr;
108   foo(ptr);
109 
110   void multi_type(unsigned char *c) __attribute__((overloadable));
111   void multi_type(signed char *c) __attribute__((overloadable));
112   unsigned char *c;
113   multi_type(c);
114 }
115 
116 // Ensure that we allow C-specific type conversions in C
fn_type_conversions()117 void fn_type_conversions() {
118   void foo(void *c) __attribute__((overloadable));
119   void foo(char *c) __attribute__((overloadable));
120   void (*ptr1)(void *) = &foo;
121   void (*ptr2)(char *) = &foo;
122   void (*ambiguous)(int *) = &foo; // expected-error{{initializing 'void (*)(int *)' with an expression of incompatible type '<overloaded function type>'}} expected-note@-4{{candidate function}} expected-note@-3{{candidate function}}
123   void *vp_ambiguous = &foo; // expected-error{{initializing 'void *' with an expression of incompatible type '<overloaded function type>'}} expected-note@-5{{candidate function}} expected-note@-4{{candidate function}}
124 
125   void (*specific1)(int *) = (void (*)(void *))&foo; // expected-error{{incompatible function pointer types initializing 'void (*)(int *)' with an expression of type 'void (*)(void *)'}}
126   void *specific2 = (void (*)(void *))&foo;
127 
128   void disabled(void *c) __attribute__((overloadable, enable_if(0, "")));
129   void disabled(int *c) __attribute__((overloadable, enable_if(c, "")));
130   void disabled(char *c) __attribute__((overloadable, enable_if(1, "The function name lies.")));
131   // To be clear, these should all point to the last overload of 'disabled'
132   void (*dptr1)(char *c) = &disabled;
133   void (*dptr2)(void *c) = &disabled; // expected-error{{incompatible function pointer types initializing 'void (*)(void *)' with an expression of type '<overloaded function type>'}} expected-note@-5{{candidate function made ineligible by enable_if}} expected-note@-4{{candidate function made ineligible by enable_if}} expected-note@-3{{candidate function has type mismatch at 1st parameter (expected 'void *' but has 'char *')}}
134   void (*dptr3)(int *c) = &disabled; // expected-error{{incompatible function pointer types initializing 'void (*)(int *)' with an expression of type '<overloaded function type>'}} expected-note@-6{{candidate function made ineligible by enable_if}} expected-note@-5{{candidate function made ineligible by enable_if}} expected-note@-4{{candidate function has type mismatch at 1st parameter (expected 'int *' but has 'char *')}}
135 
136   void *specific_disabled = &disabled;
137 }
138 
incompatible_pointer_type_conversions()139 void incompatible_pointer_type_conversions() {
140   char charbuf[1];
141   unsigned char ucharbuf[1];
142   int intbuf[1];
143 
144   void foo(char *c) __attribute__((overloadable));
145   void foo(short *c) __attribute__((overloadable));
146   foo(charbuf);
147   foo(ucharbuf); // expected-error{{call to 'foo' is ambiguous}} expected-note@-3{{candidate function}} expected-note@-2{{candidate function}}
148   foo(intbuf); // expected-error{{call to 'foo' is ambiguous}} expected-note@-4{{candidate function}} expected-note@-3{{candidate function}}
149 
150   void bar(unsigned char *c) __attribute__((overloadable));
151   void bar(signed char *c) __attribute__((overloadable));
152   bar(charbuf); // expected-error{{call to 'bar' is ambiguous}} expected-note@-2{{candidate function}} expected-note@-1{{candidate function}}
153   bar(ucharbuf);
154   bar(intbuf); // expected-error{{call to 'bar' is ambiguous}} expected-note@-4{{candidate function}} expected-note@-3{{candidate function}}
155 }
156 
dropping_qualifiers_is_incompatible()157 void dropping_qualifiers_is_incompatible() {
158   const char ccharbuf[1];
159   volatile char vcharbuf[1];
160 
161   void foo(char *c) __attribute__((overloadable));
162   void foo(const volatile unsigned char *c) __attribute__((overloadable));
163 
164   foo(ccharbuf); // expected-error{{call to 'foo' is ambiguous}} expected-note@-3{{candidate function}} expected-note@-2{{candidate function}}
165   foo(vcharbuf); // expected-error{{call to 'foo' is ambiguous}} expected-note@-4{{candidate function}} expected-note@-3{{candidate function}}
166 }
167 
overloadable_with_global()168 void overloadable_with_global() {
169   void wg_foo(void) __attribute__((overloadable)); // expected-note{{previous}}
170   void wg_foo(int) __attribute__((overloadable));
171 }
172 
173 int wg_foo; // expected-error{{redefinition of 'wg_foo' as different kind of symbol}}
174 
175 #if !__has_extension(overloadable_unmarked)
176 #error "We should have unmarked overload support"
177 #endif
178 
179 void to_foo0(int);
180 void to_foo0(double) __attribute__((overloadable)); // expected-note{{previous overload}}
181 void to_foo0(int);
182 void to_foo0(double); // expected-error{{must have the 'overloadable' attribute}}
183 void to_foo0(int);
184 
185 void to_foo1(int) __attribute__((overloadable)); // expected-note 2{{previous overload}}
186 void to_foo1(double);
187 void to_foo1(int); // expected-error{{must have the 'overloadable' attribute}}
188 void to_foo1(double);
189 void to_foo1(int); // expected-error{{must have the 'overloadable' attribute}}
190 
191 void to_foo2(int); // expected-note{{previous unmarked overload}}
192 void to_foo2(double) __attribute__((overloadable)); // expected-note 2{{previous overload}}
193 void to_foo2(int) __attribute__((overloadable)); // expected-error {{must not have the 'overloadable' attribute}}
194 void to_foo2(double); // expected-error{{must have the 'overloadable' attribute}}
195 void to_foo2(int);
196 void to_foo2(double); // expected-error{{must have the 'overloadable' attribute}}
197 void to_foo2(int);
198 
199 void to_foo3(int);
200 void to_foo3(double) __attribute__((overloadable)); // expected-note{{previous overload}}
201 void to_foo3(int);
202 void to_foo3(double); // expected-error{{must have the 'overloadable' attribute}}
203 
204 void to_foo4(int) __attribute__((overloadable)); // expected-note{{previous overload}}
205 void to_foo4(int); // expected-error{{must have the 'overloadable' attribute}}
206 void to_foo4(double) __attribute__((overloadable));
207 
208 void to_foo5(int);
209 void to_foo5(int); // expected-note 3{{previous unmarked overload}}
210 void to_foo5(float) __attribute__((overloadable));
211 void to_foo5(double); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}
212 void to_foo5(float) __attribute__((overloadable));
213 void to_foo5(short); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}
214 void to_foo5(long); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}
215 void to_foo5(double) __attribute__((overloadable));
216 
217 void to_foo6(int) __attribute__((enable_if(1, ""), overloadable)); // expected-note{{previous overload}}
218 void to_foo6(int) __attribute__((enable_if(1, ""))); // expected-error{{must have the 'overloadable' attribute}}
219 void to_foo6(int) __attribute__((enable_if(1, ""), overloadable));
220 
221 void to_foo7(int) __attribute__((enable_if(1, ""))); // expected-note{{previous unmarked overload}}
222 void to_foo7(int) __attribute__((enable_if(1, ""), overloadable)); // expected-error{{must not have the 'overloadable' attribute}}
223 void to_foo7(int) __attribute__((enable_if(1, "")));
224 
225 void to_foo8(char *__attribute__((pass_object_size(0))))
226   __attribute__((enable_if(1, "")));
227 void to_foo8(char *__attribute__((pass_object_size(0))))
228   __attribute__((overloadable));
229 
230 void to_foo9(int); // expected-note{{previous unmarked overload}}
231 // FIXME: It would be nice if we did better with the "previous unmarked
232 // overload" diag.
233 void to_foo9(int) __attribute__((overloadable)); // expected-error{{must not have the 'overloadable' attribute}} expected-note{{previous declaration}} expected-note{{previous unmarked overload}}
234 void to_foo9(float); // expected-error{{conflicting types for 'to_foo9'}}
235 void to_foo9(float) __attribute__((overloadable));
236 void to_foo9(double); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}
237 void to_foo9(double) __attribute__((overloadable));
238 
239 void to_foo10(int) __attribute__((overloadable));
240 void to_foo10(double); // expected-note{{previous unmarked overload}}
241 // no "note: previous redecl" if no previous decl has `overloadable`
242 // spelled out
243 void to_foo10(float); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}
244 void to_foo10(float); // expected-error{{must have the 'overloadable' attribute}}
245 void to_foo10(float); // expected-error{{must have the 'overloadable' attribute}}
246 
247 // Bug: we used to treat `__typeof__(foo)` as though it was `__typeof__(&foo)`
248 // if `foo` was overloaded with only one function that could have its address
249 // taken.
typeof_function_is_not_a_pointer()250 void typeof_function_is_not_a_pointer() {
251   void not_a_pointer(void *) __attribute__((overloadable));
252   void not_a_pointer(char *__attribute__((pass_object_size(1))))
253     __attribute__((overloadable));
254 
255   __typeof__(not_a_pointer) *fn;
256 
257   void take_fn(void (*)(void *));
258   // if take_fn is passed a void (**)(void *), we'll get a warning.
259   take_fn(fn);
260 }
261 
262 // PR53805
263 // We previously failed to consider the attribute being written before the
264 // declaration when considering whether to allow a variadic signature with no
265 // other parameters, and so we handled these cases differently.
266 __attribute__((overloadable)) void can_overload_1(...); // ok, was previously rejected
267 void can_overload_2(...) __attribute__((overloadable)); // ok
268 [[clang::overloadable]] void can_overload_3(...);       // ok, was previously rejected
269 void can_overload_4 [[clang::overloadable]] (...);      // ok
270 void cannot_overload(...) [[clang::overloadable]];      // expected-error {{ISO C requires a named parameter before '...'}} \
271                                                         // expected-error {{'overloadable' attribute cannot be applied to types}}
272