xref: /llvm-project/clang/test/AST/ByteCode/c.c (revision 7b4b85b75d22a792b2ef80e6af4f0faf18da0a43)
1 // RUN: %clang_cc1 -triple x86_64-linux -fexperimental-new-constant-interpreter -verify=expected,all -std=c11 -Wcast-qual %s
2 // RUN: %clang_cc1 -triple x86_64-linux -fexperimental-new-constant-interpreter -pedantic -verify=pedantic,pedantic-expected,all -std=c11 -Wcast-qual %s
3 // RUN: %clang_cc1 -triple x86_64-linux -verify=ref,all -std=c11 -Wcast-qual %s
4 // RUN: %clang_cc1 -triple x86_64-linux -pedantic -verify=pedantic,pedantic-ref,all -std=c11 -Wcast-qual %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 int t1 = sizeof(int);
177 void test4(void) {
178   t1 = sizeof(int);
179 }
180 
181 void localCompoundLiteral(void) {
182   struct S { int x, y; } s = {}; // pedantic-expected-warning {{use of an empty initializer}} \
183                                  // pedantic-ref-warning {{use of an empty initializer}}
184   struct T {
185 	int i;
186     struct S s;
187   } t1 = { 1, {} }; // pedantic-expected-warning {{use of an empty initializer}} \
188                     // pedantic-ref-warning {{use of an empty initializer}}
189 
190   struct T t3 = {
191     (int){}, // pedantic-expected-warning {{use of an empty initializer}} \
192              // pedantic-ref-warning {{use of an empty initializer}}
193     {} // pedantic-expected-warning {{use of an empty initializer}} \
194        // pedantic-ref-warning {{use of an empty initializer}}
195   };
196 }
197 
198 /// struct copy
199 struct StrA {int a; };
200 const struct StrA sa = { 12 };
201 const struct StrA * const sb = &sa;
202 const struct StrA sc = *sb;
203 _Static_assert(sc.a == 12, ""); // pedantic-ref-warning {{GNU extension}} \
204                                 // pedantic-expected-warning {{GNU extension}}
205 
206 _Static_assert(((void*)0 + 1) != (void*)0, ""); // pedantic-expected-warning {{arithmetic on a pointer to void is a GNU extension}} \
207                                                 // pedantic-expected-warning {{not an integer constant expression}} \
208                                                 // pedantic-expected-note {{cannot perform pointer arithmetic on null pointer}} \
209                                                 // pedantic-ref-warning {{arithmetic on a pointer to void is a GNU extension}} \
210                                                 // pedantic-ref-warning {{not an integer constant expression}} \
211                                                 // pedantic-ref-note {{cannot perform pointer arithmetic on null pointer}}
212 
213 typedef __INTPTR_TYPE__ intptr_t;
214 int array[(intptr_t)(int*)1]; // ref-warning {{variable length array folded to constant array}} \
215                               // pedantic-ref-warning {{variable length array folded to constant array}} \
216                               // expected-warning {{variable length array folded to constant array}} \
217                               // pedantic-expected-warning {{variable length array folded to constant array}}
218 
219 int castViaInt[*(int*)(unsigned long)"test"]; // ref-error {{variable length array}} \
220                                               // pedantic-ref-error {{variable length array}} \
221                                               // expected-error {{variable length array}} \
222                                               // pedantic-expected-error {{variable length array}}
223 
224 const void (*const funcp)(void) = (void*)123; // pedantic-warning {{converts between void pointer and function pointer}}
225 _Static_assert(funcp == (void*)0, ""); // all-error {{failed due to requirement 'funcp == (void *)0'}} \
226                                        // pedantic-warning {{expression is not an integer constant expression}}
227 _Static_assert(funcp == (void*)123, ""); // pedantic-warning {{equality comparison between function pointer and void pointer}} \
228                                          // pedantic-warning {{expression is not an integer constant expression}}
229 
230 void unaryops(void) {
231   (void)(++(struct x {unsigned x;}){3}.x);
232   (void)(--(struct y {unsigned x;}){3}.x);
233   (void)(++(struct z {float x;}){3}.x);
234   (void)(--(struct w {float x;}){3}.x);
235 
236   (void)((struct xx {unsigned x;}){3}.x++);
237   (void)((struct yy {unsigned x;}){3}.x--);
238   (void)((struct zz {float x;}){3}.x++);
239   (void)((struct ww {float x;}){3}.x--);
240 }
241 
242 /// This used to fail because we didn't properly mark the struct
243 /// initialized through a CompoundLiteralExpr as initialized.
244 struct TestStruct {
245   int a;
246   int b;
247 };
248 int Y __attribute__((annotate(
249   "GlobalValAnnotationWithArgs",
250   42,
251   (struct TestStruct) { .a = 1, .b = 2 }
252 )));
253 
254 #ifdef __SIZEOF_INT128__
255 const int *p = &b;
256 const __int128 K = (__int128)(int*)0;
257 const unsigned __int128 KU = (unsigned __int128)(int*)0;
258 #endif
259 
260 
261 int test3(void) {
262   int a[2];
263   a[0] = test3; // all-error {{incompatible pointer to integer conversion assigning to 'int' from 'int (void)'}}
264   return 0;
265 }
266 /// This tests that we have full type info, even for values we cannot read.
267 int dummyarray[5];
268 _Static_assert(&dummyarray[0] < &dummyarray[1], ""); // pedantic-warning {{GNU extension}}
269 
270 void addrlabelexpr(void) {
271  a0: ;
272   static void *ps[] = { &&a0 }; // pedantic-warning {{use of GNU address-of-label extension}}
273 }
274 
275 extern void cv2;
276 void *foo5 (void)
277 {
278   return &cv2; // pedantic-warning{{address of an expression of type 'void'}}
279 }
280 
281 __attribute__((weak)) const unsigned int test10_bound = 10;
282 char test10_global[test10_bound]; // all-error {{variable length array declaration not allowed at file scope}}
283 void test10(void) {
284   char test10_local[test10_bound] = "help"; // all-error {{variable-sized object may not be initialized}}
285 }
286 
287 void SuperSpecialFunc(void) {
288 const int SuperSpecialCase = 10;
289 _Static_assert((sizeof(SuperSpecialCase) == 12 && SuperSpecialCase == 3) || SuperSpecialCase == 10, ""); // pedantic-warning {{GNU extension}}
290 }
291 
292 
293 void T1(void) {
294   static int *y[1] = {({ static int _x = 20; (void*)0;})}; // all-error {{initializer element is not a compile-time constant}} \
295                                                            // pedantic-warning {{use of GNU statement expression extension}}
296 }
297 
298 enum teste1 test1f(void), (*test1)(void) = test1f; // pedantic-warning {{ISO C forbids forward references to 'enum' types}}
299 enum teste1 { TEST1 };
300 
301 void func(void) {
302   _Static_assert(func + 1 - func == 1, ""); // pedantic-warning {{arithmetic on a pointer to the function type}} \
303                                             // pedantic-warning {{arithmetic on pointers to the function type}} \
304                                             // pedantic-warning {{not an integer constant expression}}
305   _Static_assert(func + 0xdead000000000000UL - 0xdead000000000000UL == func, ""); // pedantic-warning 2{{arithmetic on a pointer to the function type}} \
306                                                                                   // pedantic-warning {{not an integer constant expression}} \
307                                                                                   // pedantic-note {{cannot refer to element 16045481047390945280 of non-array object in a constant expression}}
308   _Static_assert(func + 1 != func, ""); // pedantic-warning {{arithmetic on a pointer to the function type}} \
309                                         // pedantic-warning {{expression is not an integer constant expression}}
310   func + 0xdead000000000000UL; // all-warning {{expression result unused}} \
311                                // pedantic-warning {{arithmetic on a pointer to the function type}}
312   func - 0xdead000000000000UL; // all-warning {{expression result unused}} \
313                                // pedantic-warning {{arithmetic on a pointer to the function type}}
314 }
315 
316 void foo3 (void)
317 {
318  void* x = 0;
319  void* y = &*x;
320 }
321