blob: 5f3c2a26fc03e4b8d37593c3b1f184a9e3ec12a2 [file] [log] [blame]
Francois Pichetb2d899e2012-04-17 12:35:05 +00001// RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -Wno-unused-value -Wmicrosoft -verify -fms-extensions -fms-compatibility -fdelayed-template-parsing
Nick Lewycky141ecfe2010-10-12 16:46:35 +00002
3/* Microsoft attribute tests */
4[repeatable][source_annotation_attribute( Parameter|ReturnValue )]
5struct SA_Post{ SA_Post(); int attr; };
6
Aaron Ballman3cd6feb2013-07-30 01:31:03 +00007[returnvalue:SA_Post( attr=1)]
Nick Lewycky141ecfe2010-10-12 16:46:35 +00008int foo1([SA_Post(attr=1)] void *param);
9
10namespace {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +000011 [returnvalue:SA_Post(attr=1)]
Nick Lewycky141ecfe2010-10-12 16:46:35 +000012 int foo2([SA_Post(attr=1)] void *param);
13}
14
15class T {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +000016 [returnvalue:SA_Post(attr=1)]
Nick Lewycky141ecfe2010-10-12 16:46:35 +000017 int foo3([SA_Post(attr=1)] void *param);
18};
19
20extern "C" {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +000021 [returnvalue:SA_Post(attr=1)]
Nick Lewycky141ecfe2010-10-12 16:46:35 +000022 int foo5([SA_Post(attr=1)] void *param);
23}
24
Francois Pichetdfaa5fb2011-01-31 04:54:32 +000025class class_attr {
26public:
27 class_attr([SA_Pre(Null=SA_No,NullTerminated=SA_Yes)] int a)
28 {
29 }
30};
31
32
Nick Lewycky141ecfe2010-10-12 16:46:35 +000033
Francois Pichet913b7bf2010-12-20 03:51:03 +000034void uuidof_test1()
Aaron Ballman3cd6feb2013-07-30 01:31:03 +000035{
Francois Pichet913b7bf2010-12-20 03:51:03 +000036 __uuidof(0); // expected-error {{you need to include <guiddef.h> before using the '__uuidof' operator}}
37}
38
39typedef struct _GUID
40{
41 unsigned long Data1;
42 unsigned short Data2;
43 unsigned short Data3;
44 unsigned char Data4[8];
45} GUID;
46
Aaron Ballman3cd6feb2013-07-30 01:31:03 +000047struct __declspec(uuid(L"00000000-0000-0000-1234-000000000047")) uuid_attr_bad1 { };// expected-error {{'uuid' attribute requires a string}}
48struct __declspec(uuid(3)) uuid_attr_bad2 { };// expected-error {{'uuid' attribute requires a string}}
Francois Pichet913b7bf2010-12-20 03:51:03 +000049struct __declspec(uuid("0000000-0000-0000-1234-0000500000047")) uuid_attr_bad3 { };// expected-error {{uuid attribute contains a malformed GUID}}
50struct __declspec(uuid("0000000-0000-0000-Z234-000000000047")) uuid_attr_bad4 { };// expected-error {{uuid attribute contains a malformed GUID}}
51struct __declspec(uuid("000000000000-0000-1234-000000000047")) uuid_attr_bad5 { };// expected-error {{uuid attribute contains a malformed GUID}}
52
Stephen Hines651f13c2014-04-23 16:59:28 -070053__declspec(uuid("000000A0-0000-0000-C000-000000000046")) int i; // expected-warning {{'uuid' attribute only applies to classes}}
Francois Pichet913b7bf2010-12-20 03:51:03 +000054
55struct __declspec(uuid("000000A0-0000-0000-C000-000000000046"))
56struct_with_uuid { };
57struct struct_without_uuid { };
58
Francois Pichet6915c522010-12-27 01:32:00 +000059struct __declspec(uuid("000000A0-0000-0000-C000-000000000049"))
60struct_with_uuid2;
61
Aaron Ballman3cd6feb2013-07-30 01:31:03 +000062struct
Francois Pichet6915c522010-12-27 01:32:00 +000063struct_with_uuid2 {} ;
Francois Pichet913b7bf2010-12-20 03:51:03 +000064
65int uuid_sema_test()
66{
67 struct_with_uuid var_with_uuid[1];
68 struct_without_uuid var_without_uuid[1];
69
70 __uuidof(struct_with_uuid);
Francois Pichet6915c522010-12-27 01:32:00 +000071 __uuidof(struct_with_uuid2);
Francois Pichet913b7bf2010-12-20 03:51:03 +000072 __uuidof(struct_without_uuid); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
73 __uuidof(struct_with_uuid*);
74 __uuidof(struct_without_uuid*); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
David Majnemerc420e3c2013-09-27 07:57:34 +000075 __uuidof(struct_with_uuid[1]);
76 __uuidof(struct_with_uuid*[1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
77 __uuidof(const struct_with_uuid[1][1]);
78 __uuidof(const struct_with_uuid*[1][1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
Francois Pichet913b7bf2010-12-20 03:51:03 +000079
80 __uuidof(var_with_uuid);
81 __uuidof(var_without_uuid);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
82 __uuidof(var_with_uuid[1]);
83 __uuidof(var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
84 __uuidof(&var_with_uuid[1]);
85 __uuidof(&var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
86
87 __uuidof(0);
88 __uuidof(1);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
89}
Francois Pichet6915c522010-12-27 01:32:00 +000090
91
92template <class T>
93void template_uuid()
94{
95 T expr;
Aaron Ballman3cd6feb2013-07-30 01:31:03 +000096
Francois Pichet6915c522010-12-27 01:32:00 +000097 __uuidof(T);
98 __uuidof(expr);
99}
Francois Pichetdbee3412011-01-18 05:04:39 +0000100
101
David Majnemerd1282ec2013-08-23 05:39:39 +0000102template <class T, const GUID* g = &__uuidof(T)> // expected-note {{template parameter is declared here}}
Francois Picheta97d24f2011-04-28 04:39:50 +0000103class COM_CLASS_TEMPLATE { };
104
David Majnemerd1282ec2013-08-23 05:39:39 +0000105typedef 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}}
Francois Picheta97d24f2011-04-28 04:39:50 +0000106typedef COM_CLASS_TEMPLATE<struct_with_uuid> COM_TYPE_2;
107
Francois Picheta343a412011-04-29 09:08:14 +0000108template <class T, const GUID& g>
109class COM_CLASS_TEMPLATE_REF { };
Francois Pichete275a182012-04-16 04:08:35 +0000110typedef COM_CLASS_TEMPLATE_REF<struct_with_uuid, __uuidof(struct_with_uuid)> COM_TYPE_REF;
Francois Picheta97d24f2011-04-28 04:39:50 +0000111
Francois Pichet20e3c9e2011-05-10 00:08:32 +0000112 struct late_defined_uuid;
113 template<typename T>
114 void test_late_defined_uuid() {
115 __uuidof(late_defined_uuid);
116 }
117 struct __declspec(uuid("000000A0-0000-0000-C000-000000000049")) late_defined_uuid;
118
David Majnemerd1282ec2013-08-23 05:39:39 +0000119COM_CLASS_TEMPLATE_REF<int, __uuidof(struct_with_uuid)> good_template_arg;
120
121COM_CLASS_TEMPLATE<int, __uuidof(struct_with_uuid)> bad_template_arg; // expected-error {{non-type template argument of type 'const _GUID' is not a constant expression}}
Francois Pichetdbee3412011-01-18 05:04:39 +0000122
David Majnemer29b37a02013-09-07 06:59:46 +0000123namespace PR16911 {
124struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
125struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid2;
126
127template <typename T, typename T2>
128struct thing {
129};
130
131struct empty {};
132struct inher : public thing<empty, uuid2> {};
133
134struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
135const struct _GUID *w = &__uuidof(inher); // expected-error{{cannot call operator __uuidof on a type with no GUID}}
136const struct _GUID *x = &__uuidof(thing<uuid, inher>);
137const struct _GUID *y = &__uuidof(thing<uuid2, uuid>); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
138thing<uuid2, uuid> thing_obj = thing<uuid2, uuid>();
139const struct _GUID *z = &__uuidof(thing_obj); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
140}
141
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000142class CtorCall {
Francois Pichetdbee3412011-01-18 05:04:39 +0000143public:
144 CtorCall& operator=(const CtorCall& that);
145
146 int a;
147};
148
149CtorCall& CtorCall::operator=(const CtorCall& that)
150{
151 if (this != &that) {
152 this->CtorCall::~CtorCall();
153 this->CtorCall::CtorCall(that); // expected-warning {{explicit constructor calls are a Microsoft extension}}
154 }
155 return *this;
156}
Francois Pichet4147d302011-03-27 19:41:34 +0000157
158template <class A>
159class C1 {
160public:
161 template <int B>
162 class Iterator {
163 };
164};
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000165
Francois Pichet4147d302011-03-27 19:41:34 +0000166template<class T>
167class C2 {
168 typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
169};
170
171template <class T>
Francois Pichet20e3c9e2011-05-10 00:08:32 +0000172void missing_template_keyword(){
Francois Pichet4147d302011-03-27 19:41:34 +0000173 typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
174}
175
Francois Picheta5d318a2011-04-24 11:24:13 +0000176
177
David Majnemer67ac9982013-09-03 22:36:22 +0000178class AAAA {
179 typedef int D;
180};
Francois Picheta5d318a2011-04-24 11:24:13 +0000181
Richard Smith23756772012-05-14 22:43:34 +0000182template <typename T>
183class SimpleTemplate {};
184
Francois Picheta5d318a2011-04-24 11:24:13 +0000185template <class T>
186void redundant_typename() {
187 typename T t;// expected-warning {{expected a qualified name after 'typename'}}
188 typename AAAA a;// expected-warning {{expected a qualified name after 'typename'}}
Richard Smith23756772012-05-14 22:43:34 +0000189
Francois Picheta5d318a2011-04-24 11:24:13 +0000190 t = 3;
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000191
Richard Smith23756772012-05-14 22:43:34 +0000192 typedef typename T* pointerT;// expected-warning {{expected a qualified name after 'typename'}}
193 typedef typename SimpleTemplate<int> templateT;// expected-warning {{expected a qualified name after 'typename'}}
194
195 pointerT pT = &t;
196 *pT = 4;
197
198 int var;
199 int k = typename var;// expected-error {{expected a qualified name after 'typename'}}
Francois Picheta5d318a2011-04-24 11:24:13 +0000200}
201
David Majnemer67ac9982013-09-03 22:36:22 +0000202template <typename T>
203struct TypenameWrongPlace {
204 typename typedef T::D D;// expected-warning {{expected a qualified name after 'typename'}}
205};
206
207extern TypenameWrongPlace<AAAA> PR16925;
Joao Matos17d35c32012-08-31 22:18:20 +0000208
209__interface MicrosoftInterface;
210__interface MicrosoftInterface {
211 void foo1() = 0;
212 virtual void foo2() = 0;
213};
214
215__interface MicrosoftDerivedInterface : public MicrosoftInterface {
216 void foo1();
217 void foo2() override;
218 void foo3();
219};
220
221void interface_test() {
222 MicrosoftInterface* a;
223 a->foo1();
224 MicrosoftDerivedInterface* b;
225 b->foo2();
226}
227
228__int64 x7 = __int64(0);
Francois Pichet1e862692011-05-06 20:48:22 +0000229
230
Francois Pichet563a6452011-05-25 10:19:49 +0000231namespace If_exists_test {
Francois Pichet1e862692011-05-06 20:48:22 +0000232
233class IF_EXISTS {
234private:
235 typedef int Type;
236};
237
238int __if_exists_test() {
Francois Pichet1e862692011-05-06 20:48:22 +0000239 int b=0;
Francois Pichet1e862692011-05-06 20:48:22 +0000240 __if_exists(IF_EXISTS::Type) {
241 b++;
242 b++;
243 }
Francois Pichet1e862692011-05-06 20:48:22 +0000244 __if_exists(IF_EXISTS::Type_not) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700245 this will not compile.
Francois Pichet1e862692011-05-06 20:48:22 +0000246 }
Francois Pichet1e862692011-05-06 20:48:22 +0000247 __if_not_exists(IF_EXISTS::Type) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700248 this will not compile.
Francois Pichet1e862692011-05-06 20:48:22 +0000249 }
Francois Pichet1e862692011-05-06 20:48:22 +0000250 __if_not_exists(IF_EXISTS::Type_not) {
251 b++;
252 b++;
253 }
Francois Pichetf9860382011-05-07 17:30:27 +0000254}
Francois Pichet1e862692011-05-06 20:48:22 +0000255
Francois Pichetf9860382011-05-07 17:30:27 +0000256
257__if_exists(IF_EXISTS::Type) {
258 int var23;
259}
260
261__if_exists(IF_EXISTS::Type_not) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700262 this will not compile.
Francois Pichetf9860382011-05-07 17:30:27 +0000263}
264
265__if_not_exists(IF_EXISTS::Type) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700266 this will not compile.
Francois Pichetf9860382011-05-07 17:30:27 +0000267}
268
269__if_not_exists(IF_EXISTS::Type_not) {
270 int var244;
Francois Pichet1e862692011-05-06 20:48:22 +0000271}
Francois Picheta23ae3f2011-05-07 17:47:38 +0000272
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000273int __if_exists_init_list() {
274
Joao Matos17d35c32012-08-31 22:18:20 +0000275 int array1[] = {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000276 0,
Joao Matos17d35c32012-08-31 22:18:20 +0000277 __if_exists(IF_EXISTS::Type) {2, }
278 3
279 };
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000280
Joao Matos17d35c32012-08-31 22:18:20 +0000281 int array2[] = {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000282 0,
Stephen Hines651f13c2014-04-23 16:59:28 -0700283 __if_exists(IF_EXISTS::Type_not) { this will not compile }
Joao Matos17d35c32012-08-31 22:18:20 +0000284 3
285 };
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000286
Joao Matos17d35c32012-08-31 22:18:20 +0000287 int array3[] = {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000288 0,
Joao Matos17d35c32012-08-31 22:18:20 +0000289 __if_not_exists(IF_EXISTS::Type_not) {2, }
290 3
291 };
292
293 int array4[] = {
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000294 0,
Stephen Hines651f13c2014-04-23 16:59:28 -0700295 __if_not_exists(IF_EXISTS::Type) { this will not compile }
Joao Matos17d35c32012-08-31 22:18:20 +0000296 3
297 };
Francois Pichet9d24a8b2011-12-12 23:24:39 +0000298
299}
300
301
Francois Pichet563a6452011-05-25 10:19:49 +0000302class IF_EXISTS_CLASS_TEST {
303 __if_exists(IF_EXISTS::Type) {
304 // __if_exists, __if_not_exists can nest
305 __if_not_exists(IF_EXISTS::Type_not) {
306 int var123;
307 }
308 int var23;
309 }
310
311 __if_exists(IF_EXISTS::Type_not) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700312 this will not compile.
Francois Pichet563a6452011-05-25 10:19:49 +0000313 }
314
315 __if_not_exists(IF_EXISTS::Type) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700316 this will not compile.
Francois Pichet563a6452011-05-25 10:19:49 +0000317 }
318
319 __if_not_exists(IF_EXISTS::Type_not) {
320 int var244;
321 }
322};
323
324}
325
326
Francois Picheta23ae3f2011-05-07 17:47:38 +0000327int __identifier(generic) = 3;
Stephen Hines651f13c2014-04-23 16:59:28 -0700328int __identifier(int) = 4;
329struct __identifier(class) { __identifier(class) *__identifier(for); };
330__identifier(class) __identifier(struct) = { &__identifier(struct) };
331
332int __identifier for; // expected-error {{missing '(' after '__identifier'}}
333int __identifier(else} = __identifier(for); // expected-error {{missing ')' after identifier}} expected-note {{to match this '('}}
334#define identifier_weird(x) __identifier(x
335int k = identifier_weird(if)); // expected-error {{use of undeclared identifier 'if'}}
336
337// This is a bit weird, but the alternative tokens aren't keywords, and this
338// behavior matches MSVC. FIXME: Consider supporting this anyway.
339extern int __identifier(and) r; // expected-error {{cannot convert '&&' token to an identifier}}
340
341void f() {
342 __identifier(() // expected-error {{cannot convert '(' token to an identifier}}
343 __identifier(void) // expected-error {{use of undeclared identifier 'void'}}
344 __identifier()) // expected-error {{cannot convert ')' token to an identifier}}
345 // FIXME: We should pick a friendlier display name for this token kind.
346 __identifier(1) // expected-error {{cannot convert <numeric_constant> token to an identifier}}
347 __identifier(+) // expected-error {{cannot convert '+' token to an identifier}}
348 __identifier("foo") // expected-error {{cannot convert <string_literal> token to an identifier}}
349 __identifier(;) // expected-error {{cannot convert ';' token to an identifier}}
350}
Francois Picheta23ae3f2011-05-07 17:47:38 +0000351
Francois Pichet6a247472011-05-11 02:14:46 +0000352class inline_definition_pure_spec {
353 virtual int f() = 0 { return 0; }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
354 virtual int f2() = 0;
355};
356
Reid Kleckner5dbed662013-10-08 22:45:29 +0000357struct pure_virtual_dtor {
358 virtual ~pure_virtual_dtor() = 0;
359};
360pure_virtual_dtor::~pure_virtual_dtor() { }
361
362struct pure_virtual_dtor_inline {
363 virtual ~pure_virtual_dtor_inline() = 0 { }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
364};
365
Francois Pichet20e3c9e2011-05-10 00:08:32 +0000366
367int main () {
368 // Necessary to force instantiation in -fdelayed-template-parsing mode.
Aaron Ballman3cd6feb2013-07-30 01:31:03 +0000369 test_late_defined_uuid<int>();
Francois Pichet20e3c9e2011-05-10 00:08:32 +0000370 redundant_typename<int>();
371 missing_template_keyword<int>();
372}
373
John McCall6c202222012-05-22 21:28:07 +0000374namespace access_protected_PTM {
375 class A {
376 protected:
377 void f(); // expected-note {{must name member using the type of the current context 'access_protected_PTM::B'}}
378 };
Francois Pichetb2d899e2012-04-17 12:35:05 +0000379
John McCall6c202222012-05-22 21:28:07 +0000380 class B : public A{
381 public:
382 void test_access();
383 static void test_access_static();
384 };
Francois Pichetb2d899e2012-04-17 12:35:05 +0000385
John McCall6c202222012-05-22 21:28:07 +0000386 void B::test_access() {
387 &A::f; // expected-error {{'f' is a protected member of 'access_protected_PTM::A'}}
388 }
389
390 void B::test_access_static() {
391 &A::f;
392 }
393}
John McCallc052dbb2012-05-22 21:28:12 +0000394
395namespace Inheritance {
396 class __single_inheritance A;
397 class __multiple_inheritance B;
398 class __virtual_inheritance C;
399}
John McCall76da55d2013-04-16 07:28:30 +0000400
401struct StructWithProperty {
402 __declspec(property) int V0; // expected-error {{expected '(' after 'property'}}
403 __declspec(property()) int V1; // expected-error {{property does not specify a getter or a putter}}
404 __declspec(property(set)) int V2; // expected-error {{putter for property must be specified as 'put', not 'set'}} expected-error {{expected '=' after 'set'}}
405 __declspec(property(ptu)) int V3; // expected-error {{missing 'get=' or 'put='}}
406 __declspec(property(ptu=PutV)) int V4; // expected-error {{expected 'get' or 'put' in property declaration}}
407 __declspec(property(get)) int V5; // expected-error {{expected '=' after 'get'}}
408 __declspec(property(get&)) int V6; // expected-error {{expected '=' after 'get'}}
409 __declspec(property(get=)) int V7; // expected-error {{expected name of accessor method}}
410 __declspec(property(get=GetV)) int V8; // no-warning
411 __declspec(property(get=GetV=)) int V9; // expected-error {{expected ',' or ')' at end of property accessor list}}
412 __declspec(property(get=GetV,)) int V10; // expected-error {{expected 'get' or 'put' in property declaration}}
413 __declspec(property(get=GetV,put=SetV)) int V11; // no-warning
414 __declspec(property(get=GetV,put=SetV,get=GetV)) int V12; // expected-error {{property declaration specifies 'get' accessor twice}}
415
416 int GetV() { return 123; }
417 void SetV(int v) {}
418};
419void TestProperty() {
420 StructWithProperty sp;
421 sp.V8;
422 sp.V8 = 0; // expected-error {{no setter defined for property 'V8'}}
423 int i = sp.V11;
424 sp.V11 = i++;
425 sp.V11 += 8;
426 sp.V11++;
427 ++sp.V11;
428}