Daniel Dunbar | c79f767 | 2010-09-07 22:54:28 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -verify -fsyntax-only -ffreestanding |
Chris Lattner | 4ef8dd6 | 2007-11-01 02:45:17 +0000 | [diff] [blame] | 2 | |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 3 | #include <stddef.h> |
Sebastian Redl | 392cf91 | 2009-02-07 00:23:52 +0000 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | |
Chris Lattner | 4ef8dd6 | 2007-11-01 02:45:17 +0000 | [diff] [blame] | 6 | typedef void (* fp)(void); |
| 7 | void foo(void); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 8 | |
| 9 | // PR clang/3377 |
| 10 | fp a[(short int)1] = { foo }; |
Chris Lattner | 4ef8dd6 | 2007-11-01 02:45:17 +0000 | [diff] [blame] | 11 | |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 12 | int myArray[5] = {1, 2, 3, 4, 5}; |
| 13 | int *myPointer2 = myArray; |
| 14 | int *myPointer = &(myArray[2]); |
| 15 | |
Chris Lattner | d411e04 | 2007-12-02 07:47:49 +0000 | [diff] [blame] | 16 | |
| 17 | extern int x; |
| 18 | void *g = &x; |
| 19 | int *h = &x; |
| 20 | |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 21 | struct union_crash |
| 22 | { |
| 23 | union |
| 24 | { |
| 25 | }; |
| 26 | }; |
Aaron Ballman | 0ca7e8b | 2012-02-09 03:29:06 +0000 | [diff] [blame] | 27 | |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 28 | int test() { |
Aaron Ballman | 0ca7e8b | 2012-02-09 03:29:06 +0000 | [diff] [blame] | 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'}} |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 34 | } |
Chris Lattner | be34ac6 | 2008-05-04 01:13:36 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | // PR2050 |
| 38 | struct cdiff_cmd { |
| 39 | const char *name; |
| 40 | unsigned short argc; |
| 41 | int (*handler)(); |
| 42 | }; |
| 43 | int cdiff_cmd_open(); |
| 44 | struct cdiff_cmd commands[] = { |
| 45 | {"OPEN", 1, &cdiff_cmd_open } |
| 46 | }; |
| 47 | |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 48 | // PR2348 |
| 49 | static struct { int z; } s[2]; |
| 50 | int *t = &(*s).z; |
| 51 | |
| 52 | // PR2349 |
| 53 | short *a2(void) |
| 54 | { |
| 55 | short int b; |
Chris Lattner | d880363 | 2008-08-10 01:58:45 +0000 | [diff] [blame] | 56 | static short *bp = &b; // expected-error {{initializer element is not a compile-time constant}} |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 57 | |
| 58 | return bp; |
| 59 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 60 | |
| 61 | int pbool(void) { |
| 62 | typedef const _Bool cbool; |
| 63 | _Bool pbool1 = (void *) 0; |
| 64 | cbool pbool2 = &pbool; |
| 65 | return pbool2; |
| 66 | } |
| 67 | |
Chris Lattner | d5bac57 | 2008-08-19 00:58:40 +0000 | [diff] [blame] | 68 | |
| 69 | // rdar://5870981 |
| 70 | union { float f; unsigned u; } u = { 1.0f }; |
| 71 | |
Daniel Dunbar | 1e465df | 2008-08-25 20:08:27 +0000 | [diff] [blame] | 72 | // rdar://6156694 |
| 73 | int f3(int x) { return x; } |
| 74 | typedef void (*vfunc)(void); |
| 75 | void *bar = (vfunc) f3; |
Eli Friedman | b529d83 | 2008-09-02 09:37:00 +0000 | [diff] [blame] | 76 | |
| 77 | // PR2747 |
| 78 | struct sym_reg { |
| 79 | char nc_gpreg; |
| 80 | }; |
| 81 | int sym_fw1a_scr[] = { |
Nuno Lopes | 67c8601 | 2008-09-02 10:10:14 +0000 | [diff] [blame] | 82 | ((int)(&((struct sym_reg *)0)->nc_gpreg)) & 0, |
| 83 | 8 * ((int)(&((struct sym_reg *)0)->nc_gpreg)) |
Eli Friedman | b529d83 | 2008-09-02 09:37:00 +0000 | [diff] [blame] | 84 | }; |
Chris Lattner | e2f5619 | 2008-11-03 09:28:22 +0000 | [diff] [blame] | 85 | |
| 86 | // PR3001 |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 87 | struct s1 s2 = { // expected-error {{variable has incomplete type 'struct s1'}} \ |
| 88 | // expected-note {{forward declaration of 'struct s1'}} |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 89 | .a = sizeof(struct s3), // expected-error {{invalid application of 'sizeof'}} \ |
| 90 | // expected-note{{forward declaration of 'struct s3'}} |
Chris Lattner | e2f5619 | 2008-11-03 09:28:22 +0000 | [diff] [blame] | 91 | .b = bogus // expected-error {{use of undeclared identifier 'bogus'}} |
| 92 | } |
| 93 | |
Anders Carlsson | 91b9f20 | 2009-01-24 17:47:50 +0000 | [diff] [blame] | 94 | // PR3382 |
| 95 | char t[] = ("Hello"); |
Douglas Gregor | d079b2d | 2009-01-30 17:42:28 +0000 | [diff] [blame] | 96 | |
| 97 | // <rdar://problem/6094855> |
| 98 | typedef struct { } empty; |
| 99 | |
| 100 | typedef struct { |
| 101 | empty e; |
| 102 | int i2; |
| 103 | } st; |
| 104 | |
| 105 | st st1 = { .i2 = 1 }; |
| 106 | |
| 107 | // <rdar://problem/6096826> |
| 108 | struct { |
| 109 | int a; |
| 110 | int z[2]; |
| 111 | } y = { .z = {} }; |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 112 | |
| 113 | int bbb[10]; |
| 114 | |
| 115 | struct foo2 { |
Eli Friedman | c91b7d9 | 2009-02-22 07:40:57 +0000 | [diff] [blame] | 116 | uintptr_t a; |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | struct foo2 bar2[] = { |
Sebastian Redl | 392cf91 | 2009-02-07 00:23:52 +0000 | [diff] [blame] | 120 | { (intptr_t)bbb } |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 121 | }; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 122 | |
| 123 | struct foo2 bar3 = { 1, 2 }; // expected-warning{{excess elements in struct initializer}} |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 124 | |
| 125 | int* ptest1 = __builtin_choose_expr(1, (int*)0, (int*)0); |
| 126 | |
| 127 | typedef int32_t ivector4 __attribute((vector_size(16))); |
| 128 | ivector4 vtest1 = 1 ? (ivector4){1} : (ivector4){1}; |
| 129 | ivector4 vtest2 = __builtin_choose_expr(1, (ivector4){1}, (ivector4){1}); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 130 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 131 | uintptr_t ptrasintadd1 = (uintptr_t)&a - 4; |
| 132 | uintptr_t ptrasintadd2 = (uintptr_t)&a + 4; |
| 133 | uintptr_t ptrasintadd3 = 4 + (uintptr_t)&a; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 134 | |
| 135 | // PR4285 |
| 136 | const wchar_t widestr[] = L"asdf"; |
Eli Friedman | 67f85fc | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 137 | |
| 138 | // PR5447 |
| 139 | const double pr5447 = (0.05 < -1.0) ? -1.0 : 0.0499878; |
| 140 | |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 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. |
| 147 | int PR4386_bar(); |
| 148 | int PR4386_foo() __attribute((weak)); |
| 149 | int PR4386_zed(); |
| 150 | |
| 151 | int PR4386_a = ((void *) PR4386_bar) != 0; |
| 152 | int PR4386_b = ((void *) PR4386_foo) != 0; // expected-error{{initializer element is not a compile-time constant}} |
| 153 | int PR4386_c = ((void *) PR4386_zed) != 0; |
| 154 | int PR4386_zed() __attribute((weak)); |
Chris Lattner | 16c5dea | 2010-10-10 18:16:20 +0000 | [diff] [blame] | 155 | |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 156 | // <rdar://problem/10185490> (derived from SPEC vortex benchmark) |
| 157 | typedef char strty[10]; |
| 158 | struct vortexstruct { strty s; }; |
| 159 | struct vortexstruct vortexvar = { "asdf" }; |