1 // RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,ms-union-ext -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=expected,precxx17,ms-union-ext -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=expected,precxx17,ms-union-ext -fms-extensions -fexceptions -fcxx-exceptions -DTEST1
4 // RUN: %clang_cc1 -std=c++14 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify=expected,precxx17,ms-union-ext-disabled -fexceptions -fcxx-exceptions -DTEST2
5 // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -fms-compatibility -verify=expected,ms-union-ext -DTEST3
6 // RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify=ms-union-ext -fms-extensions -fms-compatibility-version=18.00
7 // RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify=ms-union-ext-disabled -fms-extensions -fms-compatibility-version=19.00
11 // MSVC allows type definition in anonymous union and struct
17 struct B
// expected-warning {{types declared in an anonymous union are a Microsoft extension}}
22 union C
// expected-warning {{types declared in an anonymous union are a Microsoft extension}}
28 typedef int D
; // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
29 struct F
; // expected-warning {{types declared in an anonymous union are a Microsoft extension}}
36 struct B2
// expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
41 union C2
// expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
47 typedef int D2
; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
48 struct F2
; // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}
55 float __stdcall
subtractP();
58 // __unaligned handling
59 typedef char __unaligned
*aligned_type
;
60 typedef struct UnalignedTag
{ int f
; } __unaligned
*aligned_type2
;
61 typedef char __unaligned aligned_type3
;
63 struct aligned_type4
{
67 __unaligned
int aligned_type4::*p1_aligned_type4
= &aligned_type4::i
;
68 int aligned_type4::* __unaligned p2_aligned_type4
= &aligned_type4::i
;
69 __unaligned
int aligned_type4::* __unaligned p3_aligned_type4
= &aligned_type4::i
;
70 void (aligned_type4::*__unaligned p4_aligned_type4
)();
72 // Check that __unaligned qualifier can be used for overloading
73 void foo_unaligned(int *arg
) {} // expected-note {{not viable: 1st argument ('const __unaligned int *') would lose const qualifier}}
74 void foo_unaligned(__unaligned
int *arg
) {} // expected-note {{not viable: 1st argument ('const __unaligned int *') would lose const qualifier}}
75 void foo_unaligned(int arg
) {} // expected-note {{previous definition is here}}
76 void foo_unaligned(__unaligned
int arg
) {} // expected-error {{redefinition of 'foo_unaligned'}} \
77 // expected-note {{not viable: no known conversion from 'const __unaligned int *' to '__unaligned int'}}
79 class B_unaligned
: public A_unaligned
{};
80 int foo_unaligned(__unaligned A_unaligned
*arg
) { return 0; } // expected-note {{not viable: no known conversion from 'const __unaligned int *' to '__unaligned A_unaligned *'}}
81 void *foo_unaligned(B_unaligned
*arg
) { return 0; } // expected-note {{not viable: no known conversion from 'const __unaligned int *' to 'B_unaligned *'}}
83 void test_unaligned() {
87 const __unaligned
int *const_p1
= 0;
88 foo_unaligned(const_p1
); // expected-error {{no matching function for call to 'foo_unaligned'}}
90 __unaligned
int *p2
= 0;
93 __unaligned B_unaligned
*p3
= 0;
94 int p4
= foo_unaligned(p3
); // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type 'void *'}}
95 // expected-warning@-1 {{implicit cast from type '__unaligned B_unaligned *' to type 'B_unaligned *' drops __unaligned qualifier}}
98 // expected-warning@-1 {{implicit cast from type '__unaligned B_unaligned *' to type 'B_unaligned *' drops __unaligned qualifier}}
100 __unaligned B_unaligned
*p6
= p3
;
102 p1_aligned_type4
= p2_aligned_type4
;
103 p2_aligned_type4
= p1_aligned_type4
;
104 // expected-warning@-1 {{implicit cast from type '__unaligned int aligned_type4::*' to type 'int aligned_type4::*' drops __unaligned qualifier}}
105 p3_aligned_type4
= p1_aligned_type4
;
107 __unaligned
int a
[10];
109 // expected-warning@-1 {{implicit cast from type '__unaligned int[10]' to type 'int *' drops __unaligned qualifier}}
113 // We should accept assignment of an __unaligned pointer to a non-__unaligned
115 typedef struct _ITEMIDLIST
{ int i
; } ITEMIDLIST
;
116 typedef ITEMIDLIST __unaligned
*LPITEMIDLIST
;
117 extern "C" __declspec(dllimport
) void __stdcall
CoTaskMemFree(void* pv
);
118 __inline
void FreeIDListArray(LPITEMIDLIST
*ppidls
) {
119 CoTaskMemFree(*ppidls
);
120 __unaligned
int *x
= 0;
125 // We should accept type conversion of __unaligned to non-__unaligned references
126 typedef struct in_addr
{
128 in_addr(in_addr
&a
) {} // precxx17-note {{candidate constructor not viable: expects an lvalue for 1st argument}}
129 in_addr(in_addr
*a
) {} // precxx17-note {{candidate constructor not viable: no known conversion from 'IN_ADDR' (aka 'in_addr') to 'in_addr *' for 1st argument}}
132 void f(IN_ADDR __unaligned
*a
) {
133 IN_ADDR local_addr
= *a
;
134 // FIXME: MSVC accepts the following; not sure why clang tries to
135 // copy-construct an in_addr.
136 IN_ADDR local_addr2
= a
; // precxx17-error {{no viable constructor copying variable of type 'IN_ADDR' (aka 'in_addr')}}
137 // expected-warning@-1 {{implicit cast from type '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to type 'in_addr *' drops __unaligned qualifier}}
138 IN_ADDR
local_addr3(a
);
139 // expected-warning@-1 {{implicit cast from type '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to type 'in_addr *' drops __unaligned qualifier}}
142 template<typename T
> void h1(T (__stdcall
M::* const )()) { }
150 namespace signed_hex_i64
{
151 void f(long long); // expected-note {{candidate function}}
152 void f(int); // expected-note {{candidate function}}
154 // This used to be controlled by -fms-extensions, but it is now under
155 // -fms-compatibility.
156 f(0xffffffffffffffffLL
); // expected-error {{call to 'f' is ambiguous}}
157 f(0xffffffffffffffffi
64);
161 // Enumeration types with a fixed underlying type.
162 const int seventeen
= 17;
166 enum E1
: Int
{ SomeOtherValue
} field
;
167 #if __cplusplus <= 199711L
168 // expected-warning@-2 {{enumeration types with a fixed underlying type are a C++11 extension}}
172 #if __cplusplus >= 201103L
173 // expected-error@-2 {{bit-field}}
177 #if __cplusplus <= 199711L
178 // expected-warning@+2 {{enumeration types with a fixed underlying type are a C++11 extension}}
181 SomeValue
= 0x100000000
186 __declspec(dllimport
) void f(void) { }
187 void f2(void); // expected-note{{previous declaration is here}}
190 __declspec(dllimport
) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}}
191 // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}}
200 void f(int g
= 10 ); // expected-note {{previous definition is here}}
204 void BB
<T
>::f(int g
= 0) { } // expected-warning {{redefinition of default argument}}
208 extern void static_func();
209 void static_func(); // expected-note {{previous declaration is here}}
212 static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}}
217 extern const int static_var
; // expected-note {{previous declaration is here}}
218 static const int static_var
= 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}
220 void pointer_to_integral_type_conv(char* ptr
) {
221 char ch
= (char)ptr
; // expected-warning {{cast to smaller integer type 'char' from 'char *'}}
222 short sh
= (short)ptr
; // expected-warning {{cast to smaller integer type 'short' from 'char *'}}
223 ch
= (char)ptr
; // expected-warning {{cast to smaller integer type 'char' from 'char *'}}
224 sh
= (short)ptr
; // expected-warning {{cast to smaller integer type 'short' from 'char *'}}
226 // These are valid C++.
228 b
= static_cast<bool>(ptr
);
231 b
= reinterpret_cast<bool>(ptr
); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
234 void void_pointer_to_integral_type_conv(void *ptr
) {
235 char ch
= (char)ptr
; // expected-warning {{cast to smaller integer type 'char' from 'void *'}}
236 short sh
= (short)ptr
; // expected-warning {{cast to smaller integer type 'short' from 'void *'}}
237 ch
= (char)ptr
; // expected-warning {{cast to smaller integer type 'char' from 'void *'}}
238 sh
= (short)ptr
; // expected-warning {{cast to smaller integer type 'short' from 'void *'}}
240 // These are valid C++.
242 b
= static_cast<bool>(ptr
);
245 b
= reinterpret_cast<bool>(ptr
); // expected-error {{cast from pointer to smaller type 'bool' loses information}}
250 virtual void f() = 0;
253 int array
[__is_abstract(X
)? 1 : -1];
256 void f() { int __except
= 0; }
258 void ::f(); // expected-warning{{extra qualification on member 'f'}}
261 C::C(); // expected-warning{{extra qualification on member 'C'}}
264 struct StructWithProperty
{
265 __declspec(property(get
=GetV
)) int V1
;
266 __declspec(property(put
=SetV
)) int V2
;
267 __declspec(property(get
=GetV
, put
=SetV_NotExist
)) int V3
;
268 __declspec(property(get
=GetV_NotExist
, put
=SetV
)) int V4
;
269 __declspec(property(get
=GetV
, put
=SetV
)) int V5
;
271 int GetV() { return 123; }
274 void TestProperty() {
275 StructWithProperty sp
;
276 int i
= sp
.V2
; // expected-error{{no getter defined for property 'V2'}}
277 sp
.V1
= 12; // expected-error{{no setter defined for property 'V1'}}
278 int j
= sp
.V4
; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}}
279 sp
.V3
= 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}}
284 /* 4 tests for PseudoObject, begin */
287 bool operator()() { return true; }
291 __declspec(property(get
=GetV
)) SP1 V
;
292 SP1
GetV() { return SP1(); }
306 __declspec(property(get
=GetV
)) int V
;
307 int GetV() { return 123; }
308 void f() { SP3 s2
; s2
.f(V
); }
318 __declspec(property(get
=GetV
)) T V
;
319 int GetV() { return 123; }
320 void f() { int *p
= new int[V
]; }
327 __declspec(property(get
=GetV
)) T V
;
328 T
GetV() { return 123; }
329 void f() { int t
= V
; }
335 /* 4 tests for PseudoObject, end */
337 // Property access: explicit, implicit, with Qualifier
339 __declspec(property(get
=GetV
, put
=SetV
)) int V
;
340 int GetV() { return 123; }
343 void ImplicitAccess() { int i
= V
; V
= i
; }
344 void ExplicitAccess() { int i
= this->V
; this->V
= i
; }
346 struct SP8
: public SP7
{
347 void AccessWithQualifier() { int i
= SP7::V
; SP7::V
= i
; }
353 __declspec(property(get
=GetV
, put
=SetV
)) T V
;
354 T
GetV() { return 0; }
356 bool f() { V
= this->V
; return V
< this->V
; }
362 bool operator<(const SP10
& v
) { return true; }
363 SP10
operator*(int v
) { return *this; }
364 SP10
operator+(int v
) { return *this; }
365 SP10
& operator=(const SP10
& v
) { return *this; }
369 int i
= c
.V
; // Decl initializer
370 i
= c
.V
; // Binary op operand
371 c
.SetV(c
.V
); // CallExpr arg
372 int *p
= new int[c
.V
+ 1]; // Array size
373 p
[c
.V
] = 1; // Array index
377 c
.V
++; // Unary op operand
378 c
.V
*= 2; // Unary op operand
381 c2
.V
[0] = 123; // Array
384 c3
.f(); // Overloaded binary op operand
385 c3
.g(); // Overloaded incdec op operand
386 c3
.h(); // Overloaded unary op operand
389 // Property getter using reference.
391 __declspec(property(get
=GetV
)) int V
;
393 int& GetV() { return _v
; }
395 void TakePtr(int *) {}
396 void TakeRef(int &) {}
406 struct StructWithUnnamedMember
{
407 __declspec(property(get
=GetV
)) int : 10; // expected-error {{anonymous property is not supported}}
410 struct MSPropertyClass
{
411 int get() { return 42; }
412 int __declspec(property(get
= get
)) n
;
415 int *f(MSPropertyClass
&x
) {
416 return &x
.n
; // expected-error {{address of property expression requested}}
418 int MSPropertyClass::*g() {
419 return &MSPropertyClass::n
; // expected-error {{address of property expression requested}}
422 namespace rdar14250378
{
425 namespace NyNamespace
{
431 class Baz
: public Foo
{
436 Bar
* Foo::EnsureBar() {
442 // expected-error@+1 {{'sealed' keyword not permitted with interface types}}
443 __interface InterfaceWithSealed sealed
{
447 virtual void OverrideMe();
449 // expected-note@+2 {{overridden virtual function is here}}
450 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
451 virtual void SealedFunction() sealed
; // expected-note {{overridden virtual function is here}}
454 // expected-note@+2 {{'SealedType' declared here}}
455 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
456 struct SealedType sealed
: SomeBase
{
457 // expected-error@+2 {{declaration of 'SealedFunction' overrides a 'sealed' function}}
458 // FIXME. warning can be suppressed if we're also issuing error for overriding a 'final' function.
459 virtual void SealedFunction(); // expected-warning {{'SealedFunction' overrides a member function but is not marked 'override'}}
461 #if __cplusplus <= 199711L
462 // expected-warning@+2 {{'override' keyword is a C++11 extension}}
464 virtual void OverrideMe() override
;
467 // expected-error@+1 {{base 'SealedType' is marked 'sealed'}}
468 struct InheritFromSealed
: SealedType
{};
470 class SealedDestructor
{ // expected-note {{mark 'SealedDestructor' as 'sealed' to silence this warning}}
471 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
472 virtual ~SealedDestructor() sealed
; // expected-warning {{class with destructor marked 'sealed' cannot be inherited from}}
475 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
476 class AbstractClass abstract
{
480 // expected-error@+1 {{variable type 'AbstractClass' is an abstract class}}
481 AbstractClass abstractInstance
;
483 // expected-warning@+4 {{abstract class is marked 'sealed'}}
484 // expected-note@+3 {{'AbstractAndSealedClass' declared here}}
485 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
486 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}
487 class AbstractAndSealedClass abstract sealed
{}; // Does no really make sense, but allowed
489 // expected-error@+1 {{variable type 'AbstractAndSealedClass' is an abstract class}}
490 AbstractAndSealedClass abstractAndSealedInstance
;
491 // expected-error@+1 {{base 'AbstractAndSealedClass' is marked 'sealed'}}
492 class InheritFromAbstractAndSealed
: AbstractAndSealedClass
{};
494 #if __cplusplus <= 199711L
495 // expected-warning@+4 {{'final' keyword is a C++11 extension}}
496 // expected-warning@+3 {{'final' keyword is a C++11 extension}}
498 // expected-error@+1 {{class already marked 'final'}}
499 class TooManyVirtSpecifiers1 final final
{};
500 #if __cplusplus <= 199711L
501 // expected-warning@+4 {{'final' keyword is a C++11 extension}}
503 // expected-warning@+2 {{'sealed' keyword is a Microsoft extension}}
504 // expected-error@+1 {{class already marked 'sealed'}}
505 class TooManyVirtSpecifiers2 final sealed
{};
506 #if __cplusplus <= 199711L
507 // expected-warning@+6 {{'final' keyword is a C++11 extension}}
508 // expected-warning@+5 {{'final' keyword is a C++11 extension}}
510 // expected-warning@+3 {{abstract class is marked 'final'}}
511 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
512 // expected-error@+1 {{class already marked 'final'}}
513 class TooManyVirtSpecifiers3 final abstract final
{};
514 #if __cplusplus <= 199711L
515 // expected-warning@+6 {{'final' keyword is a C++11 extension}}
517 // expected-warning@+4 {{abstract class is marked 'final'}}
518 // expected-warning@+3 {{'abstract' keyword is a Microsoft extension}}
519 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}}
520 // expected-error@+1 {{class already marked 'abstract'}}
521 class TooManyVirtSpecifiers4 abstract final abstract
{};
526 class AbstractFunctionInClass
: public Base
{
527 // expected-note@+2 {{unimplemented pure virtual method 'f' in 'AbstractFunctionInClass'}}
528 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
529 virtual void f() abstract
;
530 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
531 void g() abstract
; // expected-error {{'g' is not virtual and cannot be declared pure}}
532 // expected-note@+2 {{unimplemented pure virtual method 'h' in 'AbstractFunctionInClass'}}
533 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
534 virtual void h() abstract
= 0; // expected-error {{class member already marked 'abstract'}}
535 #if __cplusplus <= 199711L
536 // expected-warning@+4 {{'override' keyword is a C++11 extension}}
538 // expected-note@+2 {{unimplemented pure virtual method 'i' in 'AbstractFunctionInClass'}}
539 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}
540 virtual void i() abstract override
;
543 // expected-error@+1 {{variable type 'AbstractFunctionInClass' is an abstract class}}
544 AbstractFunctionInClass abstractFunctionInClassInstance
;
546 void AfterClassBody() {
547 // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}}
548 struct D
{} __declspec(deprecated
);
550 struct __declspec(align(4)) S
{} __declspec(align(8)) s1
;
552 _Static_assert(__alignof(S
) == 4, "");
553 _Static_assert(__alignof(s1
) == 8, "");
554 _Static_assert(__alignof(s2
) == 4, "");
558 template <typename TX
> struct A
{
559 template <bool> struct largest_type_select
;
560 template <> struct largest_type_select
<false> {
561 blah x
; // expected-error {{unknown type name 'blah'}}
566 class PR34109_class
{
568 virtual ~PR34109_class() {}
571 #if !defined(__cpp_sized_deallocation)
572 void operator delete(void *) throw();
573 // expected-note@-1 {{previous declaration is here}}
574 __declspec(dllexport
) void operator delete(void *) throw();
575 // expected-error@-1 {{redeclaration of 'operator delete' cannot add 'dllexport' attribute}}
577 void operator delete(void *, unsigned int) throw();
578 // expected-note@-1 {{previous declaration is here}}
579 __declspec(dllexport
) void operator delete(void *, unsigned int) throw();
580 // expected-error@-1 {{redeclaration of 'operator delete' cannot add 'dllexport' attribute}}
582 void PR34109(int* a
) {
588 __attribute__((nothrow
)) void Foo(); // expected-note {{previous declaration is here}}
589 __attribute__((nothrow
)) void Bar();
591 void S::Foo(){} // expected-warning {{is missing exception specification}}
592 __attribute__((nothrow
)) void S::Bar(){}
595 namespace UnalignedConv
{
597 bool operator==(int) const;
609 // Check that __unaligned is not recognized if MS extensions are not enabled
610 typedef char __unaligned
*aligned_type
; // expected-error {{expected ';' after top level declarator}}
615 template<typename T
> struct A
{};
616 template<typename T
> struct B
: A
<A
<T
>> { A
<T
>::C::D d
; }; // expected-warning {{implicit 'typename' is a C++20 extension}}
624 // ms-union-ext-warning@+2 {{union member 'i2' has reference type 'int &', which is a Microsoft extension}}
625 // ms-union-ext-disabled-error@+1 {{union member 'i2' has reference type 'int &'}}