MicrosoftExtensions.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // RUN: %clang_cc1 %s -triple i386-mingw32 -std=c++14 -fsyntax-only -Wno-unused-getter-return-value -Wno-unused-value -Wmicrosoft -verify -fms-extensions -fms-compatibility -fdelayed-template-parsing
  2. /* Microsoft attribute tests */
  3. [repeatable][source_annotation_attribute( Parameter|ReturnValue )]
  4. struct SA_Post{ SA_Post(); int attr; };
  5. [returnvalue:SA_Post( attr=1)]
  6. int foo1([SA_Post(attr=1)] void *param);
  7. namespace {
  8. [returnvalue:SA_Post(attr=1)]
  9. int foo2([SA_Post(attr=1)] void *param);
  10. }
  11. class T {
  12. [returnvalue:SA_Post(attr=1)]
  13. int foo3([SA_Post(attr=1)] void *param);
  14. };
  15. extern "C" {
  16. [returnvalue:SA_Post(attr=1)]
  17. int foo5([SA_Post(attr=1)] void *param);
  18. }
  19. class class_attr {
  20. public:
  21. class_attr([SA_Pre(Null=SA_No,NullTerminated=SA_Yes)] int a)
  22. {
  23. }
  24. };
  25. void uuidof_test1()
  26. {
  27. __uuidof(0); // expected-error {{you need to include <guiddef.h> before using the '__uuidof' operator}}
  28. }
  29. typedef struct _GUID
  30. {
  31. unsigned long Data1;
  32. unsigned short Data2;
  33. unsigned short Data3;
  34. unsigned char Data4[8];
  35. } GUID;
  36. struct __declspec(uuid(L"00000000-0000-0000-1234-000000000047")) uuid_attr_bad1 { };// expected-error {{'uuid' attribute requires a string}}
  37. struct __declspec(uuid(3)) uuid_attr_bad2 { };// expected-error {{'uuid' attribute requires a string}}
  38. struct __declspec(uuid("0000000-0000-0000-1234-0000500000047")) uuid_attr_bad3 { };// expected-error {{uuid attribute contains a malformed GUID}}
  39. struct __declspec(uuid("0000000-0000-0000-Z234-000000000047")) uuid_attr_bad4 { };// expected-error {{uuid attribute contains a malformed GUID}}
  40. struct __declspec(uuid("000000000000-0000-1234-000000000047")) uuid_attr_bad5 { };// expected-error {{uuid attribute contains a malformed GUID}}
  41. __declspec(uuid("000000A0-0000-0000-C000-000000000046")) int i; // expected-warning {{'uuid' attribute only applies to classes}}
  42. struct __declspec(uuid("000000A0-0000-0000-C000-000000000046"))
  43. struct_with_uuid { };
  44. struct struct_without_uuid { };
  45. struct __declspec(uuid("000000A0-0000-0000-C000-000000000049"))
  46. struct_with_uuid2;
  47. struct
  48. struct_with_uuid2 {} ;
  49. int uuid_sema_test()
  50. {
  51. struct_with_uuid var_with_uuid[1];
  52. struct_without_uuid var_without_uuid[1];
  53. __uuidof(struct_with_uuid);
  54. __uuidof(struct_with_uuid2);
  55. __uuidof(struct_without_uuid); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
  56. __uuidof(struct_with_uuid*);
  57. __uuidof(struct_without_uuid*); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
  58. __uuidof(struct_with_uuid[1]);
  59. __uuidof(struct_with_uuid*[1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
  60. __uuidof(const struct_with_uuid[1][1]);
  61. __uuidof(const struct_with_uuid*[1][1]); // expected-error {{cannot call operator __uuidof on a type with no GUID}}
  62. __uuidof(var_with_uuid);
  63. __uuidof(var_without_uuid);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
  64. __uuidof(var_with_uuid[1]);
  65. __uuidof(var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
  66. __uuidof(&var_with_uuid[1]);
  67. __uuidof(&var_without_uuid[1]);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
  68. __uuidof(0);
  69. __uuidof(1);// expected-error {{cannot call operator __uuidof on a type with no GUID}}
  70. }
  71. template <class T>
  72. void template_uuid()
  73. {
  74. T expr;
  75. __uuidof(T);
  76. __uuidof(expr);
  77. }
  78. template <class T, const GUID* g = &__uuidof(T)> // expected-note {{template parameter is declared here}}
  79. class COM_CLASS_TEMPLATE { };
  80. 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}}
  81. typedef COM_CLASS_TEMPLATE<struct_with_uuid> COM_TYPE_2;
  82. template <class T, const GUID& g>
  83. class COM_CLASS_TEMPLATE_REF { };
  84. typedef COM_CLASS_TEMPLATE_REF<struct_with_uuid, __uuidof(struct_with_uuid)> COM_TYPE_REF;
  85. struct late_defined_uuid;
  86. template<typename T>
  87. void test_late_defined_uuid() {
  88. __uuidof(late_defined_uuid);
  89. }
  90. struct __declspec(uuid("000000A0-0000-0000-C000-000000000049")) late_defined_uuid;
  91. COM_CLASS_TEMPLATE_REF<int, __uuidof(struct_with_uuid)> good_template_arg;
  92. 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}}
  93. namespace PR16911 {
  94. struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
  95. struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid2;
  96. template <typename T, typename T2>
  97. struct thing {
  98. };
  99. struct empty {};
  100. struct inher : public thing<empty, uuid2> {};
  101. struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
  102. const struct _GUID *w = &__uuidof(inher); // expected-error{{cannot call operator __uuidof on a type with no GUID}}
  103. const struct _GUID *x = &__uuidof(thing<uuid, inher>);
  104. const struct _GUID *y = &__uuidof(thing<uuid2, uuid>); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
  105. thing<uuid2, uuid> thing_obj = thing<uuid2, uuid>();
  106. const struct _GUID *z = &__uuidof(thing_obj); // expected-error{{cannot call operator __uuidof on a type with multiple GUIDs}}
  107. }
  108. class CtorCall {
  109. public:
  110. CtorCall& operator=(const CtorCall& that);
  111. int a;
  112. };
  113. CtorCall& CtorCall::operator=(const CtorCall& that)
  114. {
  115. if (this != &that) {
  116. this->CtorCall::~CtorCall();
  117. this->CtorCall::CtorCall(that); // expected-warning {{explicit constructor calls are a Microsoft extension}}
  118. }
  119. return *this;
  120. }
  121. template <class A>
  122. class C1 {
  123. public:
  124. template <int B>
  125. class Iterator {
  126. };
  127. };
  128. template<class T>
  129. class C2 {
  130. typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
  131. };
  132. template <class T>
  133. void missing_template_keyword(){
  134. typename C1<T>:: /*template*/ Iterator<0> Mypos; // expected-warning {{use 'template' keyword to treat 'Iterator' as a dependent template name}}
  135. }
  136. class AAAA {
  137. typedef int D;
  138. };
  139. template <typename T>
  140. class SimpleTemplate {};
  141. template <class T>
  142. void redundant_typename() {
  143. typename T t;// expected-warning {{expected a qualified name after 'typename'}}
  144. typename AAAA a;// expected-warning {{expected a qualified name after 'typename'}}
  145. t = 3;
  146. typedef typename T* pointerT;// expected-warning {{expected a qualified name after 'typename'}}
  147. typedef typename SimpleTemplate<int> templateT;// expected-warning {{expected a qualified name after 'typename'}}
  148. pointerT pT = &t;
  149. *pT = 4;
  150. int var;
  151. int k = typename var;// expected-error {{expected a qualified name after 'typename'}}
  152. }
  153. template <typename T>
  154. struct TypenameWrongPlace {
  155. typename typedef T::D D;// expected-warning {{expected a qualified name after 'typename'}}
  156. };
  157. extern TypenameWrongPlace<AAAA> PR16925;
  158. __interface MicrosoftInterface;
  159. __interface MicrosoftInterface {
  160. void foo1() = 0; // expected-note {{overridden virtual function is here}}
  161. virtual void foo2() = 0;
  162. };
  163. __interface MicrosoftDerivedInterface : public MicrosoftInterface {
  164. void foo1(); // expected-warning {{'foo1' overrides a member function but is not marked 'override'}}
  165. void foo2() override;
  166. void foo3();
  167. };
  168. void interface_test() {
  169. MicrosoftInterface* a;
  170. a->foo1();
  171. MicrosoftDerivedInterface* b;
  172. b->foo2();
  173. }
  174. __int64 x7 = __int64(0);
  175. _int64 x8 = _int64(0);
  176. static_assert(sizeof(_int64) == 8, "");
  177. static_assert(sizeof(_int32) == 4, "");
  178. static_assert(sizeof(_int16) == 2, "");
  179. static_assert(sizeof(_int8) == 1, "");
  180. int __identifier(generic) = 3;
  181. int __identifier(int) = 4;
  182. struct __identifier(class) { __identifier(class) *__identifier(for); };
  183. __identifier(class) __identifier(struct) = { &__identifier(struct) };
  184. int __identifier for; // expected-error {{missing '(' after '__identifier'}}
  185. int __identifier(else} = __identifier(for); // expected-error {{missing ')' after identifier}} expected-note {{to match this '('}}
  186. #define identifier_weird(x) __identifier(x
  187. int k = identifier_weird(if)); // expected-error {{use of undeclared identifier 'if'}}
  188. // This is a bit weird, but the alternative tokens aren't keywords, and this
  189. // behavior matches MSVC. FIXME: Consider supporting this anyway.
  190. extern int __identifier(and) r; // expected-error {{cannot convert '&&' token to an identifier}}
  191. void f() {
  192. __identifier(() // expected-error {{cannot convert '(' token to an identifier}}
  193. __identifier(void) // expected-error {{use of undeclared identifier 'void'}}
  194. __identifier()) // expected-error {{cannot convert ')' token to an identifier}}
  195. // FIXME: We should pick a friendlier display name for this token kind.
  196. __identifier(1) // expected-error {{cannot convert <numeric_constant> token to an identifier}}
  197. __identifier(+) // expected-error {{cannot convert '+' token to an identifier}}
  198. __identifier("foo") // expected-error {{cannot convert <string_literal> token to an identifier}}
  199. __identifier(;) // expected-error {{cannot convert ';' token to an identifier}}
  200. }
  201. class inline_definition_pure_spec {
  202. virtual int f() = 0 { return 0; }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
  203. virtual int f2() = 0;
  204. };
  205. struct pure_virtual_dtor {
  206. virtual ~pure_virtual_dtor() = 0;
  207. };
  208. pure_virtual_dtor::~pure_virtual_dtor() { }
  209. struct pure_virtual_dtor_inline {
  210. virtual ~pure_virtual_dtor_inline() = 0 { }// expected-warning {{function definition with pure-specifier is a Microsoft extension}}
  211. };
  212. int main () {
  213. // Necessary to force instantiation in -fdelayed-template-parsing mode.
  214. test_late_defined_uuid<int>();
  215. redundant_typename<int>();
  216. missing_template_keyword<int>();
  217. }
  218. namespace access_protected_PTM {
  219. class A {
  220. protected:
  221. void f(); // expected-note {{must name member using the type of the current context 'access_protected_PTM::B'}}
  222. };
  223. class B : public A{
  224. public:
  225. void test_access();
  226. static void test_access_static();
  227. };
  228. void B::test_access() {
  229. &A::f; // expected-error {{'f' is a protected member of 'access_protected_PTM::A'}}
  230. }
  231. void B::test_access_static() {
  232. &A::f;
  233. }
  234. }
  235. namespace Inheritance {
  236. class __single_inheritance A;
  237. class __multiple_inheritance B;
  238. class __virtual_inheritance C;
  239. }
  240. struct StructWithProperty {
  241. __declspec(property) int V0; // expected-error {{expected '(' after 'property'}}
  242. __declspec(property()) int V1; // expected-error {{property does not specify a getter or a putter}}
  243. __declspec(property(set)) int V2; // expected-error {{putter for property must be specified as 'put', not 'set'}} expected-error {{expected '=' after 'set'}}
  244. __declspec(property(ptu)) int V3; // expected-error {{missing 'get=' or 'put='}}
  245. __declspec(property(ptu=PutV)) int V4; // expected-error {{expected 'get' or 'put' in property declaration}}
  246. __declspec(property(get)) int V5; // expected-error {{expected '=' after 'get'}}
  247. __declspec(property(get&)) int V6; // expected-error {{expected '=' after 'get'}}
  248. __declspec(property(get=)) int V7; // expected-error {{expected name of accessor method}}
  249. __declspec(property(get=GetV)) int V8; // no-warning
  250. __declspec(property(get=GetV=)) int V9; // expected-error {{expected ',' or ')' at end of property accessor list}}
  251. __declspec(property(get=GetV,)) int V10; // expected-error {{expected 'get' or 'put' in property declaration}}
  252. __declspec(property(get=GetV,put=SetV)) int V11; // no-warning
  253. __declspec(property(get=GetV,put=SetV,get=GetV)) int V12; // expected-error {{property declaration specifies 'get' accessor twice}}
  254. __declspec(property(get=GetV)) int V13 = 3; // expected-error {{property declaration cannot have an in-class initializer}}
  255. int GetV() { return 123; }
  256. void SetV(int v) {}
  257. };
  258. void TestProperty() {
  259. StructWithProperty sp;
  260. sp.V8;
  261. sp.V8 = 0; // expected-error {{no setter defined for property 'V8'}}
  262. int i = sp.V11;
  263. sp.V11 = i++;
  264. sp.V11 += 8;
  265. sp.V11++;
  266. ++sp.V11;
  267. }
  268. //expected-warning@+1 {{C++ operator 'and' (aka '&&') used as a macro name}}
  269. #define and foo
  270. struct __declspec(uuid("00000000-0000-0000-C000-000000000046")) __declspec(novtable) IUnknown {};
  271. typedef bool (__stdcall __stdcall *blarg)(int);
  272. void local_callconv() {
  273. bool (__stdcall *p)(int);
  274. }
  275. struct S7 {
  276. int foo() { return 12; }
  277. __declspec(property(get=foo) deprecated) int t; // expected-note {{'t' has been explicitly marked deprecated here}}
  278. };
  279. // Technically, this is legal (though it does nothing)
  280. __declspec() void quux( void ) {
  281. struct S7 s;
  282. int i = s.t; // expected-warning {{'t' is deprecated}}
  283. }
  284. void *_alloca(int);
  285. void foo(void) {
  286. __declspec(align(16)) int *buffer = (int *)_alloca(9);
  287. }
  288. template <int *>
  289. struct NullptrArg {};
  290. NullptrArg<nullptr> a;
  291. // Ignored type qualifiers after comma in declarator lists
  292. 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}}
  293. typedef void(*ignored_quals_dummy2)(), __fastcall ignored_quals2; // expected-warning {{qualifiers after comma in declarator list are ignored}}
  294. typedef void(*ignored_quals_dummy3)(), __stdcall ignored_quals3; // expected-warning {{qualifiers after comma in declarator list are ignored}}
  295. typedef void(*ignored_quals_dummy4)(), __thiscall ignored_quals4; // expected-warning {{qualifiers after comma in declarator list are ignored}}
  296. typedef void(*ignored_quals_dummy5)(), __cdecl ignored_quals5; // expected-warning {{qualifiers after comma in declarator list are ignored}}
  297. typedef void(*ignored_quals_dummy6)(), __vectorcall ignored_quals6; // expected-warning {{qualifiers after comma in declarator list are ignored}}
  298. namespace {
  299. bool f(int);
  300. template <typename T>
  301. struct A {
  302. constexpr A(T t) {
  303. __assume(f(t)); // expected-warning{{the argument to '__assume' has side effects that will be discarded}}
  304. }
  305. constexpr bool g() { return false; }
  306. };
  307. constexpr A<int> h() {
  308. A<int> b(0); // expected-note {{in instantiation of member function}}
  309. return b;
  310. }
  311. static_assert(h().g() == false, "");
  312. }
  313. namespace {
  314. __declspec(align(16)) struct align_before_key1 {};
  315. __declspec(align(16)) struct align_before_key2 {} align_before_key2_var;
  316. __declspec(align(16)) struct align_before_key3 {} *align_before_key3_var;
  317. static_assert(__alignof(struct align_before_key1) == 16, "");
  318. static_assert(__alignof(struct align_before_key2) == 16, "");
  319. static_assert(__alignof(struct align_before_key3) == 16, "");
  320. }
  321. namespace PR24027 {
  322. struct S {
  323. template <typename T>
  324. S(T);
  325. } f([] {});
  326. }