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