blob: ef873d6430604d31d453c084d8415bf0e5bbade2 [file] [log] [blame]
Francois Picheta39371c2012-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 Lewycky8d3a1782010-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 Ballman3bf758c2013-07-30 01:31:03 +00007[returnvalue:SA_Post( attr=1)]
Nick Lewycky8d3a1782010-10-12 16:46:35 +00008int foo1([SA_Post(attr=1)] void *param);
9
10namespace {
Aaron Ballman3bf758c2013-07-30 01:31:03 +000011 [returnvalue:SA_Post(attr=1)]
Nick Lewycky8d3a1782010-10-12 16:46:35 +000012 int foo2([SA_Post(attr=1)] void *param);
13}
14
15class T {
Aaron Ballman3bf758c2013-07-30 01:31:03 +000016 [returnvalue:SA_Post(attr=1)]
Nick Lewycky8d3a1782010-10-12 16:46:35 +000017 int foo3([SA_Post(attr=1)] void *param);
18};
19
20extern "C" {
Aaron Ballman3bf758c2013-07-30 01:31:03 +000021 [returnvalue:SA_Post(attr=1)]
Nick Lewycky8d3a1782010-10-12 16:46:35 +000022 int foo5([SA_Post(attr=1)] void *param);
23}
24
Francois Pichet79f3a872011-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 Lewycky8d3a1782010-10-12 16:46:35 +000033
Francois Pichet9dddd402010-12-20 03:51:03 +000034void uuidof_test1()
Aaron Ballman3bf758c2013-07-30 01:31:03 +000035{
Francois Pichet9dddd402010-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 Ballman3bf758c2013-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 Pichet9dddd402010-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
53
54
55struct __declspec(uuid("000000A0-0000-0000-C000-000000000046"))
56struct_with_uuid { };
57struct struct_without_uuid { };
58
Francois Pichetb7577652010-12-27 01:32:00 +000059struct __declspec(uuid("000000A0-0000-0000-C000-000000000049"))
60struct_with_uuid2;
61
Aaron Ballman3bf758c2013-07-30 01:31:03 +000062struct
Francois Pichetb7577652010-12-27 01:32:00 +000063struct_with_uuid2 {} ;
Francois Pichet9dddd402010-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 Pichetb7577652010-12-27 01:32:00 +000071 __uuidof(struct_with_uuid2);
Francois Pichet9dddd402010-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}}
75
76 __uuidof(var_with_uuid);
77 __uuidof(var_without_uuid);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
78 __uuidof(var_with_uuid[1]);
79 __uuidof(var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
80 __uuidof(&var_with_uuid[1]);
81 __uuidof(&var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
82
83 __uuidof(0);
84 __uuidof(1);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
85}
Francois Pichetb7577652010-12-27 01:32:00 +000086
87
88template <class T>
89void template_uuid()
90{
91 T expr;
Aaron Ballman3bf758c2013-07-30 01:31:03 +000092
Francois Pichetb7577652010-12-27 01:32:00 +000093 __uuidof(T);
94 __uuidof(expr);
95}
Francois Pichet64225792011-01-18 05:04:39 +000096
97
David Majnemer61c39a12013-08-23 05:39:39 +000098template <class T, const GUID* g = &__uuidof(T)> // expected-note {{template parameter is declared here}}
Francois Picheta1c13522011-04-28 04:39:50 +000099class COM_CLASS_TEMPLATE { };
100
David Majnemer61c39a12013-08-23 05:39:39 +0000101typedef 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 Picheta1c13522011-04-28 04:39:50 +0000102typedef COM_CLASS_TEMPLATE<struct_with_uuid> COM_TYPE_2;
103
Francois Pichetc8679752011-04-29 09:08:14 +0000104template <class T, const GUID& g>
105class COM_CLASS_TEMPLATE_REF { };
Francois Pichet0066db92012-04-16 04:08:35 +0000106typedef COM_CLASS_TEMPLATE_REF<struct_with_uuid, __uuidof(struct_with_uuid)> COM_TYPE_REF;
Francois Picheta1c13522011-04-28 04:39:50 +0000107
Francois Pichet2f019ed2011-05-10 00:08:32 +0000108 struct late_defined_uuid;
109 template<typename T>
110 void test_late_defined_uuid() {
111 __uuidof(late_defined_uuid);
112 }
113 struct __declspec(uuid("000000A0-0000-0000-C000-000000000049")) late_defined_uuid;
114
David Majnemer61c39a12013-08-23 05:39:39 +0000115COM_CLASS_TEMPLATE_REF<int, __uuidof(struct_with_uuid)> good_template_arg;
116
117COM_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 Pichet64225792011-01-18 05:04:39 +0000118
David Majnemer59c0ec22013-09-07 06:59:46 +0000119namespace PR16911 {
120struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
121struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid2;
122
123template <typename T, typename T2>
124struct thing {
125};
126
127struct empty {};
128struct inher : public thing<empty, uuid2> {};
129
130struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
131const struct _GUID *w = &__uuidof(inher); // expected-error{{cannot call operator __uuidof on a type with no GUID}}
132const struct _GUID *x = &__uuidof(thing<uuid, inher>);
133const struct _GUID *y = &__uuidof(thing<uuid2, uuid>); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
134thing<uuid2, uuid> thing_obj = thing<uuid2, uuid>();
135const struct _GUID *z = &__uuidof(thing_obj); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
136}
137
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000138class CtorCall {
Francois Pichet64225792011-01-18 05:04:39 +0000139public:
140 CtorCall& operator=(const CtorCall& that);
141
142 int a;
143};
144
145CtorCall& CtorCall::operator=(const CtorCall& that)
146{
147 if (this != &that) {
148 this->CtorCall::~CtorCall();
149 this->CtorCall::CtorCall(that); // expected-warning {{explicit constructor calls are a Microsoft extension}}
150 }
151 return *this;
152}
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000153
154template <class A>
155class C1 {
156public:
157 template <int B>
158 class Iterator {
159 };
160};
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000161
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000162template<class T>
163class C2 {
164 typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
165};
166
167template <class T>
Francois Pichet2f019ed2011-05-10 00:08:32 +0000168void missing_template_keyword(){
Francois Pichet4e7a2c02011-03-27 19:41:34 +0000169 typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
170}
171
Francois Pichetdd876122011-04-24 11:24:13 +0000172
173
David Majnemerad018512013-09-03 22:36:22 +0000174class AAAA {
175 typedef int D;
176};
Francois Pichetdd876122011-04-24 11:24:13 +0000177
Richard Smithb71e7322012-05-14 22:43:34 +0000178template <typename T>
179class SimpleTemplate {};
180
Francois Pichetdd876122011-04-24 11:24:13 +0000181template <class T>
182void redundant_typename() {
183 typename T t;// expected-warning {{expected a qualified name after 'typename'}}
184 typename AAAA a;// expected-warning {{expected a qualified name after 'typename'}}
Richard Smithb71e7322012-05-14 22:43:34 +0000185
Francois Pichetdd876122011-04-24 11:24:13 +0000186 t = 3;
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000187
Richard Smithb71e7322012-05-14 22:43:34 +0000188 typedef typename T* pointerT;// expected-warning {{expected a qualified name after 'typename'}}
189 typedef typename SimpleTemplate<int> templateT;// expected-warning {{expected a qualified name after 'typename'}}
190
191 pointerT pT = &t;
192 *pT = 4;
193
194 int var;
195 int k = typename var;// expected-error {{expected a qualified name after 'typename'}}
Francois Pichetdd876122011-04-24 11:24:13 +0000196}
197
David Majnemerad018512013-09-03 22:36:22 +0000198template <typename T>
199struct TypenameWrongPlace {
200 typename typedef T::D D;// expected-warning {{expected a qualified name after 'typename'}}
201};
202
203extern TypenameWrongPlace<AAAA> PR16925;
Joao Matose9a3ed42012-08-31 22:18:20 +0000204
205__interface MicrosoftInterface;
206__interface MicrosoftInterface {
207 void foo1() = 0;
208 virtual void foo2() = 0;
209};
210
211__interface MicrosoftDerivedInterface : public MicrosoftInterface {
212 void foo1();
213 void foo2() override;
214 void foo3();
215};
216
217void interface_test() {
218 MicrosoftInterface* a;
219 a->foo1();
220 MicrosoftDerivedInterface* b;
221 b->foo2();
222}
223
224__int64 x7 = __int64(0);
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000225
226
Francois Pichet8f981d52011-05-25 10:19:49 +0000227namespace If_exists_test {
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000228
229class IF_EXISTS {
230private:
231 typedef int Type;
232};
233
234int __if_exists_test() {
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000235 int b=0;
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000236 __if_exists(IF_EXISTS::Type) {
237 b++;
238 b++;
239 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000240 __if_exists(IF_EXISTS::Type_not) {
241 this wont compile.
242 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000243 __if_not_exists(IF_EXISTS::Type) {
244 this wont compile.
245 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000246 __if_not_exists(IF_EXISTS::Type_not) {
247 b++;
248 b++;
249 }
Francois Picheta5b3fcb2011-05-07 17:30:27 +0000250}
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000251
Francois Picheta5b3fcb2011-05-07 17:30:27 +0000252
253__if_exists(IF_EXISTS::Type) {
254 int var23;
255}
256
257__if_exists(IF_EXISTS::Type_not) {
258 this wont compile.
259}
260
261__if_not_exists(IF_EXISTS::Type) {
262 this wont compile.
263}
264
265__if_not_exists(IF_EXISTS::Type_not) {
266 int var244;
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000267}
Francois Pichet61d818c2011-05-07 17:47:38 +0000268
Francois Pichet02513162011-12-12 23:24:39 +0000269int __if_exists_init_list() {
270
Joao Matose9a3ed42012-08-31 22:18:20 +0000271 int array1[] = {
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000272 0,
Joao Matose9a3ed42012-08-31 22:18:20 +0000273 __if_exists(IF_EXISTS::Type) {2, }
274 3
275 };
Francois Pichet02513162011-12-12 23:24:39 +0000276
Joao Matose9a3ed42012-08-31 22:18:20 +0000277 int array2[] = {
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000278 0,
Joao Matose9a3ed42012-08-31 22:18:20 +0000279 __if_exists(IF_EXISTS::Type_not) { this wont compile }
280 3
281 };
Francois Pichet02513162011-12-12 23:24:39 +0000282
Joao Matose9a3ed42012-08-31 22:18:20 +0000283 int array3[] = {
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000284 0,
Joao Matose9a3ed42012-08-31 22:18:20 +0000285 __if_not_exists(IF_EXISTS::Type_not) {2, }
286 3
287 };
288
289 int array4[] = {
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000290 0,
Joao Matose9a3ed42012-08-31 22:18:20 +0000291 __if_not_exists(IF_EXISTS::Type) { this wont compile }
292 3
293 };
Francois Pichet02513162011-12-12 23:24:39 +0000294
295}
296
297
Francois Pichet8f981d52011-05-25 10:19:49 +0000298class IF_EXISTS_CLASS_TEST {
299 __if_exists(IF_EXISTS::Type) {
300 // __if_exists, __if_not_exists can nest
301 __if_not_exists(IF_EXISTS::Type_not) {
302 int var123;
303 }
304 int var23;
305 }
306
307 __if_exists(IF_EXISTS::Type_not) {
308 this wont compile.
309 }
310
311 __if_not_exists(IF_EXISTS::Type) {
312 this wont compile.
313 }
314
315 __if_not_exists(IF_EXISTS::Type_not) {
316 int var244;
317 }
318};
319
320}
321
322
Francois Pichet61d818c2011-05-07 17:47:38 +0000323int __identifier(generic) = 3;
324
Francois Pichet3abc9b82011-05-11 02:14:46 +0000325class inline_definition_pure_spec {
326 virtual int f() = 0 { return 0; }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
327 virtual int f2() = 0;
328};
329
Francois Pichet2f019ed2011-05-10 00:08:32 +0000330
331int main () {
332 // Necessary to force instantiation in -fdelayed-template-parsing mode.
Aaron Ballman3bf758c2013-07-30 01:31:03 +0000333 test_late_defined_uuid<int>();
Francois Pichet2f019ed2011-05-10 00:08:32 +0000334 redundant_typename<int>();
335 missing_template_keyword<int>();
336}
337
John McCallfa6cf4c2012-05-22 21:28:07 +0000338namespace access_protected_PTM {
339 class A {
340 protected:
341 void f(); // expected-note {{must name member using the type of the current context 'access_protected_PTM::B'}}
342 };
Francois Picheta39371c2012-04-17 12:35:05 +0000343
John McCallfa6cf4c2012-05-22 21:28:07 +0000344 class B : public A{
345 public:
346 void test_access();
347 static void test_access_static();
348 };
Francois Picheta39371c2012-04-17 12:35:05 +0000349
John McCallfa6cf4c2012-05-22 21:28:07 +0000350 void B::test_access() {
351 &A::f; // expected-error {{'f' is a protected member of 'access_protected_PTM::A'}}
352 }
353
354 void B::test_access_static() {
355 &A::f;
356 }
357}
John McCall8d32c052012-05-22 21:28:12 +0000358
359namespace Inheritance {
360 class __single_inheritance A;
361 class __multiple_inheritance B;
362 class __virtual_inheritance C;
363}
John McCall5e77d762013-04-16 07:28:30 +0000364
365struct StructWithProperty {
366 __declspec(property) int V0; // expected-error {{expected '(' after 'property'}}
367 __declspec(property()) int V1; // expected-error {{property does not specify a getter or a putter}}
368 __declspec(property(set)) int V2; // expected-error {{putter for property must be specified as 'put', not 'set'}} expected-error {{expected '=' after 'set'}}
369 __declspec(property(ptu)) int V3; // expected-error {{missing 'get=' or 'put='}}
370 __declspec(property(ptu=PutV)) int V4; // expected-error {{expected 'get' or 'put' in property declaration}}
371 __declspec(property(get)) int V5; // expected-error {{expected '=' after 'get'}}
372 __declspec(property(get&)) int V6; // expected-error {{expected '=' after 'get'}}
373 __declspec(property(get=)) int V7; // expected-error {{expected name of accessor method}}
374 __declspec(property(get=GetV)) int V8; // no-warning
375 __declspec(property(get=GetV=)) int V9; // expected-error {{expected ',' or ')' at end of property accessor list}}
376 __declspec(property(get=GetV,)) int V10; // expected-error {{expected 'get' or 'put' in property declaration}}
377 __declspec(property(get=GetV,put=SetV)) int V11; // no-warning
378 __declspec(property(get=GetV,put=SetV,get=GetV)) int V12; // expected-error {{property declaration specifies 'get' accessor twice}}
379
380 int GetV() { return 123; }
381 void SetV(int v) {}
382};
383void TestProperty() {
384 StructWithProperty sp;
385 sp.V8;
386 sp.V8 = 0; // expected-error {{no setter defined for property 'V8'}}
387 int i = sp.V11;
388 sp.V11 = i++;
389 sp.V11 += 8;
390 sp.V11++;
391 ++sp.V11;
392}