blob: e12dea1fb62032b0bd7aa96f38e3354bb9c5c2be [file] [log] [blame]
Andrey Bokhanko45d41322016-05-11 18:38:21 +00001// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -fms-extensions -fexceptions -fcxx-exceptions -DTEST1
2// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -fexceptions -fcxx-exceptions -DTEST2
Douglas Gregor2b1bbec2010-08-30 14:44:26 +00003
Andrey Bokhanko45d41322016-05-11 18:38:21 +00004#if TEST1
Douglas Gregor2b1bbec2010-08-30 14:44:26 +00005
Francois Pichet13b4e682011-03-19 23:05:18 +00006// Microsoft doesn't validate exception specification.
Francois Picheta8032e92011-05-24 02:11:43 +00007namespace microsoft_exception_spec {
8
Douglas Gregorf62c5292010-08-30 15:04:51 +00009void foo(); // expected-note {{previous declaration}}
Francois Pichet13b4e682011-03-19 23:05:18 +000010void foo() throw(); // expected-warning {{exception specification in declaration does not match previous declaration}}
Douglas Gregor2b1bbec2010-08-30 14:44:26 +000011
Francois Pichet13b4e682011-03-19 23:05:18 +000012void r6() throw(...); // expected-note {{previous declaration}}
13void r6() throw(int); // expected-warning {{exception specification in declaration does not match previous declaration}}
Douglas Gregorf62c5292010-08-30 15:04:51 +000014
15struct Base {
16 virtual void f2();
17 virtual void f3() throw(...);
18};
19
20struct Derived : Base {
21 virtual void f2() throw(...);
22 virtual void f3();
23};
Douglas Gregoreece0ea2010-09-01 16:29:03 +000024
Francois Picheta8032e92011-05-24 02:11:43 +000025class A {
26 virtual ~A() throw(); // expected-note {{overridden virtual function is here}}
27};
28
29class B : public A {
30 virtual ~B(); // expected-warning {{exception specification of overriding function is more lax than base version}}
31};
32
33}
Francois Pichet4ad4b582010-09-08 11:32:25 +000034
35// MSVC allows type definition in anonymous union and struct
36struct A
37{
Aaron Ballman3e424b52013-12-26 18:30:57 +000038 union
Francois Pichet4ad4b582010-09-08 11:32:25 +000039 {
40 int a;
41 struct B // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
Aaron Ballman3e424b52013-12-26 18:30:57 +000042 {
Francois Pichet4ad4b582010-09-08 11:32:25 +000043 int c;
44 } d;
45
46 union C // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
47 {
48 int e;
49 int ee;
50 } f;
51
52 typedef int D; // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
53 struct F; // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
54 };
55
56 struct
57 {
58 int a2;
59
60 struct B2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
61 {
62 int c2;
63 } d2;
Aaron Ballman3e424b52013-12-26 18:30:57 +000064
Francois Pichet4ad4b582010-09-08 11:32:25 +000065 union C2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
66 {
67 int e2;
68 int ee2;
69 } f2;
70
71 typedef int D2; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
72 struct F2; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
73 };
74};
75
Douglas Gregoreece0ea2010-09-01 16:29:03 +000076// __stdcall handling
77struct M {
78 int __stdcall addP();
Aaron Ballman3e424b52013-12-26 18:30:57 +000079 float __stdcall subtractP();
Douglas Gregoreece0ea2010-09-01 16:29:03 +000080};
81
Francois Pichet17ed0202011-08-18 09:59:55 +000082// __unaligned handling
83typedef char __unaligned *aligned_type;
Nico Rieck3e1ee832014-12-04 23:30:25 +000084typedef struct UnalignedTag { int f; } __unaligned *aligned_type2;
Andrey Bokhanko45d41322016-05-11 18:38:21 +000085typedef char __unaligned aligned_type3;
Francois Pichet17ed0202011-08-18 09:59:55 +000086
Andrey Bokhanko45d41322016-05-11 18:38:21 +000087struct aligned_type4 {
88 int i;
89};
90
91__unaligned int aligned_type4::*p1_aligned_type4 = &aligned_type4::i;
92int aligned_type4::* __unaligned p2_aligned_type4 = &aligned_type4::i;
93__unaligned int aligned_type4::* __unaligned p3_aligned_type4 = &aligned_type4::i;
Andrey Bokhanko67a41862016-05-26 10:06:01 +000094void (aligned_type4::*__unaligned p4_aligned_type4)();
Andrey Bokhanko45d41322016-05-11 18:38:21 +000095
96// Check that __unaligned qualifier can be used for overloading
97void foo_unaligned(int *arg) {}
98void foo_unaligned(__unaligned int *arg) {}
99void foo_unaligned(int arg) {} // expected-note {{previous definition is here}}
100void foo_unaligned(__unaligned int arg) {} // expected-error {{redefinition of 'foo_unaligned'}}
101class A_unaligned {};
102class B_unaligned : public A_unaligned {};
103int foo_unaligned(__unaligned A_unaligned *arg) { return 0; }
104void *foo_unaligned(B_unaligned *arg) { return 0; }
105
106void test_unaligned() {
107 int *p1 = 0;
108 foo_unaligned(p1);
109
110 __unaligned int *p2 = 0;
111 foo_unaligned(p2);
112
113 __unaligned B_unaligned *p3 = 0;
114 int p4 = foo_unaligned(p3);
115
116 B_unaligned *p5 = p3; // expected-error {{cannot initialize a variable of type 'B_unaligned *' with an lvalue of type '__unaligned B_unaligned *'}}
117
118 __unaligned B_unaligned *p6 = p3;
119
120 p1_aligned_type4 = p2_aligned_type4;
121 p2_aligned_type4 = p1_aligned_type4; // expected-error {{assigning to 'int aligned_type4::*' from incompatible type '__unaligned int aligned_type4::*'}}
122 p3_aligned_type4 = p1_aligned_type4;
Andrey Bokhanko67a41862016-05-26 10:06:01 +0000123
124 __unaligned int a[10];
125 int *b = a; // expected-error {{cannot initialize a variable of type 'int *' with an lvalue of type '__unaligned int [10]'}}
Andrey Bokhanko45d41322016-05-11 18:38:21 +0000126}
127
128// Test from PR27367
129// We should accept assignment of an __unaligned pointer to a non-__unaligned
130// pointer to void
131typedef struct _ITEMIDLIST { int i; } ITEMIDLIST;
132typedef ITEMIDLIST __unaligned *LPITEMIDLIST;
133extern "C" __declspec(dllimport) void __stdcall CoTaskMemFree(void* pv);
134__inline void FreeIDListArray(LPITEMIDLIST *ppidls) {
135 CoTaskMemFree(*ppidls);
136 __unaligned int *x = 0;
137 void *y = x;
138}
139
140// Test from PR27666
141// We should accept type conversion of __unaligned to non-__unaligned references
142typedef struct in_addr {
143public:
144 in_addr(in_addr &a) {} // expected-note {{candidate constructor not viable: no known conversion from '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to 'in_addr &' for 1st argument; dereference the argument with *}}
145 in_addr(in_addr *a) {} // expected-note {{candidate constructor not viable: 1st argument ('__unaligned IN_ADDR *' (aka '__unaligned in_addr *')) would lose __unaligned qualifier}}
146} IN_ADDR;
147
148void f(IN_ADDR __unaligned *a) {
149 IN_ADDR local_addr = *a;
150 IN_ADDR local_addr2 = a; // expected-error {{no viable conversion from '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to 'IN_ADDR' (aka 'in_addr')}}
151}
Francois Pichet17ed0202011-08-18 09:59:55 +0000152
Douglas Gregoreece0ea2010-09-01 16:29:03 +0000153template<typename T> void h1(T (__stdcall M::* const )()) { }
154
155void m1() {
156 h1<int>(&M::addP);
157 h1(&M::subtractP);
Aaron Ballman3e424b52013-12-26 18:30:57 +0000158}
Francois Pichet488b4a72010-09-12 05:06:55 +0000159
Francois Picheta3108062010-10-18 15:01:13 +0000160
Reid Klecknerd2c0c252016-10-04 15:57:49 +0000161namespace signed_hex_i64 {
162void f(long long); // expected-note {{candidate function}}
163void f(int); // expected-note {{candidate function}}
164void g() {
165 // This used to be controlled by -fms-extensions, but it is now under
166 // -fms-compatibility.
167 f(0xffffffffffffffffLL); // expected-error {{call to 'f' is ambiguous}}
Francois Pichet7d92ee32011-01-17 01:08:01 +0000168 f(0xffffffffffffffffi64);
169}
Reid Klecknerd2c0c252016-10-04 15:57:49 +0000170}
Douglas Gregora1aec292011-02-22 20:32:04 +0000171
172// Enumeration types with a fixed underlying type.
173const int seventeen = 17;
174typedef int Int;
175
176struct X0 {
Eli Friedman0d0355ab2012-11-02 01:34:28 +0000177 enum E1 : Int { SomeOtherValue } field; // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}}
Douglas Gregora1aec292011-02-22 20:32:04 +0000178 enum E1 : seventeen;
179};
180
Eli Friedman0d0355ab2012-11-02 01:34:28 +0000181enum : long long { // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}}
Douglas Gregora1aec292011-02-22 20:32:04 +0000182 SomeValue = 0x100000000
183};
Francois Pichet3096d202011-03-29 10:39:17 +0000184
185
186class AAA {
187__declspec(dllimport) void f(void) { }
Nico Rieck82f0b062014-03-31 14:56:15 +0000188void f2(void); // expected-note{{previous declaration is here}}
Francois Pichet3096d202011-03-29 10:39:17 +0000189};
190
Hans Wennborgb0f2f142014-05-15 22:07:49 +0000191__declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}}
Nico Rieck82f0b062014-03-31 14:56:15 +0000192 // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}}
Francois Pichet3096d202011-03-29 10:39:17 +0000193
194}
195
196
197
Francois Pichet53fe2bb2011-04-10 03:03:52 +0000198template <class T>
199class BB {
200public:
201 void f(int g = 10 ); // expected-note {{previous definition is here}}
202};
203
204template <class T>
205void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}}
206
Francois Pichet48c946e2011-04-13 02:38:49 +0000207
Francois Pichete900b102011-04-22 08:14:00 +0000208
209extern void static_func();
210void static_func(); // expected-note {{previous declaration is here}}
211
212
David Majnemer5b63fa02014-06-18 23:26:25 +0000213static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}}
Francois Pichete900b102011-04-22 08:14:00 +0000214{
215
Francois Pichetbc6ebb52011-05-08 22:52:41 +0000216}
217
David Majnemer5b63fa02014-06-18 23:26:25 +0000218extern const int static_var; // expected-note {{previous declaration is here}}
219static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}
220
Francois Pichetb796b632011-05-11 22:13:54 +0000221void pointer_to_integral_type_conv(char* ptr) {
222 char ch = (char)ptr;
223 short sh = (short)ptr;
224 ch = (char)ptr;
225 sh = (short)ptr;
Francois Pichetefb1af92011-05-23 03:43:44 +0000226
Hans Wennborg15439bc2013-06-06 09:16:36 +0000227 // These are valid C++.
228 bool b = (bool)ptr;
229 b = static_cast<bool>(ptr);
230
231 // This is bad.
232 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
233}
Francois Pichete37eeba2011-06-01 04:14:20 +0000234
Douglas Gregor50cefbf2011-10-17 17:09:53 +0000235struct PR11150 {
236 class X {
237 virtual void f() = 0;
238 };
239
240 int array[__is_abstract(X)? 1 : -1];
241};
Douglas Gregor60060d62011-10-21 03:57:52 +0000242
243void f() { int __except = 0; }
244
Douglas Gregor43bc0362012-09-13 20:16:20 +0000245void ::f(); // expected-warning{{extra qualification on member 'f'}}
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +0000246
247class C {
248 C::C(); // expected-warning{{extra qualification on member 'C'}}
249};
John McCall5e77d762013-04-16 07:28:30 +0000250
251struct StructWithProperty {
252 __declspec(property(get=GetV)) int V1;
253 __declspec(property(put=SetV)) int V2;
254 __declspec(property(get=GetV, put=SetV_NotExist)) int V3;
255 __declspec(property(get=GetV_NotExist, put=SetV)) int V4;
256 __declspec(property(get=GetV, put=SetV)) int V5;
257
258 int GetV() { return 123; }
259 void SetV(int i) {}
260};
261void TestProperty() {
262 StructWithProperty sp;
263 int i = sp.V2; // expected-error{{no getter defined for property 'V2'}}
264 sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}}
265 int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}}
266 sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}}
267 int k = sp.V5;
268 sp.V5 = k++;
269}
270
271/* 4 tests for PseudoObject, begin */
272struct SP1
273{
274 bool operator()() { return true; }
275};
276struct SP2
277{
278 __declspec(property(get=GetV)) SP1 V;
279 SP1 GetV() { return SP1(); }
280};
281void TestSP2() {
282 SP2 sp2;
283 bool b = sp2.V();
284}
285
286struct SP3 {
287 template <class T>
288 void f(T t) {}
289};
290template <class T>
291struct SP4
292{
293 __declspec(property(get=GetV)) int V;
294 int GetV() { return 123; }
295 void f() { SP3 s2; s2.f(V); }
296};
297void TestSP4() {
298 SP4<int> s;
299 s.f();
300}
301
302template <class T>
303struct SP5
304{
305 __declspec(property(get=GetV)) T V;
306 int GetV() { return 123; }
307 void f() { int *p = new int[V]; }
308};
309
310template <class T>
311struct SP6
312{
313public:
314 __declspec(property(get=GetV)) T V;
315 T GetV() { return 123; }
316 void f() { int t = V; }
317};
318void TestSP6() {
319 SP6<int> c;
320 c.f();
321}
322/* 4 tests for PseudoObject, end */
323
324// Property access: explicit, implicit, with Qualifier
325struct SP7 {
326 __declspec(property(get=GetV, put=SetV)) int V;
327 int GetV() { return 123; }
328 void SetV(int v) {}
329
330 void ImplicitAccess() { int i = V; V = i; }
331 void ExplicitAccess() { int i = this->V; this->V = i; }
332};
333struct SP8: public SP7 {
334 void AccessWithQualifier() { int i = SP7::V; SP7::V = i; }
335};
336
337// Property usage
338template <class T>
339struct SP9 {
340 __declspec(property(get=GetV, put=SetV)) T V;
341 T GetV() { return 0; }
342 void SetV(T v) {}
Richard Trieu99e1c952014-03-11 03:11:08 +0000343 bool f() { V = this->V; return V < this->V; }
John McCall0d9dd732013-04-16 22:32:04 +0000344 void g() { V++; }
345 void h() { V*=2; }
John McCall5e77d762013-04-16 07:28:30 +0000346};
347struct SP10 {
348 SP10(int v) {}
349 bool operator<(const SP10& v) { return true; }
350 SP10 operator*(int v) { return *this; }
John McCall0d9dd732013-04-16 22:32:04 +0000351 SP10 operator+(int v) { return *this; }
John McCall5e77d762013-04-16 07:28:30 +0000352 SP10& operator=(const SP10& v) { return *this; }
353};
354void TestSP9() {
355 SP9<int> c;
356 int i = c.V; // Decl initializer
357 i = c.V; // Binary op operand
358 c.SetV(c.V); // CallExpr arg
359 int *p = new int[c.V + 1]; // Array size
360 p[c.V] = 1; // Array index
361
362 c.V = 123; // Setter
363
364 c.V++; // Unary op operand
365 c.V *= 2; // Unary op operand
366
367 SP9<int*> c2;
368 c2.V[0] = 123; // Array
369
370 SP9<SP10> c3;
371 c3.f(); // Overloaded binary op operand
John McCall0d9dd732013-04-16 22:32:04 +0000372 c3.g(); // Overloaded incdec op operand
373 c3.h(); // Overloaded unary op operand
John McCall5e77d762013-04-16 07:28:30 +0000374}
Aaron Ballmaned0ae1d2013-05-30 16:20:00 +0000375
376union u {
377 int *i1;
378 int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
379};
Reid Kleckner0a0c8892013-06-19 16:37:23 +0000380
381// Property getter using reference.
382struct SP11 {
383 __declspec(property(get=GetV)) int V;
384 int _v;
385 int& GetV() { return _v; }
386 void UseV();
387 void TakePtr(int *) {}
388 void TakeRef(int &) {}
389 void TakeVal(int) {}
390};
391
392void SP11::UseV() {
393 TakePtr(&V);
394 TakeRef(V);
395 TakeVal(V);
396}
Aaron Ballman2a6febc2013-06-26 21:28:44 +0000397
398struct StructWithUnnamedMember {
399 __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}}
400};
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000401
Reid Kleckner85c7e0a2015-02-24 20:29:40 +0000402struct MSPropertyClass {
403 int get() { return 42; }
404 int __declspec(property(get = get)) n;
405};
406
407int *f(MSPropertyClass &x) {
408 return &x.n; // expected-error {{address of property expression requested}}
409}
410int MSPropertyClass::*g() {
411 return &MSPropertyClass::n; // expected-error {{address of property expression requested}}
412}
413
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000414namespace rdar14250378 {
415 class Bar {};
Aaron Ballman3e424b52013-12-26 18:30:57 +0000416
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000417 namespace NyNamespace {
418 class Foo {
419 public:
420 Bar* EnsureBar();
421 };
Aaron Ballman3e424b52013-12-26 18:30:57 +0000422
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000423 class Baz : public Foo {
424 public:
425 friend class Bar;
426 };
Aaron Ballman3e424b52013-12-26 18:30:57 +0000427
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000428 Bar* Foo::EnsureBar() {
429 return 0;
430 }
431 }
432}
David Majnemera5433082013-10-18 00:33:31 +0000433
434// expected-error@+1 {{'sealed' keyword not permitted with interface types}}
435__interface InterfaceWithSealed sealed {
436};
437
438struct SomeBase {
439 virtual void OverrideMe();
440
441 // expected-note@+2 {{overridden virtual function is here}}
442 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
Fariborz Jahanianf920a0a2014-10-27 19:11:51 +0000443 virtual void SealedFunction() sealed; // expected-note {{overridden virtual function is here}}
David Majnemera5433082013-10-18 00:33:31 +0000444};
445
446// expected-note@+2 {{'SealedType' declared here}}
447// expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
448struct SealedType sealed : SomeBase {
Fariborz Jahanianf920a0a2014-10-27 19:11:51 +0000449 // expected-error@+2 {{declaration of 'SealedFunction' overrides a 'sealed' function}}
450 // FIXME. warning can be suppressed if we're also issuing error for overriding a 'final' function.
451 virtual void SealedFunction(); // expected-warning {{'SealedFunction' overrides a member function but is not marked 'override'}}
David Majnemera5433082013-10-18 00:33:31 +0000452
453 // expected-warning@+1 {{'override' keyword is a C++11 extension}}
454 virtual void OverrideMe() override;
455};
456
457// expected-error@+1 {{base 'SealedType' is marked 'sealed'}}
458struct InheritFromSealed : SealedType {};
Alp Tokerd3f79c52013-11-24 20:24:54 +0000459
460void AfterClassBody() {
461 // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}}
462 struct D {} __declspec(deprecated);
463
464 struct __declspec(align(4)) S {} __declspec(align(8)) s1;
465 S s2;
466 _Static_assert(__alignof(S) == 4, "");
467 _Static_assert(__alignof(s1) == 8, "");
468 _Static_assert(__alignof(s2) == 4, "");
469}
Kaelyn Takata26487022015-10-01 22:38:51 +0000470
471namespace PR24246 {
472template <typename TX> struct A {
473 template <bool> struct largest_type_select;
474 // expected-warning@+1 {{explicit specialization of 'largest_type_select' within class scope is a Microsoft extension}}
475 template <> struct largest_type_select<false> {
476 blah x; // expected-error {{unknown type name 'blah'}}
477 };
478};
479}
David Majnemer06ce8a42015-10-20 20:49:21 +0000480
481namespace PR25265 {
482struct S {
483 int fn() throw(); // expected-note {{previous declaration is here}}
484};
485
486int S::fn() { return 0; } // expected-warning {{is missing exception specification}}
487}
Andrey Bokhanko45d41322016-05-11 18:38:21 +0000488
489#elif TEST2
490
491// Check that __unaligned is not recognized if MS extensions are not enabled
492typedef char __unaligned *aligned_type; // expected-error {{expected ';' after top level declarator}}
493
494#else
495
496#error Unknown test mode
497
498#endif
499