Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -triple i686-pc-win32 -fms-extensions -verify %s
      2 // RUN: %clang_cc1 -triple i686-pc-mingw32 -verify %s
      3 // RUN: %clang_cc1 -triple i686-pc-mingw32 -fms-extensions -verify %s
      4 
      5 typedef void void_fun_t();
      6 typedef void __cdecl cdecl_fun_t();
      7 
      8 // Pointers to free functions
      9 void            free_func_default(); // expected-note 2 {{previous declaration is here}}
     10 void __cdecl    free_func_cdecl(); // expected-note 2 {{previous declaration is here}}
     11 void __stdcall  free_func_stdcall(); // expected-note 2 {{previous declaration is here}}
     12 void __fastcall free_func_fastcall(); // expected-note 2 {{previous declaration is here}}
     13 
     14 void __cdecl    free_func_default();
     15 void __stdcall  free_func_default(); // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
     16 void __fastcall free_func_default(); // expected-error {{function declared 'fastcall' here was previously declared without calling convention}}
     17 
     18 void            free_func_cdecl();
     19 void __stdcall  free_func_cdecl(); // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}}
     20 void __fastcall free_func_cdecl(); // expected-error {{function declared 'fastcall' here was previously declared 'cdecl'}}
     21 
     22 void            free_func_stdcall();
     23 void __cdecl    free_func_stdcall(); // expected-error {{function declared 'cdecl' here was previously declared 'stdcall'}}
     24 void __fastcall free_func_stdcall(); // expected-error {{function declared 'fastcall' here was previously declared 'stdcall'}}
     25 
     26 void __cdecl    free_func_fastcall(); // expected-error {{function declared 'cdecl' here was previously declared 'fastcall'}}
     27 void __stdcall  free_func_fastcall(); // expected-error {{function declared 'stdcall' here was previously declared 'fastcall'}}
     28 void            free_func_fastcall();
     29 
     30 // Overloaded functions may have different calling conventions
     31 void __fastcall free_func_default(int);
     32 void __cdecl    free_func_default(int *);
     33 
     34 void __thiscall free_func_cdecl(char *);
     35 void __cdecl    free_func_cdecl(double);
     36 
     37 typedef void void_fun_t();
     38 typedef void __cdecl cdecl_fun_t();
     39 
     40 // Pointers to member functions
     41 struct S {
     42   void            member_default1(); // expected-note {{previous declaration is here}}
     43   void            member_default2();
     44   void __cdecl    member_cdecl1();
     45   void __cdecl    member_cdecl2(); // expected-note {{previous declaration is here}}
     46   void __thiscall member_thiscall1();
     47   void __thiscall member_thiscall2(); // expected-note {{previous declaration is here}}
     48 
     49   // Typedefs carrying the __cdecl convention are adjusted to __thiscall.
     50   void_fun_t           member_typedef_default; // expected-note {{previous declaration is here}}
     51   cdecl_fun_t          member_typedef_cdecl1;  // expected-note {{previous declaration is here}}
     52   cdecl_fun_t __cdecl  member_typedef_cdecl2;
     53   void_fun_t __stdcall member_typedef_stdcall;
     54 
     55   // Static member functions can't be __thiscall
     56   static void            static_member_default1();
     57   static void            static_member_default2();
     58   static void            static_member_default3(); // expected-note {{previous declaration is here}}
     59   static void __cdecl    static_member_cdecl1();
     60   static void __cdecl    static_member_cdecl2(); // expected-note {{previous declaration is here}}
     61   static void __stdcall  static_member_stdcall1();
     62   static void __stdcall  static_member_stdcall2();
     63 
     64   // Variadic functions can't be other than default or __cdecl
     65   void            member_variadic_default(int x, ...);
     66   void __cdecl    member_variadic_cdecl(int x, ...);
     67 
     68   static void            static_member_variadic_default(int x, ...);
     69   static void __cdecl    static_member_variadic_cdecl(int x, ...);
     70 };
     71 
     72 void __cdecl    S::member_default1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
     73 void __thiscall S::member_default2() {}
     74 
     75 void __cdecl   S::member_typedef_default() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
     76 void __cdecl   S::member_typedef_cdecl1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
     77 void __cdecl   S::member_typedef_cdecl2() {}
     78 void __stdcall S::member_typedef_stdcall() {}
     79 
     80 void            S::member_cdecl1() {}
     81 void __thiscall S::member_cdecl2() {} // expected-error {{function declared 'thiscall' here was previously declared 'cdecl'}}
     82 
     83 void            S::member_thiscall1() {}
     84 void __cdecl    S::member_thiscall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'thiscall'}}
     85 
     86 void            S::static_member_default1() {}
     87 void __cdecl    S::static_member_default2() {}
     88 void __stdcall  S::static_member_default3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
     89 
     90 void            S::static_member_cdecl1() {}
     91 void __stdcall  S::static_member_cdecl2() {} // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}}
     92 
     93 void __cdecl    S::member_variadic_default(int x, ...) { (void)x; }
     94 void            S::member_variadic_cdecl(int x, ...) { (void)x; }
     95 
     96 void __cdecl    S::static_member_variadic_default(int x, ...) { (void)x; }
     97 void            S::static_member_variadic_cdecl(int x, ...) { (void)x; }
     98 
     99 // Declare a template using a calling convention.
    100 template <class CharT> inline int __cdecl mystrlen(const CharT *str) {
    101   int i;
    102   for (i = 0; str[i]; i++) { }
    103   return i;
    104 }
    105 extern int sse_strlen(const char *str);
    106 template <> inline int __cdecl mystrlen(const char *str) {
    107   return sse_strlen(str);
    108 }
    109 void use_tmpl(const char *str, const int *ints) {
    110   mystrlen(str);
    111   mystrlen(ints);
    112 }
    113 
    114 struct MixedCCStaticOverload {
    115   static void overloaded(int a);
    116   static void __stdcall overloaded(short a);
    117 };
    118 
    119 void MixedCCStaticOverload::overloaded(int a) {}
    120 void MixedCCStaticOverload::overloaded(short a) {}
    121 
    122 // Friend function decls are cdecl by default, not thiscall.  Friend method
    123 // decls should always be redeclarations, because the class cannot be
    124 // incomplete.
    125 struct FriendClass {
    126   void friend_method() {}
    127 };
    128 void __stdcall friend_stdcall1() {}
    129 class MakeFriendDecls {
    130   int x;
    131   friend void FriendClass::friend_method();
    132   friend void              friend_default();
    133   friend void              friend_stdcall1();
    134   friend void __stdcall    friend_stdcall2();
    135   friend void              friend_stdcall3(); // expected-note {{previous declaration is here}}
    136 };
    137 void           friend_default() {}
    138 void __stdcall friend_stdcall3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
    139 void __stdcall friend_stdcall2() {}
    140 
    141 // Test functions with multiple attributes.
    142 void __attribute__((noreturn)) __stdcall __attribute__((regparm(1))) multi_attribute(int x);
    143 void multi_attribute(int x) { __builtin_unreachable(); }
    144 
    145 
    146 // expected-error@+2 {{stdcall and cdecl attributes are not compatible}}
    147 // expected-error@+1 {{fastcall and cdecl attributes are not compatible}}
    148 void __cdecl __cdecl __stdcall __cdecl __fastcall multi_cc(int x);
    149 
    150 template <typename T> void __stdcall StdcallTemplate(T) {}
    151 template <> void StdcallTemplate<int>(int) {}
    152 template <> void __stdcall StdcallTemplate<short>(short) {}
    153 
    154 // FIXME: Note the template, not the implicit instantiation.
    155 // expected-error@+2 {{function declared 'cdecl' here was previously declared 'stdcall}}
    156 // expected-note@+1 {{previous declaration is here}}
    157 template <> void __cdecl StdcallTemplate<long>(long) {}
    158 
    159 struct ExactlyInt {
    160   template <typename T> static int cast_to_int(T) {
    161     return T::this_is_not_an_int();
    162   }
    163 };
    164 template <> inline int ExactlyInt::cast_to_int<int>(int x) { return x; }
    165 
    166 namespace test2 {
    167   class foo {
    168     template <typename T> void bar(T v);
    169   };
    170   extern template void foo::bar(const void *);
    171 }
    172 
    173 namespace test3 {
    174   struct foo {
    175     typedef void bar();
    176   };
    177   bool zed(foo::bar *);
    178   void bah() {}
    179   void baz() { zed(bah); }
    180 }
    181 
    182 namespace test4 {
    183   class foo {
    184     template <typename T> static void bar(T v);
    185   };
    186   extern template void foo::bar(const void *);
    187 }
    188 
    189 namespace test5 {
    190   template <class T>
    191   class valarray {
    192     void bar();
    193   };
    194   extern template void valarray<int>::bar();
    195 }
    196 
    197 namespace test6 {
    198   struct foo {
    199     int bar();
    200   };
    201   typedef int bar_t();
    202   void zed(bar_t foo::*) {
    203   }
    204   void baz() {
    205     zed(&foo::bar);
    206   }
    207 }
    208 
    209 namespace test7 {
    210   template <typename T>
    211   struct S {
    212     void f(T t) {
    213       t = 42;
    214     }
    215   };
    216   template<> void S<void*>::f(void*);
    217   void g(S<void*> s, void* p) {
    218     s.f(p);
    219   }
    220 }
    221 
    222 namespace test8 {
    223   template <typename T>
    224   struct S {
    225     void f(T t) { // expected-note {{previous declaration is here}}
    226       t = 42; // expected-error {{assigning to 'void *' from incompatible type 'int'}}
    227     }
    228   };
    229   template<> void __cdecl S<void*>::f(void*); // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
    230   void g(S<void*> s, void* p) {
    231     s.f(p); // expected-note {{in instantiation of member function 'test8::S<void *>::f' requested here}}
    232   }
    233 }
    234