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