1 // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -fms-extensions -fexceptions -fcxx-exceptions
2 
3 
4 // Microsoft doesn't validate exception specification.
5 namespace microsoft_exception_spec {
6 
7 void foo(); // expected-note {{previous declaration}}
8 void foo() throw(); // expected-warning {{exception specification in declaration does not match previous declaration}}
9 
10 void r6() throw(...); // expected-note {{previous declaration}}
11 void r6() throw(int); // expected-warning {{exception specification in declaration does not match previous declaration}}
12 
13 struct Base {
14   virtual void f2();
15   virtual void f3() throw(...);
16 };
17 
18 struct Derived : Base {
19   virtual void f2() throw(...);
20   virtual void f3();
21 };
22 
23 class A {
24   virtual ~A() throw();  // expected-note {{overridden virtual function is here}}
25 };
26 
27 class B : public A {
28   virtual ~B();  // expected-warning {{exception specification of overriding function is more lax than base version}}
29 };
30 
31 }
32 
33 // MSVC allows type definition in anonymous union and struct
34 struct A
35 {
36   union
37   {
38     int a;
39     struct B  // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
40     {
41       int c;
42     } d;
43 
44     union C   // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
45     {
46       int e;
47       int ee;
48     } f;
49 
50     typedef int D;  // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
51     struct F;  // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
52   };
53 
54   struct
55   {
56     int a2;
57 
58     struct B2  // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
59     {
60       int c2;
61     } d2;
62 
63 	union C2  // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
64     {
65       int e2;
66       int ee2;
67     } f2;
68 
69     typedef int D2;  // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
70     struct F2;  // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
71   };
72 };
73 
74 // __stdcall handling
75 struct M {
76     int __stdcall addP();
77     float __stdcall subtractP();
78 };
79 
80 // __unaligned handling
81 typedef char __unaligned *aligned_type;
82 typedef struct UnalignedTag { int f; } __unaligned *aligned_type2;
83 
84 
h1(T (__stdcall M::* const)())85 template<typename T> void h1(T (__stdcall M::* const )()) { }
86 
m1()87 void m1() {
88   h1<int>(&M::addP);
89   h1(&M::subtractP);
90 }
91 
92 
93 
94 
95 
96 void f(long long);
97 void f(int);
98 
main()99 int main()
100 {
101   // This is an ambiguous call in standard C++.
102   // This calls f(long long) in Microsoft mode because LL is always signed.
103   f(0xffffffffffffffffLL);
104   f(0xffffffffffffffffi64);
105 }
106 
107 // Enumeration types with a fixed underlying type.
108 const int seventeen = 17;
109 typedef int Int;
110 
111 struct X0 {
112   enum E1 : Int { SomeOtherValue } field; // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}}
113   enum E1 : seventeen;
114 };
115 
116 enum : long long {  // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}}
117   SomeValue = 0x100000000
118 };
119 
120 
121 class AAA {
f(void)122 __declspec(dllimport) void f(void) { }
123 void f2(void); // expected-note{{previous declaration is here}}
124 };
125 
f2(void)126 __declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}}
127                                            // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}}
128 
129 }
130 
131 
132 
133 template <class T>
134 class BB {
135 public:
136    void f(int g = 10 ); // expected-note {{previous definition is here}}
137 };
138 
139 template <class T>
f(int g=0)140 void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}}
141 
142 
143 
144 extern void static_func();
145 void static_func(); // expected-note {{previous declaration is here}}
146 
147 
static_func()148 static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}}
149 {
150 
151 }
152 
153 extern const int static_var; // expected-note {{previous declaration is here}}
154 static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}
155 
pointer_to_integral_type_conv(char * ptr)156 void pointer_to_integral_type_conv(char* ptr) {
157    char ch = (char)ptr;
158    short sh = (short)ptr;
159    ch = (char)ptr;
160    sh = (short)ptr;
161 
162    // These are valid C++.
163    bool b = (bool)ptr;
164    b = static_cast<bool>(ptr);
165 
166    // This is bad.
167    b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
168 }
169 
170 struct PR11150 {
171   class X {
172     virtual void f() = 0;
173   };
174 
175   int array[__is_abstract(X)? 1 : -1];
176 };
177 
f()178 void f() { int __except = 0; }
179 
180 void ::f(); // expected-warning{{extra qualification on member 'f'}}
181 
182 class C {
183   C::C(); // expected-warning{{extra qualification on member 'C'}}
184 };
185 
186 struct StructWithProperty {
187   __declspec(property(get=GetV)) int V1;
188   __declspec(property(put=SetV)) int V2;
189   __declspec(property(get=GetV, put=SetV_NotExist)) int V3;
190   __declspec(property(get=GetV_NotExist, put=SetV)) int V4;
191   __declspec(property(get=GetV, put=SetV)) int V5;
192 
GetVStructWithProperty193   int GetV() { return 123; }
SetVStructWithProperty194   void SetV(int i) {}
195 };
TestProperty()196 void TestProperty() {
197   StructWithProperty sp;
198   int i = sp.V2; // expected-error{{no getter defined for property 'V2'}}
199   sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}}
200   int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}}
201   sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}}
202   int k = sp.V5;
203   sp.V5 = k++;
204 }
205 
206 /* 4 tests for PseudoObject, begin */
207 struct SP1
208 {
operator ()SP1209   bool operator()() { return true; }
210 };
211 struct SP2
212 {
213   __declspec(property(get=GetV)) SP1 V;
GetVSP2214   SP1 GetV() { return SP1(); }
215 };
TestSP2()216 void TestSP2() {
217   SP2 sp2;
218   bool b = sp2.V();
219 }
220 
221 struct SP3 {
222   template <class T>
fSP3223   void f(T t) {}
224 };
225 template <class T>
226 struct SP4
227 {
228   __declspec(property(get=GetV)) int V;
GetVSP4229   int GetV() { return 123; }
fSP4230   void f() { SP3 s2; s2.f(V); }
231 };
TestSP4()232 void TestSP4() {
233   SP4<int> s;
234   s.f();
235 }
236 
237 template <class T>
238 struct SP5
239 {
240   __declspec(property(get=GetV)) T V;
GetVSP5241   int GetV() { return 123; }
fSP5242   void f() { int *p = new int[V]; }
243 };
244 
245 template <class T>
246 struct SP6
247 {
248 public:
249   __declspec(property(get=GetV)) T V;
GetVSP6250   T GetV() { return 123; }
fSP6251   void f() { int t = V; }
252 };
TestSP6()253 void TestSP6() {
254   SP6<int> c;
255   c.f();
256 }
257 /* 4 tests for PseudoObject, end */
258 
259 // Property access: explicit, implicit, with Qualifier
260 struct SP7 {
261   __declspec(property(get=GetV, put=SetV)) int V;
GetVSP7262   int GetV() { return 123; }
SetVSP7263   void SetV(int v) {}
264 
ImplicitAccessSP7265   void ImplicitAccess() { int i = V; V = i; }
ExplicitAccessSP7266   void ExplicitAccess() { int i = this->V; this->V = i; }
267 };
268 struct SP8: public SP7 {
AccessWithQualifierSP8269   void AccessWithQualifier() { int i = SP7::V; SP7::V = i; }
270 };
271 
272 // Property usage
273 template <class T>
274 struct SP9 {
275   __declspec(property(get=GetV, put=SetV)) T V;
GetVSP9276   T GetV() { return 0; }
SetVSP9277   void SetV(T v) {}
fSP9278   bool f() { V = this->V; return V < this->V; }
gSP9279   void g() { V++; }
hSP9280   void h() { V*=2; }
281 };
282 struct SP10 {
SP10SP10283   SP10(int v) {}
operator <SP10284   bool operator<(const SP10& v) { return true; }
operator *SP10285   SP10 operator*(int v) { return *this; }
operator +SP10286   SP10 operator+(int v) { return *this; }
operator =SP10287   SP10& operator=(const SP10& v) { return *this; }
288 };
TestSP9()289 void TestSP9() {
290   SP9<int> c;
291   int i = c.V; // Decl initializer
292   i = c.V; // Binary op operand
293   c.SetV(c.V); // CallExpr arg
294   int *p = new int[c.V + 1]; // Array size
295   p[c.V] = 1; // Array index
296 
297   c.V = 123; // Setter
298 
299   c.V++; // Unary op operand
300   c.V *= 2; // Unary op operand
301 
302   SP9<int*> c2;
303   c2.V[0] = 123; // Array
304 
305   SP9<SP10> c3;
306   c3.f(); // Overloaded binary op operand
307   c3.g(); // Overloaded incdec op operand
308   c3.h(); // Overloaded unary op operand
309 }
310 
311 union u {
312   int *i1;
313   int &i2;  // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
314 };
315 
316 // Property getter using reference.
317 struct SP11 {
318   __declspec(property(get=GetV)) int V;
319   int _v;
GetVSP11320   int& GetV() { return _v; }
321   void UseV();
TakePtrSP11322   void TakePtr(int *) {}
TakeRefSP11323   void TakeRef(int &) {}
TakeValSP11324   void TakeVal(int) {}
325 };
326 
UseV()327 void SP11::UseV() {
328   TakePtr(&V);
329   TakeRef(V);
330   TakeVal(V);
331 }
332 
333 struct StructWithUnnamedMember {
334   __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}}
335 };
336 
337 struct MSPropertyClass {
getMSPropertyClass338   int get() { return 42; }
339   int __declspec(property(get = get)) n;
340 };
341 
f(MSPropertyClass & x)342 int *f(MSPropertyClass &x) {
343   return &x.n; // expected-error {{address of property expression requested}}
344 }
g()345 int MSPropertyClass::*g() {
346   return &MSPropertyClass::n; // expected-error {{address of property expression requested}}
347 }
348 
349 namespace rdar14250378 {
350   class Bar {};
351 
352   namespace NyNamespace {
353     class Foo {
354     public:
355       Bar* EnsureBar();
356     };
357 
358     class Baz : public Foo {
359     public:
360       friend class Bar;
361     };
362 
EnsureBar()363     Bar* Foo::EnsureBar() {
364       return 0;
365     }
366   }
367 }
368 
369 // expected-error@+1 {{'sealed' keyword not permitted with interface types}}
370 __interface InterfaceWithSealed sealed {
371 };
372 
373 struct SomeBase {
374   virtual void OverrideMe();
375 
376   // expected-note@+2 {{overridden virtual function is here}}
377   // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
378   virtual void SealedFunction() sealed; // expected-note {{overridden virtual function is here}}
379 };
380 
381 // expected-note@+2 {{'SealedType' declared here}}
382 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
383 struct SealedType sealed : SomeBase {
384   // expected-error@+2 {{declaration of 'SealedFunction' overrides a 'sealed' function}}
385   // FIXME. warning can be suppressed if we're also issuing error for overriding a 'final' function.
386   virtual void SealedFunction(); // expected-warning {{'SealedFunction' overrides a member function but is not marked 'override'}}
387 
388   // expected-warning@+1 {{'override' keyword is a C++11 extension}}
389   virtual void OverrideMe() override;
390 };
391 
392 // expected-error@+1 {{base 'SealedType' is marked 'sealed'}}
393 struct InheritFromSealed : SealedType {};
394 
AfterClassBody()395 void AfterClassBody() {
396   // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}}
397   struct D {} __declspec(deprecated);
398 
399   struct __declspec(align(4)) S {} __declspec(align(8)) s1;
400   S s2;
401   _Static_assert(__alignof(S) == 4, "");
402   _Static_assert(__alignof(s1) == 8, "");
403   _Static_assert(__alignof(s2) == 4, "");
404 }
405 
406 namespace PR24246 {
407 template <typename TX> struct A {
408   template <bool> struct largest_type_select;
409   // expected-warning@+1 {{explicit specialization of 'largest_type_select' within class scope is a Microsoft extension}}
410   template <> struct largest_type_select<false> {
411     blah x;  // expected-error {{unknown type name 'blah'}}
412   };
413 };
414 }
415 
416 namespace PR25265 {
417 struct S {
418   int fn() throw(); // expected-note {{previous declaration is here}}
419 };
420 
fn()421 int S::fn() { return 0; } // expected-warning {{is missing exception specification}}
422 }
423