1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s 2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wgnu -Wc11-extensions -verify %s 3*f4a2713aSLionel Sambuc // REQUIRES: LP64 4*f4a2713aSLionel Sambuc 5*f4a2713aSLionel Sambuc extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}} 6*f4a2713aSLionel Sambuc 7*f4a2713aSLionel Sambuc static int x, y, z; 8*f4a2713aSLionel Sambuc 9*f4a2713aSLionel Sambuc static int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}} 10*f4a2713aSLionel Sambuc int ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}} 11*f4a2713aSLionel Sambuc 12*f4a2713aSLionel Sambuc extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}} 13*f4a2713aSLionel Sambuc 14*f4a2713aSLionel Sambuc static long ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}} 15*f4a2713aSLionel Sambuc 16*f4a2713aSLionel Sambuc void func() { 17*f4a2713aSLionel Sambuc int x = 1; 18*f4a2713aSLionel Sambuc 19*f4a2713aSLionel Sambuc typedef int TInt = 1; // expected-error{{illegal initializer (only variables can be initialized)}} 20*f4a2713aSLionel Sambuc 21*f4a2713aSLionel Sambuc int xComputeSize[] = { 1, 3, 5 }; 22*f4a2713aSLionel Sambuc 23*f4a2713aSLionel Sambuc int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}} 24*f4a2713aSLionel Sambuc 25*f4a2713aSLionel Sambuc int x4 = { 1, 2 }; // expected-warning{{excess elements in scalar initializer}} 26*f4a2713aSLionel Sambuc 27*f4a2713aSLionel Sambuc int y[4][3] = { 28*f4a2713aSLionel Sambuc { 1, 3, 5 }, 29*f4a2713aSLionel Sambuc { 2, 4, 6 }, 30*f4a2713aSLionel Sambuc { 3, 5, 7 }, 31*f4a2713aSLionel Sambuc }; 32*f4a2713aSLionel Sambuc 33*f4a2713aSLionel Sambuc int y2[4][3] = { 34*f4a2713aSLionel Sambuc 1, 3, 5, 2, 4, 6, 3, 5, 7 35*f4a2713aSLionel Sambuc }; 36*f4a2713aSLionel Sambuc 37*f4a2713aSLionel Sambuc int y3[4][3] = { 38*f4a2713aSLionel Sambuc { 1, 3, 5 }, 39*f4a2713aSLionel Sambuc { 2, 4, 6 }, 40*f4a2713aSLionel Sambuc { 3, 5, 7 }, 41*f4a2713aSLionel Sambuc { 4, 6, 8 }, 42*f4a2713aSLionel Sambuc { 5 }, // expected-warning{{excess elements in array initializer}} 43*f4a2713aSLionel Sambuc }; 44*f4a2713aSLionel Sambuc 45*f4a2713aSLionel Sambuc struct threeElements { 46*f4a2713aSLionel Sambuc int a,b,c; 47*f4a2713aSLionel Sambuc } z = { 1 }; 48*f4a2713aSLionel Sambuc 49*f4a2713aSLionel Sambuc struct threeElements *p = 7; // expected-warning{{incompatible integer to pointer conversion initializing 'struct threeElements *' with an expression of type 'int'}} 50*f4a2713aSLionel Sambuc 51*f4a2713aSLionel Sambuc extern int blockScopeExtern[3] = { 1, 3, 5 }; // expected-error{{'extern' variable cannot have an initializer}} 52*f4a2713aSLionel Sambuc 53*f4a2713aSLionel Sambuc static long x2[3] = { 1.0, 54*f4a2713aSLionel Sambuc "abc", // expected-warning{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char [4]'}} 55*f4a2713aSLionel Sambuc 5.8 }; // expected-warning {{implicit conversion from 'double' to 'long' changes value from 5.8 to 5}} 56*f4a2713aSLionel Sambuc } 57*f4a2713aSLionel Sambuc 58*f4a2713aSLionel Sambuc void test() { 59*f4a2713aSLionel Sambuc int y1[3] = { 60*f4a2713aSLionel Sambuc { 1, 2, 3 } // expected-warning{{excess elements in scalar initializer}} 61*f4a2713aSLionel Sambuc }; 62*f4a2713aSLionel Sambuc int y3[4][3] = { 63*f4a2713aSLionel Sambuc { 1, 3, 5 }, 64*f4a2713aSLionel Sambuc { 2, 4, 6 }, 65*f4a2713aSLionel Sambuc { 3, 5, 7 }, 66*f4a2713aSLionel Sambuc { 4, 6, 8 }, 67*f4a2713aSLionel Sambuc { }, // expected-warning{{use of GNU empty initializer extension}} expected-warning{{excess elements in array initializer}} 68*f4a2713aSLionel Sambuc }; 69*f4a2713aSLionel Sambuc int y4[4][3] = { 70*f4a2713aSLionel Sambuc { 1, 3, 5, 2 }, // expected-warning{{excess elements in array initializer}} 71*f4a2713aSLionel Sambuc { 4, 6 }, 72*f4a2713aSLionel Sambuc { 3, 5, 7 }, 73*f4a2713aSLionel Sambuc { 4, 6, 8 }, 74*f4a2713aSLionel Sambuc }; 75*f4a2713aSLionel Sambuc } 76*f4a2713aSLionel Sambuc 77*f4a2713aSLionel Sambuc void allLegalAndSynonymous() { 78*f4a2713aSLionel Sambuc short q[4][3][2] = { 79*f4a2713aSLionel Sambuc { 1 }, 80*f4a2713aSLionel Sambuc { 2, 3 }, 81*f4a2713aSLionel Sambuc { 4, 5, 6 } 82*f4a2713aSLionel Sambuc }; 83*f4a2713aSLionel Sambuc short q2[4][3][2] = { 84*f4a2713aSLionel Sambuc { 1, 0, 0, 0, 0, 0 }, 85*f4a2713aSLionel Sambuc { 2, 3, 0, 0, 0, 0 }, 86*f4a2713aSLionel Sambuc { 4, 5, 6 } 87*f4a2713aSLionel Sambuc }; 88*f4a2713aSLionel Sambuc short q3[4][3][2] = { 89*f4a2713aSLionel Sambuc { 90*f4a2713aSLionel Sambuc { 1 }, 91*f4a2713aSLionel Sambuc }, 92*f4a2713aSLionel Sambuc { 93*f4a2713aSLionel Sambuc { 2, 3 }, 94*f4a2713aSLionel Sambuc }, 95*f4a2713aSLionel Sambuc { 96*f4a2713aSLionel Sambuc { 4, 5 }, 97*f4a2713aSLionel Sambuc { 6 }, 98*f4a2713aSLionel Sambuc }, 99*f4a2713aSLionel Sambuc }; 100*f4a2713aSLionel Sambuc } 101*f4a2713aSLionel Sambuc 102*f4a2713aSLionel Sambuc void legal() { 103*f4a2713aSLionel Sambuc short q[][3][2] = { 104*f4a2713aSLionel Sambuc { 1 }, 105*f4a2713aSLionel Sambuc { 2, 3 }, 106*f4a2713aSLionel Sambuc { 4, 5, 6 } 107*f4a2713aSLionel Sambuc }; 108*f4a2713aSLionel Sambuc int q_sizecheck[(sizeof(q) / sizeof(short [3][2])) == 3? 1 : -1]; 109*f4a2713aSLionel Sambuc } 110*f4a2713aSLionel Sambuc 111*f4a2713aSLionel Sambuc unsigned char asso_values[] = { 34 }; 112*f4a2713aSLionel Sambuc int legal2() { 113*f4a2713aSLionel Sambuc return asso_values[0]; 114*f4a2713aSLionel Sambuc } 115*f4a2713aSLionel Sambuc 116*f4a2713aSLionel Sambuc void illegal() { 117*f4a2713aSLionel Sambuc short q2[4][][2] = { // expected-error{{array has incomplete element type 'short [][2]'}} 118*f4a2713aSLionel Sambuc { 1, 0, 0, 0, 0, 0 }, 119*f4a2713aSLionel Sambuc { 2, 3, 0, 0, 0, 0 }, 120*f4a2713aSLionel Sambuc { 4, 5, 6 } 121*f4a2713aSLionel Sambuc }; 122*f4a2713aSLionel Sambuc short q3[4][3][] = { // expected-error{{array has incomplete element type 'short []'}} 123*f4a2713aSLionel Sambuc { 124*f4a2713aSLionel Sambuc { 1 }, 125*f4a2713aSLionel Sambuc }, 126*f4a2713aSLionel Sambuc { 127*f4a2713aSLionel Sambuc { 2, 3 }, 128*f4a2713aSLionel Sambuc }, 129*f4a2713aSLionel Sambuc { 130*f4a2713aSLionel Sambuc { 4, 5 }, 131*f4a2713aSLionel Sambuc { 6 }, 132*f4a2713aSLionel Sambuc }, 133*f4a2713aSLionel Sambuc }; 134*f4a2713aSLionel Sambuc int a[][] = { 1, 2 }; // expected-error{{array has incomplete element type 'int []'}} 135*f4a2713aSLionel Sambuc } 136*f4a2713aSLionel Sambuc 137*f4a2713aSLionel Sambuc typedef int AryT[]; 138*f4a2713aSLionel Sambuc 139*f4a2713aSLionel Sambuc void testTypedef() 140*f4a2713aSLionel Sambuc { 141*f4a2713aSLionel Sambuc AryT a = { 1, 2 }, b = { 3, 4, 5 }; 142*f4a2713aSLionel Sambuc int a_sizecheck[(sizeof(a) / sizeof(int)) == 2? 1 : -1]; 143*f4a2713aSLionel Sambuc int b_sizecheck[(sizeof(b) / sizeof(int)) == 3? 1 : -1]; 144*f4a2713aSLionel Sambuc } 145*f4a2713aSLionel Sambuc 146*f4a2713aSLionel Sambuc static char const xx[] = "test"; 147*f4a2713aSLionel Sambuc int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1]; 148*f4a2713aSLionel Sambuc static char const yy[5] = "test"; 149*f4a2713aSLionel Sambuc static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}} 150*f4a2713aSLionel Sambuc 151*f4a2713aSLionel Sambuc void charArrays() { 152*f4a2713aSLionel Sambuc static char const test[] = "test"; 153*f4a2713aSLionel Sambuc int test_sizecheck[(sizeof(test) / sizeof(char)) == 5? 1 : -1]; 154*f4a2713aSLionel Sambuc static char const test2[] = { "weird stuff" }; 155*f4a2713aSLionel Sambuc static char const test3[] = { "test", "excess stuff" }; // expected-warning{{excess elements in char array initializer}} 156*f4a2713aSLionel Sambuc 157*f4a2713aSLionel Sambuc char* cp[] = { "Hello" }; 158*f4a2713aSLionel Sambuc 159*f4a2713aSLionel Sambuc char c[] = { "Hello" }; 160*f4a2713aSLionel Sambuc int l[sizeof(c) == 6 ? 1 : -1]; 161*f4a2713aSLionel Sambuc 162*f4a2713aSLionel Sambuc int i[] = { "Hello "}; // expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [7]'}} 163*f4a2713aSLionel Sambuc char c2[] = { "Hello", "Good bye" }; //expected-warning{{excess elements in char array initializer}} 164*f4a2713aSLionel Sambuc 165*f4a2713aSLionel Sambuc int i2[1] = { "Hello" }; //expected-warning{{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [6]'}} 166*f4a2713aSLionel Sambuc char c3[5] = { "Hello" }; 167*f4a2713aSLionel Sambuc char c4[4] = { "Hello" }; //expected-warning{{initializer-string for char array is too long}} 168*f4a2713aSLionel Sambuc 169*f4a2713aSLionel Sambuc int i3[] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}} 170*f4a2713aSLionel Sambuc } 171*f4a2713aSLionel Sambuc 172*f4a2713aSLionel Sambuc void variableArrayInit() { 173*f4a2713aSLionel Sambuc int a = 4; 174*f4a2713aSLionel Sambuc char strlit[a] = "foo"; //expected-error{{variable-sized object may not be initialized}} 175*f4a2713aSLionel Sambuc int b[a] = { 1, 2, 4 }; //expected-error{{variable-sized object may not be initialized}} 176*f4a2713aSLionel Sambuc } 177*f4a2713aSLionel Sambuc 178*f4a2713aSLionel Sambuc // Pure array tests 179*f4a2713aSLionel Sambuc float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}} 180*f4a2713aSLionel Sambuc float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}} 181*f4a2713aSLionel Sambuc char r3[][5] = {1,2,3,4,5,6}; 182*f4a2713aSLionel Sambuc int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1]; 183*f4a2713aSLionel Sambuc char r3_2[sizeof r3 == 10 ? 1 : -1]; 184*f4a2713aSLionel Sambuc float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}} 185*f4a2713aSLionel Sambuc char r5[][5] = {"aa", "bbb", "ccccc"}; 186*f4a2713aSLionel Sambuc char r6[sizeof r5 == 15 ? 1 : -1]; 187*f4a2713aSLionel Sambuc const char r7[] = "zxcv"; 188*f4a2713aSLionel Sambuc char r8[5] = "5char"; 189*f4a2713aSLionel Sambuc char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}} 190*f4a2713aSLionel Sambuc unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" ))); 191*f4a2713aSLionel Sambuc int r11[0] = {}; //expected-warning{{zero size arrays are an extension}} expected-warning{{use of GNU empty initializer extension}} 192*f4a2713aSLionel Sambuc 193*f4a2713aSLionel Sambuc // Some struct tests 194*f4a2713aSLionel Sambuc void autoStructTest() { 195*f4a2713aSLionel Sambuc struct s1 {char a; char b;} t1; 196*f4a2713aSLionel Sambuc struct s2 {struct s1 c;} t2 = { t1 }; 197*f4a2713aSLionel Sambuc // The following is a less than great diagnostic (though it's on par with EDG). 198*f4a2713aSLionel Sambuc struct s1 t3[] = {t1, t1, "abc", 0}; //expected-warning{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char [4]'}} 199*f4a2713aSLionel Sambuc int t4[sizeof t3 == 6 ? 1 : -1]; 200*f4a2713aSLionel Sambuc } 201*f4a2713aSLionel Sambuc struct foo { int z; } w; 202*f4a2713aSLionel Sambuc int bar (void) { 203*f4a2713aSLionel Sambuc struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}} 204*f4a2713aSLionel Sambuc return z.z; 205*f4a2713aSLionel Sambuc } 206*f4a2713aSLionel Sambuc struct s3 {void (*a)(void);} t5 = {autoStructTest}; 207*f4a2713aSLionel Sambuc struct {int a; int b[];} t6 = {1, {1, 2, 3}}; // expected-warning{{flexible array initialization is a GNU extension}} \ 208*f4a2713aSLionel Sambuc // expected-note{{initialized flexible array member 'b' is here}} 209*f4a2713aSLionel Sambuc union {char a; int b;} t7[] = {1, 2, 3}; 210*f4a2713aSLionel Sambuc int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1]; 211*f4a2713aSLionel Sambuc 212*f4a2713aSLionel Sambuc struct bittest{int : 31, a, :21, :12, b;}; 213*f4a2713aSLionel Sambuc struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}} 214*f4a2713aSLionel Sambuc 215*f4a2713aSLionel Sambuc // Not completely sure what should happen here... 216*f4a2713aSLionel Sambuc int u1 = {}; //expected-warning{{use of GNU empty initializer extension}} expected-error{{scalar initializer cannot be empty}} 217*f4a2713aSLionel Sambuc int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}} 218*f4a2713aSLionel Sambuc 219*f4a2713aSLionel Sambuc // PR2362 220*f4a2713aSLionel Sambuc void varArray() { 221*f4a2713aSLionel Sambuc int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}} 222*f4a2713aSLionel Sambuc } 223*f4a2713aSLionel Sambuc 224*f4a2713aSLionel Sambuc // PR2151 225*f4a2713aSLionel Sambuc void emptyInit() {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \ 226*f4a2713aSLionel Sambuc // expected-error{{initializer for aggregate with no elements}} 227*f4a2713aSLionel Sambuc 228*f4a2713aSLionel Sambuc void noNamedInit() { 229*f4a2713aSLionel Sambuc struct {int:5;} x[] = {6}; //expected-error{{initializer for aggregate with no elements}} \ 230*f4a2713aSLionel Sambuc // expected-warning {{struct without named members is a GNU extension}} 231*f4a2713aSLionel Sambuc } 232*f4a2713aSLionel Sambuc struct {int a; int:5;} noNamedImplicit[] = {1,2,3}; 233*f4a2713aSLionel Sambuc int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1]; 234*f4a2713aSLionel Sambuc 235*f4a2713aSLionel Sambuc 236*f4a2713aSLionel Sambuc // ptrs are constant 237*f4a2713aSLionel Sambuc struct soft_segment_descriptor { 238*f4a2713aSLionel Sambuc long ssd_base; 239*f4a2713aSLionel Sambuc }; 240*f4a2713aSLionel Sambuc static int dblfault_tss; 241*f4a2713aSLionel Sambuc 242*f4a2713aSLionel Sambuc union uniao { int ola; } xpto[1]; 243*f4a2713aSLionel Sambuc 244*f4a2713aSLionel Sambuc struct soft_segment_descriptor gdt_segs[] = { 245*f4a2713aSLionel Sambuc {(long) &dblfault_tss}, 246*f4a2713aSLionel Sambuc { (long)xpto}, 247*f4a2713aSLionel Sambuc }; 248*f4a2713aSLionel Sambuc 249*f4a2713aSLionel Sambuc static void sppp_ipv6cp_up(); 250*f4a2713aSLionel Sambuc const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \ 251*f4a2713aSLionel Sambuc // expected-warning{{excess elements in struct initializer}} 252*f4a2713aSLionel Sambuc 253*f4a2713aSLionel Sambuc struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}} 254*f4a2713aSLionel Sambuc typedef struct _Matrix Matrix; 255*f4a2713aSLionel Sambuc void test_matrix() { 256*f4a2713aSLionel Sambuc const Matrix mat1 = { 257*f4a2713aSLionel Sambuc { { 1.0f, 2.0f, 3.0f, 4.0f, 258*f4a2713aSLionel Sambuc 5.0f, 6.0f, 7.0f, 8.0f, 259*f4a2713aSLionel Sambuc 9.0f, 10.0f, 11.0f, 12.0f, 260*f4a2713aSLionel Sambuc 13.0f, 14.0f, 15.0f, 16.0f } } 261*f4a2713aSLionel Sambuc }; 262*f4a2713aSLionel Sambuc 263*f4a2713aSLionel Sambuc const Matrix mat2 = { 264*f4a2713aSLionel Sambuc 1.0f, 2.0f, 3.0f, 4.0f, 265*f4a2713aSLionel Sambuc 5.0f, 6.0f, 7.0f, 8.0f, 266*f4a2713aSLionel Sambuc 9.0f, 10.0f, 11.0f, 12.0f, 267*f4a2713aSLionel Sambuc 13.0f, 14.0f, 15.0f, 16.0f 268*f4a2713aSLionel Sambuc }; 269*f4a2713aSLionel Sambuc } 270*f4a2713aSLionel Sambuc 271*f4a2713aSLionel Sambuc char badchararray[1] = { badchararray[0], "asdf" }; // expected-warning {{excess elements in array initializer}} expected-error {{initializer element is not a compile-time constant}} 272*f4a2713aSLionel Sambuc 273*f4a2713aSLionel Sambuc // Test the GNU extension for initializing an array from an array 274*f4a2713aSLionel Sambuc // compound literal. PR9261. 275*f4a2713aSLionel Sambuc typedef int int5[5]; 276*f4a2713aSLionel Sambuc int a1[5] = (int[]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int [5]' from a compound literal of type 'int [5]' is a GNU extension}} 277*f4a2713aSLionel Sambuc int a2[5] = (int[5]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int [5]' from a compound literal of type 'int [5]' is a GNU extension}} 278*f4a2713aSLionel Sambuc int a3[] = ((int[]){1, 2, 3, 4, 5}); // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int [5]' is a GNU extension}} 279*f4a2713aSLionel Sambuc int a4[] = (int[5]){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int [5]' is a GNU extension}} 280*f4a2713aSLionel Sambuc int a5[] = (int5){1, 2, 3, 4, 5}; // expected-warning{{initialization of an array of type 'int []' from a compound literal of type 'int5' (aka 'int [5]') is a GNU extension}} 281*f4a2713aSLionel Sambuc 282*f4a2713aSLionel Sambuc int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int [5]' with array of type 'int [3]'}} 283*f4a2713aSLionel Sambuc 284*f4a2713aSLionel Sambuc int nonconst_value(); 285*f4a2713aSLionel Sambuc int a7[5] = (int[5]){ 1, 2, 3, 4, nonconst_value() }; // expected-error{{initializer element is not a compile-time constant}} 286*f4a2713aSLionel Sambuc 287*f4a2713aSLionel Sambuc // <rdar://problem/10636946> 288*f4a2713aSLionel Sambuc __attribute__((weak)) const unsigned int test10_bound = 10; 289*f4a2713aSLionel Sambuc char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}} 290*f4a2713aSLionel Sambuc void test10() { 291*f4a2713aSLionel Sambuc char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}} 292*f4a2713aSLionel Sambuc } 293