blob: c5b45a2905c8bbf3e2fddaf1e52502ebc2f63257 [file] [log] [blame]
Eli Friedmancef3a7b2012-11-02 01:34:28 +00001// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -fms-extensions -fexceptions -fcxx-exceptions
Douglas Gregor5c0ca522010-08-30 14:44:26 +00002
3
4// ::type_info is predeclared with forward class declartion
5void f(const type_info &a);
6
Francois Picheteedd4672011-03-19 23:05:18 +00007
8// Microsoft doesn't validate exception specification.
Francois Pichet0f161592011-05-24 02:11:43 +00009namespace microsoft_exception_spec {
10
Douglas Gregor5b6f7692010-08-30 15:04:51 +000011void foo(); // expected-note {{previous declaration}}
Francois Picheteedd4672011-03-19 23:05:18 +000012void foo() throw(); // expected-warning {{exception specification in declaration does not match previous declaration}}
Douglas Gregor5c0ca522010-08-30 14:44:26 +000013
Francois Picheteedd4672011-03-19 23:05:18 +000014void r6() throw(...); // expected-note {{previous declaration}}
15void r6() throw(int); // expected-warning {{exception specification in declaration does not match previous declaration}}
Douglas Gregor5b6f7692010-08-30 15:04:51 +000016
17struct Base {
18 virtual void f2();
19 virtual void f3() throw(...);
20};
21
22struct Derived : Base {
23 virtual void f2() throw(...);
24 virtual void f3();
25};
Douglas Gregorafac01d2010-09-01 16:29:03 +000026
Francois Pichet0f161592011-05-24 02:11:43 +000027class A {
28 virtual ~A() throw(); // expected-note {{overridden virtual function is here}}
29};
30
31class B : public A {
32 virtual ~B(); // expected-warning {{exception specification of overriding function is more lax than base version}}
33};
34
35}
Francois Pichet538e0d02010-09-08 11:32:25 +000036
37// MSVC allows type definition in anonymous union and struct
38struct A
39{
40 union
41 {
42 int a;
43 struct B // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
44 {
45 int c;
46 } d;
47
48 union C // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
49 {
50 int e;
51 int ee;
52 } f;
53
54 typedef int D; // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
55 struct F; // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
56 };
57
58 struct
59 {
60 int a2;
61
62 struct B2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
63 {
64 int c2;
65 } d2;
66
67 union C2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
68 {
69 int e2;
70 int ee2;
71 } f2;
72
73 typedef int D2; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
74 struct F2; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
75 };
76};
77
Douglas Gregorafac01d2010-09-01 16:29:03 +000078// __stdcall handling
79struct M {
80 int __stdcall addP();
81 float __stdcall subtractP();
82};
83
Francois Pichet3bd9aa42011-08-18 09:59:55 +000084// __unaligned handling
85typedef char __unaligned *aligned_type;
86
87
Douglas Gregorafac01d2010-09-01 16:29:03 +000088template<typename T> void h1(T (__stdcall M::* const )()) { }
89
90void m1() {
91 h1<int>(&M::addP);
92 h1(&M::subtractP);
93}
Francois Pichet8dc3abc2010-09-12 05:06:55 +000094
Francois Pichet842e7a22010-10-18 15:01:13 +000095
96
Francois Pichet5be38be2011-01-17 01:08:01 +000097
98
99void f(long long);
100void f(int);
101
102int main()
103{
104 // This is an ambiguous call in standard C++.
105 // This calls f(long long) in Microsoft mode because LL is always signed.
106 f(0xffffffffffffffffLL);
107 f(0xffffffffffffffffi64);
108}
Douglas Gregor86f208c2011-02-22 20:32:04 +0000109
110// Enumeration types with a fixed underlying type.
111const int seventeen = 17;
112typedef int Int;
113
114struct X0 {
Eli Friedmancef3a7b2012-11-02 01:34:28 +0000115 enum E1 : Int { SomeOtherValue } field; // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}}
Douglas Gregor86f208c2011-02-22 20:32:04 +0000116 enum E1 : seventeen;
117};
118
Eli Friedmancef3a7b2012-11-02 01:34:28 +0000119enum : long long { // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}}
Douglas Gregor86f208c2011-02-22 20:32:04 +0000120 SomeValue = 0x100000000
121};
Francois Pichetb613cd62011-03-29 10:39:17 +0000122
123
124class AAA {
125__declspec(dllimport) void f(void) { }
126void f2(void);
127};
128
129__declspec(dllimport) void AAA::f2(void) { // expected-error {{dllimport attribute can be applied only to symbol}}
130
131}
132
133
134
Francois Pichet8d051e02011-04-10 03:03:52 +0000135template <class T>
136class BB {
137public:
138 void f(int g = 10 ); // expected-note {{previous definition is here}}
139};
140
141template <class T>
142void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}}
143
Francois Pichet6943e9b2011-04-13 02:38:49 +0000144
Francois Pichet2e510a02011-04-22 08:14:00 +0000145
146extern void static_func();
147void static_func(); // expected-note {{previous declaration is here}}
148
149
150static void static_func() // expected-warning {{static declaration of 'static_func' follows non-static declaration}}
151{
152
Francois Picheta8ef3ac2011-05-08 22:52:41 +0000153}
154
155long function_prototype(int a);
156long (*function_ptr)(int a);
157
158void function_to_voidptr_conv() {
159 void *a1 = function_prototype;
160 void *a2 = &function_prototype;
Francois Pichetb594fac2011-05-08 23:15:10 +0000161 void *a3 = function_ptr;
Francois Picheta8ef3ac2011-05-08 22:52:41 +0000162}
Francois Pichet30aff5b2011-05-11 22:13:54 +0000163
164
165void pointer_to_integral_type_conv(char* ptr) {
166 char ch = (char)ptr;
167 short sh = (short)ptr;
168 ch = (char)ptr;
169 sh = (short)ptr;
Francois Pichetb2ee8302011-05-23 03:43:44 +0000170
Hans Wennborg649c6c52013-06-06 09:16:36 +0000171 // These are valid C++.
172 bool b = (bool)ptr;
173 b = static_cast<bool>(ptr);
174
175 // This is bad.
176 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
177}
Francois Pichetb4746032011-06-01 04:14:20 +0000178
179namespace friend_as_a_forward_decl {
180
181class A {
182 class Nested {
183 friend class B;
184 B* b;
185 };
186 B* b;
187};
188B* global_b;
189
190
191void f()
192{
193 class Local {
194 friend class Z;
195 Z* b;
196 };
197 Z* b;
198}
199
Francois Pichetc985b882011-09-13 10:26:51 +0000200}
201
Douglas Gregora2b4e5d2011-10-17 17:09:53 +0000202struct PR11150 {
203 class X {
204 virtual void f() = 0;
205 };
206
207 int array[__is_abstract(X)? 1 : -1];
208};
Douglas Gregorb57791e2011-10-21 03:57:52 +0000209
210void f() { int __except = 0; }
211
Douglas Gregor75379452012-09-13 20:16:20 +0000212void ::f(); // expected-warning{{extra qualification on member 'f'}}
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +0000213
214class C {
215 C::C(); // expected-warning{{extra qualification on member 'C'}}
216};
John McCall76da55d2013-04-16 07:28:30 +0000217
218struct StructWithProperty {
219 __declspec(property(get=GetV)) int V1;
220 __declspec(property(put=SetV)) int V2;
221 __declspec(property(get=GetV, put=SetV_NotExist)) int V3;
222 __declspec(property(get=GetV_NotExist, put=SetV)) int V4;
223 __declspec(property(get=GetV, put=SetV)) int V5;
224
225 int GetV() { return 123; }
226 void SetV(int i) {}
227};
228void TestProperty() {
229 StructWithProperty sp;
230 int i = sp.V2; // expected-error{{no getter defined for property 'V2'}}
231 sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}}
232 int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}}
233 sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}}
234 int k = sp.V5;
235 sp.V5 = k++;
236}
237
238/* 4 tests for PseudoObject, begin */
239struct SP1
240{
241 bool operator()() { return true; }
242};
243struct SP2
244{
245 __declspec(property(get=GetV)) SP1 V;
246 SP1 GetV() { return SP1(); }
247};
248void TestSP2() {
249 SP2 sp2;
250 bool b = sp2.V();
251}
252
253struct SP3 {
254 template <class T>
255 void f(T t) {}
256};
257template <class T>
258struct SP4
259{
260 __declspec(property(get=GetV)) int V;
261 int GetV() { return 123; }
262 void f() { SP3 s2; s2.f(V); }
263};
264void TestSP4() {
265 SP4<int> s;
266 s.f();
267}
268
269template <class T>
270struct SP5
271{
272 __declspec(property(get=GetV)) T V;
273 int GetV() { return 123; }
274 void f() { int *p = new int[V]; }
275};
276
277template <class T>
278struct SP6
279{
280public:
281 __declspec(property(get=GetV)) T V;
282 T GetV() { return 123; }
283 void f() { int t = V; }
284};
285void TestSP6() {
286 SP6<int> c;
287 c.f();
288}
289/* 4 tests for PseudoObject, end */
290
291// Property access: explicit, implicit, with Qualifier
292struct SP7 {
293 __declspec(property(get=GetV, put=SetV)) int V;
294 int GetV() { return 123; }
295 void SetV(int v) {}
296
297 void ImplicitAccess() { int i = V; V = i; }
298 void ExplicitAccess() { int i = this->V; this->V = i; }
299};
300struct SP8: public SP7 {
301 void AccessWithQualifier() { int i = SP7::V; SP7::V = i; }
302};
303
304// Property usage
305template <class T>
306struct SP9 {
307 __declspec(property(get=GetV, put=SetV)) T V;
308 T GetV() { return 0; }
309 void SetV(T v) {}
310 void f() { V = this->V; V < this->V; }
John McCalld314abe2013-04-16 22:32:04 +0000311 void g() { V++; }
312 void h() { V*=2; }
John McCall76da55d2013-04-16 07:28:30 +0000313};
314struct SP10 {
315 SP10(int v) {}
316 bool operator<(const SP10& v) { return true; }
317 SP10 operator*(int v) { return *this; }
John McCalld314abe2013-04-16 22:32:04 +0000318 SP10 operator+(int v) { return *this; }
John McCall76da55d2013-04-16 07:28:30 +0000319 SP10& operator=(const SP10& v) { return *this; }
320};
321void TestSP9() {
322 SP9<int> c;
323 int i = c.V; // Decl initializer
324 i = c.V; // Binary op operand
325 c.SetV(c.V); // CallExpr arg
326 int *p = new int[c.V + 1]; // Array size
327 p[c.V] = 1; // Array index
328
329 c.V = 123; // Setter
330
331 c.V++; // Unary op operand
332 c.V *= 2; // Unary op operand
333
334 SP9<int*> c2;
335 c2.V[0] = 123; // Array
336
337 SP9<SP10> c3;
338 c3.f(); // Overloaded binary op operand
John McCalld314abe2013-04-16 22:32:04 +0000339 c3.g(); // Overloaded incdec op operand
340 c3.h(); // Overloaded unary op operand
John McCall76da55d2013-04-16 07:28:30 +0000341}
Aaron Ballman76eed422013-05-30 16:20:00 +0000342
343union u {
344 int *i1;
345 int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
346};
Reid Klecknerf67129a2013-06-19 16:37:23 +0000347
348// Property getter using reference.
349struct SP11 {
350 __declspec(property(get=GetV)) int V;
351 int _v;
352 int& GetV() { return _v; }
353 void UseV();
354 void TakePtr(int *) {}
355 void TakeRef(int &) {}
356 void TakeVal(int) {}
357};
358
359void SP11::UseV() {
360 TakePtr(&V);
361 TakeRef(V);
362 TakeVal(V);
363}
Aaron Ballmanb4f0f3f2013-06-26 21:28:44 +0000364
365struct StructWithUnnamedMember {
366 __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}}
367};
Douglas Gregord9433522013-06-27 20:42:30 +0000368
369namespace rdar14250378 {
370 class Bar {};
371
372 namespace NyNamespace {
373 class Foo {
374 public:
375 Bar* EnsureBar();
376 };
377
378 class Baz : public Foo {
379 public:
380 friend class Bar;
381 };
382
383 Bar* Foo::EnsureBar() {
384 return 0;
385 }
386 }
387}
David Majnemer7121bdb2013-10-18 00:33:31 +0000388
389// expected-error@+1 {{'sealed' keyword not permitted with interface types}}
390__interface InterfaceWithSealed sealed {
391};
392
393struct SomeBase {
394 virtual void OverrideMe();
395
396 // expected-note@+2 {{overridden virtual function is here}}
397 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
398 virtual void SealedFunction() sealed;
399};
400
401// expected-note@+2 {{'SealedType' declared here}}
402// expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
403struct SealedType sealed : SomeBase {
404 // expected-error@+1 {{declaration of 'SealedFunction' overrides a 'sealed' function}}
405 virtual void SealedFunction();
406
407 // expected-warning@+1 {{'override' keyword is a C++11 extension}}
408 virtual void OverrideMe() override;
409};
410
411// expected-error@+1 {{base 'SealedType' is marked 'sealed'}}
412struct InheritFromSealed : SealedType {};