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