1 // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -Wmicrosoft -Wc++11-extensions -Wno-long-long -verify -fms-extensions -fexceptions -fcxx-exceptions 2 3 4 // Microsoft doesn't validate exception specification. 5 namespace microsoft_exception_spec { 6 7 void foo(); // expected-note {{previous declaration}} 8 void foo() throw(); // expected-warning {{exception specification in declaration does not match previous declaration}} 9 10 void r6() throw(...); // expected-note {{previous declaration}} 11 void r6() throw(int); // expected-warning {{exception specification in declaration does not match previous declaration}} 12 13 struct Base { 14 virtual void f2(); 15 virtual void f3() throw(...); 16 }; 17 18 struct Derived : Base { 19 virtual void f2() throw(...); 20 virtual void f3(); 21 }; 22 23 class A { 24 virtual ~A() throw(); // expected-note {{overridden virtual function is here}} 25 }; 26 27 class B : public A { 28 virtual ~B(); // expected-warning {{exception specification of overriding function is more lax than base version}} 29 }; 30 31 } 32 33 // MSVC allows type definition in anonymous union and struct 34 struct A 35 { 36 union 37 { 38 int a; 39 struct B // expected-warning {{types declared in an anonymous union are a Microsoft extension}} 40 { 41 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; 62 63 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 74 // __stdcall handling 75 struct M { 76 int __stdcall addP(); 77 float __stdcall subtractP(); 78 }; 79 80 // __unaligned handling 81 typedef char __unaligned *aligned_type; 82 83 84 template<typename T> void h1(T (__stdcall M::* const )()) { } 85 86 void m1() { 87 h1<int>(&M::addP); 88 h1(&M::subtractP); 89 } 90 91 92 93 94 95 void f(long long); 96 void f(int); 97 98 int main() 99 { 100 // This is an ambiguous call in standard C++. 101 // This calls f(long long) in Microsoft mode because LL is always signed. 102 f(0xffffffffffffffffLL); 103 f(0xffffffffffffffffi64); 104 } 105 106 // Enumeration types with a fixed underlying type. 107 const int seventeen = 17; 108 typedef int Int; 109 110 struct X0 { 111 enum E1 : Int { SomeOtherValue } field; // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}} 112 enum E1 : seventeen; 113 }; 114 115 enum : long long { // expected-warning{{enumeration types with a fixed underlying type are a C++11 extension}} 116 SomeValue = 0x100000000 117 }; 118 119 120 class AAA { 121 __declspec(dllimport) void f(void) { } 122 void f2(void); // expected-note{{previous declaration is here}} 123 }; 124 125 __declspec(dllimport) void AAA::f2(void) { // expected-error{{dllimport cannot be applied to non-inline function definition}} 126 // expected-error@-1{{redeclaration of 'AAA::f2' cannot add 'dllimport' attribute}} 127 128 } 129 130 131 132 template <class T> 133 class BB { 134 public: 135 void f(int g = 10 ); // expected-note {{previous definition is here}} 136 }; 137 138 template <class T> 139 void BB<T>::f(int g = 0) { } // expected-warning {{redefinition of default argument}} 140 141 142 143 extern void static_func(); 144 void static_func(); // expected-note {{previous declaration is here}} 145 146 147 static void static_func() // expected-warning {{static declaration of 'static_func' follows non-static declaration}} 148 { 149 150 } 151 152 long function_prototype(int a); 153 long (*function_ptr)(int a); 154 155 void function_to_voidptr_conv() { 156 void *a1 = function_prototype; 157 void *a2 = &function_prototype; 158 void *a3 = function_ptr; 159 } 160 161 162 void pointer_to_integral_type_conv(char* ptr) { 163 char ch = (char)ptr; 164 short sh = (short)ptr; 165 ch = (char)ptr; 166 sh = (short)ptr; 167 168 // These are valid C++. 169 bool b = (bool)ptr; 170 b = static_cast<bool>(ptr); 171 172 // This is bad. 173 b = reinterpret_cast<bool>(ptr); // expected-error {{cast from pointer to smaller type 'bool' loses information}} 174 } 175 176 namespace friend_as_a_forward_decl { 177 178 class A { 179 class Nested { 180 friend class B; 181 B* b; 182 }; 183 B* b; 184 }; 185 B* global_b; 186 187 188 void f() 189 { 190 class Local { 191 friend class Z; 192 Z* b; 193 }; 194 Z* b; 195 } 196 197 } 198 199 struct PR11150 { 200 class X { 201 virtual void f() = 0; 202 }; 203 204 int array[__is_abstract(X)? 1 : -1]; 205 }; 206 207 void f() { int __except = 0; } 208 209 void ::f(); // expected-warning{{extra qualification on member 'f'}} 210 211 class C { 212 C::C(); // expected-warning{{extra qualification on member 'C'}} 213 }; 214 215 struct StructWithProperty { 216 __declspec(property(get=GetV)) int V1; 217 __declspec(property(put=SetV)) int V2; 218 __declspec(property(get=GetV, put=SetV_NotExist)) int V3; 219 __declspec(property(get=GetV_NotExist, put=SetV)) int V4; 220 __declspec(property(get=GetV, put=SetV)) int V5; 221 222 int GetV() { return 123; } 223 void SetV(int i) {} 224 }; 225 void TestProperty() { 226 StructWithProperty sp; 227 int i = sp.V2; // expected-error{{no getter defined for property 'V2'}} 228 sp.V1 = 12; // expected-error{{no setter defined for property 'V1'}} 229 int j = sp.V4; // expected-error{{no member named 'GetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable getter for property 'V4'}} 230 sp.V3 = 14; // expected-error{{no member named 'SetV_NotExist' in 'StructWithProperty'}} expected-error{{cannot find suitable setter for property 'V3'}} 231 int k = sp.V5; 232 sp.V5 = k++; 233 } 234 235 /* 4 tests for PseudoObject, begin */ 236 struct SP1 237 { 238 bool operator()() { return true; } 239 }; 240 struct SP2 241 { 242 __declspec(property(get=GetV)) SP1 V; 243 SP1 GetV() { return SP1(); } 244 }; 245 void TestSP2() { 246 SP2 sp2; 247 bool b = sp2.V(); 248 } 249 250 struct SP3 { 251 template <class T> 252 void f(T t) {} 253 }; 254 template <class T> 255 struct SP4 256 { 257 __declspec(property(get=GetV)) int V; 258 int GetV() { return 123; } 259 void f() { SP3 s2; s2.f(V); } 260 }; 261 void TestSP4() { 262 SP4<int> s; 263 s.f(); 264 } 265 266 template <class T> 267 struct SP5 268 { 269 __declspec(property(get=GetV)) T V; 270 int GetV() { return 123; } 271 void f() { int *p = new int[V]; } 272 }; 273 274 template <class T> 275 struct SP6 276 { 277 public: 278 __declspec(property(get=GetV)) T V; 279 T GetV() { return 123; } 280 void f() { int t = V; } 281 }; 282 void TestSP6() { 283 SP6<int> c; 284 c.f(); 285 } 286 /* 4 tests for PseudoObject, end */ 287 288 // Property access: explicit, implicit, with Qualifier 289 struct SP7 { 290 __declspec(property(get=GetV, put=SetV)) int V; 291 int GetV() { return 123; } 292 void SetV(int v) {} 293 294 void ImplicitAccess() { int i = V; V = i; } 295 void ExplicitAccess() { int i = this->V; this->V = i; } 296 }; 297 struct SP8: public SP7 { 298 void AccessWithQualifier() { int i = SP7::V; SP7::V = i; } 299 }; 300 301 // Property usage 302 template <class T> 303 struct SP9 { 304 __declspec(property(get=GetV, put=SetV)) T V; 305 T GetV() { return 0; } 306 void SetV(T v) {} 307 bool f() { V = this->V; return V < this->V; } 308 void g() { V++; } 309 void h() { V*=2; } 310 }; 311 struct SP10 { 312 SP10(int v) {} 313 bool operator<(const SP10& v) { return true; } 314 SP10 operator*(int v) { return *this; } 315 SP10 operator+(int v) { return *this; } 316 SP10& operator=(const SP10& v) { return *this; } 317 }; 318 void TestSP9() { 319 SP9<int> c; 320 int i = c.V; // Decl initializer 321 i = c.V; // Binary op operand 322 c.SetV(c.V); // CallExpr arg 323 int *p = new int[c.V + 1]; // Array size 324 p[c.V] = 1; // Array index 325 326 c.V = 123; // Setter 327 328 c.V++; // Unary op operand 329 c.V *= 2; // Unary op operand 330 331 SP9<int*> c2; 332 c2.V[0] = 123; // Array 333 334 SP9<SP10> c3; 335 c3.f(); // Overloaded binary op operand 336 c3.g(); // Overloaded incdec op operand 337 c3.h(); // Overloaded unary op operand 338 } 339 340 union u { 341 int *i1; 342 int &i2; // expected-warning {{union member 'i2' has reference type 'int &', which is a Microsoft extension}} 343 }; 344 345 // Property getter using reference. 346 struct SP11 { 347 __declspec(property(get=GetV)) int V; 348 int _v; 349 int& GetV() { return _v; } 350 void UseV(); 351 void TakePtr(int *) {} 352 void TakeRef(int &) {} 353 void TakeVal(int) {} 354 }; 355 356 void SP11::UseV() { 357 TakePtr(&V); 358 TakeRef(V); 359 TakeVal(V); 360 } 361 362 struct StructWithUnnamedMember { 363 __declspec(property(get=GetV)) int : 10; // expected-error {{anonymous property is not supported}} 364 }; 365 366 namespace rdar14250378 { 367 class Bar {}; 368 369 namespace NyNamespace { 370 class Foo { 371 public: 372 Bar* EnsureBar(); 373 }; 374 375 class Baz : public Foo { 376 public: 377 friend class Bar; 378 }; 379 380 Bar* Foo::EnsureBar() { 381 return 0; 382 } 383 } 384 } 385 386 // expected-error@+1 {{'sealed' keyword not permitted with interface types}} 387 __interface InterfaceWithSealed sealed { 388 }; 389 390 struct SomeBase { 391 virtual void OverrideMe(); 392 393 // expected-note@+2 {{overridden virtual function is here}} 394 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}} 395 virtual void SealedFunction() sealed; 396 }; 397 398 // expected-note@+2 {{'SealedType' declared here}} 399 // expected-warning@+1 {{'sealed' keyword is a Microsoft extension}} 400 struct SealedType sealed : SomeBase { 401 // expected-error@+1 {{declaration of 'SealedFunction' overrides a 'sealed' function}} 402 virtual void SealedFunction(); 403 404 // expected-warning@+1 {{'override' keyword is a C++11 extension}} 405 virtual void OverrideMe() override; 406 }; 407 408 // expected-error@+1 {{base 'SealedType' is marked 'sealed'}} 409 struct InheritFromSealed : SealedType {}; 410 411 void AfterClassBody() { 412 // expected-warning@+1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}} 413 struct D {} __declspec(deprecated); 414 415 struct __declspec(align(4)) S {} __declspec(align(8)) s1; 416 S s2; 417 _Static_assert(__alignof(S) == 4, ""); 418 _Static_assert(__alignof(s1) == 8, ""); 419 _Static_assert(__alignof(s2) == 4, ""); 420 } 421