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
2
3 /* Microsoft attribute tests */
4 [repeatable][source_annotation_attribute( Parameter|ReturnValue )]
5 struct SA_Post{ SA_Post(); int attr; };
6
7 [returnvalue:SA_Post( attr=1)]
8 int foo1([SA_Post(attr=1)] void *param);
9
10 namespace {
11 [returnvalue:SA_Post(attr=1)]
12 int foo2([SA_Post(attr=1)] void *param);
13 }
14
15 class T {
16 [returnvalue:SA_Post(attr=1)]
17 int foo3([SA_Post(attr=1)] void *param);
18 };
19
20 extern "C" {
21 [returnvalue:SA_Post(attr=1)]
22 int foo5([SA_Post(attr=1)] void *param);
23 }
24
25 class class_attr {
26 public:
27 class_attr([SA_Pre(Null=SA_No,NullTerminated=SA_Yes)] int a)
28 {
29 }
30 };
31
32
33
uuidof_test1()34 void uuidof_test1()
35 {
36 __uuidof(0);
37 }
38
39 typedef struct _GUID
40 {
41 unsigned long Data1;
42 unsigned short Data2;
43 unsigned short Data3;
44 unsigned char Data4[8];
45 } GUID;
46
47 struct __declspec(uuid(L"00000000-0000-0000-1234-000000000047")) uuid_attr_bad1 { };// expected-error {{'uuid' attribute requires a string}}
48 struct __declspec(uuid(3)) uuid_attr_bad2 { };// expected-error {{'uuid' attribute requires a string}}
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}}
53
54 __declspec(uuid("000000A0-0000-0000-C000-000000000046")) int i; // expected-warning {{'uuid' attribute only applies to structs, unions, classes, and enums}}
55
56 struct __declspec(uuid("000000A0-0000-0000-C000-000000000046"))
57 struct_with_uuid { };
58 struct struct_without_uuid { };
59
60 struct __declspec(uuid("000000A0-0000-0000-C000-000000000049"))
61 struct_with_uuid2;
62
63 [uuid("000000A0-0000-0000-C000-000000000049")] struct struct_with_uuid3; // expected-warning{{specifying 'uuid' as an ATL attribute is deprecated; use __declspec instead}}
64
65 struct
66 struct_with_uuid2 {} ;
67
68 enum __declspec(uuid("000000A0-0000-0000-C000-000000000046"))
69 enum_with_uuid { };
70 enum enum_without_uuid { };
71
72 int __declspec(uuid("000000A0-0000-0000-C000-000000000046")) inappropriate_uuid; // expected-warning {{'uuid' attribute only applies to}}
73
uuid_sema_test()74 int uuid_sema_test()
75 {
76 struct_with_uuid var_with_uuid[1];
77 struct_without_uuid var_without_uuid[1];
78
79 __uuidof(struct_with_uuid);
80 __uuidof(struct_with_uuid2);
81 __uuidof(struct_with_uuid3);
82 __uuidof(struct_without_uuid); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
83 __uuidof(struct_with_uuid*);
84 __uuidof(struct_without_uuid*); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
85 __uuidof(struct_with_uuid[1]);
86 __uuidof(struct_with_uuid*[1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
87 __uuidof(const struct_with_uuid[1][1]);
88 __uuidof(const struct_with_uuid*[1][1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
89
90 __uuidof(enum_with_uuid);
91 __uuidof(enum_without_uuid); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
92 __uuidof(enum_with_uuid*);
93 __uuidof(enum_without_uuid*); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
94 __uuidof(enum_with_uuid[1]);
95 __uuidof(enum_with_uuid*[1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
96 __uuidof(const enum_with_uuid[1][1]);
97 __uuidof(const enum_with_uuid*[1][1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
98
99 __uuidof(var_with_uuid);
100 __uuidof(var_without_uuid);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
101 __uuidof(var_with_uuid[1]);
102 __uuidof(var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
103 __uuidof(&var_with_uuid[1]);
104 __uuidof(&var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
105
106 __uuidof(0);
107 __uuidof(1);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
108 }
109
110
111 template <class T>
template_uuid()112 void template_uuid()
113 {
114 T expr;
115
116 __uuidof(T);
117 __uuidof(expr);
118 }
119
120
121 template <class T, const GUID* g = &__uuidof(T)> // expected-note {{template parameter is declared here}}
122 class COM_CLASS_TEMPLATE { };
123
124 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}}
125 typedef COM_CLASS_TEMPLATE<struct_with_uuid> COM_TYPE_2;
126
127 template <class T, const GUID& g>
128 class COM_CLASS_TEMPLATE_REF { };
129 typedef COM_CLASS_TEMPLATE_REF<struct_with_uuid, __uuidof(struct_with_uuid)> COM_TYPE_REF;
130
131 struct late_defined_uuid;
132 template<typename T>
test_late_defined_uuid()133 void test_late_defined_uuid() {
134 __uuidof(late_defined_uuid);
135 }
136 struct __declspec(uuid("000000A0-0000-0000-C000-000000000049")) late_defined_uuid;
137
138 COM_CLASS_TEMPLATE_REF<int, __uuidof(struct_with_uuid)> good_template_arg;
139
140 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}}
141
142 namespace PR16911 {
143 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
144 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid2;
145
146 template <typename T, typename T2>
147 struct thing {
148 };
149
150 struct empty {};
151 struct inher : public thing<empty, uuid2> {};
152
153 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
154 const struct _GUID *w = &__uuidof(inher); // expected-error{{cannot call operator __uuidof on a type with no GUID}}
155 const struct _GUID *x = &__uuidof(thing<uuid, inher>);
156 const struct _GUID *y = &__uuidof(thing<uuid2, uuid>); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
157 thing<uuid2, uuid> thing_obj = thing<uuid2, uuid>();
158 const struct _GUID *z = &__uuidof(thing_obj); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
159 }
160
161 class CtorCall {
162 public:
163 CtorCall& operator=(const CtorCall& that);
164
165 int a;
166 };
167
operator =(const CtorCall & that)168 CtorCall& CtorCall::operator=(const CtorCall& that)
169 {
170 if (this != &that) {
171 this->CtorCall::~CtorCall();
172 this->CtorCall::CtorCall(that); // expected-warning {{explicit constructor calls are a Microsoft extension}}
173 }
174 return *this;
175 }
176
177 template <class A>
178 class C1 {
179 public:
180 template <int B>
181 class Iterator {
182 };
183 };
184
185 template<class T>
186 class C2 {
187 typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
188 };
189
190 template <class T>
missing_template_keyword()191 void missing_template_keyword(){
192 typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
193 }
194
195
196
197 class AAAA {
198 typedef int D;
199 };
200
201 template <typename T>
202 class SimpleTemplate {};
203
204 template <class T>
redundant_typename()205 void redundant_typename() {
206 typename T t;// expected-warning {{expected a qualified name after 'typename'}}
207 typename AAAA a;// expected-warning {{expected a qualified name after 'typename'}}
208
209 t = 3;
210
211 typedef typename T* pointerT;// expected-warning {{expected a qualified name after 'typename'}}
212 typedef typename SimpleTemplate<int> templateT;// expected-warning {{expected a qualified name after 'typename'}}
213
214 pointerT pT = &t;
215 *pT = 4;
216
217 int var;
218 int k = typename var;// expected-error {{expected a qualified name after 'typename'}}
219 }
220
221 template <typename T>
222 struct TypenameWrongPlace {
223 typename typedef T::D D;// expected-warning {{expected a qualified name after 'typename'}}
224 };
225
226 extern TypenameWrongPlace<AAAA> PR16925;
227
228 __interface MicrosoftInterface;
229 __interface MicrosoftInterface {
230 void foo1() = 0; // expected-note {{overridden virtual function is here}}
231 virtual void foo2() = 0;
232 };
233
234 __interface MicrosoftDerivedInterface : public MicrosoftInterface {
235 void foo1(); // expected-warning {{'foo1' overrides a member function but is not marked 'override'}}
236 void foo2() override;
237 void foo3();
238 };
239
240 void interface_test() {
241 MicrosoftInterface* a;
242 a->foo1();
243 MicrosoftDerivedInterface* b;
244 b->foo2();
245 }
246
247 __int64 x7 = __int64(0);
248 _int64 x8 = _int64(0);
249 static_assert(sizeof(_int64) == 8, "");
250 static_assert(sizeof(_int32) == 4, "");
251 static_assert(sizeof(_int16) == 2, "");
252 static_assert(sizeof(_int8) == 1, "");
253
254 int __identifier(generic) = 3;
255 int __identifier(int) = 4;
256 struct __identifier(class) { __identifier(class) *__identifier(for); };
257 __identifier(class) __identifier(struct) = { &__identifier(struct) };
258
259 int __identifier for; // expected-error {{missing '(' after '__identifier'}}
260 int __identifier(else} = __identifier(for); // expected-error {{missing ')' after identifier}} expected-note {{to match this '('}}
261 #define identifier_weird(x) __identifier(x
262 int k = identifier_weird(if)); // expected-error {{use of undeclared identifier 'if'}}
263
264 extern int __identifier(and);
265
266 int __identifier("baz") = 0;
267 int bar = baz;
268
269 void mangled_function();
270 extern "C" void __identifier("?mangled_function@@YAXXZ")() {}
271
272 void f() {
273 __identifier(() // expected-error {{cannot convert '(' token to an identifier}}
274 __identifier(void) // expected-error {{use of undeclared identifier 'void'}}
275 __identifier()) // expected-error {{cannot convert ')' token to an identifier}}
276 // FIXME: We should pick a friendlier display name for this token kind.
277 __identifier(1) // expected-error {{cannot convert <numeric_constant> token to an identifier}}
278 __identifier(+) // expected-error {{cannot convert '+' token to an identifier}}
279 __identifier(;) // expected-error {{cannot convert ';' token to an identifier}}
280 __identifier("1"); // expected-error {{use of undeclared identifier '1'}}
281 __identifier("+"); // expected-error {{use of undeclared identifier '+'}}
282 __identifier(";"); // expected-error {{use of undeclared identifier ';'}}
283 }
284
285 class inline_definition_pure_spec {
286 virtual int f() = 0 { return 0; }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
287 virtual int f2() = 0;
288 };
289
290 struct pure_virtual_dtor {
291 virtual ~pure_virtual_dtor() = 0;
292 };
293 pure_virtual_dtor::~pure_virtual_dtor() { }
294
295 struct pure_virtual_dtor_inline {
296 virtual ~pure_virtual_dtor_inline() = 0 { }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
297 };
298
299 template<typename T> struct pure_virtual_dtor_template {
300 virtual ~pure_virtual_dtor_template() = 0;
301 };
302 template<typename T> pure_virtual_dtor_template<T>::~pure_virtual_dtor_template() {}
303 template struct pure_virtual_dtor_template<int>;
304
305 template<typename T> struct pure_virtual_dtor_template_inline {
306 virtual ~pure_virtual_dtor_template_inline() = 0 {}
307 // expected-warning@-1 2{{function definition with pure-specifier is a Microsoft extension}}
308 };
309 template struct pure_virtual_dtor_template_inline<int>;
310 // expected-note@-1 {{in instantiation of member function}}
311
312 int main () {
313 // Necessary to force instantiation in -fdelayed-template-parsing mode.
314 test_late_defined_uuid<int>();
315 redundant_typename<int>();
316 missing_template_keyword<int>();
317 }
318
319 namespace access_protected_PTM {
320 class A {
321 protected:
322 void f(); // expected-note {{must name member using the type of the current context 'access_protected_PTM::B'}}
323 };
324
325 class B : public A{
326 public:
327 void test_access();
328 static void test_access_static();
329 };
330
331 void B::test_access() {
332 &A::f; // expected-error {{'f' is a protected member of 'access_protected_PTM::A'}}
333 }
334
335 void B::test_access_static() {
336 &A::f;
337 }
338 }
339
340 namespace Inheritance {
341 class __single_inheritance A;
342 class __multiple_inheritance B;
343 class __virtual_inheritance C;
344 }
345
346 struct StructWithProperty {
347 __declspec(property) int V0; // expected-error {{expected '(' after 'property'}}
348 __declspec(property()) int V1; // expected-error {{property does not specify a getter or a putter}}
349 __declspec(property(set)) int V2; // expected-error {{putter for property must be specified as 'put', not 'set'}} expected-error {{expected '=' after 'set'}}
350 __declspec(property(ptu)) int V3; // expected-error {{missing 'get=' or 'put='}}
351 __declspec(property(ptu=PutV)) int V4; // expected-error {{expected 'get' or 'put' in property declaration}}
352 __declspec(property(get)) int V5; // expected-error {{expected '=' after 'get'}}
353 __declspec(property(get&)) int V6; // expected-error {{expected '=' after 'get'}}
354 __declspec(property(get=)) int V7; // expected-error {{expected name of accessor method}}
355 __declspec(property(get=GetV)) int V8; // no-warning
356 __declspec(property(get=GetV=)) int V9; // expected-error {{expected ',' or ')' at end of property accessor list}}
357 __declspec(property(get=GetV,)) int V10; // expected-error {{expected 'get' or 'put' in property declaration}}
358 __declspec(property(get=GetV,put=SetV)) int V11; // no-warning
359 __declspec(property(get=GetV,put=SetV,get=GetV)) int V12; // expected-error {{property declaration specifies 'get' accessor twice}}
360 __declspec(property(get=GetV)) int V13 = 3; // expected-error {{property declaration cannot have a default member initializer}}
361
362 int GetV() { return 123; }
363 void SetV(int v) {}
364 };
365 void TestProperty() {
366 StructWithProperty sp;
367 sp.V8;
368 sp.V8 = 0; // expected-error {{no setter defined for property 'V8'}}
369 int i = sp.V11;
370 sp.V11 = i++;
371 sp.V11 += 8;
372 sp.V11++;
373 ++sp.V11;
374 }
375
376 //expected-warning@+1 {{C++ operator 'and' (aka '&&') used as a macro name}}
377 #define and foo
378
379 struct __declspec(uuid("00000000-0000-0000-C000-000000000046")) __declspec(novtable) IUnknown {};
380
381 typedef bool (__stdcall __stdcall *blarg)(int);
382
383 void local_callconv() {
384 bool (__stdcall *p)(int);
385 }
386
387 struct S7 {
388 int foo() { return 12; }
389 __declspec(property(get=foo) deprecated) int t; // expected-note {{'t' has been explicitly marked deprecated here}}
390 };
391
392 // Technically, this is legal (though it does nothing)
393 __declspec() void quux( void ) {
394 struct S7 s;
395 int i = s.t; // expected-warning {{'t' is deprecated}}
396 }
397
398 void *_alloca(int);
399
400 void foo(void) {
401 __declspec(align(16)) int *buffer = (int *)_alloca(9);
402 }
403
404 template <int *>
405 struct NullptrArg {};
406 NullptrArg<nullptr> a;
407
408 // Ignored type qualifiers after comma in declarator lists
409 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}}
410 typedef void(*ignored_quals_dummy2)(), __fastcall ignored_quals2; // expected-warning {{qualifiers after comma in declarator list are ignored}}
411 typedef void(*ignored_quals_dummy3)(), __stdcall ignored_quals3; // expected-warning {{qualifiers after comma in declarator list are ignored}}
412 typedef void(*ignored_quals_dummy4)(), __thiscall ignored_quals4; // expected-warning {{qualifiers after comma in declarator list are ignored}}
413 typedef void(*ignored_quals_dummy5)(), __cdecl ignored_quals5; // expected-warning {{qualifiers after comma in declarator list are ignored}}
414 typedef void(*ignored_quals_dummy6)(), __vectorcall ignored_quals6; // expected-warning {{qualifiers after comma in declarator list are ignored}}
415
416 namespace {
417 bool f(int);
418 template <typename T>
419 struct A {
420 constexpr A(T t) {
421 __assume(f(t)); // expected-warning{{the argument to '__assume' has side effects that will be discarded}}
422 }
423 constexpr bool g() { return false; }
424 };
425 constexpr A<int> h() {
426 A<int> b(0); // expected-note {{in instantiation of member function}}
427 return b;
428 }
429 static_assert(h().g() == false, "");
430 }
431
432 namespace {
433 __declspec(align(16)) struct align_before_key1 {};
434 __declspec(align(16)) struct align_before_key2 {} align_before_key2_var;
435 __declspec(align(16)) struct align_before_key3 {} *align_before_key3_var;
436 static_assert(__alignof(struct align_before_key1) == 16, "");
437 static_assert(__alignof(struct align_before_key2) == 16, "");
438 static_assert(__alignof(struct align_before_key3) == 16, "");
439 }
440
441 namespace PR24027 {
442 struct S {
443 template <typename T>
444 S(T);
445 } f([] {});
446 }
447
448 namespace pr36638 {
449 // Make sure we accept __unaligned method qualifiers on member function
450 // pointers.
451 struct A;
452 void (A::*mp1)(int) __unaligned;
453 }
454
455 namespace enum_class {
456 // MSVC allows opaque-enum-declaration syntax anywhere an
457 // elaborated-type-specifier can appear.
458 // FIXME: Most of these are missing warnings.
459 enum E0 *p0; // expected-warning {{Microsoft extension}}
460 enum class E1 : int *p1;
461 enum E2 : int *p2;
462 enum class E3 *p3;
463 auto f4() -> enum class E4 { return {}; }
464 auto f5() -> enum E5 : int { return {}; } // FIXME: MSVC rejects this and crashes if the body is {}.
465 auto f6() -> enum E6 { return {}; } // expected-warning {{Microsoft extension}}
466
467 // MSVC does not perform disambiguation for a colon that could introduce an
468 // enum-base or a bit-field.
469 enum E {};
470 struct S {
471 enum E : int(1); // expected-error {{anonymous bit-field}}
472 enum E : int : 1; // OK, bit-field
473 enum F : int a = {}; // OK, default member initializer
474 // MSVC produces a "C4353 constant 0 as function expression" for this,
475 // considering the final {} to be part of the bit-width. We follow P0683R1
476 // and treat it as a default member initializer.
477 enum E : int : int{}{}; // expected-error {{anonymous bit-field cannot have a default member initializer}}
478 };
479 }
480