272 lines · c
1// RUN: %clang_cc1 -fsyntax-only -verify %s -Wincompatible-pointer-types -Wno-strict-prototypes2 3int var __attribute__((overloadable)); // expected-error{{'overloadable' attribute only applies to functions}}4void bad_attr_target(int) [[clang::overloadable]]; // expected-error{{'clang::overloadable' attribute cannot be applied to types}}5void params(void) __attribute__((overloadable(12))); // expected-error {{'overloadable' attribute takes no arguments}}6 7int *f(int) __attribute__((overloadable)); // expected-note{{previous overload of function is here}}8float *f(float);9int *f(int); // expected-error{{redeclaration of 'f' must have the 'overloadable' attribute}} \10 // expected-note{{previous declaration is here}}11double *f(double) __attribute__((overloadable)); // okay, new12 13// Ensure we don't complain about overloadable on implicitly declared functions.14int isdigit(int) __attribute__((overloadable));15 16void 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 22int *accept_funcptr(int (*)()) __attribute__((overloadable)); // \23 // expected-note{{candidate function}}24float *accept_funcptr(int (*)(int, double)) __attribute__((overloadable)); // \25 // expected-note{{candidate function}}26 27void 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 33struct X { int x; float y; };34struct Y { int x; float y; };35int* accept_struct(struct X x) __attribute__((__overloadable__));36float* accept_struct(struct Y y) __attribute__((overloadable));37 38void test_struct(struct X x, struct Y y) {39 int *ip = accept_struct(x);40 float *fp = accept_struct(y);41}42 43double *f(int) __attribute__((overloadable)); // expected-error{{conflicting types for 'f'}}44 45double promote(float) __attribute__((__overloadable__));46double promote(double) __attribute__((__overloadable__));47long double promote(long double) __attribute__((__overloadable__));48 49void promote(...) __attribute__((__overloadable__, __unavailable__)); // expected-note {{marked unavailable here}}50 51void test_promote(short* sp) {52 promote(1.0);53 promote(sp); // expected-error{{'promote' is unavailable}}54}55 56// PR660057typedef double Double;58typedef Double DoubleVec __attribute__((vector_size(16)));59typedef int Int;60typedef Int IntVec __attribute__((vector_size(16)));61double magnitude(DoubleVec) __attribute__((__overloadable__));62double magnitude(IntVec) __attribute__((__overloadable__));63double test_p6600(DoubleVec d) {64 return magnitude(d) * magnitude(d);65}66 67// PR773868extern int __attribute__((overloadable)) f0(); // expected-error{{'overloadable' function 'f0' must have a prototype}}69typedef int f1_type();70f1_type __attribute__((overloadable)) f1; // expected-error{{'overloadable' function 'f1' must have a prototype}}71 72void test() {73 f0();74 f1();75}76 77// Validate that the invalid function doesn't stay overloadable. 78int __attribute__((overloadable)) invalid(); // expected-error{{'overloadable' function 'invalid' must have a prototype}}79int __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}}81void 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 87void before_local_1(int) __attribute__((overloadable));88void before_local_2(int); // expected-note {{here}}89void before_local_3(int) __attribute__((overloadable));90void 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}98void after_local_1(int) __attribute__((overloadable));99void after_local_2(int);100void after_local_3(int) __attribute__((overloadable));101 102// Make sure we allow C-specific conversions in C.103void 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 C117void 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 139void 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 157void dropping_qualifiers_is_incompatible() {158 const char ccharbuf[1] = {0};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 168void overloadable_with_global() {169 void wg_foo(void) __attribute__((overloadable)); // expected-note{{previous}}170 void wg_foo(int) __attribute__((overloadable));171}172 173int 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#endif178 179void to_foo0(int);180void to_foo0(double) __attribute__((overloadable)); // expected-note{{previous overload}}181void to_foo0(int);182void to_foo0(double); // expected-error{{must have the 'overloadable' attribute}}183void to_foo0(int);184 185void to_foo1(int) __attribute__((overloadable)); // expected-note 2{{previous overload}}186void to_foo1(double);187void to_foo1(int); // expected-error{{must have the 'overloadable' attribute}}188void to_foo1(double);189void to_foo1(int); // expected-error{{must have the 'overloadable' attribute}}190 191void to_foo2(int); // expected-note{{previous unmarked overload}}192void to_foo2(double) __attribute__((overloadable)); // expected-note 2{{previous overload}}193void to_foo2(int) __attribute__((overloadable)); // expected-error {{must not have the 'overloadable' attribute}}194void to_foo2(double); // expected-error{{must have the 'overloadable' attribute}}195void to_foo2(int);196void to_foo2(double); // expected-error{{must have the 'overloadable' attribute}}197void to_foo2(int);198 199void to_foo3(int);200void to_foo3(double) __attribute__((overloadable)); // expected-note{{previous overload}}201void to_foo3(int);202void to_foo3(double); // expected-error{{must have the 'overloadable' attribute}}203 204void to_foo4(int) __attribute__((overloadable)); // expected-note{{previous overload}}205void to_foo4(int); // expected-error{{must have the 'overloadable' attribute}}206void to_foo4(double) __attribute__((overloadable));207 208void to_foo5(int);209void to_foo5(int); // expected-note 3{{previous unmarked overload}}210void to_foo5(float) __attribute__((overloadable));211void to_foo5(double); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}212void to_foo5(float) __attribute__((overloadable));213void to_foo5(short); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}214void to_foo5(long); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}215void to_foo5(double) __attribute__((overloadable));216 217void to_foo6(int) __attribute__((enable_if(1, ""), overloadable)); // expected-note{{previous overload}}218void to_foo6(int) __attribute__((enable_if(1, ""))); // expected-error{{must have the 'overloadable' attribute}}219void to_foo6(int) __attribute__((enable_if(1, ""), overloadable));220 221void to_foo7(int) __attribute__((enable_if(1, ""))); // expected-note{{previous unmarked overload}}222void to_foo7(int) __attribute__((enable_if(1, ""), overloadable)); // expected-error{{must not have the 'overloadable' attribute}}223void to_foo7(int) __attribute__((enable_if(1, "")));224 225void to_foo8(char *__attribute__((pass_object_size(0))))226 __attribute__((enable_if(1, "")));227void to_foo8(char *__attribute__((pass_object_size(0))))228 __attribute__((overloadable));229 230void to_foo9(int); // expected-note{{previous unmarked overload}}231// FIXME: It would be nice if we did better with the "previous unmarked232// overload" diag.233void to_foo9(int) __attribute__((overloadable)); // expected-error{{must not have the 'overloadable' attribute}} expected-note{{previous declaration}} expected-note{{previous unmarked overload}}234void to_foo9(float); // expected-error{{conflicting types for 'to_foo9'}}235void to_foo9(float) __attribute__((overloadable));236void to_foo9(double); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}237void to_foo9(double) __attribute__((overloadable));238 239void to_foo10(int) __attribute__((overloadable));240void to_foo10(double); // expected-note{{previous unmarked overload}}241// no "note: previous redecl" if no previous decl has `overloadable`242// spelled out243void to_foo10(float); // expected-error{{at most one overload for a given name may lack the 'overloadable' attribute}}244void to_foo10(float); // expected-error{{must have the 'overloadable' attribute}}245void 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 address249// taken.250void 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// PR53805263// We previously failed to consider the attribute being written before the264// declaration when considering whether to allow a variadic signature with no265// other parameters, and so we handled these cases differently.266__attribute__((overloadable)) void can_overload_1(...); // ok, was previously rejected267void can_overload_2(...) __attribute__((overloadable)); // ok268[[clang::overloadable]] void can_overload_3(...); // ok, was previously rejected269void can_overload_4 [[clang::overloadable]] (...); // ok270void cannot_overload(...) [[clang::overloadable]]; // expected-error {{ISO C requires a named parameter before '...'}} \271 // expected-error {{'clang::overloadable' attribute cannot be applied to types}}272