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
foo_unaligned(int * arg)71 void foo_unaligned(int *arg) {}
foo_unaligned(__unaligned int * arg)72 void foo_unaligned(__unaligned int *arg) {}
foo_unaligned(int arg)73 void foo_unaligned(int arg) {} // expected-note {{previous definition is here}}
foo_unaligned(__unaligned int arg)74 void foo_unaligned(__unaligned int arg) {} // expected-error {{redefinition of 'foo_unaligned'}}
75 class A_unaligned {};
76 class B_unaligned : public A_unaligned {};
foo_unaligned(__unaligned A_unaligned * arg)77 int foo_unaligned(__unaligned A_unaligned *arg) { return 0; }
foo_unaligned(B_unaligned * arg)78 void *foo_unaligned(B_unaligned *arg) { return 0; }
79
test_unaligned()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);
FreeIDListArray(LPITEMIDLIST * ppidls)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:
in_addrin_addr118 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 *}}
in_addrin_addr119 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
f(IN_ADDR __unaligned * a)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
h1(T (__stdcall M::* const)())127 template<typename T> void h1(T (__stdcall M::* const )()) { }
128
m1()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}}
g()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 #if __cplusplus >= 201103L
158 // expected-error@-2 {{bit-field}}
159 #endif
160 };
161
162 #if __cplusplus <= 199711L
163 // expected-warning@+2 {{enumeration types with a fixed underlying type are a C++11 extension}}
164 #endif
165 enum : long long {
166 SomeValue = 0x100000000
167 };
168
169
170 class AAA {
f(void)171 __declspec(dllimport) void f(void) { }
172 void f2(void); // expected-note{{previous declaration is here}}
173 };
174
f2(void)175 __declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}}
176 // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}}
177
178 }
179
180
181
182 template <class T>
183 class BB {
184 public:
185 void f(int g = 10 ); // expected-note {{previous definition is here}}
186 };
187
188 template <class T>
f(int g=0)189 void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}}
190
191
192
193 extern void static_func();
194 void static_func(); // expected-note {{previous declaration is here}}
195
196
static_func()197 static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}}
198 {
199
200 }
201
202 extern const int static_var; // expected-note {{previous declaration is here}}
203 static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}
204
pointer_to_integral_type_conv(char * ptr)205 void pointer_to_integral_type_conv(char* ptr) {
206 char ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'char *'}}
207 short sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'char *'}}
208 ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'char *'}}
209 sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'char *'}}
210
211 // These are valid C++.
212 bool b = (bool)ptr;
213 b = static_cast<bool>(ptr);
214
215 // This is bad.
216 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
217 }
218
void_pointer_to_integral_type_conv(void * ptr)219 void void_pointer_to_integral_type_conv(void *ptr) {
220 char ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'void *'}}
221 short sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'void *'}}
222 ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'void *'}}
223 sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'void *'}}
224
225 // These are valid C++.
226 bool b = (bool)ptr;
227 b = static_cast<bool>(ptr);
228
229 // This is bad.
230 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
231 }
232
233 struct PR11150 {
234 class X {
235 virtual void f() = 0;
236 };
237
238 int array[__is_abstract(X)? 1 : -1];
239 };
240
f()241 void f() { int __except = 0; }
242
243 void ::f(); // expected-warning{{extra qualification on member 'f'}}
244
245 class C {
246 C::C(); // expected-warning{{extra qualification on member 'C'}}
247 };
248
249 struct StructWithProperty {
250 __declspec(property(get=GetV)) int V1;
251 __declspec(property(put=SetV)) int V2;
252 __declspec(property(get=GetV, put=SetV_NotExist)) int V3;
253 __declspec(property(get=GetV_NotExist, put=SetV)) int V4;
254 __declspec(property(get=GetV, put=SetV)) int V5;
255
GetVStructWithProperty256 int GetV() { return 123; }
SetVStructWithProperty257 void SetV(int i) {}
258 };
TestProperty()259 void TestProperty() {
260 StructWithProperty sp;
261 int i = sp.V2; // expected-error{{no getter defined for property 'V2'}}
262 sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}}
263 int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}}
264 sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}}
265 int k = sp.V5;
266 sp.V5 = k++;
267 }
268
269 /* 4 tests for PseudoObject, begin */
270 struct SP1
271 {
operator ()SP1272 bool operator()() { return true; }
273 };
274 struct SP2
275 {
276 __declspec(property(get=GetV)) SP1 V;
GetVSP2277 SP1 GetV() { return SP1(); }
278 };
TestSP2()279 void TestSP2() {
280 SP2 sp2;
281 bool b = sp2.V();
282 }
283
284 struct SP3 {
285 template <class T>
fSP3286 void f(T t) {}
287 };
288 template <class T>
289 struct SP4
290 {
291 __declspec(property(get=GetV)) int V;
GetVSP4292 int GetV() { return 123; }
fSP4293 void f() { SP3 s2; s2.f(V); }
294 };
TestSP4()295 void TestSP4() {
296 SP4<int> s;
297 s.f();
298 }
299
300 template <class T>
301 struct SP5
302 {
303 __declspec(property(get=GetV)) T V;
GetVSP5304 int GetV() { return 123; }
fSP5305 void f() { int *p = new int[V]; }
306 };
307
308 template <class T>
309 struct SP6
310 {
311 public:
312 __declspec(property(get=GetV)) T V;
GetVSP6313 T GetV() { return 123; }
fSP6314 void f() { int t = V; }
315 };
TestSP6()316 void TestSP6() {
317 SP6<int> c;
318 c.f();
319 }
320 /* 4 tests for PseudoObject, end */
321
322 // Property access: explicit, implicit, with Qualifier
323 struct SP7 {
324 __declspec(property(get=GetV, put=SetV)) int V;
GetVSP7325 int GetV() { return 123; }
SetVSP7326 void SetV(int v) {}
327
ImplicitAccessSP7328 void ImplicitAccess() { int i = V; V = i; }
ExplicitAccessSP7329 void ExplicitAccess() { int i = this->V; this->V = i; }
330 };
331 struct SP8: public SP7 {
AccessWithQualifierSP8332 void AccessWithQualifier() { int i = SP7::V; SP7::V = i; }
333 };
334
335 // Property usage
336 template <class T>
337 struct SP9 {
338 __declspec(property(get=GetV, put=SetV)) T V;
GetVSP9339 T GetV() { return 0; }
SetVSP9340 void SetV(T v) {}
fSP9341 bool f() { V = this->V; return V < this->V; }
gSP9342 void g() { V++; }
hSP9343 void h() { V*=2; }
344 };
345 struct SP10 {
SP10SP10346 SP10(int v) {}
operator <SP10347 bool operator<(const SP10& v) { return true; }
operator *SP10348 SP10 operator*(int v) { return *this; }
operator +SP10349 SP10 operator+(int v) { return *this; }
operator =SP10350 SP10& operator=(const SP10& v) { return *this; }
351 };
TestSP9()352 void TestSP9() {
353 SP9<int> c;
354 int i = c.V; // Decl initializer
355 i = c.V; // Binary op operand
356 c.SetV(c.V); // CallExpr arg
357 int *p = new int[c.V + 1]; // Array size
358 p[c.V] = 1; // Array index
359
360 c.V = 123; // Setter
361
362 c.V++; // Unary op operand
363 c.V *= 2; // Unary op operand
364
365 SP9<int*> c2;
366 c2.V[0] = 123; // Array
367
368 SP9<SP10> c3;
369 c3.f(); // Overloaded binary op operand
370 c3.g(); // Overloaded incdec op operand
371 c3.h(); // Overloaded unary op operand
372 }
373
374 union u {
375 int *i1;
376 int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
377 };
378
379 // Property getter using reference.
380 struct SP11 {
381 __declspec(property(get=GetV)) int V;
382 int _v;
GetVSP11383 int& GetV() { return _v; }
384 void UseV();
TakePtrSP11385 void TakePtr(int *) {}
TakeRefSP11386 void TakeRef(int &) {}
TakeValSP11387 void TakeVal(int) {}
388 };
389
UseV()390 void SP11::UseV() {
391 TakePtr(&V);
392 TakeRef(V);
393 TakeVal(V);
394 }
395
396 struct StructWithUnnamedMember {
397 __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}}
398 };
399
400 struct MSPropertyClass {
getMSPropertyClass401 int get() { return 42; }
402 int __declspec(property(get = get)) n;
403 };
404
f(MSPropertyClass & x)405 int *f(MSPropertyClass &x) {
406 return &x.n; // expected-error {{address of property expression requested}}
407 }
g()408 int MSPropertyClass::*g() {
409 return &MSPropertyClass::n; // expected-error {{address of property expression requested}}
410 }
411
412 namespace rdar14250378 {
413 class Bar {};
414
415 namespace NyNamespace {
416 class Foo {
417 public:
418 Bar* EnsureBar();
419 };
420
421 class Baz : public Foo {
422 public:
423 friend class Bar;
424 };
425
EnsureBar()426 Bar* Foo::EnsureBar() {
427 return 0;
428 }
429 }
430 }
431
432 // expected-error@+1 {{'sealed' keyword not permitted with interface types}}
433 __interface InterfaceWithSealed sealed {
434 };
435
436 struct SomeBase {
437 virtual void OverrideMe();
438
439 // expected-note@+2 {{overridden virtual function is here}}
440 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
441 virtual void SealedFunction() sealed; // expected-note {{overridden virtual function is here}}
442 };
443
444 // expected-note@+2 {{'SealedType' declared here}}
445 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
446 struct SealedType sealed : SomeBase {
447 // expected-error@+2 {{declaration of 'SealedFunction' overrides a 'sealed' function}}
448 // FIXME. warning can be suppressed if we're also issuing error for overriding a 'final' function.
449 virtual void SealedFunction(); // expected-warning {{'SealedFunction' overrides a member function but is not marked 'override'}}
450
451 #if __cplusplus <= 199711L
452 // expected-warning@+2 {{'override' keyword is a C++11 extension}}
453 #endif
454 virtual void OverrideMe() override;
455 };
456
457 // expected-error@+1 {{base 'SealedType' is marked 'sealed'}}
458 struct InheritFromSealed : SealedType {};
459
460 class SealedDestructor { // expected-note {{mark 'SealedDestructor' as 'sealed' to silence this warning}}
461 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
462 virtual ~SealedDestructor() sealed; // expected-warning {{class with destructor marked 'sealed' cannot be inherited from}}
463 };
464
465 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
466 class AbstractClass abstract {
467 int i;
468 };
469
470 // expected-error@+1 {{variable type 'AbstractClass' is an abstract class}}
471 AbstractClass abstractInstance;
472
473 // expected-warning@+4 {{abstract class is marked 'sealed'}}
474 // expected-note@+3 {{'AbstractAndSealedClass' declared here}}
475 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
476 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
477 class AbstractAndSealedClass abstract sealed {}; // Does no really make sense, but allowed
478
479 // expected-error@+1 {{variable type 'AbstractAndSealedClass' is an abstract class}}
480 AbstractAndSealedClass abstractAndSealedInstance;
481 // expected-error@+1 {{base 'AbstractAndSealedClass' is marked 'sealed'}}
482 class InheritFromAbstractAndSealed : AbstractAndSealedClass {};
483
484 #if __cplusplus <= 199711L
485 // expected-warning@+4 {{'final' keyword is a C++11 extension}}
486 // expected-warning@+3 {{'final' keyword is a C++11 extension}}
487 #endif
488 // expected-error@+1 {{class already marked 'final'}}
489 class TooManyVirtSpecifiers1 final final {};
490 #if __cplusplus <= 199711L
491 // expected-warning@+4 {{'final' keyword is a C++11 extension}}
492 #endif
493 // expected-warning@+2 {{'sealed' keyword is a Microsoft extension}}
494 // expected-error@+1 {{class already marked 'sealed'}}
495 class TooManyVirtSpecifiers2 final sealed {};
496 #if __cplusplus <= 199711L
497 // expected-warning@+6 {{'final' keyword is a C++11 extension}}
498 // expected-warning@+5 {{'final' keyword is a C++11 extension}}
499 #endif
500 // expected-warning@+3 {{abstract class is marked 'final'}}
501 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
502 // expected-error@+1 {{class already marked 'final'}}
503 class TooManyVirtSpecifiers3 final abstract final {};
504 #if __cplusplus <= 199711L
505 // expected-warning@+6 {{'final' keyword is a C++11 extension}}
506 #endif
507 // expected-warning@+4 {{abstract class is marked 'final'}}
508 // expected-warning@+3 {{'abstract' keyword is a Microsoft extension}}
509 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
510 // expected-error@+1 {{class already marked 'abstract'}}
511 class TooManyVirtSpecifiers4 abstract final abstract {};
512
513 class Base {
514 virtual void i();
515 };
516 class AbstractFunctionInClass : public Base {
517 // expected-note@+2 {{unimplemented pure virtual method 'f' in 'AbstractFunctionInClass'}}
518 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
519 virtual void f() abstract;
520 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
521 void g() abstract; // expected-error {{'g' is not virtual and cannot be declared pure}}
522 // expected-note@+2 {{unimplemented pure virtual method 'h' in 'AbstractFunctionInClass'}}
523 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
524 virtual void h() abstract = 0; // expected-error {{class member already marked 'abstract'}}
525 #if __cplusplus <= 199711L
526 // expected-warning@+4 {{'override' keyword is a C++11 extension}}
527 #endif
528 // expected-note@+2 {{unimplemented pure virtual method 'i' in 'AbstractFunctionInClass'}}
529 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
530 virtual void i() abstract override;
531 };
532
533 // expected-error@+1 {{variable type 'AbstractFunctionInClass' is an abstract class}}
534 AbstractFunctionInClass abstractFunctionInClassInstance;
535
AfterClassBody()536 void AfterClassBody() {
537 // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}}
538 struct D {} __declspec(deprecated);
539
540 struct __declspec(align(4)) S {} __declspec(align(8)) s1;
541 S s2;
542 _Static_assert(__alignof(S) == 4, "");
543 _Static_assert(__alignof(s1) == 8, "");
544 _Static_assert(__alignof(s2) == 4, "");
545 }
546
547 namespace PR24246 {
548 template <typename TX> struct A {
549 template <bool> struct largest_type_select;
550 template <> struct largest_type_select<false> {
551 blah x; // expected-error {{unknown type name 'blah'}}
552 };
553 };
554 }
555
556 class PR34109_class {
PR34109_class()557 PR34109_class() {}
~PR34109_class()558 virtual ~PR34109_class() {}
559 };
560
561 void operator delete(void *) throw();
562 // expected-note@-1 {{previous declaration is here}}
563 __declspec(dllexport) void operator delete(void *) throw();
564 // expected-error@-1 {{redeclaration of 'operator delete' cannot add 'dllexport' attribute}}
565
PR34109(int * a)566 void PR34109(int* a) {
567 delete a;
568 }
569
570 namespace PR42089 {
571 struct S {
572 __attribute__((nothrow)) void Foo(); // expected-note {{previous declaration is here}}
573 __attribute__((nothrow)) void Bar();
574 };
Foo()575 void S::Foo(){} // expected-warning {{is missing exception specification}}
Bar()576 __attribute__((nothrow)) void S::Bar(){}
577 }
578
579 #elif TEST2
580
581 // Check that __unaligned is not recognized if MS extensions are not enabled
582 typedef char __unaligned *aligned_type; // expected-error {{expected ';' after top level declarator}}
583
584 #elif TEST3
585
586 namespace PR32750 {
587 template<typename T> struct A {};
588 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'}}
589 }
590
591 #else
592
593 #error Unknown test mode
594
595 #endif
596
597