blob: 4282047278698a559413ca434c1c2bf9791b4c4d [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001// RUN: %clang_cc1 -triple i686-pc-win32 -fms-extensions -verify %s
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +00002
Reid Kleckneref072032013-08-27 23:08:25 +00003typedef void void_fun_t();
4typedef void __cdecl cdecl_fun_t();
5
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +00006// Pointers to free functions
Reid Kleckneref072032013-08-27 23:08:25 +00007void free_func_default(); // expected-note 2 {{previous declaration is here}}
8void __cdecl free_func_cdecl(); // expected-note 2 {{previous declaration is here}}
9void __stdcall free_func_stdcall(); // expected-note 2 {{previous declaration is here}}
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000010void __fastcall free_func_fastcall(); // expected-note 2 {{previous declaration is here}}
11
Reid Kleckneref072032013-08-27 23:08:25 +000012void __cdecl free_func_default();
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000013void __stdcall free_func_default(); // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
14void __fastcall free_func_default(); // expected-error {{function declared 'fastcall' here was previously declared without calling convention}}
15
Reid Kleckneref072032013-08-27 23:08:25 +000016void free_func_cdecl();
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000017void __stdcall free_func_cdecl(); // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}}
18void __fastcall free_func_cdecl(); // expected-error {{function declared 'fastcall' here was previously declared 'cdecl'}}
19
Reid Kleckneref072032013-08-27 23:08:25 +000020void free_func_stdcall();
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000021void __cdecl free_func_stdcall(); // expected-error {{function declared 'cdecl' here was previously declared 'stdcall'}}
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000022void __fastcall free_func_stdcall(); // expected-error {{function declared 'fastcall' here was previously declared 'stdcall'}}
23
24void __cdecl free_func_fastcall(); // expected-error {{function declared 'cdecl' here was previously declared 'fastcall'}}
25void __stdcall free_func_fastcall(); // expected-error {{function declared 'stdcall' here was previously declared 'fastcall'}}
26void free_func_fastcall();
27
28// Overloaded functions may have different calling conventions
29void __fastcall free_func_default(int);
30void __cdecl free_func_default(int *);
31
32void __thiscall free_func_cdecl(char *);
33void __cdecl free_func_cdecl(double);
34
Reid Klecknerc66e7e92013-07-31 21:00:18 +000035typedef void void_fun_t();
36typedef void __cdecl cdecl_fun_t();
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000037
38// Pointers to member functions
39struct 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 Klecknerc66e7e92013-07-31 21:00:18 +000046
Reid Kleckneref072032013-08-27 23:08:25 +000047 // 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 Klecknerc66e7e92013-07-31 21:00:18 +000052
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000053 // Static member functions can't be __thiscall
54 static void static_member_default1();
Reid Kleckneref072032013-08-27 23:08:25 +000055 static void static_member_default2();
56 static void static_member_default3(); // expected-note {{previous declaration is here}}
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000057 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
70void __cdecl S::member_default1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
71void __thiscall S::member_default2() {}
72
Reid Kleckneref072032013-08-27 23:08:25 +000073void __cdecl S::member_typedef_default() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
74void __cdecl S::member_typedef_cdecl1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
75void __cdecl S::member_typedef_cdecl2() {}
Reid Klecknerc66e7e92013-07-31 21:00:18 +000076void __stdcall S::member_typedef_stdcall() {}
77
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000078void S::member_cdecl1() {}
79void __thiscall S::member_cdecl2() {} // expected-error {{function declared 'thiscall' here was previously declared 'cdecl'}}
80
81void S::member_thiscall1() {}
82void __cdecl S::member_thiscall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'thiscall'}}
83
Reid Kleckneref072032013-08-27 23:08:25 +000084void S::static_member_default1() {}
85void __cdecl S::static_member_default2() {}
86void __stdcall S::static_member_default3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000087
88void S::static_member_cdecl1() {}
89void __stdcall S::static_member_cdecl2() {} // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}}
90
Reid Kleckneref072032013-08-27 23:08:25 +000091void __cdecl S::member_variadic_default(int x, ...) { (void)x; }
92void S::member_variadic_cdecl(int x, ...) { (void)x; }
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000093
Reid Kleckneref072032013-08-27 23:08:25 +000094void __cdecl S::static_member_variadic_default(int x, ...) { (void)x; }
95void S::static_member_variadic_cdecl(int x, ...) { (void)x; }
Timur Iskhodzhanov33b52ad2013-02-22 12:42:50 +000096
Reid Klecknerc66e7e92013-07-31 21:00:18 +000097// Declare a template using a calling convention.
98template <class CharT> inline int __cdecl mystrlen(const CharT *str) {
99 int i;
100 for (i = 0; str[i]; i++) { }
101 return i;
102}
103extern int sse_strlen(const char *str);
104template <> inline int __cdecl mystrlen(const char *str) {
105 return sse_strlen(str);
106}
107void use_tmpl(const char *str, const int *ints) {
108 mystrlen(str);
109 mystrlen(ints);
110}
Reid Kleckneref072032013-08-27 23:08:25 +0000111
112struct MixedCCStaticOverload {
113 static void overloaded(int a);
114 static void __stdcall overloaded(short a);
115};
116
117void MixedCCStaticOverload::overloaded(int a) {}
118void 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.
123struct FriendClass {
124 void friend_method() {}
125};
126void __stdcall friend_stdcall1() {}
127class 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};
135void friend_default() {}
136void __stdcall friend_stdcall3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
137void __stdcall friend_stdcall2() {}
138
139// Test functions with multiple attributes.
140void __attribute__((noreturn)) __stdcall __attribute__((regparm(1))) multi_attribute(int x);
141void 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}}
146void __cdecl __cdecl __stdcall __cdecl __fastcall multi_cc(int x);
Reid Kleckner35bc0052013-09-10 01:04:45 +0000147
148template <typename T> void __stdcall StdcallTemplate(T) {}
149template <> void StdcallTemplate<int>(int) {}
150template <> 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}}
155template <> void __cdecl StdcallTemplate<long>(long) {}
156
157struct ExactlyInt {
158 template <typename T> static int cast_to_int(T) {
159 return T::this_is_not_an_int();
160 }
161};
162template <> inline int ExactlyInt::cast_to_int<int>(int x) { return x; }
Rafael Espindolacdf7cfc2013-11-18 20:05:33 +0000163
164namespace test2 {
165 class foo {
166 template <typename T> void bar(T v);
167 };
168 extern template void foo::bar(const void *);
169}
Rafael Espindolaf4966552013-11-18 22:40:04 +0000170
171namespace test3 {
172 struct foo {
173 typedef void bar();
174 };
175 bool zed(foo::bar *);
176 void bah() {}
177 void baz() { zed(bah); }
178}
Bill Wendling579d45f2013-11-20 06:45:19 +0000179
180namespace test4 {
181 class foo {
182 template <typename T> static void bar(T v);
183 };
184 extern template void foo::bar(const void *);
185}
Bill Wendling37c07bf2013-12-02 02:05:28 +0000186
187namespace test5 {
188 template <class T>
189 class valarray {
190 void bar();
191 };
192 extern template void valarray<int>::bar();
193}
Stephen Hines651f13c2014-04-23 16:59:28 -0700194
195namespace 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
207namespace 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
220namespace 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}