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