Ted Kremenek | 9f3d942 | 2007-09-26 20:14:22 +0000 | [diff] [blame] | 1 | // RUN: clang -parse-ast -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 | |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 12 | static int ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible types assigning 'char *' to '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 | |
| 47 | struct threeElements *p = 7; // expected-warning{{incompatible types assigning 'int' to '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 | |
| 51 | static int x2[3] = { 1.0, "abc" , 5.8 }; // expected-warning{{incompatible types assigning 'char *' to '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 }, |
| 69 | { 4, 6, 8 }, // expected-warning{{excess elements in array initializer}} |
| 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 | |
| 106 | void illegal() { |
| 107 | short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}} |
| 108 | { 1, 0, 0, 0, 0, 0 }, |
| 109 | { 2, 3, 0, 0, 0, 0 }, |
| 110 | { 4, 5, 6 } |
| 111 | }; |
| 112 | short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}} |
| 113 | { |
| 114 | { 1 }, |
| 115 | }, |
| 116 | { |
| 117 | { 2, 3 }, |
| 118 | }, |
| 119 | { |
| 120 | { 4, 5 }, |
| 121 | { 6 }, |
| 122 | }, |
| 123 | }; |
| 124 | // FIXME: the following two errors are redundant |
| 125 | int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}} expected-error{{variable has incomplete type 'int []'}} |
| 126 | } |
| 127 | |
| 128 | typedef int AryT[]; |
| 129 | |
| 130 | void testTypedef() |
| 131 | { |
| 132 | AryT a = { 1, 2 }, b = { 3, 4, 5 }; |
| 133 | } |
| 134 | |