[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Parser / MicrosoftExtensions.cpp
blob9102bca8f6bb2c999dcc7701b566d057dd8feffe
1 // RUN: %clang_cc1 %s -triple i386-pc-win32 -std=c++14 -fsyntax-only -Wno-unused-getter-return-value -Wno-unused-value -Wmicrosoft -verify -fms-extensions -fms-compatibility -fdelayed-template-parsing
3 /* Microsoft attribute tests */
4 [repeatable][source_annotation_attribute( Parameter|ReturnValue )]
5 struct SA_Post{ SA_Post(); int attr; };
7 [returnvalue:SA_Post( attr=1)]
8 int foo1([SA_Post(attr=1)] void *param);
10 namespace {
11 [returnvalue:SA_Post(attr=1)]
12 int foo2([SA_Post(attr=1)] void *param);
15 class T {
16 [returnvalue:SA_Post(attr=1)]
17 int foo3([SA_Post(attr=1)] void *param);
20 extern "C" {
21 [returnvalue:SA_Post(attr=1)]
22 int foo5([SA_Post(attr=1)] void *param);
25 class class_attr {
26 public:
27 class_attr([SA_Pre(Null=SA_No,NullTerminated=SA_Yes)] int a)
34 void uuidof_test1()
36 __uuidof(0);
39 typedef struct _GUID
41 unsigned long Data1;
42 unsigned short Data2;
43 unsigned short Data3;
44 unsigned char Data4[8];
45 } GUID;
47 struct __declspec(uuid(L"00000000-0000-0000-1234-000000000047")) uuid_attr_bad1 { };// expected-warning {{encoding prefix 'L' on an unevaluated string literal has no effect and is incompatible with c++2c}}
48 struct __declspec(uuid(3)) uuid_attr_bad2 { };// expected-error {{expected string literal as argument of 'uuid' attribute}}
49 struct __declspec(uuid("0000000-0000-0000-1234-0000500000047")) uuid_attr_bad3 { };// expected-error {{uuid attribute contains a malformed GUID}}
50 struct __declspec(uuid("0000000-0000-0000-Z234-000000000047")) uuid_attr_bad4 { };// expected-error {{uuid attribute contains a malformed GUID}}
51 struct __declspec(uuid("000000000000-0000-1234-000000000047")) uuid_attr_bad5 { };// expected-error {{uuid attribute contains a malformed GUID}}
52 [uuid("000000000000-0000-1234-000000000047")] struct uuid_attr_bad6 { };// expected-error {{uuid attribute contains a malformed GUID}}
54 __declspec(uuid("000000A0-0000-0000-C000-000000000046")) int i; // expected-warning {{'uuid' attribute only applies to structs, unions, classes, and enums}}
56 struct __declspec(uuid("000000A0-0000-0000-C000-000000000046"))
57 struct_with_uuid { };
58 struct struct_without_uuid { };
60 struct base {
61 int a;
63 struct derived : base {
64 // Can't apply a UUID to a using declaration.
65 [uuid("000000A0-0000-0000-C000-00000000004A")] using base::a; // expected-error {{expected member name}}
68 struct __declspec(uuid("000000A0-0000-0000-C000-000000000049"))
69 struct_with_uuid2;
71 [uuid("000000A0-0000-0000-C000-000000000049")] struct struct_with_uuid3; // expected-warning{{specifying 'uuid' as an ATL attribute is deprecated; use __declspec instead}}
73 struct
74 struct_with_uuid2 {} ;
76 enum __declspec(uuid("000000A0-0000-0000-C000-000000000046"))
77 enum_with_uuid { };
78 enum enum_without_uuid { };
80 int __declspec(uuid("000000A0-0000-0000-C000-000000000046")) inappropriate_uuid; // expected-warning {{'uuid' attribute only applies to}}
82 int uuid_sema_test()
84 struct_with_uuid var_with_uuid[1];
85 struct_without_uuid var_without_uuid[1];
87 __uuidof(struct_with_uuid);
88 __uuidof(struct_with_uuid2);
89 __uuidof(struct_with_uuid3);
90 __uuidof(struct_without_uuid); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
91 __uuidof(struct_with_uuid*);
92 __uuidof(struct_without_uuid*); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
93 __uuidof(struct_with_uuid[1]);
94 __uuidof(struct_with_uuid*[1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
95 __uuidof(const struct_with_uuid[1][1]);
96 __uuidof(const struct_with_uuid*[1][1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
98 __uuidof(enum_with_uuid);
99 __uuidof(enum_without_uuid); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
100 __uuidof(enum_with_uuid*);
101 __uuidof(enum_without_uuid*); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
102 __uuidof(enum_with_uuid[1]);
103 __uuidof(enum_with_uuid*[1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
104 __uuidof(const enum_with_uuid[1][1]);
105 __uuidof(const enum_with_uuid*[1][1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
107 __uuidof(var_with_uuid);
108 __uuidof(var_without_uuid);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
109 __uuidof(var_with_uuid[1]);
110 __uuidof(var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
111 __uuidof(&var_with_uuid[1]);
112 __uuidof(&var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
114 __uuidof(0);
115 __uuidof(1);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
119 template <class T>
120 void template_uuid()
122 T expr;
124 __uuidof(T);
125 __uuidof(expr);
129 template <class T, const GUID* g = &__uuidof(T)> // expected-note {{template parameter is declared here}}
130 class COM_CLASS_TEMPLATE { };
132 typedef COM_CLASS_TEMPLATE<struct_with_uuid, &*&__uuidof(struct_with_uuid)> COM_TYPE_1; // expected-warning {{non-type template argument containing a dereference operation is a Microsoft extension}}
133 typedef COM_CLASS_TEMPLATE<struct_with_uuid> COM_TYPE_2;
135 template <class T, const GUID& g>
136 class COM_CLASS_TEMPLATE_REF { };
137 typedef COM_CLASS_TEMPLATE_REF<struct_with_uuid, __uuidof(struct_with_uuid)> COM_TYPE_REF;
139 struct late_defined_uuid;
140 template<typename T>
141 void test_late_defined_uuid() {
142 __uuidof(late_defined_uuid);
144 struct __declspec(uuid("000000A0-0000-0000-C000-000000000049")) late_defined_uuid;
146 COM_CLASS_TEMPLATE_REF<int, __uuidof(struct_with_uuid)> good_template_arg;
148 COM_CLASS_TEMPLATE<int, __uuidof(struct_with_uuid)> bad_template_arg; // expected-error {{non-type template argument for template parameter of pointer type 'const GUID *' (aka 'const _GUID *') must have its address taken}}
150 namespace PR16911 {
151 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
152 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid2;
154 template <typename T, typename T2>
155 struct thing {
158 struct empty {};
159 struct inher : public thing<empty, uuid2> {};
161 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
162 const struct _GUID *w = &__uuidof(inher); // expected-error{{cannot call operator __uuidof on a type with no GUID}}
163 const struct _GUID *x = &__uuidof(thing<uuid, inher>);
164 const struct _GUID *y = &__uuidof(thing<uuid2, uuid>); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
165 thing<uuid2, uuid> thing_obj = thing<uuid2, uuid>();
166 const struct _GUID *z = &__uuidof(thing_obj); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
169 class CtorCall {
170 public:
171 CtorCall& operator=(const CtorCall& that);
173 int a;
176 CtorCall& CtorCall::operator=(const CtorCall& that)
178 if (this != &that) {
179 this->CtorCall::~CtorCall();
180 this->CtorCall::CtorCall(that); // expected-warning {{explicit constructor calls are a Microsoft extension}}
182 return *this;
185 template <class A>
186 class C1 {
187 public:
188 template <int B>
189 class Iterator {
193 template<class T>
194 class C2 {
195 typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
198 template <class T>
199 void missing_template_keyword(){
200 typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
205 class AAAA {
206 typedef int D;
209 template <typename T>
210 class SimpleTemplate {};
212 template <class T>
213 void redundant_typename() {
214 typename T t;// expected-warning {{expected a qualified name after 'typename'}}
215 typename AAAA a;// expected-warning {{expected a qualified name after 'typename'}}
217 t = 3;
219 typedef typename T* pointerT;// expected-warning {{expected a qualified name after 'typename'}}
220 typedef typename SimpleTemplate<int> templateT;// expected-warning {{expected a qualified name after 'typename'}}
222 pointerT pT = &t;
223 *pT = 4;
225 int var;
226 int k = typename var;// expected-error {{expected a qualified name after 'typename'}}
229 template <typename T>
230 struct TypenameWrongPlace {
231 typename typedef T::D D;// expected-warning {{expected a qualified name after 'typename'}}
234 extern TypenameWrongPlace<AAAA> PR16925;
236 __interface MicrosoftInterface;
237 __interface MicrosoftInterface {
238 void foo1() = 0; // expected-note {{overridden virtual function is here}}
239 virtual void foo2() = 0;
242 __interface MicrosoftDerivedInterface : public MicrosoftInterface {
243 void foo1(); // expected-warning {{'foo1' overrides a member function but is not marked 'override'}}
244 void foo2() override;
245 void foo3();
248 void interface_test() {
249 MicrosoftInterface* a;
250 a->foo1();
251 MicrosoftDerivedInterface* b;
252 b->foo2();
255 __int64 x7 = __int64(0);
256 _int64 x8 = _int64(0);
257 static_assert(sizeof(_int64) == 8, "");
258 static_assert(sizeof(_int32) == 4, "");
259 static_assert(sizeof(_int16) == 2, "");
260 static_assert(sizeof(_int8) == 1, "");
262 int __identifier(generic) = 3;
263 int __identifier(int) = 4;
264 struct __identifier(class) { __identifier(class) *__identifier(for); };
265 __identifier(class) __identifier(struct) = { &__identifier(struct) };
267 int __identifier for; // expected-error {{missing '(' after '__identifier'}}
268 int __identifier(else} = __identifier(for); // expected-error {{missing ')' after identifier}} expected-note {{to match this '('}}
269 #define identifier_weird(x) __identifier(x
270 int k = identifier_weird(if)); // expected-error {{use of undeclared identifier 'if'}}
272 extern int __identifier(and);
274 int __identifier("baz") = 0;
275 int bar = baz;
277 void mangled_function();
278 extern "C" void __identifier("?mangled_function@@YAXXZ")() {}
280 void f() {
281 __identifier(() // expected-error {{cannot convert '(' token to an identifier}}
282 __identifier(void) // expected-error {{use of undeclared identifier 'void'}}
283 __identifier()) // expected-error {{cannot convert ')' token to an identifier}}
284 // FIXME: We should pick a friendlier display name for this token kind.
285 __identifier(1) // expected-error {{cannot convert <numeric_constant> token to an identifier}}
286 __identifier(+) // expected-error {{cannot convert '+' token to an identifier}}
287 __identifier(;) // expected-error {{cannot convert ';' token to an identifier}}
288 __identifier("1"); // expected-error {{use of undeclared identifier '1'}}
289 __identifier("+"); // expected-error {{use of undeclared identifier '+'}}
290 __identifier(";"); // expected-error {{use of undeclared identifier ';'}}
293 class inline_definition_pure_spec {
294 virtual int f() = 0 { return 0; }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
295 virtual int f2() = 0;
298 struct pure_virtual_dtor {
299 virtual ~pure_virtual_dtor() = 0;
301 pure_virtual_dtor::~pure_virtual_dtor() { }
303 struct pure_virtual_dtor_inline {
304 virtual ~pure_virtual_dtor_inline() = 0 { }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
307 template<typename T> struct pure_virtual_dtor_template {
308 virtual ~pure_virtual_dtor_template() = 0;
310 template<typename T> pure_virtual_dtor_template<T>::~pure_virtual_dtor_template() {}
311 template struct pure_virtual_dtor_template<int>;
313 template<typename T> struct pure_virtual_dtor_template_inline {
314 virtual ~pure_virtual_dtor_template_inline() = 0 {}
315 // expected-warning@-1 2{{function definition with pure-specifier is a Microsoft extension}}
317 template struct pure_virtual_dtor_template_inline<int>;
318 // expected-note@-1 {{in instantiation of member function}}
320 int main () {
321 // Necessary to force instantiation in -fdelayed-template-parsing mode.
322 test_late_defined_uuid<int>();
323 redundant_typename<int>();
324 missing_template_keyword<int>();
327 namespace access_protected_PTM {
328 class A {
329 protected:
330 void f(); // expected-note {{must name member using the type of the current context 'access_protected_PTM::B'}}
333 class B : public A{
334 public:
335 void test_access();
336 static void test_access_static();
339 void B::test_access() {
340 &A::f; // expected-error {{'f' is a protected member of 'access_protected_PTM::A'}}
343 void B::test_access_static() {
344 &A::f;
348 namespace Inheritance {
349 class __single_inheritance A;
350 class __multiple_inheritance B;
351 class __virtual_inheritance C;
354 struct StructWithProperty {
355 __declspec(property) int V0; // expected-error {{expected '(' after 'property'}}
356 __declspec(property()) int V1; // expected-error {{property does not specify a getter or a putter}}
357 __declspec(property(set)) int V2; // expected-error {{putter for property must be specified as 'put', not 'set'}} expected-error {{expected '=' after 'set'}}
358 __declspec(property(ptu)) int V3; // expected-error {{missing 'get=' or 'put='}}
359 __declspec(property(ptu=PutV)) int V4; // expected-error {{expected 'get' or 'put' in property declaration}}
360 __declspec(property(get)) int V5; // expected-error {{expected '=' after 'get'}}
361 __declspec(property(get&)) int V6; // expected-error {{expected '=' after 'get'}}
362 __declspec(property(get=)) int V7; // expected-error {{expected name of accessor method}}
363 __declspec(property(get=GetV)) int V8; // no-warning
364 __declspec(property(get=GetV=)) int V9; // expected-error {{expected ',' or ')' at end of property accessor list}}
365 __declspec(property(get=GetV,)) int V10; // expected-error {{expected 'get' or 'put' in property declaration}}
366 __declspec(property(get=GetV,put=SetV)) int V11; // no-warning
367 __declspec(property(get=GetV,put=SetV,get=GetV)) int V12; // expected-error {{property declaration specifies 'get' accessor twice}}
368 __declspec(property(get=GetV)) int V13 = 3; // expected-error {{property declaration cannot have a default member initializer}}
370 int GetV() { return 123; }
371 void SetV(int v) {}
373 void TestProperty() {
374 StructWithProperty sp;
375 sp.V8;
376 sp.V8 = 0; // expected-error {{no setter defined for property 'V8'}}
377 int i = sp.V11;
378 sp.V11 = i++;
379 sp.V11 += 8;
380 sp.V11++;
381 ++sp.V11;
384 //expected-warning@+1 {{C++ operator 'and' (aka '&&') used as a macro name}}
385 #define and foo
387 struct __declspec(uuid("00000000-0000-0000-C000-000000000046")) __declspec(novtable) IUnknown {};
389 typedef bool (__stdcall __stdcall *blarg)(int);
391 void local_callconv() {
392 bool (__stdcall *p)(int);
395 struct S7 {
396 int foo() { return 12; }
397 __declspec(property(get=foo) deprecated) int t; // expected-note {{'t' has been explicitly marked deprecated here}}
400 // Technically, this is legal (though it does nothing)
401 __declspec() void quux( void ) {
402 struct S7 s;
403 int i = s.t; // expected-warning {{'t' is deprecated}}
406 void *_alloca(int);
408 void foo(void) {
409 __declspec(align(16)) int *buffer = (int *)_alloca(9);
412 template <int *>
413 struct NullptrArg {};
414 NullptrArg<nullptr> a;
416 // Ignored type qualifiers after comma in declarator lists
417 typedef int ignored_quals_dummy1, const volatile __ptr32 __ptr64 __w64 __unaligned __sptr __uptr ignored_quals1; // expected-warning {{qualifiers after comma in declarator list are ignored}}
418 typedef void(*ignored_quals_dummy2)(), __fastcall ignored_quals2; // expected-warning {{qualifiers after comma in declarator list are ignored}}
419 typedef void(*ignored_quals_dummy3)(), __stdcall ignored_quals3; // expected-warning {{qualifiers after comma in declarator list are ignored}}
420 typedef void(*ignored_quals_dummy4)(), __thiscall ignored_quals4; // expected-warning {{qualifiers after comma in declarator list are ignored}}
421 typedef void(*ignored_quals_dummy5)(), __cdecl ignored_quals5; // expected-warning {{qualifiers after comma in declarator list are ignored}}
422 typedef void(*ignored_quals_dummy6)(), __vectorcall ignored_quals6; // expected-warning {{qualifiers after comma in declarator list are ignored}}
424 namespace {
425 bool f(int);
426 template <typename T>
427 struct A {
428 constexpr A(T t) {
429 __assume(f(t)); // expected-warning{{assumption is ignored because it contains (potential) side-effects}}
431 constexpr bool g() { return false; }
433 constexpr A<int> h() {
434 A<int> b(0); // expected-note {{in instantiation of member function}}
435 return b;
437 static_assert(h().g() == false, "");
440 namespace {
441 __declspec(align(16)) struct align_before_key1 {};
442 __declspec(align(16)) struct align_before_key2 {} align_before_key2_var;
443 __declspec(align(16)) struct align_before_key3 {} *align_before_key3_var;
444 static_assert(__alignof(struct align_before_key1) == 16, "");
445 static_assert(__alignof(struct align_before_key2) == 16, "");
446 static_assert(__alignof(struct align_before_key3) == 16, "");
449 namespace PR24027 {
450 struct S {
451 template <typename T>
452 S(T);
453 } f([] {});
456 namespace pr36638 {
457 // Make sure we accept __unaligned method qualifiers on member function
458 // pointers.
459 struct A;
460 void (A::*mp1)(int) __unaligned;
463 namespace enum_class {
464 // MSVC allows opaque-enum-declaration syntax anywhere an
465 // elaborated-type-specifier can appear.
466 // FIXME: Most of these are missing warnings.
467 enum E0 *p0; // expected-warning {{Microsoft extension}}
468 enum class E1 : int *p1;
469 enum E2 : int *p2;
470 enum class E3 *p3;
471 auto f4() -> enum class E4 { return {}; }
472 auto f5() -> enum E5 : int { return {}; } // FIXME: MSVC rejects this and crashes if the body is {}.
473 auto f6() -> enum E6 { return {}; } // expected-warning {{Microsoft extension}}
475 // MSVC does not perform disambiguation for a colon that could introduce an
476 // enum-base or a bit-field.
477 enum E {};
478 struct S {
479 enum E : int(1); // expected-error {{anonymous bit-field}}
480 enum E : int : 1; // OK, bit-field
481 enum F : int a = {}; // OK, default member initializer
482 // MSVC produces a "C4353 constant 0 as function expression" for this,
483 // considering the final {} to be part of the bit-width. We follow P0683R1
484 // and treat it as a default member initializer.
485 enum E : int : int{}{}; // expected-error {{anonymous bit-field cannot have a default member initializer}}