1 // RUN: %clang_cc1 %s -ffreestanding -Wno-int-to-pointer-cast -fsyntax-only -verify -pedantic -fpascal-strings -std=c99
2 
3 #include <stdint.h>
4 #include <limits.h>
5 
a()6 int a() {int p; *(1 ? &p : (void*)(0 && (a(),1))) = 10;} // expected-error {{incomplete type 'void' is not assignable}}
7 
8 // rdar://6091492 - ?: with __builtin_constant_p as the operand is an i-c-e.
9 int expr;
10 char w[__builtin_constant_p(expr) ? expr : 1];
11 
12 char v[sizeof(__builtin_constant_p(0)) == sizeof(int) ? 1 : -1];
13 
14 int implicitConversion = 1.0;
15 char floatArith[(int)(1.0+2.0)]; // expected-warning {{variable length array folded to constant array as an extension}}
16 
17 // __builtin_constant_p as the condition of ?: allows arbitrary foldable
18 // constants to be transmogrified into i-c-e's.
19 char b[__builtin_constant_p((int)(1.0+2.0)) ? (int)(1.0+2.0) : -1];
20 
21 struct c {
22   int a : (
23            __builtin_constant_p((int)(1.0+2.0)) ? (int)(1.0+
24      expr // expected-error {{expression is not an integer constant expression}}
25            ) : -1);
26 };
27 
28 // Check that we can evaluate statement-expressions properly when
29 // constant-folding inside an ICE.
PR49239()30 void PR49239() {
31   goto check_not_vla;
32   char not_vla[__builtin_constant_p(1) ? ({ 42; }) : -1]; // expected-warning {{statement expression}}
33 check_not_vla:;
34   _Static_assert(sizeof(not_vla) == 42, ""); // expected-warning {{C11 extension}}
35 
36   // It's not clear that this should be valid: __builtin_expect(expr1, expr2)
37   // should probably be an ICE if and only if expr1 is an ICE, but we roughly
38   // follow GCC in treating it as an ICE if and only if we can evaluate expr1
39   // regardless of whether it's an ICE.
40   goto check_also_not_vla;
41   char also_not_vla[__builtin_expect(({ 76; }), 0)]; // expected-warning {{statement expression}}
42 check_also_not_vla:;
43   _Static_assert(sizeof(also_not_vla) == 76, ""); // expected-warning {{C11 extension}}
44 }
45 
46 
test1(int n,int * p)47 void test1(int n, int* p) { *(n ? p : (void *)(7-7)) = 1; }
test2(int n,int * p)48 void test2(int n, int* p) { *(n ? p : (void *)0) = 1; }
49 
50 
51 
52 char array[1024/(sizeof (long))];
53 
54 int x['\xBb' == (char) 187 ? 1: -1];
55 
56 // PR1992
func(int x)57 void func(int x)
58 {
59   switch (x) {
60     case sizeof("abc"): break;
61     case sizeof("loooong"): func(4);
62     case sizeof("\ploooong"): func(4);
63   }
64 }
65 
66 
67 // rdar://4213768
68 int expr;
69 char y[__builtin_constant_p(expr) ? -1 : 1];
70 char z[__builtin_constant_p(4) ? 1 : -1];
71 
72 // Comma tests
73 int comma1[0?1,2:3];
74 int comma2[1||(1,2)]; // expected-warning {{use of logical '||' with constant operand}} \
75                       // expected-note {{use '|' for a bitwise operation}}
76 int comma3[(1,2)]; // expected-warning {{variable length array folded to constant array as an extension}}
77 
78 // Pointer + __builtin_constant_p
79 char pbcp[__builtin_constant_p(4) ? (intptr_t)&expr : 0]; // expected-error {{variable length array declaration not allowed at file scope}}
80 
81 int illegaldiv1a[1 || 1/0];
82 int illegaldiv1b[1 && 1/0];  //expected-error{{variable length array declaration not allowed at file scope}}
83 
84 int illegaldiv2[1/0]; // expected-error {{variable length array declaration not allowed at file scope}}
85 int illegaldiv3[INT_MIN / -1]; // expected-error {{variable length array declaration not allowed at file scope}}
86 // PR9262
87 int illegaldiv4[0 / (1 / 0)]; // expected-error {{variable length array declaration not allowed at file scope}}
88 
89 int chooseexpr[__builtin_choose_expr(1, 1, expr)];
90 int realop[(__real__ 4) == 4 ? 1 : -1];
91 int imagop[(__imag__ 4) == 0 ? 1 : -1];
92 
93 int *PR14729 = 0 ?: 1/0; // expected-error {{not a compile-time constant}} expected-warning 3{{}}
94 
95 int bcp_call_v;
96 int bcp_call_a[] = {__builtin_constant_p(bcp_call_v && 0) ? bcp_call_v && 0 : -1};
97