blob: 22cf2be7c1ac927b19ee21f0d90a58e291b09836 [file] [log] [blame]
Nico Weber2801d322016-05-06 14:34:29 +00001// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -fms-extensions -fexceptions -fcxx-exceptions
Douglas Gregor2b1bbec2010-08-30 14:44:26 +00002
3
Francois Pichet13b4e682011-03-19 23:05:18 +00004// Microsoft doesn't validate exception specification.
Francois Picheta8032e92011-05-24 02:11:43 +00005namespace microsoft_exception_spec {
6
Douglas Gregorf62c5292010-08-30 15:04:51 +00007void foo(); // expected-note {{previous declaration}}
Francois Pichet13b4e682011-03-19 23:05:18 +00008void foo() throw(); // expected-warning {{exception specification in declaration does not match previous declaration}}
Douglas Gregor2b1bbec2010-08-30 14:44:26 +00009
Francois Pichet13b4e682011-03-19 23:05:18 +000010void r6() throw(...); // expected-note {{previous declaration}}
11void r6() throw(int); // expected-warning {{exception specification in declaration does not match previous declaration}}
Douglas Gregorf62c5292010-08-30 15:04:51 +000012
13struct Base {
14 virtual void f2();
15 virtual void f3() throw(...);
16};
17
18struct Derived : Base {
19 virtual void f2() throw(...);
20 virtual void f3();
21};
Douglas Gregoreece0ea2010-09-01 16:29:03 +000022
Francois Picheta8032e92011-05-24 02:11:43 +000023class A {
24 virtual ~A() throw(); // expected-note {{overridden virtual function is here}}
25};
26
27class B : public A {
28 virtual ~B(); // expected-warning {{exception specification of overriding function is more lax than base version}}
29};
30
31}
Francois Pichet4ad4b582010-09-08 11:32:25 +000032
33// MSVC allows type definition in anonymous union and struct
34struct A
35{
Aaron Ballman3e424b52013-12-26 18:30:57 +000036 union
Francois Pichet4ad4b582010-09-08 11:32:25 +000037 {
38 int a;
39 struct B // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
Aaron Ballman3e424b52013-12-26 18:30:57 +000040 {
Francois Pichet4ad4b582010-09-08 11:32:25 +000041 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;
Aaron Ballman3e424b52013-12-26 18:30:57 +000062
Francois Pichet4ad4b582010-09-08 11:32:25 +000063 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
Douglas Gregoreece0ea2010-09-01 16:29:03 +000074// __stdcall handling
75struct M {
76 int __stdcall addP();
Aaron Ballman3e424b52013-12-26 18:30:57 +000077 float __stdcall subtractP();
Douglas Gregoreece0ea2010-09-01 16:29:03 +000078};
79
Francois Pichet17ed0202011-08-18 09:59:55 +000080// __unaligned handling
81typedef char __unaligned *aligned_type;
Nico Rieck3e1ee832014-12-04 23:30:25 +000082typedef struct UnalignedTag { int f; } __unaligned *aligned_type2;
Francois Pichet17ed0202011-08-18 09:59:55 +000083
84
Douglas Gregoreece0ea2010-09-01 16:29:03 +000085template<typename T> void h1(T (__stdcall M::* const )()) { }
86
87void m1() {
88 h1<int>(&M::addP);
89 h1(&M::subtractP);
Aaron Ballman3e424b52013-12-26 18:30:57 +000090}
Francois Pichet488b4a72010-09-12 05:06:55 +000091
Francois Picheta3108062010-10-18 15:01:13 +000092
93
Francois Pichet7d92ee32011-01-17 01:08:01 +000094
95
96void f(long long);
97void f(int);
Aaron Ballman3e424b52013-12-26 18:30:57 +000098
Francois Pichet7d92ee32011-01-17 01:08:01 +000099int 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}
Douglas Gregora1aec292011-02-22 20:32:04 +0000106
107// Enumeration types with a fixed underlying type.
108const int seventeen = 17;
109typedef int Int;
110
111struct X0 {
Eli Friedman0d0355ab2012-11-02 01:34:28 +0000112 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 +0000113 enum E1 : seventeen;
114};
115
Eli Friedman0d0355ab2012-11-02 01:34:28 +0000116enum : long long { // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}}
Douglas Gregora1aec292011-02-22 20:32:04 +0000117 SomeValue = 0x100000000
118};
Francois Pichet3096d202011-03-29 10:39:17 +0000119
120
121class AAA {
122__declspec(dllimport) void f(void) { }
Nico Rieck82f0b062014-03-31 14:56:15 +0000123void f2(void); // expected-note{{previous declaration is here}}
Francois Pichet3096d202011-03-29 10:39:17 +0000124};
125
Hans Wennborgb0f2f142014-05-15 22:07:49 +0000126__declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}}
Nico Rieck82f0b062014-03-31 14:56:15 +0000127 // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}}
Francois Pichet3096d202011-03-29 10:39:17 +0000128
129}
130
131
132
Francois Pichet53fe2bb2011-04-10 03:03:52 +0000133template <class T>
134class BB {
135public:
136 void f(int g = 10 ); // expected-note {{previous definition is here}}
137};
138
139template <class T>
140void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}}
141
Francois Pichet48c946e2011-04-13 02:38:49 +0000142
Francois Pichete900b102011-04-22 08:14:00 +0000143
144extern void static_func();
145void static_func(); // expected-note {{previous declaration is here}}
146
147
David Majnemer5b63fa02014-06-18 23:26:25 +0000148static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}}
Francois Pichete900b102011-04-22 08:14:00 +0000149{
150
Francois Pichetbc6ebb52011-05-08 22:52:41 +0000151}
152
David Majnemer5b63fa02014-06-18 23:26:25 +0000153extern const int static_var; // expected-note {{previous declaration is here}}
154static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}
155
Francois Pichetb796b632011-05-11 22:13:54 +0000156void 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;
Francois Pichetefb1af92011-05-23 03:43:44 +0000161
Hans Wennborg15439bc2013-06-06 09:16:36 +0000162 // 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}
Francois Pichete37eeba2011-06-01 04:14:20 +0000169
Douglas Gregor50cefbf2011-10-17 17:09:53 +0000170struct PR11150 {
171 class X {
172 virtual void f() = 0;
173 };
174
175 int array[__is_abstract(X)? 1 : -1];
176};
Douglas Gregor60060d62011-10-21 03:57:52 +0000177
178void f() { int __except = 0; }
179
Douglas Gregor43bc0362012-09-13 20:16:20 +0000180void ::f(); // expected-warning{{extra qualification on member 'f'}}
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +0000181
182class C {
183 C::C(); // expected-warning{{extra qualification on member 'C'}}
184};
John McCall5e77d762013-04-16 07:28:30 +0000185
186struct 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
193 int GetV() { return 123; }
194 void SetV(int i) {}
195};
196void 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 */
207struct SP1
208{
209 bool operator()() { return true; }
210};
211struct SP2
212{
213 __declspec(property(get=GetV)) SP1 V;
214 SP1 GetV() { return SP1(); }
215};
216void TestSP2() {
217 SP2 sp2;
218 bool b = sp2.V();
219}
220
221struct SP3 {
222 template <class T>
223 void f(T t) {}
224};
225template <class T>
226struct SP4
227{
228 __declspec(property(get=GetV)) int V;
229 int GetV() { return 123; }
230 void f() { SP3 s2; s2.f(V); }
231};
232void TestSP4() {
233 SP4<int> s;
234 s.f();
235}
236
237template <class T>
238struct SP5
239{
240 __declspec(property(get=GetV)) T V;
241 int GetV() { return 123; }
242 void f() { int *p = new int[V]; }
243};
244
245template <class T>
246struct SP6
247{
248public:
249 __declspec(property(get=GetV)) T V;
250 T GetV() { return 123; }
251 void f() { int t = V; }
252};
253void TestSP6() {
254 SP6<int> c;
255 c.f();
256}
257/* 4 tests for PseudoObject, end */
258
259// Property access: explicit, implicit, with Qualifier
260struct SP7 {
261 __declspec(property(get=GetV, put=SetV)) int V;
262 int GetV() { return 123; }
263 void SetV(int v) {}
264
265 void ImplicitAccess() { int i = V; V = i; }
266 void ExplicitAccess() { int i = this->V; this->V = i; }
267};
268struct SP8: public SP7 {
269 void AccessWithQualifier() { int i = SP7::V; SP7::V = i; }
270};
271
272// Property usage
273template <class T>
274struct SP9 {
275 __declspec(property(get=GetV, put=SetV)) T V;
276 T GetV() { return 0; }
277 void SetV(T v) {}
Richard Trieu99e1c952014-03-11 03:11:08 +0000278 bool f() { V = this->V; return V < this->V; }
John McCall0d9dd732013-04-16 22:32:04 +0000279 void g() { V++; }
280 void h() { V*=2; }
John McCall5e77d762013-04-16 07:28:30 +0000281};
282struct SP10 {
283 SP10(int v) {}
284 bool operator<(const SP10& v) { return true; }
285 SP10 operator*(int v) { return *this; }
John McCall0d9dd732013-04-16 22:32:04 +0000286 SP10 operator+(int v) { return *this; }
John McCall5e77d762013-04-16 07:28:30 +0000287 SP10& operator=(const SP10& v) { return *this; }
288};
289void 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
John McCall0d9dd732013-04-16 22:32:04 +0000307 c3.g(); // Overloaded incdec op operand
308 c3.h(); // Overloaded unary op operand
John McCall5e77d762013-04-16 07:28:30 +0000309}
Aaron Ballmaned0ae1d2013-05-30 16:20:00 +0000310
311union u {
312 int *i1;
313 int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
314};
Reid Kleckner0a0c8892013-06-19 16:37:23 +0000315
316// Property getter using reference.
317struct SP11 {
318 __declspec(property(get=GetV)) int V;
319 int _v;
320 int& GetV() { return _v; }
321 void UseV();
322 void TakePtr(int *) {}
323 void TakeRef(int &) {}
324 void TakeVal(int) {}
325};
326
327void SP11::UseV() {
328 TakePtr(&V);
329 TakeRef(V);
330 TakeVal(V);
331}
Aaron Ballman2a6febc2013-06-26 21:28:44 +0000332
333struct StructWithUnnamedMember {
334 __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}}
335};
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000336
Reid Kleckner85c7e0a2015-02-24 20:29:40 +0000337struct MSPropertyClass {
338 int get() { return 42; }
339 int __declspec(property(get = get)) n;
340};
341
342int *f(MSPropertyClass &x) {
343 return &x.n; // expected-error {{address of property expression requested}}
344}
345int MSPropertyClass::*g() {
346 return &MSPropertyClass::n; // expected-error {{address of property expression requested}}
347}
348
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000349namespace rdar14250378 {
350 class Bar {};
Aaron Ballman3e424b52013-12-26 18:30:57 +0000351
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000352 namespace NyNamespace {
353 class Foo {
354 public:
355 Bar* EnsureBar();
356 };
Aaron Ballman3e424b52013-12-26 18:30:57 +0000357
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000358 class Baz : public Foo {
359 public:
360 friend class Bar;
361 };
Aaron Ballman3e424b52013-12-26 18:30:57 +0000362
Douglas Gregorf83b4ea2013-06-27 20:42:30 +0000363 Bar* Foo::EnsureBar() {
364 return 0;
365 }
366 }
367}
David Majnemera5433082013-10-18 00:33:31 +0000368
369// expected-error@+1 {{'sealed' keyword not permitted with interface types}}
370__interface InterfaceWithSealed sealed {
371};
372
373struct 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}}
Fariborz Jahanianf920a0a2014-10-27 19:11:51 +0000378 virtual void SealedFunction() sealed; // expected-note {{overridden virtual function is here}}
David Majnemera5433082013-10-18 00:33:31 +0000379};
380
381// expected-note@+2 {{'SealedType' declared here}}
382// expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
383struct SealedType sealed : SomeBase {
Fariborz Jahanianf920a0a2014-10-27 19:11:51 +0000384 // 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'}}
David Majnemera5433082013-10-18 00:33:31 +0000387
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'}}
393struct InheritFromSealed : SealedType {};
Alp Tokerd3f79c52013-11-24 20:24:54 +0000394
395void 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}
Kaelyn Takata26487022015-10-01 22:38:51 +0000405
406namespace PR24246 {
407template <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}
David Majnemer06ce8a42015-10-20 20:49:21 +0000415
416namespace PR25265 {
417struct S {
418 int fn() throw(); // expected-note {{previous declaration is here}}
419};
420
421int S::fn() { return 0; } // expected-warning {{is missing exception specification}}
422}