Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 1 | // RUN: clang -fsyntax-only -verify -pedantic %s |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 2 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 3 | extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}} |
| 4 | |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 5 | static int x, y, z; |
| 6 | |
| 7 | static int ary[] = { x, y, z }; // expected-error{{initializer element is not constant}} |
| 8 | int ary2[] = { x, y, z }; // expected-error{{initializer element is not constant}} |
| 9 | |
| 10 | extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}} |
| 11 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 12 | static int ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'char [4]', expected 'int'}} |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 13 | |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 14 | void func() { |
| 15 | int x = 1; |
| 16 | |
Steve Naroff | 410e3e2 | 2007-09-12 20:13:48 +0000 | [diff] [blame] | 17 | typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}} |
| 18 | |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 19 | int xComputeSize[] = { 1, 3, 5 }; |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 20 | |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 21 | int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}} |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 22 | |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 23 | int x4 = { 1, 2 }; // expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}} |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 24 | |
| 25 | int y[4][3] = { |
| 26 | { 1, 3, 5 }, |
| 27 | { 2, 4, 6 }, |
| 28 | { 3, 5, 7 }, |
| 29 | }; |
| 30 | |
| 31 | int y2[4][3] = { |
| 32 | 1, 3, 5, 2, 4, 6, 3, 5, 7 |
| 33 | }; |
| 34 | |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 35 | int y3[4][3] = { |
| 36 | { 1, 3, 5 }, |
| 37 | { 2, 4, 6 }, |
| 38 | { 3, 5, 7 }, |
| 39 | { 4, 6, 8 }, |
| 40 | { 5 }, // expected-warning{{excess elements in array initializer}} |
| 41 | }; |
| 42 | |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 43 | struct threeElements { |
| 44 | int a,b,c; |
| 45 | } z = { 1 }; |
| 46 | |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 47 | struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'int', expected 'struct threeElements *'}} |
Steve Naroff | 6f9f307 | 2007-09-02 15:34:30 +0000 | [diff] [blame] | 48 | |
| 49 | extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}} |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 50 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 51 | static int x2[3] = { 1.0, "abc" , 5.8 }; // expected-warning{{incompatible pointer to integer conversion initializing 'char [4]', expected 'int'}} |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 52 | } |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 53 | |
| 54 | void test() { |
| 55 | int y1[3] = { |
| 56 | { 1, 2, 3 } // expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}} |
| 57 | }; |
| 58 | int y3[4][3] = { |
| 59 | { 1, 3, 5 }, |
| 60 | { 2, 4, 6 }, |
| 61 | { 3, 5, 7 }, |
| 62 | { 4, 6, 8 }, |
| 63 | { }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}} |
| 64 | }; |
| 65 | int y4[4][3] = { |
| 66 | { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}} |
| 67 | { 4, 6 }, |
| 68 | { 3, 5, 7 }, |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 69 | { 4, 6, 8 }, |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 70 | }; |
| 71 | } |
| 72 | |
| 73 | void allLegalAndSynonymous() { |
| 74 | short q[4][3][2] = { |
| 75 | { 1 }, |
| 76 | { 2, 3 }, |
| 77 | { 4, 5, 6 } |
| 78 | }; |
| 79 | short q2[4][3][2] = { |
| 80 | { 1, 0, 0, 0, 0, 0 }, |
| 81 | { 2, 3, 0, 0, 0, 0 }, |
| 82 | { 4, 5, 6 } |
| 83 | }; |
| 84 | short q3[4][3][2] = { |
| 85 | { |
| 86 | { 1 }, |
| 87 | }, |
| 88 | { |
| 89 | { 2, 3 }, |
| 90 | }, |
| 91 | { |
| 92 | { 4, 5 }, |
| 93 | { 6 }, |
| 94 | }, |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | void legal() { |
| 99 | short q[][3][2] = { |
| 100 | { 1 }, |
| 101 | { 2, 3 }, |
| 102 | { 4, 5, 6 } |
| 103 | }; |
| 104 | } |
| 105 | |
Steve Naroff | fd8b4a4 | 2007-10-18 03:27:23 +0000 | [diff] [blame] | 106 | unsigned char asso_values[] = { 34 }; |
| 107 | int legal2() { |
| 108 | return asso_values[0]; |
| 109 | } |
| 110 | |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 111 | void illegal() { |
| 112 | short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}} |
| 113 | { 1, 0, 0, 0, 0, 0 }, |
| 114 | { 2, 3, 0, 0, 0, 0 }, |
| 115 | { 4, 5, 6 } |
| 116 | }; |
| 117 | short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}} |
| 118 | { |
| 119 | { 1 }, |
| 120 | }, |
| 121 | { |
| 122 | { 2, 3 }, |
| 123 | }, |
| 124 | { |
| 125 | { 4, 5 }, |
| 126 | { 6 }, |
| 127 | }, |
| 128 | }; |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 129 | int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}} |
Steve Naroff | 371227d | 2007-09-04 02:20:04 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | typedef int AryT[]; |
| 133 | |
| 134 | void testTypedef() |
| 135 | { |
| 136 | AryT a = { 1, 2 }, b = { 3, 4, 5 }; |
| 137 | } |
| 138 | |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 139 | static char const xx[] = "test"; |
| 140 | static char const yy[5] = "test"; |
| 141 | static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}} |
| 142 | |
| 143 | void charArrays() |
| 144 | { |
| 145 | static char const test[] = "test"; |
| 146 | static char const test2[] = { "weird stuff" }; |
| 147 | static char const test3[] = { "test", "excess stuff" }; // expected-error{{excess elements in char array initializer}} |
| 148 | |
| 149 | char* cp[] = { "Hello" }; |
| 150 | |
| 151 | char c[] = { "Hello" }; |
| 152 | int l[sizeof(c) == 6 ? 1 : -1]; |
| 153 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 154 | int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'char [7]', expected 'int'}} |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 155 | char c2[] = { "Hello", "Good bye" }; //expected-error{{excess elements in char array initializer}} |
| 156 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 157 | int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'char [6]', expected 'int'}} |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 158 | char c3[5] = { "Hello" }; |
| 159 | char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}} |
| 160 | |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 161 | int i3[] = {}; //expected-error{{at least one initializer value required to size array}} expected-warning{{use of GNU empty initializer extension}} |
Steve Naroff | 2fdc374 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Steve Naroff | ca10730 | 2008-01-21 23:53:58 +0000 | [diff] [blame] | 164 | void variableArrayInit() { |
| 165 | int a = 4; |
| 166 | char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}} |
| 167 | int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}} |
| 168 | } |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 169 | |
| 170 | // Pure array tests |
| 171 | float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}} |
| 172 | float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}} |
| 173 | char r3[][5] = {1,2,3,4,5,6}; |
| 174 | char r3_2[sizeof r3 == 10 ? 1 : -1]; |
| 175 | float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}} |
| 176 | char r5[][5] = {"aa", "bbb", "ccccc"}; |
| 177 | char r6[sizeof r5 == 15 ? 1 : -1]; |
| 178 | const char r7[] = "zxcv"; |
| 179 | char r8[5] = "5char"; |
| 180 | char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}} |
| 181 | |
| 182 | int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}} |
| 183 | |
| 184 | // Some struct tests |
| 185 | void autoStructTest() { |
| 186 | struct s1 {char a; char b;} t1; |
| 187 | struct s2 {struct s1 c;} t2 = { t1 }; |
| 188 | // The following is a less than great diagnostic (though it's on par with EDG). |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 189 | struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char [4]', expected 'char'}} |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 190 | int t4[sizeof t3 == 6 ? 1 : -1]; |
| 191 | } |
Steve Naroff | 578edc6 | 2008-01-28 02:00:41 +0000 | [diff] [blame] | 192 | struct foo { int z; } w; |
| 193 | int bar (void) { |
| 194 | struct foo z = { w }; //expected-error{{incompatible type initializing 'struct foo', expected 'int'}} |
| 195 | return z.z; |
| 196 | } |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 197 | struct s3 {void (*a)(void);} t5 = {autoStructTest}; |
| 198 | // GCC extension; flexible array init. Once this is implemented, the warning should be removed. |
| 199 | // Note that clang objc implementation depends on this extension. |
| 200 | struct {int a; int b[];} t6 = {1, {1, 2, 3}}; //expected-warning{{excess elements in array initializer}} |
| 201 | union {char a; int b;} t7[] = {1, 2, 3}; |
| 202 | int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1]; |
| 203 | |
| 204 | struct bittest{int : 31, a, :21, :12, b;}; |
| 205 | struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in array initializer}} |
| 206 | |
| 207 | // Not completely sure what should happen here... |
Eli Friedman | c56c977 | 2008-05-19 20:29:35 +0000 | [diff] [blame] | 208 | int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}} |
| 209 | int u2 = {{3}}; //expected-error{{too many braces around scalar initializer}} |
Steve Naroff | a996033 | 2008-01-25 00:51:06 +0000 | [diff] [blame] | 210 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame^] | 211 | // PR2362 |
| 212 | void varArray() { |
| 213 | int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}} |
| 214 | } |