xref: /aosp_15_r20/external/clang/test/Sema/overloadable.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify %s -Wincompatible-pointer-types
2*67e74705SXin Li 
3*67e74705SXin Li int var __attribute__((overloadable)); // expected-error{{'overloadable' attribute only applies to functions}}
4*67e74705SXin Li void params(void) __attribute__((overloadable(12))); // expected-error {{'overloadable' attribute takes no arguments}}
5*67e74705SXin Li 
6*67e74705SXin Li int *f(int) __attribute__((overloadable)); // expected-note 2{{previous overload of function is here}}
7*67e74705SXin Li float *f(float); // expected-error{{overloaded function 'f' must have the 'overloadable' attribute}}
8*67e74705SXin Li int *f(int); // expected-error{{redeclaration of 'f' must have the 'overloadable' attribute}} \
9*67e74705SXin Li              // expected-note{{previous declaration is here}}
10*67e74705SXin Li double *f(double) __attribute__((overloadable)); // okay, new
11*67e74705SXin Li 
test_f(int iv,float fv,double dv)12*67e74705SXin Li void test_f(int iv, float fv, double dv) {
13*67e74705SXin Li   int *ip = f(iv);
14*67e74705SXin Li   float *fp = f(fv);
15*67e74705SXin Li   double *dp = f(dv);
16*67e74705SXin Li }
17*67e74705SXin Li 
18*67e74705SXin Li int *accept_funcptr(int (*)()) __attribute__((overloadable)); //         \
19*67e74705SXin Li   // expected-note{{candidate function}}
20*67e74705SXin Li float *accept_funcptr(int (*)(int, double)) __attribute__((overloadable)); //  \
21*67e74705SXin Li   // expected-note{{candidate function}}
22*67e74705SXin Li 
test_funcptr(int (* f1)(int,double),int (* f2)(int,float))23*67e74705SXin Li void test_funcptr(int (*f1)(int, double),
24*67e74705SXin Li                   int (*f2)(int, float)) {
25*67e74705SXin Li   float *fp = accept_funcptr(f1);
26*67e74705SXin Li   accept_funcptr(f2); // expected-error{{no matching function for call to 'accept_funcptr'}}
27*67e74705SXin Li }
28*67e74705SXin Li 
29*67e74705SXin Li struct X { int x; float y; };
30*67e74705SXin Li struct Y { int x; float y; };
31*67e74705SXin Li int* accept_struct(struct X x) __attribute__((__overloadable__));
32*67e74705SXin Li float* accept_struct(struct Y y) __attribute__((overloadable));
33*67e74705SXin Li 
test_struct(struct X x,struct Y y)34*67e74705SXin Li void test_struct(struct X x, struct Y y) {
35*67e74705SXin Li   int *ip = accept_struct(x);
36*67e74705SXin Li   float *fp = accept_struct(y);
37*67e74705SXin Li }
38*67e74705SXin Li 
39*67e74705SXin Li double *f(int) __attribute__((overloadable)); // expected-error{{conflicting types for 'f'}}
40*67e74705SXin Li 
41*67e74705SXin Li double promote(float) __attribute__((__overloadable__)); // expected-note {{candidate}}
42*67e74705SXin Li double promote(double) __attribute__((__overloadable__)); // expected-note {{candidate}}
43*67e74705SXin Li long double promote(long double) __attribute__((__overloadable__)); // expected-note {{candidate}}
44*67e74705SXin Li 
45*67e74705SXin Li void promote(...) __attribute__((__overloadable__, __unavailable__)); // \
46*67e74705SXin Li     // expected-note{{candidate function}}
47*67e74705SXin Li 
test_promote(short * sp)48*67e74705SXin Li void test_promote(short* sp) {
49*67e74705SXin Li   promote(1.0);
50*67e74705SXin Li   promote(sp); // expected-error{{call to unavailable function 'promote'}}
51*67e74705SXin Li }
52*67e74705SXin Li 
53*67e74705SXin Li // PR6600
54*67e74705SXin Li typedef double Double;
55*67e74705SXin Li typedef Double DoubleVec __attribute__((vector_size(16)));
56*67e74705SXin Li typedef int Int;
57*67e74705SXin Li typedef Int IntVec __attribute__((vector_size(16)));
58*67e74705SXin Li double magnitude(DoubleVec) __attribute__((__overloadable__));
59*67e74705SXin Li double magnitude(IntVec) __attribute__((__overloadable__));
test_p6600(DoubleVec d)60*67e74705SXin Li double test_p6600(DoubleVec d) {
61*67e74705SXin Li   return magnitude(d) * magnitude(d);
62*67e74705SXin Li }
63*67e74705SXin Li 
64*67e74705SXin Li // PR7738
65*67e74705SXin Li extern int __attribute__((overloadable)) f0(); // expected-error{{'overloadable' function 'f0' must have a prototype}}
66*67e74705SXin Li typedef int f1_type();
67*67e74705SXin Li f1_type __attribute__((overloadable)) f1; // expected-error{{'overloadable' function 'f1' must have a prototype}}
68*67e74705SXin Li 
test()69*67e74705SXin Li void test() {
70*67e74705SXin Li   f0();
71*67e74705SXin Li   f1();
72*67e74705SXin Li }
73*67e74705SXin Li 
74*67e74705SXin Li void before_local_1(int) __attribute__((overloadable)); // expected-note {{here}}
75*67e74705SXin Li void before_local_2(int); // expected-note {{here}}
76*67e74705SXin Li void before_local_3(int) __attribute__((overloadable));
local()77*67e74705SXin Li void local() {
78*67e74705SXin Li   void before_local_1(char); // expected-error {{must have the 'overloadable' attribute}}
79*67e74705SXin Li   void before_local_2(char) __attribute__((overloadable)); // expected-error {{conflicting types}}
80*67e74705SXin Li   void before_local_3(char) __attribute__((overloadable));
81*67e74705SXin Li   void after_local_1(char); // expected-note {{here}}
82*67e74705SXin Li   void after_local_2(char) __attribute__((overloadable)); // expected-note {{here}}
83*67e74705SXin Li   void after_local_3(char) __attribute__((overloadable));
84*67e74705SXin Li }
85*67e74705SXin Li void after_local_1(int) __attribute__((overloadable)); // expected-error {{conflicting types}}
86*67e74705SXin Li void after_local_2(int); // expected-error {{must have the 'overloadable' attribute}}
87*67e74705SXin Li void after_local_3(int) __attribute__((overloadable));
88*67e74705SXin Li 
89*67e74705SXin Li // Make sure we allow C-specific conversions in C.
conversions()90*67e74705SXin Li void conversions() {
91*67e74705SXin Li   void foo(char *c) __attribute__((overloadable));
92*67e74705SXin Li   void foo(char *c) __attribute__((overloadable, enable_if(c, "nope.jpg")));
93*67e74705SXin Li 
94*67e74705SXin Li   void *ptr;
95*67e74705SXin Li   foo(ptr);
96*67e74705SXin Li 
97*67e74705SXin Li   void multi_type(unsigned char *c) __attribute__((overloadable));
98*67e74705SXin Li   void multi_type(signed char *c) __attribute__((overloadable));
99*67e74705SXin Li   unsigned char *c;
100*67e74705SXin Li   multi_type(c);
101*67e74705SXin Li }
102*67e74705SXin Li 
103*67e74705SXin Li // Ensure that we allow C-specific type conversions in C
fn_type_conversions()104*67e74705SXin Li void fn_type_conversions() {
105*67e74705SXin Li   void foo(void *c) __attribute__((overloadable));
106*67e74705SXin Li   void foo(char *c) __attribute__((overloadable));
107*67e74705SXin Li   void (*ptr1)(void *) = &foo;
108*67e74705SXin Li   void (*ptr2)(char *) = &foo;
109*67e74705SXin Li   void (*ambiguous)(int *) = &foo; // expected-error{{initializing 'void (*)(int *)' with an expression of incompatible type '<overloaded function type>'}} expected-note@105{{candidate function}} expected-note@106{{candidate function}}
110*67e74705SXin Li   void *vp_ambiguous = &foo; // expected-error{{initializing 'void *' with an expression of incompatible type '<overloaded function type>'}} expected-note@105{{candidate function}} expected-note@106{{candidate function}}
111*67e74705SXin Li 
112*67e74705SXin Li   void (*specific1)(int *) = (void (*)(void *))&foo; // expected-warning{{incompatible pointer types initializing 'void (*)(int *)' with an expression of type 'void (*)(void *)'}}
113*67e74705SXin Li   void *specific2 = (void (*)(void *))&foo;
114*67e74705SXin Li 
115*67e74705SXin Li   void disabled(void *c) __attribute__((overloadable, enable_if(0, "")));
116*67e74705SXin Li   void disabled(int *c) __attribute__((overloadable, enable_if(c, "")));
117*67e74705SXin Li   void disabled(char *c) __attribute__((overloadable, enable_if(1, "The function name lies.")));
118*67e74705SXin Li   // To be clear, these should all point to the last overload of 'disabled'
119*67e74705SXin Li   void (*dptr1)(char *c) = &disabled;
120*67e74705SXin Li   void (*dptr2)(void *c) = &disabled; // expected-warning{{incompatible pointer types initializing 'void (*)(void *)' with an expression of type '<overloaded function type>'}} expected-note@115{{candidate function made ineligible by enable_if}} expected-note@116{{candidate function made ineligible by enable_if}} expected-note@117{{candidate function has type mismatch at 1st parameter (expected 'void *' but has 'char *')}}
121*67e74705SXin Li   void (*dptr3)(int *c) = &disabled; // expected-warning{{incompatible pointer types initializing 'void (*)(int *)' with an expression of type '<overloaded function type>'}} expected-note@115{{candidate function made ineligible by enable_if}} expected-note@116{{candidate function made ineligible by enable_if}} expected-note@117{{candidate function has type mismatch at 1st parameter (expected 'int *' but has 'char *')}}
122*67e74705SXin Li 
123*67e74705SXin Li   void *specific_disabled = &disabled;
124*67e74705SXin Li }
125