Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / MicrosoftExtensions.cpp
blob7286217b1644f3ee00eb64e7fc979a5259b12471
1 // RUN: %clang_cc1 -std=c++17 %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 -std=c++98 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,precxx17 -fms-extensions -fexceptions -fcxx-exceptions -DTEST1
3 // RUN: %clang_cc1 -std=c++11 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,precxx17 -fms-extensions -fexceptions -fcxx-exceptions -DTEST1
4 // RUN: %clang_cc1 -std=c++14 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,precxx17 -fexceptions -fcxx-exceptions -DTEST2
5 // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -fms-compatibility -verify -DTEST3
7 #if TEST1
9 // MSVC allows type definition in anonymous union and struct
10 struct A
12 union
14 int a;
15 struct B // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
17 int c;
18 } d;
20 union C // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
22 int e;
23 int ee;
24 } f;
26 typedef int D; // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
27 struct F; // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
30 struct
32 int a2;
34 struct B2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
36 int c2;
37 } d2;
39 union C2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
41 int e2;
42 int ee2;
43 } f2;
45 typedef int D2; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
46 struct F2; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
50 // __stdcall handling
51 struct M {
52 int __stdcall addP();
53 float __stdcall subtractP();
56 // __unaligned handling
57 typedef char __unaligned *aligned_type;
58 typedef struct UnalignedTag { int f; } __unaligned *aligned_type2;
59 typedef char __unaligned aligned_type3;
61 struct aligned_type4 {
62 int i;
65 __unaligned int aligned_type4::*p1_aligned_type4 = &aligned_type4::i;
66 int aligned_type4::* __unaligned p2_aligned_type4 = &aligned_type4::i;
67 __unaligned int aligned_type4::* __unaligned p3_aligned_type4 = &aligned_type4::i;
68 void (aligned_type4::*__unaligned p4_aligned_type4)();
70 // Check that __unaligned qualifier can be used for overloading
71 void foo_unaligned(int *arg) {} // expected-note {{not viable: 1st argument ('const __unaligned int *') would lose const qualifier}}
72 void foo_unaligned(__unaligned int *arg) {} // expected-note {{not viable: 1st argument ('const __unaligned int *') would lose const qualifier}}
73 void foo_unaligned(int arg) {} // expected-note {{previous definition is here}}
74 void foo_unaligned(__unaligned int arg) {} // expected-error {{redefinition of 'foo_unaligned'}} \
75 // expected-note {{not viable: no known conversion from 'const __unaligned int *' to '__unaligned int'}}
76 class A_unaligned {};
77 class B_unaligned : public A_unaligned {};
78 int foo_unaligned(__unaligned A_unaligned *arg) { return 0; } // expected-note {{not viable: no known conversion from 'const __unaligned int *' to '__unaligned A_unaligned *'}}
79 void *foo_unaligned(B_unaligned *arg) { return 0; } // expected-note {{not viable: no known conversion from 'const __unaligned int *' to 'B_unaligned *'}}
81 void test_unaligned() {
82 int *p1 = 0;
83 foo_unaligned(p1);
85 const __unaligned int *const_p1 = 0;
86 foo_unaligned(const_p1); // expected-error {{no matching function for call to 'foo_unaligned'}}
88 __unaligned int *p2 = 0;
89 foo_unaligned(p2);
91 __unaligned B_unaligned *p3 = 0;
92 int p4 = foo_unaligned(p3); // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type 'void *'}}
93 // expected-warning@-1 {{implicit cast from type '__unaligned B_unaligned *' to type 'B_unaligned *' drops __unaligned qualifier}}
95 B_unaligned *p5 = p3;
96 // expected-warning@-1 {{implicit cast from type '__unaligned B_unaligned *' to type 'B_unaligned *' drops __unaligned qualifier}}
98 __unaligned B_unaligned *p6 = p3;
100 p1_aligned_type4 = p2_aligned_type4;
101 p2_aligned_type4 = p1_aligned_type4;
102 // expected-warning@-1 {{implicit cast from type '__unaligned int aligned_type4::*' to type 'int aligned_type4::*' drops __unaligned qualifier}}
103 p3_aligned_type4 = p1_aligned_type4;
105 __unaligned int a[10];
106 int *b = a;
107 // expected-warning@-1 {{implicit cast from type '__unaligned int[10]' to type 'int *' drops __unaligned qualifier}}
110 // Test from PR27367
111 // We should accept assignment of an __unaligned pointer to a non-__unaligned
112 // pointer to void
113 typedef struct _ITEMIDLIST { int i; } ITEMIDLIST;
114 typedef ITEMIDLIST __unaligned *LPITEMIDLIST;
115 extern "C" __declspec(dllimport) void __stdcall CoTaskMemFree(void* pv);
116 __inline void FreeIDListArray(LPITEMIDLIST *ppidls) {
117 CoTaskMemFree(*ppidls);
118 __unaligned int *x = 0;
119 void *y = x;
122 // Test from PR27666
123 // We should accept type conversion of __unaligned to non-__unaligned references
124 typedef struct in_addr {
125 public:
126 in_addr(in_addr &a) {} // precxx17-note {{candidate constructor not viable: expects an lvalue for 1st argument}}
127 in_addr(in_addr *a) {} // precxx17-note {{candidate constructor not viable: no known conversion from 'IN_ADDR' (aka 'in_addr') to 'in_addr *' for 1st argument}}
128 } IN_ADDR;
130 void f(IN_ADDR __unaligned *a) {
131 IN_ADDR local_addr = *a;
132 // FIXME: MSVC accepts the following; not sure why clang tries to
133 // copy-construct an in_addr.
134 IN_ADDR local_addr2 = a; // precxx17-error {{no viable constructor copying variable of type 'IN_ADDR' (aka 'in_addr')}}
135 // expected-warning@-1 {{implicit cast from type '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to type 'in_addr *' drops __unaligned qualifier}}
136 IN_ADDR local_addr3(a);
137 // expected-warning@-1 {{implicit cast from type '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to type 'in_addr *' drops __unaligned qualifier}}
140 template<typename T> void h1(T (__stdcall M::* const )()) { }
142 void m1() {
143 h1<int>(&M::addP);
144 h1(&M::subtractP);
148 namespace signed_hex_i64 {
149 void f(long long); // expected-note {{candidate function}}
150 void f(int); // expected-note {{candidate function}}
151 void g() {
152 // This used to be controlled by -fms-extensions, but it is now under
153 // -fms-compatibility.
154 f(0xffffffffffffffffLL); // expected-error {{call to 'f' is ambiguous}}
155 f(0xffffffffffffffffi64);
159 // Enumeration types with a fixed underlying type.
160 const int seventeen = 17;
161 typedef int Int;
163 struct X0 {
164 enum E1 : Int { SomeOtherValue } field;
165 #if __cplusplus <= 199711L
166 // expected-warning@-2 {{enumeration types with a fixed underlying type are a C++11 extension}}
167 #endif
169 enum E1 : seventeen;
170 #if __cplusplus >= 201103L
171 // expected-error@-2 {{bit-field}}
172 #endif
175 #if __cplusplus <= 199711L
176 // expected-warning@+2 {{enumeration types with a fixed underlying type are a C++11 extension}}
177 #endif
178 enum : long long {
179 SomeValue = 0x100000000
183 class AAA {
184 __declspec(dllimport) void f(void) { }
185 void f2(void); // expected-note{{previous declaration is here}}
188 __declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}}
189 // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}}
195 template <class T>
196 class BB {
197 public:
198 void f(int g = 10 ); // expected-note {{previous definition is here}}
201 template <class T>
202 void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}}
206 extern void static_func();
207 void static_func(); // expected-note {{previous declaration is here}}
210 static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}}
215 extern const int static_var; // expected-note {{previous declaration is here}}
216 static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}
218 void pointer_to_integral_type_conv(char* ptr) {
219 char ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'char *'}}
220 short sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'char *'}}
221 ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'char *'}}
222 sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'char *'}}
224 // These are valid C++.
225 bool b = (bool)ptr;
226 b = static_cast<bool>(ptr);
228 // This is bad.
229 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
232 void void_pointer_to_integral_type_conv(void *ptr) {
233 char ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'void *'}}
234 short sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'void *'}}
235 ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'void *'}}
236 sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'void *'}}
238 // These are valid C++.
239 bool b = (bool)ptr;
240 b = static_cast<bool>(ptr);
242 // This is bad.
243 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
246 struct PR11150 {
247 class X {
248 virtual void f() = 0;
251 int array[__is_abstract(X)? 1 : -1];
254 void f() { int __except = 0; }
256 void ::f(); // expected-warning{{extra qualification on member 'f'}}
258 class C {
259 C::C(); // expected-warning{{extra qualification on member 'C'}}
262 struct StructWithProperty {
263 __declspec(property(get=GetV)) int V1;
264 __declspec(property(put=SetV)) int V2;
265 __declspec(property(get=GetV, put=SetV_NotExist)) int V3;
266 __declspec(property(get=GetV_NotExist, put=SetV)) int V4;
267 __declspec(property(get=GetV, put=SetV)) int V5;
269 int GetV() { return 123; }
270 void SetV(int i) {}
272 void TestProperty() {
273 StructWithProperty sp;
274 int i = sp.V2; // expected-error{{no getter defined for property 'V2'}}
275 sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}}
276 int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}}
277 sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}}
278 int k = sp.V5;
279 sp.V5 = k++;
282 /* 4 tests for PseudoObject, begin */
283 struct SP1
285 bool operator()() { return true; }
287 struct SP2
289 __declspec(property(get=GetV)) SP1 V;
290 SP1 GetV() { return SP1(); }
292 void TestSP2() {
293 SP2 sp2;
294 bool b = sp2.V();
297 struct SP3 {
298 template <class T>
299 void f(T t) {}
301 template <class T>
302 struct SP4
304 __declspec(property(get=GetV)) int V;
305 int GetV() { return 123; }
306 void f() { SP3 s2; s2.f(V); }
308 void TestSP4() {
309 SP4<int> s;
310 s.f();
313 template <class T>
314 struct SP5
316 __declspec(property(get=GetV)) T V;
317 int GetV() { return 123; }
318 void f() { int *p = new int[V]; }
321 template <class T>
322 struct SP6
324 public:
325 __declspec(property(get=GetV)) T V;
326 T GetV() { return 123; }
327 void f() { int t = V; }
329 void TestSP6() {
330 SP6<int> c;
331 c.f();
333 /* 4 tests for PseudoObject, end */
335 // Property access: explicit, implicit, with Qualifier
336 struct SP7 {
337 __declspec(property(get=GetV, put=SetV)) int V;
338 int GetV() { return 123; }
339 void SetV(int v) {}
341 void ImplicitAccess() { int i = V; V = i; }
342 void ExplicitAccess() { int i = this->V; this->V = i; }
344 struct SP8: public SP7 {
345 void AccessWithQualifier() { int i = SP7::V; SP7::V = i; }
348 // Property usage
349 template <class T>
350 struct SP9 {
351 __declspec(property(get=GetV, put=SetV)) T V;
352 T GetV() { return 0; }
353 void SetV(T v) {}
354 bool f() { V = this->V; return V < this->V; }
355 void g() { V++; }
356 void h() { V*=2; }
358 struct SP10 {
359 SP10(int v) {}
360 bool operator<(const SP10& v) { return true; }
361 SP10 operator*(int v) { return *this; }
362 SP10 operator+(int v) { return *this; }
363 SP10& operator=(const SP10& v) { return *this; }
365 void TestSP9() {
366 SP9<int> c;
367 int i = c.V; // Decl initializer
368 i = c.V; // Binary op operand
369 c.SetV(c.V); // CallExpr arg
370 int *p = new int[c.V + 1]; // Array size
371 p[c.V] = 1; // Array index
373 c.V = 123; // Setter
375 c.V++; // Unary op operand
376 c.V *= 2; // Unary op operand
378 SP9<int*> c2;
379 c2.V[0] = 123; // Array
381 SP9<SP10> c3;
382 c3.f(); // Overloaded binary op operand
383 c3.g(); // Overloaded incdec op operand
384 c3.h(); // Overloaded unary op operand
387 union u {
388 int *i1;
389 int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
392 // Property getter using reference.
393 struct SP11 {
394 __declspec(property(get=GetV)) int V;
395 int _v;
396 int& GetV() { return _v; }
397 void UseV();
398 void TakePtr(int *) {}
399 void TakeRef(int &) {}
400 void TakeVal(int) {}
403 void SP11::UseV() {
404 TakePtr(&V);
405 TakeRef(V);
406 TakeVal(V);
409 struct StructWithUnnamedMember {
410 __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}}
413 struct MSPropertyClass {
414 int get() { return 42; }
415 int __declspec(property(get = get)) n;
418 int *f(MSPropertyClass &x) {
419 return &x.n; // expected-error {{address of property expression requested}}
421 int MSPropertyClass::*g() {
422 return &MSPropertyClass::n; // expected-error {{address of property expression requested}}
425 namespace rdar14250378 {
426 class Bar {};
428 namespace NyNamespace {
429 class Foo {
430 public:
431 Bar* EnsureBar();
434 class Baz : public Foo {
435 public:
436 friend class Bar;
439 Bar* Foo::EnsureBar() {
440 return 0;
445 // expected-error@+1 {{'sealed' keyword not permitted with interface types}}
446 __interface InterfaceWithSealed sealed {
449 struct SomeBase {
450 virtual void OverrideMe();
452 // expected-note@+2 {{overridden virtual function is here}}
453 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
454 virtual void SealedFunction() sealed; // expected-note {{overridden virtual function is here}}
457 // expected-note@+2 {{'SealedType' declared here}}
458 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
459 struct SealedType sealed : SomeBase {
460 // expected-error@+2 {{declaration of 'SealedFunction' overrides a 'sealed' function}}
461 // FIXME. warning can be suppressed if we're also issuing error for overriding a 'final' function.
462 virtual void SealedFunction(); // expected-warning {{'SealedFunction' overrides a member function but is not marked 'override'}}
464 #if __cplusplus <= 199711L
465 // expected-warning@+2 {{'override' keyword is a C++11 extension}}
466 #endif
467 virtual void OverrideMe() override;
470 // expected-error@+1 {{base 'SealedType' is marked 'sealed'}}
471 struct InheritFromSealed : SealedType {};
473 class SealedDestructor { // expected-note {{mark 'SealedDestructor' as 'sealed' to silence this warning}}
474 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
475 virtual ~SealedDestructor() sealed; // expected-warning {{class with destructor marked 'sealed' cannot be inherited from}}
478 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
479 class AbstractClass abstract {
480 int i;
483 // expected-error@+1 {{variable type 'AbstractClass' is an abstract class}}
484 AbstractClass abstractInstance;
486 // expected-warning@+4 {{abstract class is marked 'sealed'}}
487 // expected-note@+3 {{'AbstractAndSealedClass' declared here}}
488 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
489 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
490 class AbstractAndSealedClass abstract sealed {}; // Does no really make sense, but allowed
492 // expected-error@+1 {{variable type 'AbstractAndSealedClass' is an abstract class}}
493 AbstractAndSealedClass abstractAndSealedInstance;
494 // expected-error@+1 {{base 'AbstractAndSealedClass' is marked 'sealed'}}
495 class InheritFromAbstractAndSealed : AbstractAndSealedClass {};
497 #if __cplusplus <= 199711L
498 // expected-warning@+4 {{'final' keyword is a C++11 extension}}
499 // expected-warning@+3 {{'final' keyword is a C++11 extension}}
500 #endif
501 // expected-error@+1 {{class already marked 'final'}}
502 class TooManyVirtSpecifiers1 final final {};
503 #if __cplusplus <= 199711L
504 // expected-warning@+4 {{'final' keyword is a C++11 extension}}
505 #endif
506 // expected-warning@+2 {{'sealed' keyword is a Microsoft extension}}
507 // expected-error@+1 {{class already marked 'sealed'}}
508 class TooManyVirtSpecifiers2 final sealed {};
509 #if __cplusplus <= 199711L
510 // expected-warning@+6 {{'final' keyword is a C++11 extension}}
511 // expected-warning@+5 {{'final' keyword is a C++11 extension}}
512 #endif
513 // expected-warning@+3 {{abstract class is marked 'final'}}
514 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
515 // expected-error@+1 {{class already marked 'final'}}
516 class TooManyVirtSpecifiers3 final abstract final {};
517 #if __cplusplus <= 199711L
518 // expected-warning@+6 {{'final' keyword is a C++11 extension}}
519 #endif
520 // expected-warning@+4 {{abstract class is marked 'final'}}
521 // expected-warning@+3 {{'abstract' keyword is a Microsoft extension}}
522 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
523 // expected-error@+1 {{class already marked 'abstract'}}
524 class TooManyVirtSpecifiers4 abstract final abstract {};
526 class Base {
527 virtual void i();
529 class AbstractFunctionInClass : public Base {
530 // expected-note@+2 {{unimplemented pure virtual method 'f' in 'AbstractFunctionInClass'}}
531 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
532 virtual void f() abstract;
533 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
534 void g() abstract; // expected-error {{'g' is not virtual and cannot be declared pure}}
535 // expected-note@+2 {{unimplemented pure virtual method 'h' in 'AbstractFunctionInClass'}}
536 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
537 virtual void h() abstract = 0; // expected-error {{class member already marked 'abstract'}}
538 #if __cplusplus <= 199711L
539 // expected-warning@+4 {{'override' keyword is a C++11 extension}}
540 #endif
541 // expected-note@+2 {{unimplemented pure virtual method 'i' in 'AbstractFunctionInClass'}}
542 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
543 virtual void i() abstract override;
546 // expected-error@+1 {{variable type 'AbstractFunctionInClass' is an abstract class}}
547 AbstractFunctionInClass abstractFunctionInClassInstance;
549 void AfterClassBody() {
550 // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}}
551 struct D {} __declspec(deprecated);
553 struct __declspec(align(4)) S {} __declspec(align(8)) s1;
554 S s2;
555 _Static_assert(__alignof(S) == 4, "");
556 _Static_assert(__alignof(s1) == 8, "");
557 _Static_assert(__alignof(s2) == 4, "");
560 namespace PR24246 {
561 template <typename TX> struct A {
562 template <bool> struct largest_type_select;
563 template <> struct largest_type_select<false> {
564 blah x; // expected-error {{unknown type name 'blah'}}
569 class PR34109_class {
570 PR34109_class() {}
571 virtual ~PR34109_class() {}
574 void operator delete(void *) throw();
575 // expected-note@-1 {{previous declaration is here}}
576 __declspec(dllexport) void operator delete(void *) throw();
577 // expected-error@-1 {{redeclaration of 'operator delete' cannot add 'dllexport' attribute}}
579 void PR34109(int* a) {
580 delete a;
583 namespace PR42089 {
584 struct S {
585 __attribute__((nothrow)) void Foo(); // expected-note {{previous declaration is here}}
586 __attribute__((nothrow)) void Bar();
588 void S::Foo(){} // expected-warning {{is missing exception specification}}
589 __attribute__((nothrow)) void S::Bar(){}
592 namespace UnalignedConv {
593 struct S {
594 bool operator==(int) const;
597 int func() {
598 S __unaligned s;
599 return s == 42;
604 #elif TEST2
606 // Check that __unaligned is not recognized if MS extensions are not enabled
607 typedef char __unaligned *aligned_type; // expected-error {{expected ';' after top level declarator}}
609 #elif TEST3
611 namespace PR32750 {
612 template<typename T> struct A {};
613 template<typename T> struct B : A<A<T>> { A<T>::C::D d; }; // expected-warning {{implicit 'typename' is a C++20 extension}}
616 #else
618 #error Unknown test mode
620 #endif