628 lines · cpp
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 -DTEST12// 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 -DTEST13// 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 -DTEST14// 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 -DTEST25// RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -fms-compatibility -verify=expected,ms-union-ext -DTEST36// RUN: %clang_cc1 -std=c++17 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -verify=ms-union-ext -fms-extensions -fms-compatibility-version=18.007// 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.008 9#if TEST110 11// MSVC allows type definition in anonymous union and struct12struct A13{14 union15 {16 int a;17 struct B // expected-warning {{types declared in an anonymous union are a Microsoft extension}}18 {19 int c;20 } d;21 22 union C // expected-warning {{types declared in an anonymous union are a Microsoft extension}}23 {24 int e;25 int ee;26 } f;27 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}}30 };31 32 struct33 {34 int a2;35 36 struct B2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}37 {38 int c2;39 } d2;40 41 union C2 // expected-warning {{types declared in an anonymous struct are a Microsoft extension}}42 {43 int e2;44 int ee2;45 } f2;46 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}}49 };50};51 52// __stdcall handling53struct M {54 int __stdcall addP();55 float __stdcall subtractP();56};57 58// __unaligned handling59typedef char __unaligned *aligned_type;60typedef struct UnalignedTag { int f; } __unaligned *aligned_type2;61typedef char __unaligned aligned_type3;62 63struct aligned_type4 {64 int i;65};66 67__unaligned int aligned_type4::*p1_aligned_type4 = &aligned_type4::i;68int aligned_type4::* __unaligned p2_aligned_type4 = &aligned_type4::i;69__unaligned int aligned_type4::* __unaligned p3_aligned_type4 = &aligned_type4::i;70void (aligned_type4::*__unaligned p4_aligned_type4)();71 72// Check that __unaligned qualifier can be used for overloading73void foo_unaligned(int *arg) {} // expected-note {{not viable: 1st argument ('const __unaligned int *') would lose const qualifier}}74void foo_unaligned(__unaligned int *arg) {} // expected-note {{not viable: 1st argument ('const __unaligned int *') would lose const qualifier}}75void foo_unaligned(int arg) {} // expected-note {{previous definition is here}}76void 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'}}78class A_unaligned {};79class B_unaligned : public A_unaligned {};80int foo_unaligned(__unaligned A_unaligned *arg) { return 0; } // expected-note {{not viable: no known conversion from 'const __unaligned int *' to '__unaligned A_unaligned *'}}81void *foo_unaligned(B_unaligned *arg) { return 0; } // expected-note {{not viable: no known conversion from 'const __unaligned int *' to 'B_unaligned *'}}82 83void test_unaligned() {84 int *p1 = 0;85 foo_unaligned(p1);86 87 const __unaligned int *const_p1 = 0;88 foo_unaligned(const_p1); // expected-error {{no matching function for call to 'foo_unaligned'}}89 90 __unaligned int *p2 = 0;91 foo_unaligned(p2);92 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}}96 97 B_unaligned *p5 = p3;98 // expected-warning@-1 {{implicit cast from type '__unaligned B_unaligned *' to type 'B_unaligned *' drops __unaligned qualifier}}99 100 __unaligned B_unaligned *p6 = p3;101 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;106 107 __unaligned int a[10];108 int *b = a;109 // expected-warning@-1 {{implicit cast from type '__unaligned int[10]' to type 'int *' drops __unaligned qualifier}}110}111 112// Test from PR27367113// We should accept assignment of an __unaligned pointer to a non-__unaligned114// pointer to void115typedef struct _ITEMIDLIST { int i; } ITEMIDLIST;116typedef ITEMIDLIST __unaligned *LPITEMIDLIST;117extern "C" __declspec(dllimport) void __stdcall CoTaskMemFree(void* pv);118__inline void FreeIDListArray(LPITEMIDLIST *ppidls) {119 CoTaskMemFree(*ppidls);120 __unaligned int *x = 0;121 void *y = x;122}123 124// Test from PR27666125// We should accept type conversion of __unaligned to non-__unaligned references126typedef struct in_addr {127public: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 'struct in_addr') to 'in_addr *' for 1st argument}}130} IN_ADDR;131 132void f(IN_ADDR __unaligned *a) {133 IN_ADDR local_addr = *a;134 // FIXME: MSVC accepts the following; not sure why clang tries to135 // copy-construct an in_addr.136 IN_ADDR local_addr2 = a; // precxx17-error {{no viable constructor copying variable of type 'IN_ADDR' (aka 'struct in_addr')}}137 // expected-warning@-1 {{implicit cast from type '__unaligned IN_ADDR *' (aka '__unaligned struct 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 struct in_addr *') to type 'in_addr *' drops __unaligned qualifier}}140}141 142template<typename T> void h1(T (__stdcall M::* const )()) { }143 144void m1() {145 h1<int>(&M::addP);146 h1(&M::subtractP);147}148 149 150namespace signed_hex_i64 {151void f(long long); // expected-note {{candidate function}}152void f(int); // expected-note {{candidate function}}153void g() {154 // This used to be controlled by -fms-extensions, but it is now under155 // -fms-compatibility.156 f(0xffffffffffffffffLL); // expected-error {{call to 'f' is ambiguous}}157 f(0xffffffffffffffffi64);158}159}160 161// Enumeration types with a fixed underlying type.162const int seventeen = 17;163typedef int Int;164 165struct X0 {166 enum E1 : Int { SomeOtherValue } field;167#if __cplusplus <= 199711L168 // expected-warning@-2 {{enumeration types with a fixed underlying type are a C++11 extension}}169#endif170 171 enum E1 : seventeen;172#if __cplusplus >= 201103L173 // expected-error@-2 {{bit-field}}174#endif175};176 177#if __cplusplus <= 199711L178// expected-warning@+2 {{enumeration types with a fixed underlying type are a C++11 extension}}179#endif180enum : long long {181 SomeValue = 0x100000000182};183 184 185class AAA {186__declspec(dllimport) void f(void) { }187void f2(void); // expected-note{{previous declaration is here}}188};189 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}}192 193}194 195 196 197template <class T>198class BB {199public:200 void f(int g = 10 ); // expected-note {{previous definition is here}}201};202 203template <class T>204void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}}205 206 207 208extern void static_func();209void static_func(); // expected-note {{previous declaration is here}}210 211 212static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}}213{214 215}216 217extern const int static_var; // expected-note {{previous declaration is here}}218static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}}219 220void 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 *'}}225 226 // These are valid C++.227 bool b = (bool)ptr;228 b = static_cast<bool>(ptr);229 230 // This is bad.231 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}232}233 234void 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 *'}}239 240 // These are valid C++.241 bool b = (bool)ptr;242 b = static_cast<bool>(ptr);243 244 // This is bad.245 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}}246}247 248struct PR11150 {249 class X {250 virtual void f() = 0;251 };252 253 int array[__is_abstract(X)? 1 : -1];254};255 256void f() { int __except = 0; }257 258void ::f(); // expected-warning{{extra qualification on member 'f'}}259 260class C {261 C::C(); // expected-warning{{extra qualification on member 'C'}}262};263 264struct 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;270 271 int GetV() { return 123; }272 void SetV(int i) {}273};274void 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'}}280 int k = sp.V5;281 sp.V5 = k++;282}283 284/* 4 tests for PseudoObject, begin */285struct SP1286{287 bool operator()() { return true; }288};289struct SP2290{291 __declspec(property(get=GetV)) SP1 V;292 SP1 GetV() { return SP1(); }293};294void TestSP2() {295 SP2 sp2;296 bool b = sp2.V();297}298 299struct SP3 {300 template <class T>301 void f(T t) {}302};303template <class T>304struct SP4305{306 __declspec(property(get=GetV)) int V;307 int GetV() { return 123; }308 void f() { SP3 s2; s2.f(V); }309};310void TestSP4() {311 SP4<int> s;312 s.f();313}314 315template <class T>316struct SP5317{318 __declspec(property(get=GetV)) T V;319 int GetV() { return 123; }320 void f() { int *p = new int[V]; }321};322 323template <class T>324struct SP6325{326public:327 __declspec(property(get=GetV)) T V;328 T GetV() { return 123; }329 void f() { int t = V; }330};331void TestSP6() {332 SP6<int> c;333 c.f();334}335/* 4 tests for PseudoObject, end */336 337// Property access: explicit, implicit, with Qualifier338struct SP7 {339 __declspec(property(get=GetV, put=SetV)) int V;340 int GetV() { return 123; }341 void SetV(int v) {}342 343 void ImplicitAccess() { int i = V; V = i; }344 void ExplicitAccess() { int i = this->V; this->V = i; }345};346struct SP8: public SP7 {347 void AccessWithQualifier() { int i = SP7::V; SP7::V = i; }348};349 350// Property usage351template <class T>352struct SP9 {353 __declspec(property(get=GetV, put=SetV)) T V;354 T GetV() { return 0; }355 void SetV(T v) {}356 bool f() { V = this->V; return V < this->V; }357 void g() { V++; }358 void h() { V*=2; }359};360struct SP10 {361 SP10(int 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; }366};367void TestSP9() {368 SP9<int> c;369 int i = c.V; // Decl initializer370 i = c.V; // Binary op operand371 c.SetV(c.V); // CallExpr arg372 int *p = new int[c.V + 1]; // Array size373 p[c.V] = 1; // Array index374 375 c.V = 123; // Setter376 377 c.V++; // Unary op operand378 c.V *= 2; // Unary op operand379 380 SP9<int*> c2;381 c2.V[0] = 123; // Array382 383 SP9<SP10> c3;384 c3.f(); // Overloaded binary op operand385 c3.g(); // Overloaded incdec op operand386 c3.h(); // Overloaded unary op operand387}388 389// Property getter using reference.390struct SP11 {391 __declspec(property(get=GetV)) int V;392 int _v;393 int& GetV() { return _v; }394 void UseV();395 void TakePtr(int *) {}396 void TakeRef(int &) {}397 void TakeVal(int) {}398};399 400void SP11::UseV() {401 TakePtr(&V);402 TakeRef(V);403 TakeVal(V);404}405 406struct StructWithUnnamedMember {407 __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}}408};409 410struct MSPropertyClass {411 int get() { return 42; }412 int __declspec(property(get = get)) n;413};414 415int *f(MSPropertyClass &x) {416 return &x.n; // expected-error {{address of property expression requested}}417}418int MSPropertyClass::*g() {419 return &MSPropertyClass::n; // expected-error {{address of property expression requested}}420}421 422namespace rdar14250378 {423 class Bar {};424 425 namespace NyNamespace {426 class Foo {427 public:428 Bar* EnsureBar();429 };430 431 class Baz : public Foo {432 public:433 friend class Bar;434 };435 436 Bar* Foo::EnsureBar() {437 return 0;438 }439 }440}441 442// expected-error@+1 {{'sealed' keyword not permitted with interface types}}443__interface InterfaceWithSealed sealed {444};445 446struct SomeBase {447 virtual void OverrideMe();448 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}}452};453 454// expected-note@+2 {{'SealedType' declared here}}455// expected-warning@+1 {{'sealed' keyword is a Microsoft extension}}456struct 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'}}460 461#if __cplusplus <= 199711L462 // expected-warning@+2 {{'override' keyword is a C++11 extension}}463#endif464 virtual void OverrideMe() override;465};466 467// expected-error@+1 {{base 'SealedType' is marked 'sealed'}}468struct InheritFromSealed : SealedType {};469 470class 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}}473};474 475// expected-warning@+1 {{'abstract' keyword is a Microsoft extension}}476class AbstractClass abstract {477 int i;478};479 480// expected-error@+1 {{variable type 'AbstractClass' is an abstract class}}481AbstractClass abstractInstance;482 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}}487class AbstractAndSealedClass abstract sealed {}; // Does no really make sense, but allowed488 489// expected-error@+1 {{variable type 'AbstractAndSealedClass' is an abstract class}}490AbstractAndSealedClass abstractAndSealedInstance;491// expected-error@+1 {{base 'AbstractAndSealedClass' is marked 'sealed'}}492class InheritFromAbstractAndSealed : AbstractAndSealedClass {};493 494#if __cplusplus <= 199711L495// expected-warning@+4 {{'final' keyword is a C++11 extension}}496// expected-warning@+3 {{'final' keyword is a C++11 extension}}497#endif498// expected-error@+1 {{class already marked 'final'}}499class TooManyVirtSpecifiers1 final final {};500#if __cplusplus <= 199711L501// expected-warning@+4 {{'final' keyword is a C++11 extension}}502#endif503// expected-warning@+2 {{'sealed' keyword is a Microsoft extension}}504// expected-error@+1 {{class already marked 'sealed'}}505class TooManyVirtSpecifiers2 final sealed {};506#if __cplusplus <= 199711L507// expected-warning@+6 {{'final' keyword is a C++11 extension}}508// expected-warning@+5 {{'final' keyword is a C++11 extension}}509#endif510// 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'}}513class TooManyVirtSpecifiers3 final abstract final {};514#if __cplusplus <= 199711L515// expected-warning@+6 {{'final' keyword is a C++11 extension}}516#endif517// 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'}}521class TooManyVirtSpecifiers4 abstract final abstract {};522 523class Base {524 virtual void i();525};526class 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 <= 199711L536 // expected-warning@+4 {{'override' keyword is a C++11 extension}}537#endif538 // 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;541};542 543// expected-error@+1 {{variable type 'AbstractFunctionInClass' is an abstract class}}544AbstractFunctionInClass abstractFunctionInClassInstance;545 546void AfterClassBody() {547 // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}}548 struct D {} __declspec(deprecated);549 550 struct __declspec(align(4)) S {} __declspec(align(8)) s1;551 S s2;552 _Static_assert(__alignof(S) == 4, "");553 _Static_assert(__alignof(s1) == 8, "");554 _Static_assert(__alignof(s2) == 4, "");555}556 557namespace PR24246 {558template <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'}}562 };563};564}565 566class PR34109_class {567 PR34109_class() {}568 virtual ~PR34109_class() {}569};570 571#if !defined(__cpp_sized_deallocation)572void 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}}576#else577void 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}}581#endif582void PR34109(int* a) {583 delete a;584}585 586namespace PR42089 {587 struct S {588 __attribute__((nothrow)) void Foo(); // expected-note {{previous declaration is here}}589 __attribute__((nothrow)) void Bar();590 };591 void S::Foo(){} // expected-warning {{is missing exception specification}}592 __attribute__((nothrow)) void S::Bar(){}593}594 595namespace UnalignedConv {596struct S {597 bool operator==(int) const;598};599 600int func() {601 S __unaligned s;602 return s == 42;603}604}605 606 607#elif TEST2608 609// Check that __unaligned is not recognized if MS extensions are not enabled610typedef char __unaligned *aligned_type; // expected-error {{expected ';' after top level declarator}}611 612#elif TEST3613 614namespace PR32750 {615template<typename T> struct A {};616template<typename T> struct B : A<A<T>> { A<T>::C::D d; }; // expected-warning {{missing 'typename' prior to dependent type name 'A<T>::C::D' is a C++20 extension}}617}618 619#endif620 621union u {622 int *i1;623 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 &'}}626 int &i2;627};628