brintos

brintos / llvm-project-archived public Read only

0
0
Text · 10.8 KiB · 54a7877 Raw
305 lines · c
1// RUN: %clang_cc1 -std=gnu99 -fsyntax-only -pedantic -verify=expected,pedantic %s2// RUN: %clang_cc1 -std=gnu99 -fsyntax-only -Wgnu -Wc11-extensions -verify %s3// REQUIRES: LP644 5extern int foof(void) = 1; // expected-error{{illegal initializer (only variables can be initialized)}}6 7static int x, y, z;8 9static int ary[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}10int ary2[] = { x, y, z }; // expected-error{{initializer element is not a compile-time constant}}11 12extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}13 14static long ary3[] = { 1, "abc", 3, 4 }; // expected-error{{incompatible pointer to integer conversion initializing 'long' with an expression of type 'char[4]'}}15 16void 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, 735  };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 58void 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 77void 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 102void 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 111unsigned char asso_values[] = { 34 };112int legal2(void) {113  return asso_values[0];114}115 116void 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 137typedef int AryT[];138 139void 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 146static char const xx[] = "test";147int xx_sizecheck[(sizeof(xx) / sizeof(char)) == 5? 1 : -1];148static char const yy[5] = "test";149static char const zz[3] = "test"; // expected-warning{{initializer-string for char array is too long}}150 151#pragma clang diagnostic push152#pragma clang diagnostic ignored "-Wexcess-initializers"153static char const zz_quiet[3] = "test";154#pragma clang diagnostic pop155 156void 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 push162#pragma clang diagnostic ignored "-Wexcess-initializers"163  static char const test3_quiet[] = {"test", "excess stuff"};164#pragma clang diagnostic pop165 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 181void 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 tests188float r1[10] = {{7}}; //expected-warning{{braces around scalar initializer}}189float r2[] = {{8}}; //expected-warning{{braces around scalar initializer}}190char r3[][5] = {1,2,3,4,5,6};191int r3_sizecheck[(sizeof(r3) / sizeof(char[5])) == 2? 1 : -1];192char r3_2[sizeof r3 == 10 ? 1 : -1];193float r4[1][2] = {1,{2},3,4}; //expected-warning{{braces around scalar initializer}} expected-warning{{excess elements in array initializer}}194char r5[][5] = {"aa", "bbb", "ccccc"};195char r6[sizeof r5 == 15 ? 1 : -1];196const char r7[] = "zxcv";197char r8[5] = "5char";198char r9[5] = "6chars"; //expected-warning{{initializer-string for char array is too long}}199unsigned char r10[] = __extension__ (_Generic(0, int: (__extension__ "foo" )));200int 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 tests203void autoStructTest(void) {204struct s1 {char a; char b;} t1;205struct s2 {struct s1 c;} t2 = { t1 };206// The following is a less than great diagnostic (though it's on par with EDG).207struct s1 t3[] = {t1, t1, "abc", 0}; //expected-error{{incompatible pointer to integer conversion initializing 'char' with an expression of type 'char[4]'}}208int t4[sizeof t3 == 6 ? 1 : -1];209}210struct foo { int z; } w;211int bar (void) {212  struct foo z = { w }; //expected-error{{initializing 'int' with an expression of incompatible type 'struct foo'}}213  return z.z;214}215struct s3 {void (*a)(void);} t5 = {autoStructTest};216struct {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}}218union {char a; int b;} t7[] = {1, 2, 3};219int t8[sizeof t7 == (3*sizeof(int)) ? 1 : -1];220 221struct bittest{int : 31, a, :21, :12, b;};222struct bittest bittestvar = {1, 2, 3, 4}; //expected-warning{{excess elements in struct initializer}}223 224int u1 = {}; //pedantic-warning{{use of an empty initializer is a C23 extension}}225int u2 = {{3}}; //expected-warning{{too many braces around scalar initializer}}226 227// PR2362228void varArray(void) {229  int c[][x] = { 0 }; //expected-error{{variable-sized object may not be initialized}}230}231 232// PR2151233void emptyInit(void) {struct {} x[] = {6};} //expected-warning{{empty struct is a GNU extension}} \234// expected-error{{initializer for aggregate with no elements}}235 236void 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}240struct {int a; int:5;} noNamedImplicit[] = {1,2,3};241int noNamedImplicitCheck[sizeof(noNamedImplicit) == 3 * sizeof(*noNamedImplicit) ? 1 : -1];242 243 244// ptrs are constant245struct soft_segment_descriptor {246  long ssd_base;247};248static int dblfault_tss;249 250union uniao { int ola; } xpto[1];251 252struct soft_segment_descriptor gdt_segs[] = {253  {(long) &dblfault_tss},254  { (long)xpto},255};256 257static void sppp_ipv6cp_up(void);258const struct {} ipcp = { sppp_ipv6cp_up }; //expected-warning{{empty struct is a GNU extension}} \259// expected-warning{{excess elements in struct initializer}}260 261struct _Matrix { union { float m[4][4]; }; }; //expected-warning{{anonymous unions are a C11 extension}}262typedef struct _Matrix Matrix;263void 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.0f276  };277}278 279char 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 array282// compound literal. PR9261.283typedef int int5[5];284int 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}}285int 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}}286int 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}}287int 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}}288int 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 290int a6[5] = (int[]){1, 2, 3}; // expected-error{{cannot initialize array of type 'int[5]' with array of type 'int[3]'}}291 292int nonconst_value(void);293int 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;301char test10_global[test10_bound]; // expected-error {{variable length array declaration not allowed at file scope}}302void test10(void) {303  char test10_local[test10_bound] = "help"; // expected-error {{variable-sized object may not be initialized}}304}305