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 | |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 21 | int test() { |
| 22 | int a[10]; |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 23 | int b[10] = a; // expected-error {{array initializer must be an initializer list}} |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 24 | int +; // expected-error {{expected identifier or '('}} |
Eli Friedman | a312ce2 | 2008-02-08 00:48:24 +0000 | [diff] [blame] | 25 | } |
Chris Lattner | be34ac6 | 2008-05-04 01:13:36 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | // PR2050 |
| 29 | struct cdiff_cmd { |
| 30 | const char *name; |
| 31 | unsigned short argc; |
| 32 | int (*handler)(); |
| 33 | }; |
| 34 | int cdiff_cmd_open(); |
| 35 | struct cdiff_cmd commands[] = { |
| 36 | {"OPEN", 1, &cdiff_cmd_open } |
| 37 | }; |
| 38 | |
Eli Friedman | 97c0a39 | 2008-05-21 03:39:11 +0000 | [diff] [blame] | 39 | // PR2348 |
| 40 | static struct { int z; } s[2]; |
| 41 | int *t = &(*s).z; |
| 42 | |
| 43 | // PR2349 |
| 44 | short *a2(void) |
| 45 | { |
| 46 | short int b; |
Chris Lattner | d880363 | 2008-08-10 01:58:45 +0000 | [diff] [blame] | 47 | 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] | 48 | |
| 49 | return bp; |
| 50 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 51 | |
| 52 | int pbool(void) { |
| 53 | typedef const _Bool cbool; |
| 54 | _Bool pbool1 = (void *) 0; |
| 55 | cbool pbool2 = &pbool; |
| 56 | return pbool2; |
| 57 | } |
| 58 | |
Chris Lattner | d5bac57 | 2008-08-19 00:58:40 +0000 | [diff] [blame] | 59 | |
| 60 | // rdar://5870981 |
| 61 | union { float f; unsigned u; } u = { 1.0f }; |
| 62 | |
Daniel Dunbar | 1e465df | 2008-08-25 20:08:27 +0000 | [diff] [blame] | 63 | // rdar://6156694 |
| 64 | int f3(int x) { return x; } |
| 65 | typedef void (*vfunc)(void); |
| 66 | void *bar = (vfunc) f3; |
Eli Friedman | b529d83 | 2008-09-02 09:37:00 +0000 | [diff] [blame] | 67 | |
| 68 | // PR2747 |
| 69 | struct sym_reg { |
| 70 | char nc_gpreg; |
| 71 | }; |
| 72 | int sym_fw1a_scr[] = { |
Nuno Lopes | 67c8601 | 2008-09-02 10:10:14 +0000 | [diff] [blame] | 73 | ((int)(&((struct sym_reg *)0)->nc_gpreg)) & 0, |
| 74 | 8 * ((int)(&((struct sym_reg *)0)->nc_gpreg)) |
Eli Friedman | b529d83 | 2008-09-02 09:37:00 +0000 | [diff] [blame] | 75 | }; |
Chris Lattner | e2f5619 | 2008-11-03 09:28:22 +0000 | [diff] [blame] | 76 | |
| 77 | // PR3001 |
John McCall | 7727acf | 2010-03-31 02:13:20 +0000 | [diff] [blame] | 78 | struct s1 s2 = { // expected-error {{variable has incomplete type 'struct s1'}} \ |
| 79 | // expected-note {{forward declaration of 'struct s1'}} |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 80 | .a = sizeof(struct s3), // expected-error {{invalid application of 'sizeof'}} \ |
| 81 | // expected-note{{forward declaration of 'struct s3'}} |
Chris Lattner | e2f5619 | 2008-11-03 09:28:22 +0000 | [diff] [blame] | 82 | .b = bogus // expected-error {{use of undeclared identifier 'bogus'}} |
| 83 | } |
| 84 | |
Anders Carlsson | 91b9f20 | 2009-01-24 17:47:50 +0000 | [diff] [blame] | 85 | // PR3382 |
| 86 | char t[] = ("Hello"); |
Douglas Gregor | d079b2d | 2009-01-30 17:42:28 +0000 | [diff] [blame] | 87 | |
| 88 | // <rdar://problem/6094855> |
| 89 | typedef struct { } empty; |
| 90 | |
| 91 | typedef struct { |
| 92 | empty e; |
| 93 | int i2; |
| 94 | } st; |
| 95 | |
| 96 | st st1 = { .i2 = 1 }; |
| 97 | |
| 98 | // <rdar://problem/6096826> |
| 99 | struct { |
| 100 | int a; |
| 101 | int z[2]; |
| 102 | } y = { .z = {} }; |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 103 | |
| 104 | int bbb[10]; |
| 105 | |
| 106 | struct foo2 { |
Eli Friedman | c91b7d9 | 2009-02-22 07:40:57 +0000 | [diff] [blame] | 107 | uintptr_t a; |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct foo2 bar2[] = { |
Sebastian Redl | 392cf91 | 2009-02-07 00:23:52 +0000 | [diff] [blame] | 111 | { (intptr_t)bbb } |
Douglas Gregor | d863517 | 2009-02-02 21:35:47 +0000 | [diff] [blame] | 112 | }; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 113 | |
| 114 | struct foo2 bar3 = { 1, 2 }; // expected-warning{{excess elements in struct initializer}} |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 115 | |
| 116 | int* ptest1 = __builtin_choose_expr(1, (int*)0, (int*)0); |
| 117 | |
| 118 | typedef int32_t ivector4 __attribute((vector_size(16))); |
| 119 | ivector4 vtest1 = 1 ? (ivector4){1} : (ivector4){1}; |
| 120 | ivector4 vtest2 = __builtin_choose_expr(1, (ivector4){1}, (ivector4){1}); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 121 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 122 | uintptr_t ptrasintadd1 = (uintptr_t)&a - 4; |
| 123 | uintptr_t ptrasintadd2 = (uintptr_t)&a + 4; |
| 124 | uintptr_t ptrasintadd3 = 4 + (uintptr_t)&a; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 125 | |
| 126 | // PR4285 |
| 127 | const wchar_t widestr[] = L"asdf"; |
Eli Friedman | 67f85fc | 2009-12-04 02:12:53 +0000 | [diff] [blame] | 128 | |
| 129 | // PR5447 |
| 130 | const double pr5447 = (0.05 < -1.0) ? -1.0 : 0.0499878; |
| 131 | |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 132 | // PR4386 |
| 133 | |
| 134 | // None of these are constant initializers, but we implement GCC's old |
| 135 | // behaviour of accepting bar and zed but not foo. GCC's behaviour was |
| 136 | // changed in 2007 (rev 122551), so we should be able to change too one |
| 137 | // day. |
| 138 | int PR4386_bar(); |
| 139 | int PR4386_foo() __attribute((weak)); |
| 140 | int PR4386_zed(); |
| 141 | |
| 142 | int PR4386_a = ((void *) PR4386_bar) != 0; |
| 143 | int PR4386_b = ((void *) PR4386_foo) != 0; // expected-error{{initializer element is not a compile-time constant}} |
| 144 | int PR4386_c = ((void *) PR4386_zed) != 0; |
| 145 | int PR4386_zed() __attribute((weak)); |