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