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 void __vectorcall free_func_vectorcall(); // expected-note 2 {{previous declaration is here}}
14
15 void __cdecl free_func_default();
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
19 void free_func_cdecl();
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
23 void free_func_stdcall();
24 void __cdecl free_func_stdcall(); // expected-error {{function declared 'cdecl' here was previously declared 'stdcall'}}
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
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
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
42 typedef void void_fun_t();
43 typedef void __cdecl cdecl_fun_t();
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}}
53 void __vectorcall member_vectorcall1();
54 void __vectorcall member_vectorcall2(); // expected-note {{previous declaration is here}}
55
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;
61
62 // Static member functions can't be __thiscall
63 static void static_member_default1();
64 static void static_member_default2();
65 static void static_member_default3(); // expected-note {{previous declaration is here}}
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, ...);
77 };
78
member_default1()79 void __cdecl S::member_default1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
member_default2()80 void __thiscall S::member_default2() {}
81
member_typedef_default()82 void __cdecl S::member_typedef_default() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
member_typedef_cdecl1()83 void __cdecl S::member_typedef_cdecl1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
member_typedef_cdecl2()84 void __cdecl S::member_typedef_cdecl2() {}
member_typedef_stdcall()85 void __stdcall S::member_typedef_stdcall() {}
86
member_cdecl1()87 void S::member_cdecl1() {}
member_cdecl2()88 void __thiscall S::member_cdecl2() {} // expected-error {{function declared 'thiscall' here was previously declared 'cdecl'}}
89
member_thiscall1()90 void S::member_thiscall1() {}
member_thiscall2()91 void __cdecl S::member_thiscall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'thiscall'}}
92
member_vectorcall1()93 void S::member_vectorcall1() {}
member_vectorcall2()94 void __cdecl S::member_vectorcall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'vectorcall'}}
95
static_member_default1()96 void S::static_member_default1() {}
static_member_default2()97 void __cdecl S::static_member_default2() {}
static_member_default3()98 void __stdcall S::static_member_default3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
99
static_member_cdecl1()100 void S::static_member_cdecl1() {}
static_member_cdecl2()101 void __stdcall S::static_member_cdecl2() {} // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}}
102
member_variadic_default(int x,...)103 void __cdecl S::member_variadic_default(int x, ...) { (void)x; }
member_variadic_cdecl(int x,...)104 void S::member_variadic_cdecl(int x, ...) { (void)x; }
105
static_member_variadic_default(int x,...)106 void __cdecl S::static_member_variadic_default(int x, ...) { (void)x; }
static_member_variadic_cdecl(int x,...)107 void S::static_member_variadic_cdecl(int x, ...) { (void)x; }
108
109 // Declare a template using a calling convention.
mystrlen(const CharT * str)110 template <class CharT> inline int __cdecl mystrlen(const CharT *str) {
111 int i;
112 for (i = 0; str[i]; i++) { }
113 return i;
114 }
115 extern int sse_strlen(const char *str);
mystrlen(const char * str)116 template <> inline int __cdecl mystrlen(const char *str) {
117 return sse_strlen(str);
118 }
use_tmpl(const char * str,const int * ints)119 void use_tmpl(const char *str, const int *ints) {
120 mystrlen(str);
121 mystrlen(ints);
122 }
123
124 struct MixedCCStaticOverload {
125 static void overloaded(int a);
126 static void __stdcall overloaded(short a);
127 };
128
overloaded(int a)129 void MixedCCStaticOverload::overloaded(int a) {}
overloaded(short a)130 void MixedCCStaticOverload::overloaded(short a) {}
131
132 // Friend function decls are cdecl by default, not thiscall. Friend method
133 // decls should always be redeclarations, because the class cannot be
134 // incomplete.
135 struct FriendClass {
friend_methodFriendClass136 void friend_method() {}
137 };
friend_stdcall1()138 void __stdcall friend_stdcall1() {}
139 class MakeFriendDecls {
140 int x;
141 friend void FriendClass::friend_method();
142 friend void friend_default();
143 friend void friend_stdcall1();
144 friend void __stdcall friend_stdcall2();
145 friend void friend_stdcall3(); // expected-note {{previous declaration is here}}
146 };
friend_default()147 void friend_default() {}
friend_stdcall3()148 void __stdcall friend_stdcall3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
friend_stdcall2()149 void __stdcall friend_stdcall2() {}
150
151 // Test functions with multiple attributes.
152 void __attribute__((noreturn)) __stdcall __attribute__((regparm(1))) multi_attribute(int x);
multi_attribute(int x)153 void multi_attribute(int x) { __builtin_unreachable(); }
154
155
156 // expected-error@+3 {{vectorcall and cdecl attributes are not compatible}}
157 // expected-error@+2 {{stdcall and cdecl attributes are not compatible}}
158 // expected-error@+1 {{fastcall and cdecl attributes are not compatible}}
159 void __cdecl __cdecl __stdcall __cdecl __fastcall __vectorcall multi_cc(int x);
160
StdcallTemplate(T)161 template <typename T> void __stdcall StdcallTemplate(T) {}
StdcallTemplate(int)162 template <> void StdcallTemplate<int>(int) {}
StdcallTemplate(short)163 template <> void __stdcall StdcallTemplate<short>(short) {}
164
165 // FIXME: Note the template, not the implicit instantiation.
166 // expected-error@+2 {{function declared 'cdecl' here was previously declared 'stdcall}}
167 // expected-note@+1 {{previous declaration is here}}
StdcallTemplate(long)168 template <> void __cdecl StdcallTemplate<long>(long) {}
169
170 struct ExactlyInt {
cast_to_intExactlyInt171 template <typename T> static int cast_to_int(T) {
172 return T::this_is_not_an_int();
173 }
174 };
cast_to_int(int x)175 template <> inline int ExactlyInt::cast_to_int<int>(int x) { return x; }
176
177 namespace test2 {
178 class foo {
179 template <typename T> void bar(T v);
180 };
181 extern template void foo::bar(const void *);
182 }
183
184 namespace test3 {
185 struct foo {
186 typedef void bar();
187 };
188 bool zed(foo::bar *);
bah()189 void bah() {}
baz()190 void baz() { zed(bah); }
191 }
192
193 namespace test4 {
194 class foo {
195 template <typename T> static void bar(T v);
196 };
197 extern template void foo::bar(const void *);
198 }
199
200 namespace test5 {
201 template <class T>
202 class valarray {
203 void bar();
204 };
205 extern template void valarray<int>::bar();
206 }
207
208 namespace test6 {
209 struct foo {
210 int bar();
211 };
212 typedef int bar_t();
zed(bar_t foo::*)213 void zed(bar_t foo::*) {
214 }
baz()215 void baz() {
216 zed(&foo::bar);
217 }
218 }
219
220 namespace test7 {
221 template <typename T>
222 struct S {
ftest7::S223 void f(T t) {
224 t = 42;
225 }
226 };
227 template<> void S<void*>::f(void*);
g(S<void * > s,void * p)228 void g(S<void*> s, void* p) {
229 s.f(p);
230 }
231 }
232
233 namespace test8 {
234 template <typename T>
235 struct S {
ftest8::S236 void f(T t) { // expected-note {{previous declaration is here}}
237 t = 42; // expected-error {{assigning to 'void *' from incompatible type 'int'}}
238 }
239 };
240 template<> void __cdecl S<void*>::f(void*); // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
g(S<void * > s,void * p)241 void g(S<void*> s, void* p) {
242 s.f(p); // expected-note {{in instantiation of member function 'test8::S<void *>::f' requested here}}
243 }
244 }
245