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