xref: /aosp_15_r20/external/clang/test/Sema/MicrosoftExtensions.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -triple i686-windows %s -fsyntax-only -Wno-unused-value -Wmicrosoft -verify -fms-extensions
2*67e74705SXin Li 
3*67e74705SXin Li 
4*67e74705SXin Li struct A
5*67e74705SXin Li {
6*67e74705SXin Li    int a[];  /* expected-warning {{flexible array member 'a' in otherwise empty struct is a Microsoft extension}} */
7*67e74705SXin Li };
8*67e74705SXin Li 
9*67e74705SXin Li struct PR28407
10*67e74705SXin Li {
11*67e74705SXin Li   int : 1;
12*67e74705SXin Li   int a[]; /* expected-warning {{flexible array member 'a' in otherwise empty struct is a Microsoft extension}} */
13*67e74705SXin Li };
14*67e74705SXin Li 
15*67e74705SXin Li struct C {
16*67e74705SXin Li    int l;
17*67e74705SXin Li    union {
18*67e74705SXin Li        int c1[];   /* expected-warning {{flexible array member 'c1' in a union is a Microsoft extension}}  */
19*67e74705SXin Li        char c2[];  /* expected-warning {{flexible array member 'c2' in a union is a Microsoft extension}} */
20*67e74705SXin Li    };
21*67e74705SXin Li };
22*67e74705SXin Li 
23*67e74705SXin Li 
24*67e74705SXin Li struct D {
25*67e74705SXin Li    int l;
26*67e74705SXin Li    int D[];
27*67e74705SXin Li };
28*67e74705SXin Li 
29*67e74705SXin Li struct __declspec(uuid("00000000-0000-0000-C000-000000000046")) IUnknown {}; /* expected-error {{'uuid' attribute is not supported in C}} */
30*67e74705SXin Li 
31*67e74705SXin Li typedef struct notnested {
32*67e74705SXin Li   long bad1;
33*67e74705SXin Li   long bad2;
34*67e74705SXin Li } NOTNESTED;
35*67e74705SXin Li 
36*67e74705SXin Li 
37*67e74705SXin Li typedef struct nested1 {
38*67e74705SXin Li   long a;
39*67e74705SXin Li   struct notnested var1;
40*67e74705SXin Li   NOTNESTED var2;
41*67e74705SXin Li } NESTED1;
42*67e74705SXin Li 
43*67e74705SXin Li struct nested2 {
44*67e74705SXin Li   long b;
45*67e74705SXin Li   NESTED1;  // expected-warning {{anonymous structs are a Microsoft extension}}
46*67e74705SXin Li };
47*67e74705SXin Li 
48*67e74705SXin Li struct nested2 PR20573 = { .a = 3 };
49*67e74705SXin Li 
50*67e74705SXin Li struct nested3 {
51*67e74705SXin Li   long d;
52*67e74705SXin Li   struct nested4 { // expected-warning {{anonymous structs are a Microsoft extension}}
53*67e74705SXin Li     long e;
54*67e74705SXin Li   };
55*67e74705SXin Li   union nested5 { // expected-warning {{anonymous unions are a Microsoft extension}}
56*67e74705SXin Li     long f;
57*67e74705SXin Li   };
58*67e74705SXin Li };
59*67e74705SXin Li 
60*67e74705SXin Li typedef union nested6 {
61*67e74705SXin Li   long f;
62*67e74705SXin Li } NESTED6;
63*67e74705SXin Li 
64*67e74705SXin Li struct test {
65*67e74705SXin Li   int c;
66*67e74705SXin Li   struct nested2;   // expected-warning {{anonymous structs are a Microsoft extension}}
67*67e74705SXin Li   NESTED6;   // expected-warning {{anonymous unions are a Microsoft extension}}
68*67e74705SXin Li };
69*67e74705SXin Li 
foo()70*67e74705SXin Li void foo()
71*67e74705SXin Li {
72*67e74705SXin Li   struct test var;
73*67e74705SXin Li   var.a;
74*67e74705SXin Li   var.b;
75*67e74705SXin Li   var.c;
76*67e74705SXin Li   var.bad1;   // expected-error {{no member named 'bad1' in 'struct test'}}
77*67e74705SXin Li   var.bad2;   // expected-error {{no member named 'bad2' in 'struct test'}}
78*67e74705SXin Li }
79*67e74705SXin Li 
80*67e74705SXin Li // Enumeration types with a fixed underlying type.
81*67e74705SXin Li const int seventeen = 17;
82*67e74705SXin Li typedef int Int;
83*67e74705SXin Li 
84*67e74705SXin Li struct X0 {
85*67e74705SXin Li   enum E1 : Int { SomeOtherValue } field;  // expected-warning{{enumeration types with a fixed underlying type are a Microsoft extension}}
86*67e74705SXin Li   enum E1 : seventeen;
87*67e74705SXin Li };
88*67e74705SXin Li 
89*67e74705SXin Li enum : long long {  // expected-warning{{enumeration types with a fixed underlying type are a Microsoft extension}}
90*67e74705SXin Li   SomeValue = 0x100000000
91*67e74705SXin Li };
92*67e74705SXin Li 
93*67e74705SXin Li 
pointer_to_integral_type_conv(char * ptr)94*67e74705SXin Li void pointer_to_integral_type_conv(char* ptr) {
95*67e74705SXin Li    char ch = (char)ptr;
96*67e74705SXin Li    short sh = (short)ptr;
97*67e74705SXin Li    ch = (char)ptr;
98*67e74705SXin Li    sh = (short)ptr;
99*67e74705SXin Li 
100*67e74705SXin Li    // This is valid ISO C.
101*67e74705SXin Li    _Bool b = (_Bool)ptr;
102*67e74705SXin Li }
103*67e74705SXin Li 
104*67e74705SXin Li 
105*67e74705SXin Li typedef struct {
106*67e74705SXin Li   UNKNOWN u; // expected-error {{unknown type name 'UNKNOWN'}}
107*67e74705SXin Li } AA;
108*67e74705SXin Li 
109*67e74705SXin Li typedef struct {
110*67e74705SXin Li   AA; // expected-warning {{anonymous structs are a Microsoft extension}}
111*67e74705SXin Li } BB;
112*67e74705SXin Li 
113*67e74705SXin Li struct anon_fault {
114*67e74705SXin Li   struct undefined; // expected-warning {{anonymous structs are a Microsoft extension}}
115*67e74705SXin Li                     // expected-error@-1 {{field has incomplete type 'struct undefined'}}
116*67e74705SXin Li                     // expected-note@-2 {{forward declaration of 'struct undefined'}}
117*67e74705SXin Li };
118*67e74705SXin Li 
119*67e74705SXin Li const int anon_falt_size = sizeof(struct anon_fault);
120*67e74705SXin Li 
121*67e74705SXin Li __declspec(deprecated("This is deprecated")) enum DE1 { one, two } e1; // expected-note {{'e1' has been explicitly marked deprecated here}}
122*67e74705SXin Li struct __declspec(deprecated) DS1 { int i; float f; }; // expected-note {{'DS1' has been explicitly marked deprecated here}}
123*67e74705SXin Li 
124*67e74705SXin Li #define MY_TEXT		"This is also deprecated"
Dfunc1(void)125*67e74705SXin Li __declspec(deprecated(MY_TEXT)) void Dfunc1( void ) {} // expected-note {{'Dfunc1' has been explicitly marked deprecated here}}
126*67e74705SXin Li 
127*67e74705SXin Li struct __declspec(deprecated(123)) DS2 {};	// expected-error {{'deprecated' attribute requires a string}}
128*67e74705SXin Li 
test(void)129*67e74705SXin Li void test( void ) {
130*67e74705SXin Li 	e1 = one;	// expected-warning {{'e1' is deprecated: This is deprecated}}
131*67e74705SXin Li 	struct DS1 s = { 0 };	// expected-warning {{'DS1' is deprecated}}
132*67e74705SXin Li 	Dfunc1();	// expected-warning {{'Dfunc1' is deprecated: This is also deprecated}}
133*67e74705SXin Li 
134*67e74705SXin Li 	enum DE1 no;	// no warning because E1 is not deprecated
135*67e74705SXin Li }
136*67e74705SXin Li 
137*67e74705SXin Li int __sptr wrong1; // expected-error {{'__sptr' attribute only applies to pointer arguments}}
138*67e74705SXin Li // The modifier must follow the asterisk
139*67e74705SXin Li int __sptr *wrong_psp; // expected-error {{'__sptr' attribute only applies to pointer arguments}}
140*67e74705SXin Li int * __sptr __uptr wrong2; // expected-error {{'__sptr' and '__uptr' attributes are not compatible}}
141*67e74705SXin Li int * __sptr __sptr wrong3; // expected-warning {{attribute '__sptr' is already applied}}
142*67e74705SXin Li 
143*67e74705SXin Li // It is illegal to overload based on the type attribute.
ptr_func(int * __ptr32 i)144*67e74705SXin Li void ptr_func(int * __ptr32 i) {}  // expected-note {{previous definition is here}}
ptr_func(int * __ptr64 i)145*67e74705SXin Li void ptr_func(int * __ptr64 i) {} // expected-error {{redefinition of 'ptr_func'}}
146*67e74705SXin Li 
147*67e74705SXin Li // It is also illegal to overload based on the pointer type attribute.
ptr_func2(int * __sptr __ptr32 i)148*67e74705SXin Li void ptr_func2(int * __sptr __ptr32 i) {}  // expected-note {{previous definition is here}}
ptr_func2(int * __uptr __ptr32 i)149*67e74705SXin Li void ptr_func2(int * __uptr __ptr32 i) {} // expected-error {{redefinition of 'ptr_func2'}}
150*67e74705SXin Li 
151*67e74705SXin Li int * __sptr __ptr32 __sptr wrong4; // expected-warning {{attribute '__sptr' is already applied}}
152*67e74705SXin Li 
153*67e74705SXin Li __ptr32 int *wrong5; // expected-error {{'__ptr32' attribute only applies to pointer arguments}}
154*67e74705SXin Li 
155*67e74705SXin Li int *wrong6 __ptr32;  // expected-error {{expected ';' after top level declarator}} expected-warning {{declaration does not declare anything}}
156*67e74705SXin Li 
157*67e74705SXin Li int * __ptr32 __ptr64 wrong7;  // expected-error {{'__ptr32' and '__ptr64' attributes are not compatible}}
158*67e74705SXin Li 
159*67e74705SXin Li int * __ptr32 __ptr32 wrong8;	// expected-warning {{attribute '__ptr32' is already applied}}
160*67e74705SXin Li 
161*67e74705SXin Li int *(__ptr32 __sptr wrong9); // expected-error {{'__sptr' attribute only applies to pointer arguments}} // expected-error {{'__ptr32' attribute only applies to pointer arguments}}
162*67e74705SXin Li 
163*67e74705SXin Li typedef int *T;
164*67e74705SXin Li T __ptr32 wrong10; // expected-error {{'__ptr32' attribute only applies to pointer arguments}}
165*67e74705SXin Li 
166*67e74705SXin Li typedef char *my_va_list;
167*67e74705SXin Li void __va_start(my_va_list *ap, ...); // expected-note {{passing argument to parameter 'ap' here}}
168*67e74705SXin Li void vmyprintf(const char *f, my_va_list ap);
myprintf(const char * f,...)169*67e74705SXin Li void myprintf(const char *f, ...) {
170*67e74705SXin Li   my_va_list ap;
171*67e74705SXin Li   if (1) {
172*67e74705SXin Li     __va_start(&ap, f);
173*67e74705SXin Li     vmyprintf(f, ap);
174*67e74705SXin Li     ap = 0;
175*67e74705SXin Li   } else {
176*67e74705SXin Li     __va_start(ap, f); // expected-warning {{incompatible pointer types passing 'my_va_list'}}
177*67e74705SXin Li   }
178*67e74705SXin Li }
179*67e74705SXin Li 
180*67e74705SXin Li // __unaligned handling
test_unaligned()181*67e74705SXin Li void test_unaligned() {
182*67e74705SXin Li   __unaligned int *p1 = 0;
183*67e74705SXin Li   int *p2 = p1; // expected-warning {{initializing 'int *' with an expression of type '__unaligned int *' discards qualifiers}}
184*67e74705SXin Li   __unaligned int *p3 = p2;
185*67e74705SXin Li }
186*67e74705SXin Li 
test_unaligned2(int x[__unaligned4])187*67e74705SXin Li void test_unaligned2(int x[__unaligned 4]) {}
188*67e74705SXin Li 
189