xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/init.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -verify -fsyntax-only -ffreestanding
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc #include <stddef.h>
4*f4a2713aSLionel Sambuc #include <stdint.h>
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc typedef void (* fp)(void);
7*f4a2713aSLionel Sambuc void foo(void);
8*f4a2713aSLionel Sambuc 
9*f4a2713aSLionel Sambuc // PR clang/3377
10*f4a2713aSLionel Sambuc fp a[(short int)1] = { foo };
11*f4a2713aSLionel Sambuc 
12*f4a2713aSLionel Sambuc int myArray[5] = {1, 2, 3, 4, 5};
13*f4a2713aSLionel Sambuc int *myPointer2 = myArray;
14*f4a2713aSLionel Sambuc int *myPointer = &(myArray[2]);
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc 
17*f4a2713aSLionel Sambuc extern int x;
18*f4a2713aSLionel Sambuc void *g = &x;
19*f4a2713aSLionel Sambuc int *h = &x;
20*f4a2713aSLionel Sambuc 
21*f4a2713aSLionel Sambuc struct union_crash
22*f4a2713aSLionel Sambuc {
23*f4a2713aSLionel Sambuc     union
24*f4a2713aSLionel Sambuc     {
25*f4a2713aSLionel Sambuc     };
26*f4a2713aSLionel Sambuc };
27*f4a2713aSLionel Sambuc 
test()28*f4a2713aSLionel Sambuc int test() {
29*f4a2713aSLionel Sambuc   int a[10];
30*f4a2713aSLionel Sambuc   int b[10] = a; // expected-error {{array initializer must be an initializer list}}
31*f4a2713aSLionel Sambuc   int +; // expected-error {{expected identifier or '('}}
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc   struct union_crash u = { .d = 1 }; // expected-error {{field designator 'd' does not refer to any field in type 'struct union_crash'}}
34*f4a2713aSLionel Sambuc }
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc // PR2050
38*f4a2713aSLionel Sambuc struct cdiff_cmd {
39*f4a2713aSLionel Sambuc           const char *name;
40*f4a2713aSLionel Sambuc           unsigned short argc;
41*f4a2713aSLionel Sambuc           int (*handler)();
42*f4a2713aSLionel Sambuc };
43*f4a2713aSLionel Sambuc int cdiff_cmd_open();
44*f4a2713aSLionel Sambuc struct cdiff_cmd commands[] = {
45*f4a2713aSLionel Sambuc         {"OPEN", 1, &cdiff_cmd_open }
46*f4a2713aSLionel Sambuc };
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc // PR2348
49*f4a2713aSLionel Sambuc static struct { int z; } s[2];
50*f4a2713aSLionel Sambuc int *t = &(*s).z;
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc // PR2349
a2(void)53*f4a2713aSLionel Sambuc short *a2(void)
54*f4a2713aSLionel Sambuc {
55*f4a2713aSLionel Sambuc   short int b;
56*f4a2713aSLionel Sambuc   static short *bp = &b; // expected-error {{initializer element is not a compile-time constant}}
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   return bp;
59*f4a2713aSLionel Sambuc }
60*f4a2713aSLionel Sambuc 
pbool(void)61*f4a2713aSLionel Sambuc int pbool(void) {
62*f4a2713aSLionel Sambuc   typedef const _Bool cbool;
63*f4a2713aSLionel Sambuc   _Bool pbool1 = (void *) 0;
64*f4a2713aSLionel Sambuc   cbool pbool2 = &pbool;
65*f4a2713aSLionel Sambuc   return pbool2;
66*f4a2713aSLionel Sambuc }
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc // rdar://5870981
70*f4a2713aSLionel Sambuc union { float f; unsigned u; } u = { 1.0f };
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc // rdar://6156694
f3(int x)73*f4a2713aSLionel Sambuc int f3(int x) { return x; }
74*f4a2713aSLionel Sambuc typedef void (*vfunc)(void);
75*f4a2713aSLionel Sambuc void *bar = (vfunc) f3;
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc // PR2747
78*f4a2713aSLionel Sambuc struct sym_reg {
79*f4a2713aSLionel Sambuc         char nc_gpreg;
80*f4a2713aSLionel Sambuc };
81*f4a2713aSLionel Sambuc int sym_fw1a_scr[] = {
82*f4a2713aSLionel Sambuc            ((int)(&((struct sym_reg *)0)->nc_gpreg)) & 0,
83*f4a2713aSLionel Sambuc            8 * ((int)(&((struct sym_reg *)0)->nc_gpreg))
84*f4a2713aSLionel Sambuc };
85*f4a2713aSLionel Sambuc 
86*f4a2713aSLionel Sambuc // PR3001
87*f4a2713aSLionel Sambuc struct s1 s2 = { // expected-error {{variable has incomplete type 'struct s1'}}  \
88*f4a2713aSLionel Sambuc                  // expected-note {{forward declaration of 'struct s1'}}
89*f4a2713aSLionel Sambuc     .a = sizeof(struct s3), // expected-error {{invalid application of 'sizeof'}} \
90*f4a2713aSLionel Sambuc                             // expected-note{{forward declaration of 'struct s3'}}
91*f4a2713aSLionel Sambuc     .b = bogus // expected-error {{use of undeclared identifier 'bogus'}}
92*f4a2713aSLionel Sambuc }
93*f4a2713aSLionel Sambuc 
94*f4a2713aSLionel Sambuc // PR3382
95*f4a2713aSLionel Sambuc char t[] = ("Hello");
96*f4a2713aSLionel Sambuc 
97*f4a2713aSLionel Sambuc // <rdar://problem/6094855>
98*f4a2713aSLionel Sambuc typedef struct { } empty;
99*f4a2713aSLionel Sambuc 
100*f4a2713aSLionel Sambuc typedef struct {
101*f4a2713aSLionel Sambuc   empty e;
102*f4a2713aSLionel Sambuc   int i2;
103*f4a2713aSLionel Sambuc } st;
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc st st1 = { .i2 = 1 };
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc // <rdar://problem/6096826>
108*f4a2713aSLionel Sambuc struct {
109*f4a2713aSLionel Sambuc   int a;
110*f4a2713aSLionel Sambuc   int z[2];
111*f4a2713aSLionel Sambuc } y = { .z = {} };
112*f4a2713aSLionel Sambuc 
113*f4a2713aSLionel Sambuc int bbb[10];
114*f4a2713aSLionel Sambuc 
115*f4a2713aSLionel Sambuc struct foo2 {
116*f4a2713aSLionel Sambuc    uintptr_t a;
117*f4a2713aSLionel Sambuc };
118*f4a2713aSLionel Sambuc 
119*f4a2713aSLionel Sambuc struct foo2 bar2[] = {
120*f4a2713aSLionel Sambuc    { (intptr_t)bbb }
121*f4a2713aSLionel Sambuc };
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc struct foo2 bar3 = { 1, 2 }; // expected-warning{{excess elements in struct initializer}}
124*f4a2713aSLionel Sambuc 
125*f4a2713aSLionel Sambuc int* ptest1 = __builtin_choose_expr(1, (int*)0, (int*)0);
126*f4a2713aSLionel Sambuc 
127*f4a2713aSLionel Sambuc typedef int32_t ivector4 __attribute((vector_size(16)));
128*f4a2713aSLionel Sambuc ivector4 vtest1 = 1 ? (ivector4){1} : (ivector4){1};
129*f4a2713aSLionel Sambuc ivector4 vtest2 = __builtin_choose_expr(1, (ivector4){1}, (ivector4){1});
130*f4a2713aSLionel Sambuc 
131*f4a2713aSLionel Sambuc uintptr_t ptrasintadd1 = (uintptr_t)&a - 4;
132*f4a2713aSLionel Sambuc uintptr_t ptrasintadd2 = (uintptr_t)&a + 4;
133*f4a2713aSLionel Sambuc uintptr_t ptrasintadd3 = 4 + (uintptr_t)&a;
134*f4a2713aSLionel Sambuc 
135*f4a2713aSLionel Sambuc // PR4285
136*f4a2713aSLionel Sambuc const wchar_t widestr[] = L"asdf";
137*f4a2713aSLionel Sambuc 
138*f4a2713aSLionel Sambuc // PR5447
139*f4a2713aSLionel Sambuc const double pr5447 = (0.05 < -1.0) ? -1.0 : 0.0499878;
140*f4a2713aSLionel Sambuc 
141*f4a2713aSLionel Sambuc // PR4386
142*f4a2713aSLionel Sambuc 
143*f4a2713aSLionel Sambuc // None of these are constant initializers, but we implement GCC's old
144*f4a2713aSLionel Sambuc // behaviour of accepting bar and zed but not foo. GCC's behaviour was
145*f4a2713aSLionel Sambuc // changed in 2007 (rev 122551), so we should be able to change too one
146*f4a2713aSLionel Sambuc // day.
147*f4a2713aSLionel Sambuc int PR4386_bar();
148*f4a2713aSLionel Sambuc int PR4386_foo() __attribute((weak));
149*f4a2713aSLionel Sambuc int PR4386_zed();
150*f4a2713aSLionel Sambuc 
151*f4a2713aSLionel Sambuc int PR4386_a = ((void *) PR4386_bar) != 0;
152*f4a2713aSLionel Sambuc int PR4386_b = ((void *) PR4386_foo) != 0; // expected-error{{initializer element is not a compile-time constant}}
153*f4a2713aSLionel Sambuc int PR4386_c = ((void *) PR4386_zed) != 0;
154*f4a2713aSLionel Sambuc int PR4386_zed() __attribute((weak));
155*f4a2713aSLionel Sambuc 
156*f4a2713aSLionel Sambuc // <rdar://problem/10185490> (derived from SPEC vortex benchmark)
157*f4a2713aSLionel Sambuc typedef char strty[10];
158*f4a2713aSLionel Sambuc struct vortexstruct { strty s; };
159*f4a2713aSLionel Sambuc struct vortexstruct vortexvar = { "asdf" };
160*f4a2713aSLionel Sambuc 
161*f4a2713aSLionel Sambuc typedef struct { uintptr_t x : 2; } StructWithBitfield;
162*f4a2713aSLionel Sambuc StructWithBitfield bitfieldvar = { (uintptr_t)&bitfieldvar }; // expected-error {{initializer element is not a compile-time constant}}
163