]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/SemaCXX/MicrosoftExtensions.cpp
Vendor import of clang trunk r300422:
[FreeBSD/FreeBSD.git] / test / SemaCXX / MicrosoftExtensions.cpp
1 // 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 -std=c++98 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -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 -fms-extensions -fexceptions -fcxx-exceptions -DTEST1
4 // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -fexceptions -fcxx-exceptions -DTEST2
5
6 #if TEST1
7
8 // Microsoft doesn't validate exception specification.
9 namespace microsoft_exception_spec {
10
11 void foo(); // expected-note {{previous declaration}}
12 void foo() throw(); // expected-warning {{exception specification in declaration does not match previous declaration}}
13
14 void r6() throw(...); // expected-note {{previous declaration}}
15 void r6() throw(int); // expected-warning {{exception specification in declaration does not match previous declaration}}
16
17 struct Base {
18   virtual void f2();
19   virtual void f3() throw(...);
20 };
21
22 struct Derived : Base {
23   virtual void f2() throw(...);
24   virtual void f3();
25 };
26
27 class A {
28   virtual ~A() throw();
29 #if __cplusplus <= 199711L
30   // expected-note@-2 {{overridden virtual function is here}}
31 #endif
32 };
33
34 class B : public A {
35   virtual ~B();
36 #if __cplusplus <= 199711L
37   // expected-warning@-2 {{exception specification of overriding function is more lax than base version}}
38 #endif
39 };
40
41 }
42
43 // MSVC allows type definition in anonymous union and struct
44 struct A
45 {
46   union
47   {
48     int a;
49     struct B  // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
50     {
51       int c;
52     } d;
53
54     union C   // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
55     {
56       int e;
57       int ee;
58     } f;
59
60     typedef int D;  // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
61     struct F;  // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
62   };
63
64   struct
65   {
66     int a2;
67
68     struct B2  // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
69     {
70       int c2;
71     } d2;
72
73         union C2  // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
74     {
75       int e2;
76       int ee2;
77     } f2;
78
79     typedef int D2;  // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
80     struct F2;  // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
81   };
82 };
83
84 // __stdcall handling
85 struct M {
86     int __stdcall addP();
87     float __stdcall subtractP();
88 };
89
90 // __unaligned handling
91 typedef char __unaligned *aligned_type;
92 typedef struct UnalignedTag { int f; } __unaligned *aligned_type2;
93 typedef char __unaligned aligned_type3;
94
95 struct aligned_type4 {
96   int i;
97 };
98
99 __unaligned int aligned_type4::*p1_aligned_type4 = &aligned_type4::i;
100 int aligned_type4::* __unaligned p2_aligned_type4 = &aligned_type4::i;
101 __unaligned int aligned_type4::* __unaligned p3_aligned_type4 = &aligned_type4::i;
102 void (aligned_type4::*__unaligned p4_aligned_type4)();
103
104 // Check that __unaligned qualifier can be used for overloading
105 void foo_unaligned(int *arg) {}
106 void foo_unaligned(__unaligned int *arg) {}
107 void foo_unaligned(int arg) {} // expected-note {{previous definition is here}}
108 void foo_unaligned(__unaligned int arg) {} // expected-error {{redefinition of 'foo_unaligned'}}
109 class A_unaligned {};
110 class B_unaligned : public A_unaligned {};
111 int foo_unaligned(__unaligned A_unaligned *arg) { return 0; }
112 void *foo_unaligned(B_unaligned *arg) { return 0; }
113
114 void test_unaligned() {
115   int *p1 = 0;
116   foo_unaligned(p1);
117
118   __unaligned int *p2 = 0;
119   foo_unaligned(p2);
120
121   __unaligned B_unaligned *p3 = 0;
122   int p4 = foo_unaligned(p3);
123
124   B_unaligned *p5 = p3; // expected-error {{cannot initialize a variable of type 'B_unaligned *' with an lvalue of type '__unaligned B_unaligned *'}}
125
126   __unaligned B_unaligned *p6 = p3;
127
128   p1_aligned_type4 = p2_aligned_type4;
129   p2_aligned_type4 = p1_aligned_type4; // expected-error {{assigning to 'int aligned_type4::*' from incompatible type '__unaligned int aligned_type4::*'}}
130   p3_aligned_type4 = p1_aligned_type4;
131
132   __unaligned int a[10];
133   int *b = a; // expected-error {{cannot initialize a variable of type 'int *' with an lvalue of type '__unaligned int [10]'}}
134 }
135
136 // Test from PR27367
137 // We should accept assignment of an __unaligned pointer to a non-__unaligned
138 // pointer to void
139 typedef struct _ITEMIDLIST { int i; } ITEMIDLIST;
140 typedef ITEMIDLIST __unaligned *LPITEMIDLIST;
141 extern "C" __declspec(dllimport) void __stdcall CoTaskMemFree(void* pv);
142 __inline void FreeIDListArray(LPITEMIDLIST *ppidls) {
143   CoTaskMemFree(*ppidls);
144   __unaligned int *x = 0;
145   void *y = x;
146 }
147
148 // Test from PR27666
149 // We should accept type conversion of __unaligned to non-__unaligned references
150 typedef struct in_addr {
151 public:
152   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 *}}
153   in_addr(in_addr *a) {} // expected-note {{candidate constructor not viable: 1st argument ('__unaligned IN_ADDR *' (aka '__unaligned in_addr *')) would lose __unaligned qualifier}}
154 } IN_ADDR;
155
156 void f(IN_ADDR __unaligned *a) {
157   IN_ADDR local_addr = *a;
158   IN_ADDR local_addr2 = a; // expected-error {{no viable conversion from '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to 'IN_ADDR' (aka 'in_addr')}}
159 }
160
161 template<typename T> void h1(T (__stdcall M::* const )()) { }
162
163 void m1() {
164   h1<int>(&M::addP);
165   h1(&M::subtractP);
166 }
167
168
169 namespace signed_hex_i64 {
170 void f(long long); // expected-note {{candidate function}}
171 void f(int); // expected-note {{candidate function}}
172 void g() {
173   // This used to be controlled by -fms-extensions, but it is now under
174   // -fms-compatibility.
175   f(0xffffffffffffffffLL); // expected-error {{call to 'f' is ambiguous}}
176   f(0xffffffffffffffffi64);
177 }
178 }
179
180 // Enumeration types with a fixed underlying type.
181 const int seventeen = 17;
182 typedef int Int;
183
184 struct X0 {
185   enum E1 : Int { SomeOtherValue } field;
186 #if __cplusplus <= 199711L
187   // expected-warning@-2 {{enumeration types with a fixed underlying type are a C++11 extension}}
188 #endif
189
190   enum E1 : seventeen;
191 };
192
193 #if __cplusplus <= 199711L
194 // expected-warning@+2 {{enumeration types with a fixed underlying type are a C++11 extension}}
195 #endif
196 enum : long long {
197   SomeValue = 0x100000000
198 };
199
200
201 class AAA {
202 __declspec(dllimport) void f(void) { }
203 void f2(void); // expected-note{{previous declaration is here}}
204 };
205
206 __declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}}
207                                            // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}}
208
209 }
210
211
212
213 template <class T>
214 class BB {
215 public:
216    void f(int g = 10 ); // expected-note {{previous definition is here}}
217 };
218
219 template <class T>
220 void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}}
221
222
223
224 extern void static_func();
225 void static_func(); // expected-note {{previous declaration is here}}
226
227
228 static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}}
229 {
230
231 }
232
233 extern const int static_var; // expected-note {{previous declaration is here}}
234 static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}
235
236 void pointer_to_integral_type_conv(char* ptr) {
237    char ch = (char)ptr;
238    short sh = (short)ptr;
239    ch = (char)ptr;
240    sh = (short)ptr;
241
242    // These are valid C++.
243    bool b = (bool)ptr;
244    b = static_cast<bool>(ptr);
245
246    // This is bad.
247    b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
248 }
249
250 struct PR11150 {
251   class X {
252     virtual void f() = 0;
253   };
254
255   int array[__is_abstract(X)? 1 : -1];
256 };
257
258 void f() { int __except = 0; }
259
260 void ::f(); // expected-warning{{extra qualification on member 'f'}}
261
262 class C {
263   C::C(); // expected-warning{{extra qualification on member 'C'}}
264 };
265
266 struct StructWithProperty {
267   __declspec(property(get=GetV)) int V1;
268   __declspec(property(put=SetV)) int V2;
269   __declspec(property(get=GetV, put=SetV_NotExist)) int V3;
270   __declspec(property(get=GetV_NotExist, put=SetV)) int V4;
271   __declspec(property(get=GetV, put=SetV)) int V5;
272
273   int GetV() { return 123; }
274   void SetV(int i) {}
275 };
276 void TestProperty() {
277   StructWithProperty sp;
278   int i = sp.V2; // expected-error{{no getter defined for property 'V2'}}
279   sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}}
280   int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}}
281   sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}}
282   int k = sp.V5;
283   sp.V5 = k++;
284 }
285
286 /* 4 tests for PseudoObject, begin */
287 struct SP1
288 {
289   bool operator()() { return true; }
290 };
291 struct SP2
292 {
293   __declspec(property(get=GetV)) SP1 V;
294   SP1 GetV() { return SP1(); }
295 };
296 void TestSP2() {
297   SP2 sp2;
298   bool b = sp2.V();
299 }
300
301 struct SP3 {
302   template <class T>
303   void f(T t) {}
304 };
305 template <class T>
306 struct SP4
307 {
308   __declspec(property(get=GetV)) int V;
309   int GetV() { return 123; }
310   void f() { SP3 s2; s2.f(V); }
311 };
312 void TestSP4() {
313   SP4<int> s;
314   s.f();
315 }
316
317 template <class T>
318 struct SP5
319 {
320   __declspec(property(get=GetV)) T V;
321   int GetV() { return 123; }
322   void f() { int *p = new int[V]; }
323 };
324
325 template <class T>
326 struct SP6
327 {
328 public:
329   __declspec(property(get=GetV)) T V;
330   T GetV() { return 123; }
331   void f() { int t = V; }
332 };
333 void TestSP6() {
334   SP6<int> c;
335   c.f();
336 }
337 /* 4 tests for PseudoObject, end */
338
339 // Property access: explicit, implicit, with Qualifier
340 struct SP7 {
341   __declspec(property(get=GetV, put=SetV)) int V;
342   int GetV() { return 123; }
343   void SetV(int v) {}
344
345   void ImplicitAccess() { int i = V; V = i; }
346   void ExplicitAccess() { int i = this->V; this->V = i; }
347 };
348 struct SP8: public SP7 {
349   void AccessWithQualifier() { int i = SP7::V; SP7::V = i; }
350 };
351
352 // Property usage
353 template <class T>
354 struct SP9 {
355   __declspec(property(get=GetV, put=SetV)) T V;
356   T GetV() { return 0; }
357   void SetV(T v) {}
358   bool f() { V = this->V; return V < this->V; }
359   void g() { V++; }
360   void h() { V*=2; }
361 };
362 struct SP10 {
363   SP10(int v) {}
364   bool operator<(const SP10& v) { return true; }
365   SP10 operator*(int v) { return *this; }
366   SP10 operator+(int v) { return *this; }
367   SP10& operator=(const SP10& v) { return *this; }
368 };
369 void TestSP9() {
370   SP9<int> c;
371   int i = c.V; // Decl initializer
372   i = c.V; // Binary op operand
373   c.SetV(c.V); // CallExpr arg
374   int *p = new int[c.V + 1]; // Array size
375   p[c.V] = 1; // Array index
376
377   c.V = 123; // Setter
378
379   c.V++; // Unary op operand
380   c.V *= 2; // Unary op operand
381
382   SP9<int*> c2;
383   c2.V[0] = 123; // Array
384
385   SP9<SP10> c3;
386   c3.f(); // Overloaded binary op operand
387   c3.g(); // Overloaded incdec op operand
388   c3.h(); // Overloaded unary op operand
389 }
390
391 union u {
392   int *i1;
393   int &i2;  // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
394 };
395
396 // Property getter using reference.
397 struct SP11 {
398   __declspec(property(get=GetV)) int V;
399   int _v;
400   int& GetV() { return _v; }
401   void UseV();
402   void TakePtr(int *) {}
403   void TakeRef(int &) {}
404   void TakeVal(int) {}
405 };
406
407 void SP11::UseV() {
408   TakePtr(&V);
409   TakeRef(V);
410   TakeVal(V);
411 }
412
413 struct StructWithUnnamedMember {
414   __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}}
415 };
416
417 struct MSPropertyClass {
418   int get() { return 42; }
419   int __declspec(property(get = get)) n;
420 };
421
422 int *f(MSPropertyClass &x) {
423   return &x.n; // expected-error {{address of property expression requested}}
424 }
425 int MSPropertyClass::*g() {
426   return &MSPropertyClass::n; // expected-error {{address of property expression requested}}
427 }
428
429 namespace rdar14250378 {
430   class Bar {};
431
432   namespace NyNamespace {
433     class Foo {
434     public:
435       Bar* EnsureBar();
436     };
437
438     class Baz : public Foo {
439     public:
440       friend class Bar;
441     };
442
443     Bar* Foo::EnsureBar() {
444       return 0;
445     }
446   }
447 }
448
449 // expected-error@+1 {{'sealed' keyword not permitted with interface types}}
450 __interface InterfaceWithSealed sealed {
451 };
452
453 struct SomeBase {
454   virtual void OverrideMe();
455
456   // expected-note@+2 {{overridden virtual function is here}}
457   // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
458   virtual void SealedFunction() sealed; // expected-note {{overridden virtual function is here}}
459 };
460
461 // expected-note@+2 {{'SealedType' declared here}}
462 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
463 struct SealedType sealed : SomeBase {
464   // expected-error@+2 {{declaration of 'SealedFunction' overrides a 'sealed' function}}
465   // FIXME. warning can be suppressed if we're also issuing error for overriding a 'final' function.
466   virtual void SealedFunction(); // expected-warning {{'SealedFunction' overrides a member function but is not marked 'override'}}
467
468 #if __cplusplus <= 199711L
469   // expected-warning@+2 {{'override' keyword is a C++11 extension}}
470 #endif
471   virtual void OverrideMe() override;
472 };
473
474 // expected-error@+1 {{base 'SealedType' is marked 'sealed'}}
475 struct InheritFromSealed : SealedType {};
476
477 void AfterClassBody() {
478   // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}}
479   struct D {} __declspec(deprecated);
480
481   struct __declspec(align(4)) S {} __declspec(align(8)) s1;
482   S s2;
483   _Static_assert(__alignof(S) == 4, "");
484   _Static_assert(__alignof(s1) == 8, "");
485   _Static_assert(__alignof(s2) == 4, "");
486 }
487
488 namespace PR24246 {
489 template <typename TX> struct A {
490   template <bool> struct largest_type_select;
491   // expected-warning@+1 {{explicit specialization of 'largest_type_select' within class scope is a Microsoft extension}}
492   template <> struct largest_type_select<false> {
493     blah x;  // expected-error {{unknown type name 'blah'}}
494   };
495 };
496 }
497
498 namespace PR25265 {
499 struct S {
500   int fn() throw(); // expected-note {{previous declaration is here}}
501 };
502
503 int S::fn() { return 0; } // expected-warning {{is missing exception specification}}
504 }
505
506 #elif TEST2
507
508 // Check that __unaligned is not recognized if MS extensions are not enabled
509 typedef char __unaligned *aligned_type; // expected-error {{expected ';' after top level declarator}}
510
511 #else
512
513 #error Unknown test mode
514
515 #endif
516