Debian Codegraph @

clang/test/AST/Interp/c.c

raw
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 /// FIXME: The following test is incorrect in the new interpreter.
105 /// The null pointer returns 16 from its getIntegerRepresentation().
106 #pragma clang diagnostic ignored "-Wpointer-to-int-cast"
107 struct ArrayStruct {
108 char n[1];
109 };
110 char name2[(int)&((struct ArrayStruct*)0)->n]; // expected-warning {{folded to constant array}} \
111 // pedantic-expected-warning {{folded to constant array}} \
112 // ref-warning {{folded to constant array}} \
113 // pedantic-ref-warning {{folded to constant array}}
114 _Static_assert(sizeof(name2) == 0, ""); // expected-error {{failed}} \
115 // expected-note {{evaluates to}} \
116 // pedantic-expected-error {{failed}} \
117 // pedantic-expected-note {{evaluates to}}
118
119 #ifdef __SIZEOF_INT128__
120 void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // all-warning {{refers past the last possible element}}
121 #endif
122
123 extern float global_float;
124 struct XX { int a, *b; };
125 struct XY { int before; struct XX xx, *xp; float* after; } xy[] = {
126 0, 0, &xy[0].xx.a, &xy[0].xx, &global_float,
127 [1].xx = 0, &xy[1].xx.a, &xy[1].xx, &global_float,
128 0, // all-note {{previous initialization is here}}
129 0, // all-note {{previous initialization is here}}
130 [2].before = 0, // all-warning {{initializer overrides prior initialization of this subobject}}
131 0, // all-warning {{initializer overrides prior initialization of this subobject}}
132 &xy[2].xx.a, &xy[2].xx, &global_float
133 };
134
135 void t14(void) {
136 int array[256] = { 0 }; // expected-note {{array 'array' declared here}} \
137 // pedantic-expected-note {{array 'array' declared here}} \
138 // ref-note {{array 'array' declared here}} \
139 // pedantic-ref-note {{array 'array' declared here}}
140 const char b = -1;
141 int val = array[b]; // expected-warning {{array index -1 is before the beginning of the array}} \
142 // pedantic-expected-warning {{array index -1 is before the beginning of the array}} \
143 // ref-warning {{array index -1 is before the beginning of the array}} \
144 // pedantic-ref-warning {{array index -1 is before the beginning of the array}}
145
146 }
147
148 void bar_0(void) {
149 struct C {
150 const int a;
151 int b;
152 };
153
154 const struct C S = {0, 0};
155
156 *(int *)(&S.a) = 0; // all-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
157 *(int *)(&S.b) = 0; // all-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
158 }
159
160 /// Complex-to-bool casts.
161 const int A = ((_Complex double)1.0 ? 21 : 1);
162 _Static_assert(A == 21, ""); // pedantic-ref-warning {{GNU extension}} \
163 // pedantic-expected-warning {{GNU extension}}
164
165 const int CTI1 = ((_Complex double){0.0, 1.0}); // pedantic-ref-warning {{extension}} \
166 // pedantic-expected-warning {{extension}}
167 _Static_assert(CTI1 == 0, ""); // pedantic-ref-warning {{GNU extension}} \
168 // pedantic-expected-warning {{GNU extension}}
169
170 const _Bool CTB2 = (_Bool)(_Complex double){0.0, 1.0}; // pedantic-ref-warning {{extension}} \
171 // pedantic-expected-warning {{extension}}
172 _Static_assert(CTB2, ""); // pedantic-ref-warning {{GNU extension}} \
173 // pedantic-expected-warning {{GNU extension}}
174
175 const _Bool CTB3 = (_Complex double){0.0, 1.0}; // pedantic-ref-warning {{extension}} \
176 // pedantic-expected-warning {{extension}}
177 _Static_assert(CTB3, ""); // pedantic-ref-warning {{GNU extension}} \
178 // pedantic-expected-warning {{GNU extension}}
179
180
181 int t1 = sizeof(int);
182 void test4(void) {
183 t1 = sizeof(int);
184 }
185
186 void localCompoundLiteral(void) {
187 struct S { int x, y; } s = {}; // pedantic-expected-warning {{use of an empty initializer}} \
188 // pedantic-ref-warning {{use of an empty initializer}}
189 struct T {
190 int i;
191 struct S s;
192 } t1 = { 1, {} }; // pedantic-expected-warning {{use of an empty initializer}} \
193 // pedantic-ref-warning {{use of an empty initializer}}
194
195 struct T t3 = {
196 (int){}, // pedantic-expected-warning {{use of an empty initializer}} \
197 // pedantic-ref-warning {{use of an empty initializer}}
198 {} // pedantic-expected-warning {{use of an empty initializer}} \
199 // pedantic-ref-warning {{use of an empty initializer}}
200 };
201 }
202
203 /// struct copy
204 struct StrA {int a; };
205 const struct StrA sa = { 12 };
206 const struct StrA * const sb = &sa;
207 const struct StrA sc = *sb;
208 _Static_assert(sc.a == 12, ""); // pedantic-ref-warning {{GNU extension}} \
209 // pedantic-expected-warning {{GNU extension}}
210
211 _Static_assert(((void*)0 + 1) != (void*)0, ""); // pedantic-expected-warning {{arithmetic on a pointer to void is a GNU extension}} \
212 // pedantic-expected-warning {{not an integer constant expression}} \
213 // pedantic-expected-note {{cannot perform pointer arithmetic on null pointer}} \
214 // pedantic-ref-warning {{arithmetic on a pointer to void is a GNU extension}} \
215 // pedantic-ref-warning {{not an integer constant expression}} \
216 // pedantic-ref-note {{cannot perform pointer arithmetic on null pointer}}
217
218 typedef __INTPTR_TYPE__ intptr_t;
219 int array[(intptr_t)(int*)1]; // ref-warning {{variable length array folded to constant array}} \
220 // pedantic-ref-warning {{variable length array folded to constant array}} \
221 // expected-warning {{variable length array folded to constant array}} \
222 // pedantic-expected-warning {{variable length array folded to constant array}}
223
224 int castViaInt[*(int*)(unsigned long)"test"]; // ref-error {{variable length array}} \
225 // pedantic-ref-error {{variable length array}} \
226 // expected-error {{variable length array}} \
227 // pedantic-expected-error {{variable length array}}
228
229 const void (*const funcp)(void) = (void*)123; // pedantic-warning {{converts between void pointer and function pointer}}
230 _Static_assert(funcp == (void*)0, ""); // all-error {{failed due to requirement 'funcp == (void *)0'}} \
231 // pedantic-warning {{expression is not an integer constant expression}}
232 _Static_assert(funcp == (void*)123, ""); // pedantic-warning {{equality comparison between function pointer and void pointer}} \
233 // pedantic-warning {{expression is not an integer constant expression}}
234
235 void unaryops(void) {
236 (void)(++(struct x {unsigned x;}){3}.x);
237 (void)(--(struct y {unsigned x;}){3}.x);
238 (void)(++(struct z {float x;}){3}.x);
239 (void)(--(struct w {float x;}){3}.x);
240
241 (void)((struct xx {unsigned x;}){3}.x++);
242 (void)((struct yy {unsigned x;}){3}.x--);
243 (void)((struct zz {float x;}){3}.x++);
244 (void)((struct ww {float x;}){3}.x--);
245 }
246
247 /// This used to fail because we didn't properly mark the struct
248 /// initialized through a CompoundLiteralExpr as initialized.
249 struct TestStruct {
250 int a;
251 int b;
252 };
253 int Y __attribute__((annotate(
254 "GlobalValAnnotationWithArgs",
255 42,
256 (struct TestStruct) { .a = 1, .b = 2 }
257 )));
258
259 #ifdef __SIZEOF_INT128__
260 const int *p = &b;
261 const __int128 K = (__int128)(int*)0;
262 const unsigned __int128 KU = (unsigned __int128)(int*)0;
263 #endif
264
265
266 int test3(void) {
267 int a[2];
268 a[0] = test3; // all-error {{incompatible pointer to integer conversion assigning to 'int' from 'int (void)'}}
269 return 0;
270 }
271 /// This tests that we have full type info, even for values we cannot read.
272 int dummyarray[5];
273 _Static_assert(&dummyarray[0] < &dummyarray[1], ""); // pedantic-warning {{GNU extension}}
274
275 void addrlabelexpr(void) {
276 a0: ;
277 static void *ps[] = { &&a0 }; // pedantic-warning {{use of GNU address-of-label extension}}
278 }
279
280 extern void cv2;
281 void *foo5 (void)
282 {
283 return &cv2; // pedantic-warning{{address of an expression of type 'void'}}
284 }
285
286 __attribute__((weak)) const unsigned int test10_bound = 10;
287 char test10_global[test10_bound]; // all-error {{variable length array declaration not allowed at file scope}}
288 void test10(void) {
289 char test10_local[test10_bound] = "help"; // all-error {{variable-sized object may not be initialized}}
290 }
291
292 void SuperSpecialFunc(void) {
293 const int SuperSpecialCase = 10;
294 _Static_assert((sizeof(SuperSpecialCase) == 12 && SuperSpecialCase == 3) || SuperSpecialCase == 10, ""); // pedantic-warning {{GNU extension}}
295 }
296
297
298 void T1(void) {
299 static int *y[1] = {({ static int _x = 20; (void*)0;})}; // all-error {{initializer element is not a compile-time constant}} \
300 // pedantic-warning {{use of GNU statement expression extension}}
301 }
302
303