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 71 void foo_unaligned(int *arg) {} 72 void foo_unaligned(__unaligned int *arg) {} 73 void foo_unaligned(int arg) {} // expected-note {{previous definition is here}} 74 void foo_unaligned(__unaligned int arg) {} // expected-error {{redefinition of 'foo_unaligned'}} 75 class A_unaligned {}; 76 class B_unaligned : public A_unaligned {}; 77 int foo_unaligned(__unaligned A_unaligned *arg) { return 0; } 78 void *foo_unaligned(B_unaligned *arg) { return 0; } 79 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); // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type 'void *'}} 89 // expected-warning@-1 {{implicit cast from type '__unaligned B_unaligned *' to type 'B_unaligned *' drops __unaligned qualifier}} 90 91 B_unaligned *p5 = p3; 92 // expected-warning@-1 {{implicit cast from type '__unaligned B_unaligned *' to type 'B_unaligned *' drops __unaligned qualifier}} 93 94 __unaligned B_unaligned *p6 = p3; 95 96 p1_aligned_type4 = p2_aligned_type4; 97 p2_aligned_type4 = p1_aligned_type4; 98 // expected-warning@-1 {{implicit cast from type '__unaligned int aligned_type4::*' to type 'int aligned_type4::*' drops __unaligned qualifier}} 99 p3_aligned_type4 = p1_aligned_type4; 100 101 __unaligned int a[10]; 102 int *b = a; 103 // expected-warning@-1 {{implicit cast from type '__unaligned int[10]' to type 'int *' drops __unaligned qualifier}} 104 } 105 106 // Test from PR27367 107 // We should accept assignment of an __unaligned pointer to a non-__unaligned 108 // pointer to void 109 typedef struct _ITEMIDLIST { int i; } ITEMIDLIST; 110 typedef ITEMIDLIST __unaligned *LPITEMIDLIST; 111 extern "C" __declspec(dllimport) void __stdcall CoTaskMemFree(void* pv); 112 __inline void FreeIDListArray(LPITEMIDLIST *ppidls) { 113 CoTaskMemFree(*ppidls); 114 __unaligned int *x = 0; 115 void *y = x; 116 } 117 118 // Test from PR27666 119 // We should accept type conversion of __unaligned to non-__unaligned references 120 typedef struct in_addr { 121 public: 122 in_addr(in_addr &a) {} // expected-note {{candidate constructor not viable: expects an lvalue for 1st argument}} 123 in_addr(in_addr *a) {} // expected-note {{candidate constructor not viable: no known conversion from 'IN_ADDR' (aka 'in_addr') to 'in_addr *' for 1st argument}} 124 } IN_ADDR; 125 126 void f(IN_ADDR __unaligned *a) { 127 IN_ADDR local_addr = *a; 128 // FIXME: MSVC accepts the following; not sure why clang tries to 129 // copy-construct an in_addr. 130 IN_ADDR local_addr2 = a; // expected-error {{no viable constructor copying variable of type 'IN_ADDR' (aka 'in_addr')}} 131 // expected-warning@-1 {{implicit cast from type '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to type 'in_addr *' drops __unaligned qualifier}} 132 IN_ADDR local_addr3(a); 133 // expected-warning@-1 {{implicit cast from type '__unaligned IN_ADDR *' (aka '__unaligned in_addr *') to type 'in_addr *' drops __unaligned qualifier}} 134 } 135 136 template<typename T> void h1(T (__stdcall M::* const )()) { } 137 138 void m1() { 139 h1<int>(&M::addP); 140 h1(&M::subtractP); 141 } 142 143 144 namespace signed_hex_i64 { 145 void f(long long); // expected-note {{candidate function}} 146 void f(int); // expected-note {{candidate function}} 147 void g() { 148 // This used to be controlled by -fms-extensions, but it is now under 149 // -fms-compatibility. 150 f(0xffffffffffffffffLL); // expected-error {{call to 'f' is ambiguous}} 151 f(0xffffffffffffffffi64); 152 } 153 } 154 155 // Enumeration types with a fixed underlying type. 156 const int seventeen = 17; 157 typedef int Int; 158 159 struct X0 { 160 enum E1 : Int { SomeOtherValue } field; 161 #if __cplusplus <= 199711L 162 // expected-warning@-2 {{enumeration types with a fixed underlying type are a C++11 extension}} 163 #endif 164 165 enum E1 : seventeen; 166 #if __cplusplus >= 201103L 167 // expected-error@-2 {{bit-field}} 168 #endif 169 }; 170 171 #if __cplusplus <= 199711L 172 // expected-warning@+2 {{enumeration types with a fixed underlying type are a C++11 extension}} 173 #endif 174 enum : long long { 175 SomeValue = 0x100000000 176 }; 177 178 179 class AAA { 180 __declspec(dllimport) void f(void) { } 181 void f2(void); // expected-note{{previous declaration is here}} 182 }; 183 184 __declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}} 185 // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}} 186 187 } 188 189 190 191 template <class T> 192 class BB { 193 public: 194 void f(int g = 10 ); // expected-note {{previous definition is here}} 195 }; 196 197 template <class T> 198 void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}} 199 200 201 202 extern void static_func(); 203 void static_func(); // expected-note {{previous declaration is here}} 204 205 206 static void static_func() // expected-warning {{redeclaring non-static 'static_func' as static is a Microsoft extension}} 207 { 208 209 } 210 211 extern const int static_var; // expected-note {{previous declaration is here}} 212 static const int static_var = 3; // expected-warning {{redeclaring non-static 'static_var' as static is a Microsoft extension}} 213 214 void pointer_to_integral_type_conv(char* ptr) { 215 char ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'char *'}} 216 short sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'char *'}} 217 ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'char *'}} 218 sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'char *'}} 219 220 // These are valid C++. 221 bool b = (bool)ptr; 222 b = static_cast<bool>(ptr); 223 224 // This is bad. 225 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}} 226 } 227 228 void void_pointer_to_integral_type_conv(void *ptr) { 229 char ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'void *'}} 230 short sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'void *'}} 231 ch = (char)ptr; // expected-warning {{cast to smaller integer type 'char' from 'void *'}} 232 sh = (short)ptr; // expected-warning {{cast to smaller integer type 'short' from 'void *'}} 233 234 // These are valid C++. 235 bool b = (bool)ptr; 236 b = static_cast<bool>(ptr); 237 238 // This is bad. 239 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}} 240 } 241 242 struct PR11150 { 243 class X { 244 virtual void f() = 0; 245 }; 246 247 int array[__is_abstract(X)? 1 : -1]; 248 }; 249 250 void f() { int __except = 0; } 251 252 void ::f(); // expected-warning{{extra qualification on member 'f'}} 253 254 class C { 255 C::C(); // expected-warning{{extra qualification on member 'C'}} 256 }; 257 258 struct StructWithProperty { 259 __declspec(property(get=GetV)) int V1; 260 __declspec(property(put=SetV)) int V2; 261 __declspec(property(get=GetV, put=SetV_NotExist)) int V3; 262 __declspec(property(get=GetV_NotExist, put=SetV)) int V4; 263 __declspec(property(get=GetV, put=SetV)) int V5; 264 265 int GetV() { return 123; } 266 void SetV(int i) {} 267 }; 268 void TestProperty() { 269 StructWithProperty sp; 270 int i = sp.V2; // expected-error{{no getter defined for property 'V2'}} 271 sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}} 272 int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}} 273 sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}} 274 int k = sp.V5; 275 sp.V5 = k++; 276 } 277 278 /* 4 tests for PseudoObject, begin */ 279 struct SP1 280 { 281 bool operator()() { return true; } 282 }; 283 struct SP2 284 { 285 __declspec(property(get=GetV)) SP1 V; 286 SP1 GetV() { return SP1(); } 287 }; 288 void TestSP2() { 289 SP2 sp2; 290 bool b = sp2.V(); 291 } 292 293 struct SP3 { 294 template <class T> 295 void f(T t) {} 296 }; 297 template <class T> 298 struct SP4 299 { 300 __declspec(property(get=GetV)) int V; 301 int GetV() { return 123; } 302 void f() { SP3 s2; s2.f(V); } 303 }; 304 void TestSP4() { 305 SP4<int> s; 306 s.f(); 307 } 308 309 template <class T> 310 struct SP5 311 { 312 __declspec(property(get=GetV)) T V; 313 int GetV() { return 123; } 314 void f() { int *p = new int[V]; } 315 }; 316 317 template <class T> 318 struct SP6 319 { 320 public: 321 __declspec(property(get=GetV)) T V; 322 T GetV() { return 123; } 323 void f() { int t = V; } 324 }; 325 void TestSP6() { 326 SP6<int> c; 327 c.f(); 328 } 329 /* 4 tests for PseudoObject, end */ 330 331 // Property access: explicit, implicit, with Qualifier 332 struct SP7 { 333 __declspec(property(get=GetV, put=SetV)) int V; 334 int GetV() { return 123; } 335 void SetV(int v) {} 336 337 void ImplicitAccess() { int i = V; V = i; } 338 void ExplicitAccess() { int i = this->V; this->V = i; } 339 }; 340 struct SP8: public SP7 { 341 void AccessWithQualifier() { int i = SP7::V; SP7::V = i; } 342 }; 343 344 // Property usage 345 template <class T> 346 struct SP9 { 347 __declspec(property(get=GetV, put=SetV)) T V; 348 T GetV() { return 0; } 349 void SetV(T v) {} 350 bool f() { V = this->V; return V < this->V; } 351 void g() { V++; } 352 void h() { V*=2; } 353 }; 354 struct SP10 { 355 SP10(int v) {} 356 bool operator<(const SP10& v) { return true; } 357 SP10 operator*(int v) { return *this; } 358 SP10 operator+(int v) { return *this; } 359 SP10& operator=(const SP10& v) { return *this; } 360 }; 361 void TestSP9() { 362 SP9<int> c; 363 int i = c.V; // Decl initializer 364 i = c.V; // Binary op operand 365 c.SetV(c.V); // CallExpr arg 366 int *p = new int[c.V + 1]; // Array size 367 p[c.V] = 1; // Array index 368 369 c.V = 123; // Setter 370 371 c.V++; // Unary op operand 372 c.V *= 2; // Unary op operand 373 374 SP9<int*> c2; 375 c2.V[0] = 123; // Array 376 377 SP9<SP10> c3; 378 c3.f(); // Overloaded binary op operand 379 c3.g(); // Overloaded incdec op operand 380 c3.h(); // Overloaded unary op operand 381 } 382 383 union u { 384 int *i1; 385 int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}} 386 }; 387 388 // Property getter using reference. 389 struct SP11 { 390 __declspec(property(get=GetV)) int V; 391 int _v; 392 int& GetV() { return _v; } 393 void UseV(); 394 void TakePtr(int *) {} 395 void TakeRef(int &) {} 396 void TakeVal(int) {} 397 }; 398 399 void SP11::UseV() { 400 TakePtr(&V); 401 TakeRef(V); 402 TakeVal(V); 403 } 404 405 struct StructWithUnnamedMember { 406 __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}} 407 }; 408 409 struct MSPropertyClass { 410 int get() { return 42; } 411 int __declspec(property(get = get)) n; 412 }; 413 414 int *f(MSPropertyClass &x) { 415 return &x.n; // expected-error {{address of property expression requested}} 416 } 417 int MSPropertyClass::*g() { 418 return &MSPropertyClass::n; // expected-error {{address of property expression requested}} 419 } 420 421 namespace rdar14250378 { 422 class Bar {}; 423 424 namespace NyNamespace { 425 class Foo { 426 public: 427 Bar* EnsureBar(); 428 }; 429 430 class Baz : public Foo { 431 public: 432 friend class Bar; 433 }; 434 435 Bar* Foo::EnsureBar() { 436 return 0; 437 } 438 } 439 } 440 441 // expected-error@+1 {{'sealed' keyword not permitted with interface types}} 442 __interface InterfaceWithSealed sealed { 443 }; 444 445 struct SomeBase { 446 virtual void OverrideMe(); 447 448 // expected-note@+2 {{overridden virtual function is here}} 449 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}} 450 virtual void SealedFunction() sealed; // expected-note {{overridden virtual function is here}} 451 }; 452 453 // expected-note@+2 {{'SealedType' declared here}} 454 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}} 455 struct SealedType sealed : SomeBase { 456 // expected-error@+2 {{declaration of 'SealedFunction' overrides a 'sealed' function}} 457 // FIXME. warning can be suppressed if we're also issuing error for overriding a 'final' function. 458 virtual void SealedFunction(); // expected-warning {{'SealedFunction' overrides a member function but is not marked 'override'}} 459 460 #if __cplusplus <= 199711L 461 // expected-warning@+2 {{'override' keyword is a C++11 extension}} 462 #endif 463 virtual void OverrideMe() override; 464 }; 465 466 // expected-error@+1 {{base 'SealedType' is marked 'sealed'}} 467 struct InheritFromSealed : SealedType {}; 468 469 class SealedDestructor { // expected-note {{mark 'SealedDestructor' as 'sealed' to silence this warning}} 470 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}} 471 virtual ~SealedDestructor() sealed; // expected-warning {{class with destructor marked 'sealed' cannot be inherited from}} 472 }; 473 474 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}} 475 class AbstractClass abstract { 476 int i; 477 }; 478 479 // expected-error@+1 {{variable type 'AbstractClass' is an abstract class}} 480 AbstractClass abstractInstance; 481 482 // expected-warning@+4 {{abstract class is marked 'sealed'}} 483 // expected-note@+3 {{'AbstractAndSealedClass' declared here}} 484 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}} 485 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}} 486 class AbstractAndSealedClass abstract sealed {}; // Does no really make sense, but allowed 487 488 // expected-error@+1 {{variable type 'AbstractAndSealedClass' is an abstract class}} 489 AbstractAndSealedClass abstractAndSealedInstance; 490 // expected-error@+1 {{base 'AbstractAndSealedClass' is marked 'sealed'}} 491 class InheritFromAbstractAndSealed : AbstractAndSealedClass {}; 492 493 #if __cplusplus <= 199711L 494 // expected-warning@+4 {{'final' keyword is a C++11 extension}} 495 // expected-warning@+3 {{'final' keyword is a C++11 extension}} 496 #endif 497 // expected-error@+1 {{class already marked 'final'}} 498 class TooManyVirtSpecifiers1 final final {}; 499 #if __cplusplus <= 199711L 500 // expected-warning@+4 {{'final' keyword is a C++11 extension}} 501 #endif 502 // expected-warning@+2 {{'sealed' keyword is a Microsoft extension}} 503 // expected-error@+1 {{class already marked 'sealed'}} 504 class TooManyVirtSpecifiers2 final sealed {}; 505 #if __cplusplus <= 199711L 506 // expected-warning@+6 {{'final' keyword is a C++11 extension}} 507 // expected-warning@+5 {{'final' keyword is a C++11 extension}} 508 #endif 509 // expected-warning@+3 {{abstract class is marked 'final'}} 510 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}} 511 // expected-error@+1 {{class already marked 'final'}} 512 class TooManyVirtSpecifiers3 final abstract final {}; 513 #if __cplusplus <= 199711L 514 // expected-warning@+6 {{'final' keyword is a C++11 extension}} 515 #endif 516 // expected-warning@+4 {{abstract class is marked 'final'}} 517 // expected-warning@+3 {{'abstract' keyword is a Microsoft extension}} 518 // expected-warning@+2 {{'abstract' keyword is a Microsoft extension}} 519 // expected-error@+1 {{class already marked 'abstract'}} 520 class TooManyVirtSpecifiers4 abstract final abstract {}; 521 522 class Base { 523 virtual void i(); 524 }; 525 class AbstractFunctionInClass : public Base { 526 // expected-note@+2 {{unimplemented pure virtual method 'f' in 'AbstractFunctionInClass'}} 527 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}} 528 virtual void f() abstract; 529 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}} 530 void g() abstract; // expected-error {{'g' is not virtual and cannot be declared pure}} 531 // expected-note@+2 {{unimplemented pure virtual method 'h' in 'AbstractFunctionInClass'}} 532 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}} 533 virtual void h() abstract = 0; // expected-error {{class member already marked 'abstract'}} 534 #if __cplusplus <= 199711L 535 // expected-warning@+4 {{'override' keyword is a C++11 extension}} 536 #endif 537 // expected-note@+2 {{unimplemented pure virtual method 'i' in 'AbstractFunctionInClass'}} 538 // expected-warning@+1 {{'abstract' keyword is a Microsoft extension}} 539 virtual void i() abstract override; 540 }; 541 542 // expected-error@+1 {{variable type 'AbstractFunctionInClass' is an abstract class}} 543 AbstractFunctionInClass abstractFunctionInClassInstance; 544 545 void AfterClassBody() { 546 // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}} 547 struct D {} __declspec(deprecated); 548 549 struct __declspec(align(4)) S {} __declspec(align(8)) s1; 550 S s2; 551 _Static_assert(__alignof(S) == 4, ""); 552 _Static_assert(__alignof(s1) == 8, ""); 553 _Static_assert(__alignof(s2) == 4, ""); 554 } 555 556 namespace PR24246 { 557 template <typename TX> struct A { 558 template <bool> struct largest_type_select; 559 template <> struct largest_type_select<false> { 560 blah x; // expected-error {{unknown type name 'blah'}} 561 }; 562 }; 563 } 564 565 class PR34109_class { 566 PR34109_class() {} 567 virtual ~PR34109_class() {} 568 }; 569 570 void operator delete(void *) throw(); 571 // expected-note@-1 {{previous declaration is here}} 572 __declspec(dllexport) void operator delete(void *) throw(); 573 // expected-error@-1 {{redeclaration of 'operator delete' cannot add 'dllexport' attribute}} 574 575 void PR34109(int* a) { 576 delete a; 577 } 578 579 namespace PR42089 { 580 struct S { 581 __attribute__((nothrow)) void Foo(); // expected-note {{previous declaration is here}} 582 __attribute__((nothrow)) void Bar(); 583 }; 584 void S::Foo(){} // expected-warning {{is missing exception specification}} 585 __attribute__((nothrow)) void S::Bar(){} 586 } 587 588 #elif TEST2 589 590 // Check that __unaligned is not recognized if MS extensions are not enabled 591 typedef char __unaligned *aligned_type; // expected-error {{expected ';' after top level declarator}} 592 593 #elif TEST3 594 595 namespace PR32750 { 596 template<typename T> struct A {}; 597 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'}} 598 } 599 600 #else 601 602 #error Unknown test mode 603 604 #endif 605 606