xref: /aosp_15_r20/external/clang/test/SemaCXX/MicrosoftCompatibility.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -Wmicrosoft -verify -fms-compatibility -fexceptions -fcxx-exceptions -fms-compatibility-version=19.00
2*67e74705SXin Li // RUN: %clang_cc1 %s -triple i686-pc-win32 -fsyntax-only -std=c++11 -Wmicrosoft -verify -fms-compatibility -fexceptions -fcxx-exceptions -fms-compatibility-version=18.00
3*67e74705SXin Li 
4*67e74705SXin Li #if defined(_HAS_CHAR16_T_LANGUAGE_SUPPORT) && _HAS_CHAR16_T_LANGUAGE_SUPPORT
5*67e74705SXin Li char16_t x;
6*67e74705SXin Li char32_t y;
7*67e74705SXin Li #else
8*67e74705SXin Li typedef unsigned short char16_t;
9*67e74705SXin Li typedef unsigned int char32_t;
10*67e74705SXin Li #endif
11*67e74705SXin Li 
12*67e74705SXin Li _Atomic(int) z;
13*67e74705SXin Li template <typename T>
14*67e74705SXin Li struct _Atomic {
_Atomic_Atomic15*67e74705SXin Li   _Atomic() {}
~_Atomic_Atomic16*67e74705SXin Li   ~_Atomic() {}
17*67e74705SXin Li };
18*67e74705SXin Li template <typename T>
19*67e74705SXin Li struct atomic : _Atomic<T> {
20*67e74705SXin Li   typedef _Atomic<T> TheBase;
21*67e74705SXin Li   TheBase field;
22*67e74705SXin Li };
23*67e74705SXin Li _Atomic(int) alpha;
24*67e74705SXin Li 
25*67e74705SXin Li typename decltype(3) a; // expected-warning {{expected a qualified name after 'typename'}}
26*67e74705SXin Li 
27*67e74705SXin Li namespace ms_conversion_rules {
28*67e74705SXin Li 
29*67e74705SXin Li void f(float a);
30*67e74705SXin Li void f(int a);
31*67e74705SXin Li 
test()32*67e74705SXin Li void test()
33*67e74705SXin Li {
34*67e74705SXin Li     long a = 0;
35*67e74705SXin Li     f((long)0);
36*67e74705SXin Li 	f(a);
37*67e74705SXin Li }
38*67e74705SXin Li 
39*67e74705SXin Li }
40*67e74705SXin Li 
41*67e74705SXin Li 
42*67e74705SXin Li namespace ms_predefined_types {
43*67e74705SXin Li   // ::type_info is a built-in forward class declaration.
44*67e74705SXin Li   void f(const type_info &a);
45*67e74705SXin Li   void f(size_t);
46*67e74705SXin Li }
47*67e74705SXin Li 
48*67e74705SXin Li 
49*67e74705SXin Li namespace ms_protected_scope {
50*67e74705SXin Li   struct C { C(); };
51*67e74705SXin Li 
jump_over_variable_init(bool b)52*67e74705SXin Li   int jump_over_variable_init(bool b) {
53*67e74705SXin Li     if (b)
54*67e74705SXin Li       goto foo; // expected-warning {{jump from this goto statement to its label is a Microsoft extension}}
55*67e74705SXin Li     C c; // expected-note {{jump bypasses variable initialization}}
56*67e74705SXin Li   foo:
57*67e74705SXin Li     return 1;
58*67e74705SXin Li   }
59*67e74705SXin Li 
60*67e74705SXin Li struct Y {
61*67e74705SXin Li   ~Y();
62*67e74705SXin Li };
63*67e74705SXin Li 
jump_over_var_with_dtor()64*67e74705SXin Li void jump_over_var_with_dtor() {
65*67e74705SXin Li   goto end; // expected-warning{{jump from this goto statement to its label is a Microsoft extension}}
66*67e74705SXin Li   Y y; // expected-note {{jump bypasses variable with a non-trivial destructor}}
67*67e74705SXin Li  end:
68*67e74705SXin Li     ;
69*67e74705SXin Li }
70*67e74705SXin Li 
jump_over_variable_case(int c)71*67e74705SXin Li   void jump_over_variable_case(int c) {
72*67e74705SXin Li     switch (c) {
73*67e74705SXin Li     case 0:
74*67e74705SXin Li       int x = 56; // expected-note {{jump bypasses variable initialization}}
75*67e74705SXin Li     case 1:       // expected-error {{cannot jump}}
76*67e74705SXin Li       x = 10;
77*67e74705SXin Li     }
78*67e74705SXin Li   }
79*67e74705SXin Li 
80*67e74705SXin Li 
exception_jump()81*67e74705SXin Li void exception_jump() {
82*67e74705SXin Li   goto l2; // expected-error {{cannot jump}}
83*67e74705SXin Li   try { // expected-note {{jump bypasses initialization of try block}}
84*67e74705SXin Li      l2: ;
85*67e74705SXin Li   } catch(int) {
86*67e74705SXin Li   }
87*67e74705SXin Li }
88*67e74705SXin Li 
jump_over_indirect_goto()89*67e74705SXin Li int jump_over_indirect_goto() {
90*67e74705SXin Li   static void *ps[] = { &&a0 };
91*67e74705SXin Li   goto *&&a0; // expected-warning {{jump from this goto statement to its label is a Microsoft extension}}
92*67e74705SXin Li   int a = 3; // expected-note {{jump bypasses variable initialization}}
93*67e74705SXin Li  a0:
94*67e74705SXin Li   return 0;
95*67e74705SXin Li }
96*67e74705SXin Li 
97*67e74705SXin Li }
98*67e74705SXin Li 
99*67e74705SXin Li namespace PR11826 {
100*67e74705SXin Li   struct pair {
pairPR11826::pair101*67e74705SXin Li     pair(int v) { }
operator =PR11826::pair102*67e74705SXin Li     void operator=(pair&& rhs) { }
103*67e74705SXin Li   };
f()104*67e74705SXin Li   void f() {
105*67e74705SXin Li     pair p0(3);
106*67e74705SXin Li     pair p = p0;
107*67e74705SXin Li   }
108*67e74705SXin Li }
109*67e74705SXin Li 
110*67e74705SXin Li namespace PR11826_for_symmetry {
111*67e74705SXin Li   struct pair {
pairPR11826_for_symmetry::pair112*67e74705SXin Li     pair(int v) { }
pairPR11826_for_symmetry::pair113*67e74705SXin Li     pair(pair&& rhs) { }
114*67e74705SXin Li   };
f()115*67e74705SXin Li   void f() {
116*67e74705SXin Li     pair p0(3);
117*67e74705SXin Li     pair p(4);
118*67e74705SXin Li     p = p0;
119*67e74705SXin Li   }
120*67e74705SXin Li }
121*67e74705SXin Li 
122*67e74705SXin Li namespace ms_using_declaration_bug {
123*67e74705SXin Li 
124*67e74705SXin Li class A {
125*67e74705SXin Li public:
126*67e74705SXin Li   int f();
127*67e74705SXin Li };
128*67e74705SXin Li 
129*67e74705SXin Li class B : public A {
130*67e74705SXin Li private:
131*67e74705SXin Li   using A::f;
g()132*67e74705SXin Li   void g() {
133*67e74705SXin Li     f(); // no diagnostic
134*67e74705SXin Li   }
135*67e74705SXin Li };
136*67e74705SXin Li 
137*67e74705SXin Li class C : public B {
138*67e74705SXin Li private:
139*67e74705SXin Li   using B::f; // expected-warning {{using declaration referring to inaccessible member 'ms_using_declaration_bug::B::f' (which refers to accessible member 'ms_using_declaration_bug::A::f') is a Microsoft compatibility extension}}
140*67e74705SXin Li };
141*67e74705SXin Li 
142*67e74705SXin Li }
143*67e74705SXin Li 
144*67e74705SXin Li namespace using_tag_redeclaration
145*67e74705SXin Li {
146*67e74705SXin Li   struct S;
147*67e74705SXin Li   namespace N {
148*67e74705SXin Li     using ::using_tag_redeclaration::S;
149*67e74705SXin Li     struct S {}; // expected-note {{previous definition is here}}
150*67e74705SXin Li   }
f()151*67e74705SXin Li   void f() {
152*67e74705SXin Li     N::S s1;
153*67e74705SXin Li     S s2;
154*67e74705SXin Li   }
g()155*67e74705SXin Li   void g() {
156*67e74705SXin Li     struct S; // expected-note {{forward declaration of 'S'}}
157*67e74705SXin Li     S s3; // expected-error {{variable has incomplete type 'S'}}
158*67e74705SXin Li   }
h()159*67e74705SXin Li   void h() {
160*67e74705SXin Li     using ::using_tag_redeclaration::S;
161*67e74705SXin Li     struct S {}; // expected-error {{redefinition of 'S'}}
162*67e74705SXin Li   }
163*67e74705SXin Li }
164*67e74705SXin Li 
165*67e74705SXin Li 
166*67e74705SXin Li namespace MissingTypename {
167*67e74705SXin Li 
168*67e74705SXin Li template<class T> class A {
169*67e74705SXin Li public:
170*67e74705SXin Li 	 typedef int TYPE;
171*67e74705SXin Li };
172*67e74705SXin Li 
173*67e74705SXin Li template<class T> class B {
174*67e74705SXin Li public:
175*67e74705SXin Li 	 typedef int TYPE;
176*67e74705SXin Li };
177*67e74705SXin Li 
178*67e74705SXin Li 
179*67e74705SXin Li template<class T, class U>
180*67e74705SXin Li class C : private A<T>, public B<U> {
181*67e74705SXin Li public:
182*67e74705SXin Li    typedef A<T> Base1;
183*67e74705SXin Li    typedef B<U> Base2;
184*67e74705SXin Li    typedef A<U> Base3;
185*67e74705SXin Li 
186*67e74705SXin Li    A<T>::TYPE a1; // expected-warning {{missing 'typename' prior to dependent type name}}
187*67e74705SXin Li    Base1::TYPE a2; // expected-warning {{missing 'typename' prior to dependent type name}}
188*67e74705SXin Li 
189*67e74705SXin Li    B<U>::TYPE a3; // expected-warning {{missing 'typename' prior to dependent type name}}
190*67e74705SXin Li    Base2::TYPE a4; // expected-warning {{missing 'typename' prior to dependent type name}}
191*67e74705SXin Li 
192*67e74705SXin Li    A<U>::TYPE a5; // expected-error {{missing 'typename' prior to dependent type name}}
193*67e74705SXin Li    Base3::TYPE a6; // expected-error {{missing 'typename' prior to dependent type name}}
194*67e74705SXin Li  };
195*67e74705SXin Li 
196*67e74705SXin Li class D {
197*67e74705SXin Li public:
198*67e74705SXin Li     typedef int Type;
199*67e74705SXin Li };
200*67e74705SXin Li 
201*67e74705SXin Li template <class T>
function_missing_typename(const T::Type param)202*67e74705SXin Li void function_missing_typename(const T::Type param)// expected-warning {{missing 'typename' prior to dependent type name}}
203*67e74705SXin Li {
204*67e74705SXin Li     const T::Type var = 2; // expected-warning {{missing 'typename' prior to dependent type name}}
205*67e74705SXin Li }
206*67e74705SXin Li 
207*67e74705SXin Li template void function_missing_typename<D>(const D::Type param);
208*67e74705SXin Li 
209*67e74705SXin Li }
210*67e74705SXin Li 
211*67e74705SXin Li enum ENUM2 {
212*67e74705SXin Li 	ENUM2_a = (enum ENUM2) 4,
213*67e74705SXin Li 	ENUM2_b = 0x9FFFFFFF, // expected-warning {{enumerator value is not representable in the underlying type 'int'}}
214*67e74705SXin Li 	ENUM2_c = 0x100000000 // expected-warning {{enumerator value is not representable in the underlying type 'int'}}
215*67e74705SXin Li };
216*67e74705SXin Li 
217*67e74705SXin Li 
218*67e74705SXin Li namespace PR11791 {
219*67e74705SXin Li   template<class _Ty>
del(_Ty * _Ptr)220*67e74705SXin Li   void del(_Ty *_Ptr) {
221*67e74705SXin Li     _Ptr->~_Ty();  // expected-warning {{pseudo-destructors on type void are a Microsoft extension}}
222*67e74705SXin Li   }
223*67e74705SXin Li 
f()224*67e74705SXin Li   void f() {
225*67e74705SXin Li     int* a = 0;
226*67e74705SXin Li     del((void*)a);  // expected-note {{in instantiation of function template specialization}}
227*67e74705SXin Li   }
228*67e74705SXin Li }
229*67e74705SXin Li 
230*67e74705SXin Li namespace IntToNullPtrConv {
231*67e74705SXin Li   struct Foo {
232*67e74705SXin Li     static const int ZERO = 0;
233*67e74705SXin Li     typedef void (Foo::*MemberFcnPtr)();
234*67e74705SXin Li   };
235*67e74705SXin Li 
236*67e74705SXin Li   struct Bar {
237*67e74705SXin Li     const Foo::MemberFcnPtr pB;
238*67e74705SXin Li   };
239*67e74705SXin Li 
240*67e74705SXin Li   Bar g_bar = { (Foo::MemberFcnPtr)Foo::ZERO };
241*67e74705SXin Li 
get_n()242*67e74705SXin Li   template<int N> int *get_n() { return N; }   // expected-warning {{expression which evaluates to zero treated as a null pointer constant}}
243*67e74705SXin Li   int *g_nullptr = get_n<0>();  // expected-note {{in instantiation of function template specialization}}
244*67e74705SXin Li }
245