Debian Codegraph @

clang/test/AST/ByteCode/c.c

raw
1 // RUN: %clang_cc1 -triple x86_64-linux -verify=expected,all -std=c11 -Wcast-qual %s -fexperimental-new-constant-interpreter
2 // RUN: %clang_cc1 -triple x86_64-linux -verify=pedantic,pedantic-expected,all -std=c11 -Wcast-qual -pedantic %s -fexperimental-new-constant-interpreter
3 // RUN: %clang_cc1 -triple x86_64-linux -verify=ref,all -std=c11 -Wcast-qual %s
4 // RUN: %clang_cc1 -triple x86_64-linux -verify=pedantic,pedantic-ref,all -std=c11 -Wcast-qual -pedantic %s
5
6 typedef __INTPTR_TYPE__ intptr_t;
7 typedef __PTRDIFF_TYPE__ ptrdiff_t;
8
9 _Static_assert(1, "");
10
11 _Static_assert(__objc_yes, "");
12 _Static_assert(!__objc_no, "");
13
14 _Static_assert(0 != 1, "");
15 _Static_assert(1.0 == 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
16 // pedantic-expected-warning {{not an integer constant expression}}
17 _Static_assert(1 && 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
18 // pedantic-expected-warning {{not an integer constant expression}}
19 _Static_assert( (5 > 4) + (3 > 2) == 2, "");
20 _Static_assert(!!1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
21 // pedantic-expected-warning {{not an integer constant expression}}
22 _Static_assert(!!1, "");
23
24 _Static_assert(!(_Bool){(void*)0}, ""); // pedantic-ref-warning {{not an integer constant expression}} \
25 // pedantic-expected-warning {{not an integer constant expression}}
26
27 int a = (1 == 1 ? 5 : 3);
28 _Static_assert(a == 5, ""); // all-error {{not an integral constant expression}}
29
30 const int DiscardedPtrToIntCast = ((intptr_t)((void*)0), 0); // all-warning {{left operand of comma operator has no effect}}
31
32 const int b = 3;
33 _Static_assert(b == 3, ""); // pedantic-ref-warning {{not an integer constant expression}} \
34 // pedantic-expected-warning {{not an integer constant expression}}
35
36 const int c; // all-note {{declared here}}
37 _Static_assert(c == 0, ""); // ref-error {{not an integral constant expression}} \
38 // ref-note {{initializer of 'c' is unknown}} \
39 // pedantic-ref-error {{not an integral constant expression}} \
40 // pedantic-ref-note {{initializer of 'c' is unknown}} \
41 // expected-error {{not an integral constant expression}} \
42 // expected-note {{initializer of 'c' is unknown}} \
43 // pedantic-expected-error {{not an integral constant expression}} \
44 // pedantic-expected-note {{initializer of 'c' is unknown}}
45
46 _Static_assert(&c != 0, ""); // ref-warning {{always true}} \
47 // pedantic-ref-warning {{always true}} \
48 // pedantic-ref-warning {{is a GNU extension}} \
49 // expected-warning {{always true}} \
50 // pedantic-expected-warning {{always true}} \
51 // pedantic-expected-warning {{is a GNU extension}}
52 _Static_assert(&a != 0, ""); // ref-warning {{always true}} \
53 // pedantic-ref-warning {{always true}} \
54 // pedantic-ref-warning {{is a GNU extension}} \
55 // expected-warning {{always true}} \
56 // pedantic-expected-warning {{always true}} \
57 // pedantic-expected-warning {{is a GNU extension}}
58 _Static_assert((&c + 1) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \
59 // pedantic-expected-warning {{is a GNU extension}}
60 _Static_assert((&a + 100) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \
61 // pedantic-ref-note {{100 of non-array}} \
62 // pedantic-expected-note {{100 of non-array}} \
63 // pedantic-expected-warning {{is a GNU extension}}
64 _Static_assert((&a - 100) != 0, ""); // pedantic-ref-warning {{is a GNU extension}} \
65 // pedantic-expected-warning {{is a GNU extension}} \
66 // pedantic-ref-note {{-100 of non-array}} \
67 // pedantic-expected-note {{-100 of non-array}}
68 /// extern variable of a composite type.
69 extern struct Test50S Test50;
70 _Static_assert(&Test50 != (void*)0, ""); // all-warning {{always true}} \
71 // pedantic-warning {{is a GNU extension}} \
72 // pedantic-note {{this conversion is not allowed in a constant expression}}
73
74 struct y {int x,y;};
75 int a2[(intptr_t)&((struct y*)0)->y]; // all-warning {{folded to constant array}}
76
77 const struct y *yy = (struct y*)0;
78 const intptr_t L = (intptr_t)(&(yy->y)); // all-error {{not a compile-time constant}}
79
80 _Static_assert((long)&((struct y*)0)->y > 0, ""); // pedantic-ref-warning {{GNU extension}} \
81 // pedantic-ref-note {{this conversion is not allowed in a constant expression}} \
82 // pedantic-expected-warning {{GNU extension}} \
83 // pedantic-expected-note {{this conversion is not allowed in a constant expression}}
84
85 const ptrdiff_t m = &m + 137 - &m;
86 _Static_assert(m == 137, ""); // pedantic-ref-warning {{GNU extension}} \
87 // pedantic-expected-warning {{GNU extension}}
88
89 /// from test/Sema/switch.c, used to cause an assertion failure.
90 void f (int z) {
91 while (z) {
92 default: z--; // all-error {{'default' statement not in switch}}
93 }
94 }
95
96 int expr;
97 int chooseexpr[__builtin_choose_expr(1, 1, expr)];
98
99 int somefunc(int i) {
100 return (i, 65537) * 65537; // all-warning {{left operand of comma operator has no effect}} \
101 // all-warning {{overflow in expression; result is 131'073 with type 'int'}}
102 }
103
104 #pragma clang diagnostic ignored "-Wpointer-to-int-cast"
105 struct ArrayStruct {
106 char n[1];
107 };
108 char name2[(int)&((struct ArrayStruct*)0)->n]; // expected-warning {{folded to constant array}} \
109 // pedantic-expected-warning {{folded to constant array}} \
110 // ref-warning {{folded to constant array}} \
111 // pedantic-ref-warning {{folded to constant array}}
112 _Static_assert(sizeof(name2) == 0, "");
113
114 #ifdef __SIZEOF_INT128__
115 void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // all-warning {{refers past the last possible element}}
116 #endif
117
118 extern float global_float;
119 struct XX { int a, *b; };
120 struct XY { int before; struct XX xx, *xp; float* after; } xy[] = {
121 0, 0, &xy[0].xx.a, &xy[0].xx, &global_float,
122 [1].xx = 0, &xy[1].xx.a, &xy[1].xx, &global_float,
123 0, // all-note {{previous initialization is here}}
124 0, // all-note {{previous initialization is here}}
125 [2].before = 0, // all-warning {{initializer overrides prior initialization of this subobject}}
126 0, // all-warning {{initializer overrides prior initialization of this subobject}}
127 &xy[2].xx.a, &xy[2].xx, &global_float
128 };
129
130 void t14(void) {
131 int array[256] = { 0 }; // expected-note {{array 'array' declared here}} \
132 // pedantic-expected-note {{array 'array' declared here}} \
133 // ref-note {{array 'array' declared here}} \
134 // pedantic-ref-note {{array 'array' declared here}}
135 const char b = -1;
136 int val = array[b]; // expected-warning {{array index -1 is before the beginning of the array}} \
137 // pedantic-expected-warning {{array index -1 is before the beginning of the array}} \
138 // ref-warning {{array index -1 is before the beginning of the array}} \
139 // pedantic-ref-warning {{array index -1 is before the beginning of the array}}
140
141 }
142
143 void bar_0(void) {
144 struct C {
145 const int a;
146 int b;
147 };
148
149 const struct C S = {0, 0};
150
151 *(int *)(&S.a) = 0; // all-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
152 *(int *)(&S.b) = 0; // all-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
153 }
154
155 /// Complex-to-bool casts.
156 const int A = ((_Complex double)1.0 ? 21 : 1);
157 _Static_assert(A == 21, ""); // pedantic-ref-warning {{GNU extension}} \
158 // pedantic-expected-warning {{GNU extension}}
159
160 const int CTI1 = ((_Complex double){0.0, 1.0}); // pedantic-ref-warning {{extension}} \
161 // pedantic-expected-warning {{extension}}
162 _Static_assert(CTI1 == 0, ""); // pedantic-ref-warning {{GNU extension}} \
163 // pedantic-expected-warning {{GNU extension}}
164
165 const _Bool CTB2 = (_Bool)(_Complex double){0.0, 1.0}; // pedantic-ref-warning {{extension}} \
166 // pedantic-expected-warning {{extension}}
167 _Static_assert(CTB2, ""); // pedantic-ref-warning {{GNU extension}} \
168 // pedantic-expected-warning {{GNU extension}}
169
170 const _Bool CTB3 = (_Complex double){0.0, 1.0}; // pedantic-ref-warning {{extension}} \
171 // pedantic-expected-warning {{extension}}
172 _Static_assert(CTB3, ""); // pedantic-ref-warning {{GNU extension}} \
173 // pedantic-expected-warning {{GNU extension}}
174
175
176 void nonComplexToComplexCast(void) {
177 _Complex double z = *(_Complex double *)&(struct { double r, i; }){0.0, 1.0};
178 }
179
180 int t1 = sizeof(int);
181 void test4(void) {
182 t1 = sizeof(int);
183 }
184
185 void localCompoundLiteral(void) {
186 struct S { int x, y; } s = {}; // pedantic-expected-warning {{use of an empty initializer}} \
187 // pedantic-ref-warning {{use of an empty initializer}}
188 struct T {
189 int i;
190 struct S s;
191 } t1 = { 1, {} }; // pedantic-expected-warning {{use of an empty initializer}} \
192 // pedantic-ref-warning {{use of an empty initializer}}
193
194 struct T t3 = {
195 (int){}, // pedantic-expected-warning {{use of an empty initializer}} \
196 // pedantic-ref-warning {{use of an empty initializer}}
197 {} // pedantic-expected-warning {{use of an empty initializer}} \
198 // pedantic-ref-warning {{use of an empty initializer}}
199 };
200 }
201
202 /// struct copy
203 struct StrA {int a; };
204 const struct StrA sa = { 12 };
205 const struct StrA * const sb = &sa;
206 const struct StrA sc = *sb;
207 _Static_assert(sc.a == 12, ""); // pedantic-ref-warning {{GNU extension}} \
208 // pedantic-expected-warning {{GNU extension}}
209
210 struct ComplexS {
211 int a;
212 float b;
213 struct StrA sa[2];
214 };
215 const struct ComplexS CS = {12, 23.0f, {{1}, {2}}};
216 const struct ComplexS CS2 = CS;
217 _Static_assert(CS2.sa[0].a == 1, ""); // pedantic-ref-warning {{GNU extension}} \
218 // pedantic-expected-warning {{GNU extension}}
219
220 _Static_assert(((void*)0 + 1) != (void*)0, ""); // pedantic-expected-warning {{arithmetic on a pointer to void is a GNU extension}} \
221 // pedantic-expected-warning {{not an integer constant expression}} \
222 // pedantic-expected-note {{cannot perform pointer arithmetic on null pointer}} \
223 // pedantic-ref-warning {{arithmetic on a pointer to void is a GNU extension}} \
224 // pedantic-ref-warning {{not an integer constant expression}} \
225 // pedantic-ref-note {{cannot perform pointer arithmetic on null pointer}}
226
227 typedef __INTPTR_TYPE__ intptr_t;
228 int array[(intptr_t)(int*)1]; // ref-warning {{variable length array folded to constant array}} \
229 // pedantic-ref-warning {{variable length array folded to constant array}} \
230 // expected-warning {{variable length array folded to constant array}} \
231 // pedantic-expected-warning {{variable length array folded to constant array}}
232
233 int castViaInt[*(int*)(unsigned long)"test"]; // ref-error {{variable length array}} \
234 // pedantic-ref-error {{variable length array}} \
235 // expected-error {{variable length array}} \
236 // pedantic-expected-error {{variable length array}}
237
238 const void (*const funcp)(void) = (void*)123; // pedantic-warning {{converts between void pointer and function pointer}} \
239 // pedantic-expected-note {{this conversion is not allowed in a constant expression}}
240 _Static_assert(funcp == (void*)0, ""); // all-error {{failed due to requirement 'funcp == (void *)0'}} \
241 // pedantic-warning {{expression is not an integer constant expression}}
242 _Static_assert(funcp == (void*)123, ""); // pedantic-warning {{equality comparison between function pointer and void pointer}} \
243 // pedantic-warning {{expression is not an integer constant expression}}
244
245 void unaryops(void) {
246 (void)(++(struct x {unsigned x;}){3}.x);
247 (void)(--(struct y {unsigned x;}){3}.x);
248 (void)(++(struct z {float x;}){3}.x);
249 (void)(--(struct w {float x;}){3}.x);
250
251 (void)((struct xx {unsigned x;}){3}.x++);
252 (void)((struct yy {unsigned x;}){3}.x--);
253 (void)((struct zz {float x;}){3}.x++);
254 (void)((struct ww {float x;}){3}.x--);
255 }
256
257 /// This used to fail because we didn't properly mark the struct
258 /// initialized through a CompoundLiteralExpr as initialized.
259 struct TestStruct {
260 int a;
261 int b;
262 };
263 int Y __attribute__((annotate(
264 "GlobalValAnnotationWithArgs",
265 42,
266 (struct TestStruct) { .a = 1, .b = 2 }
267 )));
268
269 #ifdef __SIZEOF_INT128__
270 const int *p = &b;
271 const __int128 K = (__int128)(int*)0;
272 const unsigned __int128 KU = (unsigned __int128)(int*)0;
273 #endif
274
275
276 int test3(void) {
277 int a[2];
278 a[0] = test3; // all-error {{incompatible pointer to integer conversion assigning to 'int' from 'int (void)'}}
279 return 0;
280 }
281 /// This tests that we have full type info, even for values we cannot read.
282 int dummyarray[5];
283 _Static_assert(&dummyarray[0] < &dummyarray[1], ""); // pedantic-warning {{GNU extension}}
284
285 void addrlabelexpr(void) {
286 a0: ;
287 static void *ps[] = { &&a0 }; // pedantic-warning {{use of GNU address-of-label extension}}
288 }
289
290 extern void cv2;
291 void *foo5 (void)
292 {
293 return &cv2; // pedantic-warning{{address of an expression of type 'void'}}
294 }
295
296 __attribute__((weak)) const unsigned int test10_bound = 10;
297 char test10_global[test10_bound]; // all-error {{variable length array declaration not allowed at file scope}}
298 void test10(void) {
299 char test10_local[test10_bound] = "help"; // all-error {{variable-sized object may not be initialized}}
300 }
301
302 void SuperSpecialFunc(void) {
303 const int SuperSpecialCase = 10;
304 _Static_assert((sizeof(SuperSpecialCase) == 12 && SuperSpecialCase == 3) || SuperSpecialCase == 10, ""); // pedantic-warning {{GNU extension}}
305 }
306
307
308 void T1(void) {
309 static int *y[1] = {({ static int _x = 20; (void*)0;})}; // all-error {{initializer element is not a compile-time constant}} \
310 // pedantic-warning {{use of GNU statement expression extension}}
311 }
312
313 enum teste1 test1f(void), (*test1)(void) = test1f; // pedantic-warning {{ISO C forbids forward references to 'enum' types}}
314 enum teste1 { TEST1 };
315
316 void func(void) {
317 _Static_assert(func + 1 - func == 1, ""); // pedantic-warning {{arithmetic on a pointer to the function type}} \
318 // pedantic-warning {{arithmetic on pointers to the function type}} \
319 // pedantic-warning {{not an integer constant expression}}
320 _Static_assert(func + 0xdead000000000000UL - 0xdead000000000000UL == func, ""); // pedantic-warning 2{{arithmetic on a pointer to the function type}} \
321 // pedantic-warning {{not an integer constant expression}} \
322 // pedantic-note {{cannot refer to element 16045481047390945280 of non-array object in a constant expression}}
323 _Static_assert(func + 1 != func, ""); // pedantic-warning {{arithmetic on a pointer to the function type}} \
324 // pedantic-warning {{expression is not an integer constant expression}}
325 func + 0xdead000000000000UL; // all-warning {{expression result unused}} \
326 // pedantic-warning {{arithmetic on a pointer to the function type}}
327 func - 0xdead000000000000UL; // all-warning {{expression result unused}} \
328 // pedantic-warning {{arithmetic on a pointer to the function type}}
329 }
330
331 void foo3 (void)
332 {
333 void* x = 0;
334 void* y = &*x;
335 }
336
337 static void *FooTable[1] = {
338 [0] = (void *[1]) { // 1
339 [0] = (void *[1]) { // 2
340 [0] = (void *[1]) {} // pedantic-warning {{use of an empty initializer}}
341 },
342 }
343 };
344
345 int strcmp(const char *, const char *); // all-note {{passing argument to parameter here}}
346 #define S "\x01\x02\x03\x04\x05\x06\x07\x08"
347 const char _str[] = {S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7]};
348 const unsigned char _str2[] = {S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7]};
349 const int compared = strcmp(_str, (const char *)_str2); // all-error {{initializer element is not a compile-time constant}}
350
351
352 const int compared2 = strcmp(strcmp, _str); // all-error {{incompatible pointer types}} \
353 // all-error {{initializer element is not a compile-time constant}}
354
355 int foo(x) // all-warning {{a function definition without a prototype is deprecated in all versions of C}}
356 int x;
357 {
358 return x;
359 }
360
361 void bar() { // pedantic-warning {{a function declaration without a prototype}}
362 int x;
363 x = foo(); // all-warning {{too few arguments}}
364 }
365
366 int *_b = &a;
367 void discardedCmp(void)
368 {
369 (*_b) = ((&a == &a) , a); // all-warning {{left operand of comma operator has no effect}}
370 }
371
372 /// ArraySubscriptExpr that's not an lvalue
373 typedef unsigned char U __attribute__((vector_size(1)));
374 void nonLValueASE(U f) { f[0] = f[((U)(U){0})[0]]; }
375
376 static char foo_(a) // all-warning {{definition without a prototype}}
377 char a;
378 {
379 return 'a';
380 }
381 static void bar_(void) {
382 foo_(foo_(1));
383 }
384
385 void foo2(void*);
386 void bar2(void) {
387 int a[2][3][4][5]; // all-note {{array 'a' declared here}}
388 foo2(&a[0][4]); // all-warning {{array index 4 is past the end of the array}}
389 }
390
391 void plainComplex(void) {
392 _Complex cd; // all-warning {{_Complex double}}
393 cd = *(_Complex *)&(struct { double r, i; }){0.0, 0.0}; // all-warning {{_Complex double}}
394 }
395
396 /// This test results in an ImplicitValueInitExpr with DiscardResult set.
397 struct M{
398 char c;
399 };
400 typedef struct S64 {
401 struct M m;
402 char a[64];
403 } I64;
404
405 _Static_assert((((I64){}, 1)), ""); // all-warning {{left operand of comma operator has no effect}} \
406 // pedantic-warning {{use of an empty initializer is a C23 extension}} \
407 // pedantic-warning {{expression is not an integer constant expression; folding it to a constant is a GNU extension}}
408
409 #define V(N) __attribute__((vector_size(N)))
410 #define C2 (VC2){0, 1}
411 char func_(void);
412 typedef V(2) char VC2;
413 void CopyArrayToFnPtr(void) { *(VC2 *)func_ = C2; }