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 
34 void uuidof_test1()
35 {
36   __uuidof(0);  // expected-error {{you need to include <guiddef.h> before using the '__uuidof' operator}}
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 classes}}
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;
64 
65 struct
66 struct_with_uuid2 {} ;
67 
68 int uuid_sema_test()
69 {
70    struct_with_uuid var_with_uuid[1];
71    struct_without_uuid var_without_uuid[1];
72 
73    __uuidof(struct_with_uuid);
74    __uuidof(struct_with_uuid2);
75    __uuidof(struct_with_uuid3);
76    __uuidof(struct_without_uuid); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
77    __uuidof(struct_with_uuid*);
78    __uuidof(struct_without_uuid*); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
79    __uuidof(struct_with_uuid[1]);
80    __uuidof(struct_with_uuid*[1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
81    __uuidof(const struct_with_uuid[1][1]);
82    __uuidof(const struct_with_uuid*[1][1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
83 
84    __uuidof(var_with_uuid);
85    __uuidof(var_without_uuid);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
86    __uuidof(var_with_uuid[1]);
87    __uuidof(var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
88    __uuidof(&var_with_uuid[1]);
89    __uuidof(&var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
90 
91    __uuidof(0);
92    __uuidof(1);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
93 }
94 
95 
96 template <class T>
97 void template_uuid()
98 {
99    T expr;
100 
101    __uuidof(T);
102    __uuidof(expr);
103 }
104 
105 
106 template <class T, const GUID* g = &__uuidof(T)> // expected-note {{template parameter is declared here}}
107 class COM_CLASS_TEMPLATE  { };
108 
109 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}}
110 typedef COM_CLASS_TEMPLATE<struct_with_uuid> COM_TYPE_2;
111 
112 template <class T, const GUID& g>
113 class COM_CLASS_TEMPLATE_REF  { };
114 typedef COM_CLASS_TEMPLATE_REF<struct_with_uuid, __uuidof(struct_with_uuid)> COM_TYPE_REF;
115 
116   struct late_defined_uuid;
117   template<typename T>
118   void test_late_defined_uuid() {
119     __uuidof(late_defined_uuid);
120   }
121   struct __declspec(uuid("000000A0-0000-0000-C000-000000000049")) late_defined_uuid;
122 
123 COM_CLASS_TEMPLATE_REF<int, __uuidof(struct_with_uuid)> good_template_arg;
124 
125 COM_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}}
126 
127 namespace PR16911 {
128 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
129 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid2;
130 
131 template <typename T, typename T2>
132 struct thing {
133 };
134 
135 struct empty {};
136 struct inher : public thing<empty, uuid2> {};
137 
138 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
139 const struct _GUID *w = &__uuidof(inher); // expected-error{{cannot call operator __uuidof on a type with no GUID}}
140 const struct _GUID *x = &__uuidof(thing<uuid, inher>);
141 const struct _GUID *y = &__uuidof(thing<uuid2, uuid>); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
142 thing<uuid2, uuid> thing_obj = thing<uuid2, uuid>();
143 const struct _GUID *z = &__uuidof(thing_obj); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
144 }
145 
146 class CtorCall {
147 public:
148   CtorCall& operator=(const CtorCall& that);
149 
150   int a;
151 };
152 
153 CtorCall& CtorCall::operator=(const CtorCall& that)
154 {
155     if (this != &that) {
156         this->CtorCall::~CtorCall();
157         this->CtorCall::CtorCall(that); // expected-warning {{explicit constructor calls are a Microsoft extension}}
158     }
159     return *this;
160 }
161 
162 template <class A>
163 class C1 {
164 public:
165   template <int B>
166   class Iterator {
167   };
168 };
169 
170 template<class T>
171 class C2  {
172   typename C1<T>:: /*template*/  Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
173 };
174 
175 template <class T>
176 void missing_template_keyword(){
177   typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
178 }
179 
180 
181 
182 class AAAA {
183    typedef int D;
184 };
185 
186 template <typename T>
187 class SimpleTemplate {};
188 
189 template <class T>
190 void redundant_typename() {
191    typename T t;// expected-warning {{expected a qualified name after 'typename'}}
192    typename AAAA a;// expected-warning {{expected a qualified name after 'typename'}}
193 
194    t = 3;
195 
196    typedef typename T* pointerT;// expected-warning {{expected a qualified name after 'typename'}}
197    typedef typename SimpleTemplate<int> templateT;// expected-warning {{expected a qualified name after 'typename'}}
198 
199    pointerT pT = &t;
200    *pT = 4;
201 
202    int var;
203    int k = typename var;// expected-error {{expected a qualified name after 'typename'}}
204 }
205 
206 template <typename T>
207 struct TypenameWrongPlace {
208   typename typedef T::D D;// expected-warning {{expected a qualified name after 'typename'}}
209 };
210 
211 extern TypenameWrongPlace<AAAA> PR16925;
212 
213 __interface MicrosoftInterface;
214 __interface MicrosoftInterface {
215    void foo1() = 0; // expected-note {{overridden virtual function is here}}
216    virtual void foo2() = 0;
217 };
218 
219 __interface MicrosoftDerivedInterface : public MicrosoftInterface {
220   void foo1(); // expected-warning {{'foo1' overrides a member function but is not marked 'override'}}
221   void foo2() override;
222   void foo3();
223 };
224 
225 void interface_test() {
226   MicrosoftInterface* a;
227   a->foo1();
228   MicrosoftDerivedInterface* b;
229   b->foo2();
230 }
231 
232 __int64 x7 = __int64(0);
233 _int64 x8 = _int64(0);
234 static_assert(sizeof(_int64) == 8, "");
235 static_assert(sizeof(_int32) == 4, "");
236 static_assert(sizeof(_int16) == 2, "");
237 static_assert(sizeof(_int8) == 1, "");
238 
239 int __identifier(generic) = 3;
240 int __identifier(int) = 4;
241 struct __identifier(class) { __identifier(class) *__identifier(for); };
242 __identifier(class) __identifier(struct) = { &__identifier(struct) };
243 
244 int __identifier for; // expected-error {{missing '(' after '__identifier'}}
245 int __identifier(else} = __identifier(for); // expected-error {{missing ')' after identifier}} expected-note {{to match this '('}}
246 #define identifier_weird(x) __identifier(x
247 int k = identifier_weird(if)); // expected-error {{use of undeclared identifier 'if'}}
248 
249 // This is a bit weird, but the alternative tokens aren't keywords, and this
250 // behavior matches MSVC. FIXME: Consider supporting this anyway.
251 extern int __identifier(and) r; // expected-error {{cannot convert '&&' token to an identifier}}
252 
253 void f() {
254   __identifier(() // expected-error {{cannot convert '(' token to an identifier}}
255   __identifier(void) // expected-error {{use of undeclared identifier 'void'}}
256   __identifier()) // expected-error {{cannot convert ')' token to an identifier}}
257   // FIXME: We should pick a friendlier display name for this token kind.
258   __identifier(1) // expected-error {{cannot convert <numeric_constant> token to an identifier}}
259   __identifier(+) // expected-error {{cannot convert '+' token to an identifier}}
260   __identifier("foo") // expected-error {{cannot convert <string_literal> token to an identifier}}
261   __identifier(;) // expected-error {{cannot convert ';' token to an identifier}}
262 }
263 
264 class inline_definition_pure_spec {
265    virtual int f() = 0 { return 0; }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
266    virtual int f2() = 0;
267 };
268 
269 struct pure_virtual_dtor {
270   virtual ~pure_virtual_dtor() = 0;
271 };
272 pure_virtual_dtor::~pure_virtual_dtor() { }
273 
274 struct pure_virtual_dtor_inline {
275   virtual ~pure_virtual_dtor_inline() = 0 { }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
276 };
277 
278 
279 int main () {
280   // Necessary to force instantiation in -fdelayed-template-parsing mode.
281   test_late_defined_uuid<int>();
282   redundant_typename<int>();
283   missing_template_keyword<int>();
284 }
285 
286 namespace access_protected_PTM {
287   class A {
288   protected:
289     void f(); // expected-note {{must name member using the type of the current context 'access_protected_PTM::B'}}
290   };
291 
292   class B : public A{
293   public:
294     void test_access();
295     static void test_access_static();
296   };
297 
298   void B::test_access() {
299     &A::f; // expected-error {{'f' is a protected member of 'access_protected_PTM::A'}}
300   }
301 
302   void B::test_access_static() {
303     &A::f;
304   }
305 }
306 
307 namespace Inheritance {
308   class __single_inheritance A;
309   class __multiple_inheritance B;
310   class __virtual_inheritance C;
311 }
312 
313 struct StructWithProperty {
314   __declspec(property) int V0; // expected-error {{expected '(' after 'property'}}
315   __declspec(property()) int V1; // expected-error {{property does not specify a getter or a putter}}
316   __declspec(property(set)) int V2; // expected-error {{putter for property must be specified as 'put', not 'set'}} expected-error {{expected '=' after 'set'}}
317   __declspec(property(ptu)) int V3; // expected-error {{missing 'get=' or 'put='}}
318   __declspec(property(ptu=PutV)) int V4; // expected-error {{expected 'get' or 'put' in property declaration}}
319   __declspec(property(get)) int V5; // expected-error {{expected '=' after 'get'}}
320   __declspec(property(get&)) int V6; // expected-error {{expected '=' after 'get'}}
321   __declspec(property(get=)) int V7; // expected-error {{expected name of accessor method}}
322   __declspec(property(get=GetV)) int V8; // no-warning
323   __declspec(property(get=GetV=)) int V9; // expected-error {{expected ',' or ')' at end of property accessor list}}
324   __declspec(property(get=GetV,)) int V10; // expected-error {{expected 'get' or 'put' in property declaration}}
325   __declspec(property(get=GetV,put=SetV)) int V11; // no-warning
326   __declspec(property(get=GetV,put=SetV,get=GetV)) int V12; // expected-error {{property declaration specifies 'get' accessor twice}}
327   __declspec(property(get=GetV)) int V13 = 3; // expected-error {{property declaration cannot have an in-class initializer}}
328 
329   int GetV() { return 123; }
330   void SetV(int v) {}
331 };
332 void TestProperty() {
333   StructWithProperty sp;
334   sp.V8;
335   sp.V8 = 0; // expected-error {{no setter defined for property 'V8'}}
336   int i = sp.V11;
337   sp.V11 = i++;
338   sp.V11 += 8;
339   sp.V11++;
340   ++sp.V11;
341 }
342 
343 //expected-warning@+1 {{C++ operator 'and' (aka '&&') used as a macro name}}
344 #define and foo
345 
346 struct __declspec(uuid("00000000-0000-0000-C000-000000000046")) __declspec(novtable) IUnknown {};
347 
348 typedef bool (__stdcall __stdcall *blarg)(int);
349 
350 void local_callconv() {
351   bool (__stdcall *p)(int);
352 }
353 
354 struct S7 {
355 	int foo() { return 12; }
356 	__declspec(property(get=foo) deprecated) int t; // expected-note {{'t' has been explicitly marked deprecated here}}
357 };
358 
359 // Technically, this is legal (though it does nothing)
360 __declspec() void quux( void ) {
361   struct S7 s;
362   int i = s.t;	// expected-warning {{'t' is deprecated}}
363 }
364 
365 void *_alloca(int);
366 
367 void foo(void) {
368   __declspec(align(16)) int *buffer = (int *)_alloca(9);
369 }
370 
371 template <int *>
372 struct NullptrArg {};
373 NullptrArg<nullptr> a;
374 
375 // Ignored type qualifiers after comma in declarator lists
376 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}}
377 typedef void(*ignored_quals_dummy2)(), __fastcall ignored_quals2; // expected-warning {{qualifiers after comma in declarator list are ignored}}
378 typedef void(*ignored_quals_dummy3)(), __stdcall ignored_quals3; // expected-warning {{qualifiers after comma in declarator list are ignored}}
379 typedef void(*ignored_quals_dummy4)(), __thiscall ignored_quals4; // expected-warning {{qualifiers after comma in declarator list are ignored}}
380 typedef void(*ignored_quals_dummy5)(), __cdecl ignored_quals5; // expected-warning {{qualifiers after comma in declarator list are ignored}}
381 typedef void(*ignored_quals_dummy6)(), __vectorcall ignored_quals6; // expected-warning {{qualifiers after comma in declarator list are ignored}}
382 
383 namespace {
384 bool f(int);
385 template <typename T>
386 struct A {
387   constexpr A(T t) {
388     __assume(f(t)); // expected-warning{{the argument to '__assume' has side effects that will be discarded}}
389   }
390   constexpr bool g() { return false; }
391 };
392 constexpr A<int> h() {
393   A<int> b(0); // expected-note {{in instantiation of member function}}
394   return b;
395 }
396 static_assert(h().g() == false, "");
397 }
398 
399 namespace {
400 __declspec(align(16)) struct align_before_key1 {};
401 __declspec(align(16)) struct align_before_key2 {} align_before_key2_var;
402 __declspec(align(16)) struct align_before_key3 {} *align_before_key3_var;
403 static_assert(__alignof(struct align_before_key1) == 16, "");
404 static_assert(__alignof(struct align_before_key2) == 16, "");
405 static_assert(__alignof(struct align_before_key3) == 16, "");
406 }
407 
408 namespace PR24027 {
409 struct S {
410   template <typename T>
411   S(T);
412 } f([] {});
413 }
414