Andrey Bokhanko | ddc04ef | 2015-09-14 21:29:57 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++14 -triple i686-pc-win32 -fms-extensions -DMSABI -verify %s |
Reid Kleckner | d2f6c72 | 2015-04-01 20:22:13 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -std=c++14 -triple i686-pc-mingw32 -verify %s |
| 3 | // RUN: %clang_cc1 -std=c++14 -triple i686-pc-mingw32 -fms-extensions -verify %s |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 4 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 5 | typedef void void_fun_t(); |
| 6 | typedef void __cdecl cdecl_fun_t(); |
| 7 | |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 8 | // Pointers to free functions |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 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}} |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 12 | void __fastcall free_func_fastcall(); // expected-note 2 {{previous declaration is here}} |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 13 | void __vectorcall free_func_vectorcall(); // expected-note 2 {{previous declaration is here}} |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 14 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 15 | void __cdecl free_func_default(); |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 16 | void __stdcall free_func_default(); // expected-error {{function declared 'stdcall' here was previously declared without calling convention}} |
| 17 | void __fastcall free_func_default(); // expected-error {{function declared 'fastcall' here was previously declared without calling convention}} |
| 18 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 19 | void free_func_cdecl(); |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 20 | void __stdcall free_func_cdecl(); // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}} |
| 21 | void __fastcall free_func_cdecl(); // expected-error {{function declared 'fastcall' here was previously declared 'cdecl'}} |
| 22 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 23 | void free_func_stdcall(); |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 24 | void __cdecl free_func_stdcall(); // expected-error {{function declared 'cdecl' here was previously declared 'stdcall'}} |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 25 | void __fastcall free_func_stdcall(); // expected-error {{function declared 'fastcall' here was previously declared 'stdcall'}} |
| 26 | |
| 27 | void __cdecl free_func_fastcall(); // expected-error {{function declared 'cdecl' here was previously declared 'fastcall'}} |
| 28 | void __stdcall free_func_fastcall(); // expected-error {{function declared 'stdcall' here was previously declared 'fastcall'}} |
| 29 | void free_func_fastcall(); |
| 30 | |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 31 | void __cdecl free_func_vectorcall(); // expected-error {{function declared 'cdecl' here was previously declared 'vectorcall'}} |
| 32 | void __stdcall free_func_vectorcall(); // expected-error {{function declared 'stdcall' here was previously declared 'vectorcall'}} |
| 33 | void free_func_vectorcall(); |
| 34 | |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 35 | // Overloaded functions may have different calling conventions |
| 36 | void __fastcall free_func_default(int); |
| 37 | void __cdecl free_func_default(int *); |
| 38 | |
| 39 | void __thiscall free_func_cdecl(char *); |
| 40 | void __cdecl free_func_cdecl(double); |
| 41 | |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 42 | typedef void void_fun_t(); |
| 43 | typedef void __cdecl cdecl_fun_t(); |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 44 | |
| 45 | // Pointers to member functions |
| 46 | struct S { |
| 47 | void member_default1(); // expected-note {{previous declaration is here}} |
| 48 | void member_default2(); |
| 49 | void __cdecl member_cdecl1(); |
| 50 | void __cdecl member_cdecl2(); // expected-note {{previous declaration is here}} |
| 51 | void __thiscall member_thiscall1(); |
| 52 | void __thiscall member_thiscall2(); // expected-note {{previous declaration is here}} |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 53 | void __vectorcall member_vectorcall1(); |
| 54 | void __vectorcall member_vectorcall2(); // expected-note {{previous declaration is here}} |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 55 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 56 | // Typedefs carrying the __cdecl convention are adjusted to __thiscall. |
| 57 | void_fun_t member_typedef_default; // expected-note {{previous declaration is here}} |
| 58 | cdecl_fun_t member_typedef_cdecl1; // expected-note {{previous declaration is here}} |
| 59 | cdecl_fun_t __cdecl member_typedef_cdecl2; |
| 60 | void_fun_t __stdcall member_typedef_stdcall; |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 61 | |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 62 | // Static member functions can't be __thiscall |
| 63 | static void static_member_default1(); |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 64 | static void static_member_default2(); |
| 65 | static void static_member_default3(); // expected-note {{previous declaration is here}} |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 66 | static void __cdecl static_member_cdecl1(); |
| 67 | static void __cdecl static_member_cdecl2(); // expected-note {{previous declaration is here}} |
| 68 | static void __stdcall static_member_stdcall1(); |
| 69 | static void __stdcall static_member_stdcall2(); |
| 70 | |
| 71 | // Variadic functions can't be other than default or __cdecl |
| 72 | void member_variadic_default(int x, ...); |
| 73 | void __cdecl member_variadic_cdecl(int x, ...); |
| 74 | |
| 75 | static void static_member_variadic_default(int x, ...); |
| 76 | static void __cdecl static_member_variadic_cdecl(int x, ...); |
Andrey Bokhanko | ddc04ef | 2015-09-14 21:29:57 +0000 | [diff] [blame] | 77 | |
| 78 | // Structors can't be other than default in MS ABI environment |
| 79 | #ifdef MSABI |
| 80 | __vectorcall S(); // expected-warning {{vectorcall calling convention ignored on constructor/destructor}} |
| 81 | #endif |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | void __cdecl S::member_default1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}} |
| 85 | void __thiscall S::member_default2() {} |
| 86 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 87 | void __cdecl S::member_typedef_default() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}} |
| 88 | void __cdecl S::member_typedef_cdecl1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}} |
| 89 | void __cdecl S::member_typedef_cdecl2() {} |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 90 | void __stdcall S::member_typedef_stdcall() {} |
| 91 | |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 92 | void S::member_cdecl1() {} |
| 93 | void __thiscall S::member_cdecl2() {} // expected-error {{function declared 'thiscall' here was previously declared 'cdecl'}} |
| 94 | |
| 95 | void S::member_thiscall1() {} |
| 96 | void __cdecl S::member_thiscall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'thiscall'}} |
| 97 | |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 98 | void S::member_vectorcall1() {} |
| 99 | void __cdecl S::member_vectorcall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'vectorcall'}} |
| 100 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 101 | void S::static_member_default1() {} |
| 102 | void __cdecl S::static_member_default2() {} |
| 103 | void __stdcall S::static_member_default3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}} |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 104 | |
| 105 | void S::static_member_cdecl1() {} |
| 106 | void __stdcall S::static_member_cdecl2() {} // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}} |
| 107 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 108 | void __cdecl S::member_variadic_default(int x, ...) { (void)x; } |
| 109 | void S::member_variadic_cdecl(int x, ...) { (void)x; } |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 110 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 111 | void __cdecl S::static_member_variadic_default(int x, ...) { (void)x; } |
| 112 | void S::static_member_variadic_cdecl(int x, ...) { (void)x; } |
Timur Iskhodzhanov | f2afe63 | 2013-02-22 12:42:50 +0000 | [diff] [blame] | 113 | |
Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 114 | // Declare a template using a calling convention. |
| 115 | template <class CharT> inline int __cdecl mystrlen(const CharT *str) { |
| 116 | int i; |
| 117 | for (i = 0; str[i]; i++) { } |
| 118 | return i; |
| 119 | } |
| 120 | extern int sse_strlen(const char *str); |
| 121 | template <> inline int __cdecl mystrlen(const char *str) { |
| 122 | return sse_strlen(str); |
| 123 | } |
| 124 | void use_tmpl(const char *str, const int *ints) { |
| 125 | mystrlen(str); |
| 126 | mystrlen(ints); |
| 127 | } |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 128 | |
| 129 | struct MixedCCStaticOverload { |
| 130 | static void overloaded(int a); |
| 131 | static void __stdcall overloaded(short a); |
| 132 | }; |
| 133 | |
| 134 | void MixedCCStaticOverload::overloaded(int a) {} |
| 135 | void MixedCCStaticOverload::overloaded(short a) {} |
| 136 | |
| 137 | // Friend function decls are cdecl by default, not thiscall. Friend method |
| 138 | // decls should always be redeclarations, because the class cannot be |
| 139 | // incomplete. |
| 140 | struct FriendClass { |
| 141 | void friend_method() {} |
| 142 | }; |
| 143 | void __stdcall friend_stdcall1() {} |
| 144 | class MakeFriendDecls { |
| 145 | int x; |
| 146 | friend void FriendClass::friend_method(); |
| 147 | friend void friend_default(); |
| 148 | friend void friend_stdcall1(); |
| 149 | friend void __stdcall friend_stdcall2(); |
| 150 | friend void friend_stdcall3(); // expected-note {{previous declaration is here}} |
| 151 | }; |
| 152 | void friend_default() {} |
| 153 | void __stdcall friend_stdcall3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}} |
| 154 | void __stdcall friend_stdcall2() {} |
| 155 | |
| 156 | // Test functions with multiple attributes. |
| 157 | void __attribute__((noreturn)) __stdcall __attribute__((regparm(1))) multi_attribute(int x); |
| 158 | void multi_attribute(int x) { __builtin_unreachable(); } |
| 159 | |
| 160 | |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 161 | // expected-error@+3 {{vectorcall and cdecl attributes are not compatible}} |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 162 | // expected-error@+2 {{stdcall and cdecl attributes are not compatible}} |
| 163 | // expected-error@+1 {{fastcall and cdecl attributes are not compatible}} |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 164 | void __cdecl __cdecl __stdcall __cdecl __fastcall __vectorcall multi_cc(int x); |
Reid Kleckner | 7680805 | 2013-09-10 01:04:45 +0000 | [diff] [blame] | 165 | |
| 166 | template <typename T> void __stdcall StdcallTemplate(T) {} |
| 167 | template <> void StdcallTemplate<int>(int) {} |
| 168 | template <> void __stdcall StdcallTemplate<short>(short) {} |
| 169 | |
| 170 | // FIXME: Note the template, not the implicit instantiation. |
| 171 | // expected-error@+2 {{function declared 'cdecl' here was previously declared 'stdcall}} |
| 172 | // expected-note@+1 {{previous declaration is here}} |
| 173 | template <> void __cdecl StdcallTemplate<long>(long) {} |
| 174 | |
| 175 | struct ExactlyInt { |
| 176 | template <typename T> static int cast_to_int(T) { |
| 177 | return T::this_is_not_an_int(); |
| 178 | } |
| 179 | }; |
| 180 | template <> inline int ExactlyInt::cast_to_int<int>(int x) { return x; } |
Rafael Espindola | f02d8b1 | 2013-11-18 20:05:33 +0000 | [diff] [blame] | 181 | |
| 182 | namespace test2 { |
| 183 | class foo { |
| 184 | template <typename T> void bar(T v); |
| 185 | }; |
| 186 | extern template void foo::bar(const void *); |
| 187 | } |
Rafael Espindola | 4903c87 | 2013-11-18 22:40:04 +0000 | [diff] [blame] | 188 | |
| 189 | namespace test3 { |
| 190 | struct foo { |
| 191 | typedef void bar(); |
| 192 | }; |
| 193 | bool zed(foo::bar *); |
| 194 | void bah() {} |
| 195 | void baz() { zed(bah); } |
| 196 | } |
Rafael Espindola | 92045bc | 2013-11-19 21:07:04 +0000 | [diff] [blame] | 197 | |
| 198 | namespace test4 { |
| 199 | class foo { |
| 200 | template <typename T> static void bar(T v); |
| 201 | }; |
| 202 | extern template void foo::bar(const void *); |
| 203 | } |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 204 | |
| 205 | namespace test5 { |
| 206 | template <class T> |
| 207 | class valarray { |
| 208 | void bar(); |
| 209 | }; |
| 210 | extern template void valarray<int>::bar(); |
| 211 | } |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 212 | |
| 213 | namespace test6 { |
| 214 | struct foo { |
| 215 | int bar(); |
| 216 | }; |
| 217 | typedef int bar_t(); |
| 218 | void zed(bar_t foo::*) { |
| 219 | } |
| 220 | void baz() { |
| 221 | zed(&foo::bar); |
| 222 | } |
| 223 | } |
Rafael Espindola | 6674722 | 2013-12-10 00:59:31 +0000 | [diff] [blame] | 224 | |
| 225 | namespace test7 { |
| 226 | template <typename T> |
| 227 | struct S { |
| 228 | void f(T t) { |
| 229 | t = 42; |
| 230 | } |
| 231 | }; |
| 232 | template<> void S<void*>::f(void*); |
| 233 | void g(S<void*> s, void* p) { |
| 234 | s.f(p); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | namespace test8 { |
| 239 | template <typename T> |
| 240 | struct S { |
| 241 | void f(T t) { // expected-note {{previous declaration is here}} |
| 242 | t = 42; // expected-error {{assigning to 'void *' from incompatible type 'int'}} |
| 243 | } |
| 244 | }; |
| 245 | template<> void __cdecl S<void*>::f(void*); // expected-error {{function declared 'cdecl' here was previously declared without calling convention}} |
| 246 | void g(S<void*> s, void* p) { |
| 247 | s.f(p); // expected-note {{in instantiation of member function 'test8::S<void *>::f' requested here}} |
| 248 | } |
| 249 | } |
Reid Kleckner | d2f6c72 | 2015-04-01 20:22:13 +0000 | [diff] [blame] | 250 | |
| 251 | namespace test9 { |
| 252 | // Used to fail when we forgot to make lambda call operators use __thiscall. |
| 253 | template <typename F> |
| 254 | decltype(auto) deduce(F f) { |
| 255 | return &decltype(f)::operator(); |
| 256 | } |
| 257 | template <typename C, typename R, typename A> |
| 258 | decltype(auto) signaturehelper(R (C::*f)(A) const) { |
| 259 | return R(); |
| 260 | } |
| 261 | void f() { |
| 262 | auto l = [](int x) { return x * 2; }; |
| 263 | decltype(signaturehelper(deduce(l))) p; |
| 264 | } |
| 265 | } |