xref: /llvm-project/clang/test/Sema/sizeless-1.c (revision f75b73549d4adb7e111444f4144af7bffb532f91)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wall -W -Wno-comment -Wno-strict-prototypes -triple arm64-linux-gnu -target-feature +sve -std=c90 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wall -W -Wno-strict-prototypes -triple arm64-linux-gnu -target-feature +sve -std=c11 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wall -W -Wno-strict-prototypes -triple arm64-linux-gnu -target-feature +sve -std=gnu11 %s
4 
5 typedef __SVInt8_t svint8_t;
6 typedef __SVInt16_t svint16_t;
7 
8 svint8_t global_int8;          // expected-error {{non-local variable with sizeless type 'svint8_t'}}
9 extern svint8_t extern_int8;   // expected-error {{non-local variable with sizeless type 'svint8_t'}}
10 static svint8_t static_int8;   // expected-error {{non-local variable with sizeless type 'svint8_t'}}
11 __thread svint8_t thread_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
12 svint8_t *global_int8_ptr;
13 extern svint8_t *extern_int8_ptr;
14 static svint8_t *static_int8_ptr;
15 
16 typedef svint8_t int8_typedef;
17 typedef svint8_t *int8_ptr_typedef;
18 
19 int sizeof_int8 = sizeof(svint8_t);             // expected-error {{invalid application of 'sizeof' to sizeless type 'svint8_t'}}
20 int sizeof_int8_var = sizeof(*extern_int8_ptr); // expected-error {{invalid application of 'sizeof' to sizeless type 'svint8_t'}}
21 int sizeof_int8_var_ptr = sizeof(extern_int8_ptr);
22 
23 int alignof_int8 = _Alignof(svint8_t);                // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}}
24 int alignof_int8_var = _Alignof(*extern_int8_ptr);    // expected-error {{invalid application of 'alignof' to sizeless type 'svint8_t'}} expected-warning {{GNU extension}}
25 int alignof_int8_var_ptr = _Alignof(extern_int8_ptr); // expected-warning {{GNU extension}}
26 
27 void pass_int8(svint8_t); // expected-note {{passing argument to parameter here}}
28 
29 svint8_t return_int8(void);
30 
31 typedef svint8_t vec_int8_a __attribute__((vector_size(64)));    // expected-error {{invalid vector element type}}
32 typedef svint8_t vec_int8_b __attribute__((ext_vector_type(4))); // expected-error {{invalid vector element type}}
33 
34 void dump(const volatile void *);
35 
36 void __attribute__((overloadable)) overf(svint8_t);
37 void __attribute__((overloadable)) overf(svint16_t);
38 
39 void __attribute__((overloadable)) overf8(svint8_t); // expected-note + {{not viable}}
40 void __attribute__((overloadable)) overf8(int);      // expected-note + {{not viable}}
41 
42 void __attribute__((overloadable)) overf16(svint16_t); // expected-note + {{not viable}}
43 void __attribute__((overloadable)) overf16(int);       // expected-note + {{not viable}}
44 
45 void noproto();
46 void varargs(int, ...);
47 
unused(void)48 void unused(void) {
49   svint8_t unused_int8; // expected-warning {{unused}}
50 }
51 
52 struct incomplete_struct *incomplete_ptr;
53 
54 typedef svint8_t sizeless_array[1]; // expected-error {{array has sizeless element type}}
55 
func(int sel)56 void func(int sel) {
57   static svint8_t static_int8; // expected-error {{non-local variable with sizeless type 'svint8_t'}}
58 
59   svint8_t local_int8;
60   int8_typedef typedef_int8;
61   svint16_t local_int16;
62 
63   svint8_t __attribute__((aligned)) aligned_int8_1;    // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
64   svint8_t __attribute__((aligned(4))) aligned_int8_2; // expected-error {{'aligned' attribute cannot be applied to sizeless type 'svint8_t'}}
65   svint8_t _Alignas(int) aligned_int8_3;               // expected-error {{'_Alignas' attribute cannot be applied to sizeless type 'svint8_t'}}
66 
67   int _Alignas(svint8_t) aligned_int; // expected-error {{invalid application of '_Alignas' to sizeless type 'svint8_t'}}
68 
69   // Using pointers to sizeless data isn't wrong here, but because the
70   // type is incomplete, it doesn't provide any alignment guarantees.
71   _Static_assert(__atomic_is_lock_free(1, &local_int8) == __atomic_is_lock_free(1, incomplete_ptr), "");
72   _Static_assert(__atomic_is_lock_free(2, &local_int8) == __atomic_is_lock_free(2, incomplete_ptr), ""); // expected-error {{static assertion expression is not an integral constant expression}}
73   _Static_assert(__atomic_always_lock_free(1, &local_int8) == __atomic_always_lock_free(1, incomplete_ptr), "");
74 
75   local_int8; // expected-warning {{expression result unused}}
76 
77   (void)local_int8;
78 
79   local_int8, 0; // expected-warning {{left operand of comma operator has no effect}}
80 
81   0, local_int8; // expected-warning {{left operand of comma operator has no effect}} expected-warning {{expression result unused}}
82 
83   svint8_t init_int8 = local_int8;
84   svint8_t bad_init_int8 = for; // expected-error {{expected expression}}
85 
86   int empty_brace_init_int = {};
87   svint8_t empty_brace_init_int8 = {};
88   svint8_t brace_init_int8 = {local_int8};
89   svint8_t bad_brace_init_int8_1 = {local_int8, 0};    // expected-warning {{excess elements in initializer for indivisible sizeless type 'svint8_t'}}
90   svint8_t bad_brace_init_int8_2 = {0};                // expected-error {{incompatible type 'int'}}
91   svint8_t bad_brace_init_int8_3 = {local_int16};      // expected-error {{incompatible type 'svint16_t'}}
92   svint8_t bad_brace_init_int8_4 = {[0] = local_int8}; // expected-error {{initialization of non-aggregate type 'svint8_t' (aka '__SVInt8_t') with a designated initializer list}}
93   svint8_t bad_brace_init_int8_5 = {{local_int8}};     // expected-warning {{too many braces around initializer}}
94   svint8_t bad_brace_init_int8_6 = {{local_int8, 0}};  // expected-warning {{too many braces around initializer}}
95 
96   const svint8_t const_int8 = local_int8; // expected-note {{declared const here}}
97   const svint8_t uninit_const_int8;
98 
99   volatile svint8_t volatile_int8;
100 
101   const volatile svint8_t const_volatile_int8 = local_int8; // expected-note {{declared const here}}
102   const volatile svint8_t uninit_const_volatile_int8;
103 
104   _Atomic svint8_t atomic_int8;      // expected-error {{_Atomic cannot be applied to sizeless type 'svint8_t'}}
105   __restrict svint8_t restrict_int8; // expected-error {{requires a pointer or reference}}
106 
107   svint8_t array_int8[1];          // expected-error {{array has sizeless element type}}
108   svint8_t array_int8_init[] = {}; // expected-error {{array has sizeless element type}}
109 
110   _Bool test_int8 = init_int8; // expected-error {{initializing '_Bool' with an expression of incompatible type 'svint8_t'}}
111 
112   int int_int8 = init_int8; // expected-error {{initializing 'int' with an expression of incompatible type 'svint8_t'}}
113 
114   init_int8 = local_int8;
115   init_int8 = local_int16; // expected-error {{assigning to 'svint8_t' (aka '__SVInt8_t') from incompatible type 'svint16_t'}}
116   init_int8 = sel;         // expected-error {{assigning to 'svint8_t' (aka '__SVInt8_t') from incompatible type 'int'}}
117 
118   sel = local_int8; // expected-error {{assigning to 'int' from incompatible type 'svint8_t'}}
119 
120   local_int8 = (svint8_t)local_int8;
121   local_int8 = (const svint8_t)local_int8;
122   local_int8 = (svint8_t)local_int16; // expected-error {{used type 'svint8_t' (aka '__SVInt8_t') where arithmetic or pointer type is required}}
123   local_int8 = (svint8_t)0;           // expected-error {{used type 'svint8_t' (aka '__SVInt8_t') where arithmetic or pointer type is required}}
124   sel = (int)local_int8;              // expected-error {{operand of type 'svint8_t' (aka '__SVInt8_t') where arithmetic or pointer type is required}}
125 
126   init_int8 = local_int8;
127   init_int8 = const_int8;
128   init_int8 = volatile_int8;
129   init_int8 = const_volatile_int8;
130 
131   const_int8 = local_int8; // expected-error {{cannot assign to variable 'const_int8' with const-qualified type 'const svint8_t'}}
132 
133   volatile_int8 = local_int8;
134   volatile_int8 = const_int8;
135   volatile_int8 = volatile_int8;
136   volatile_int8 = const_volatile_int8;
137 
138   const_volatile_int8 = local_int8; // expected-error {{cannot assign to variable 'const_volatile_int8' with const-qualified type 'const volatile svint8_t'}}
139 
140   init_int8 = sel ? init_int8 : local_int8;
141   init_int8 = sel ? init_int8 : typedef_int8;
142   init_int8 = sel ? init_int8 : const_int8;
143   init_int8 = sel ? volatile_int8 : const_int8;
144   init_int8 = sel ? volatile_int8 : const_volatile_int8;
145 
146   pass_int8(local_int8);
147   pass_int8(local_int16); // expected-error {{passing 'svint16_t' (aka '__SVInt16_t') to parameter of incompatible type 'svint8_t'}}
148 
149   local_int8 = return_int8();
150   local_int16 = return_int8(); // expected-error {{assigning to 'svint16_t' (aka '__SVInt16_t') from incompatible type 'svint8_t'}}
151 
152   dump(&local_int8);
153   dump(&const_int8);
154   dump(&volatile_int8);
155   dump(&const_volatile_int8);
156 
157   dump(&local_int8 + 1); // expected-error {{arithmetic on a pointer to sizeless type}}
158 
159   *&local_int8 = local_int8;
160   *&const_int8 = local_int8; // expected-error {{read-only variable is not assignable}}
161   *&volatile_int8 = local_int8;
162   *&const_volatile_int8 = local_int8; // expected-error {{read-only variable is not assignable}}
163 
164   global_int8_ptr[0] = local_int8;       // expected-error {{subscript of pointer to sizeless type 'svint8_t'}}
165   global_int8_ptr[1] = local_int8;       // expected-error {{subscript of pointer to sizeless type 'svint8_t'}}
166   global_int8_ptr = &global_int8_ptr[2]; // expected-error {{subscript of pointer to sizeless type 'svint8_t'}}
167 
168   overf(local_int8);
169   overf(local_int16);
170 
171   overf8(local_int8);
172   overf8(local_int16); // expected-error {{no matching function}}
173 
174   overf16(local_int8); // expected-error {{no matching function}}
175   overf16(local_int16);
176 
177   noproto(local_int8);
178   varargs(1, local_int8, local_int16);
179 
180   global_int8_ptr++;                 // expected-error {{arithmetic on a pointer to sizeless type}}
181   global_int8_ptr--;                 // expected-error {{arithmetic on a pointer to sizeless type}}
182   ++global_int8_ptr;                 // expected-error {{arithmetic on a pointer to sizeless type}}
183   --global_int8_ptr;                 // expected-error {{arithmetic on a pointer to sizeless type}}
184   global_int8_ptr + 1;               // expected-error {{arithmetic on a pointer to sizeless type}}
185   global_int8_ptr - 1;               // expected-error {{arithmetic on a pointer to sizeless type}}
186   global_int8_ptr += 1;              // expected-error {{arithmetic on a pointer to sizeless type}}
187   global_int8_ptr -= 1;              // expected-error {{arithmetic on a pointer to sizeless type}}
188   global_int8_ptr - global_int8_ptr; // expected-error {{arithmetic on a pointer to sizeless type}}
189 
190   ++init_int8;      // expected-error {{cannot increment value of type 'svint8_t'}}
191   init_int8++;      // expected-error {{cannot increment value of type 'svint8_t'}}
192   --init_int8;      // expected-error {{cannot decrement value of type 'svint8_t'}}
193   init_int8--;      // expected-error {{cannot decrement value of type 'svint8_t'}}
194   !init_int8;       // expected-error {{invalid argument type 'svint8_t'}}
195   *init_int8;       // expected-error {{indirection requires pointer operand}}
196   __real init_int8; // expected-error {{invalid type 'svint8_t'}}
197   __imag init_int8; // expected-error {{invalid type 'svint8_t'}}
198 
199   local_int8 &&init_int8;  // expected-error {{invalid operands to binary expression}}
200   local_int8 || init_int8; // expected-error {{invalid operands to binary expression}}
201 
202   if (local_int8) { // expected-error {{statement requires expression of scalar type}}
203   }
204   while (local_int8) { // expected-error {{statement requires expression of scalar type}}
205   }
206   do { // expected-error {{statement requires expression of scalar type}}
207   } while (local_int8);
208   switch (local_int8) { // expected-error {{statement requires expression of integer type}}
209   default:;
210   }
211 }
212 
vararg_receiver(int count,svint8_t first,...)213 int vararg_receiver(int count, svint8_t first, ...) {
214   __builtin_va_list va;
215 
216   __builtin_va_start(va, first);
217   __builtin_va_arg(va, svint8_t);
218   __builtin_va_end(va);
219   return count;
220 }
221 
222 struct sized_struct {
223   int f1;
224   svint8_t f2;     // expected-error {{field has sizeless type 'svint8_t'}}
225   svint8_t f3 : 2; // expected-error {{field has sizeless type 'svint8_t'}}
226   svint8_t : 3;    // expected-error {{field has sizeless type 'svint8_t'}}
227 };
228 
229 union sized_union {
230   int f1;
231   svint8_t f2;     // expected-error {{field has sizeless type 'svint8_t'}}
232   svint8_t f3 : 2; // expected-error {{field has sizeless type 'svint8_t'}}
233   svint8_t : 3;    // expected-error {{field has sizeless type 'svint8_t'}}
234 };
235 
236 #if __STDC_VERSION__ >= 201112L
test_generic(void)237 void test_generic(void) {
238   svint8_t local_int8;
239   svint16_t local_int16;
240 
241   int a1[_Generic(local_int8, svint8_t : 1, svint16_t : 2, default : 3) == 1 ? 1 : -1];
242   int a2[_Generic(local_int16, svint8_t : 1, svint16_t : 2, default : 3) == 2 ? 1 : -1];
243   int a3[_Generic(0, svint8_t : 1, svint16_t : 2, default : 3) == 3 ? 1 : -1];
244   (void)_Generic(0, svint8_t : 1, svint8_t : 2, default : 3); // expected-error {{type 'svint8_t' (aka '__SVInt8_t') in generic association compatible with previously specified type 'svint8_t'}} expected-note {{compatible type}}
245 }
246 
test_compound_literal(void)247 void test_compound_literal(void) {
248   svint8_t local_int8;
249 
250   (void)(svint8_t){local_int8};
251 }
252 #endif
253