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