(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
c99-complit-2.c
       1  /* Test for compound literals: in C99 only.  Test for invalid uses.  */
       2  /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
       3  /* { dg-do compile } */
       4  /* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
       5  
       6  struct s { int a; int b; };
       7  union u { int c; int d; };
       8  
       9  struct si;
      10  union ui;
      11  
      12  void
      13  foo (int a)
      14  {
      15    /* The type name must not be incomplete (apart from arrays of unknown
      16       size), or a function type, or a VLA type.  */
      17    (void) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
      18    /* { dg-error "init" "void type" { target *-*-* } .-1 } */
      19    &(struct si) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
      20    /* { dg-error "init" "incomplete struct type" { target *-*-* } .-1 } */
      21    /* { dg-error "invalid use of undefined type" "" { target *-*-* } .-2 } */
      22    &(union ui) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
      23    /* { dg-error "init" "incomplete union type" { target *-*-* } .-1 } */
      24    /* { dg-error "invalid use of undefined type" "" { target *-*-* } .-2 } */
      25    (void (void)) { 0 }; /* { dg-bogus "warning" "warning in place of error" } */
      26    /* { dg-error "compound literal has function type" "function type" { target *-*-* } .-1 } */
      27    (int [a]) { 1 }; /* { dg-bogus "warning" "warning in place of error" } */
      28    /* { dg-error "init|variable" "VLA type" { target *-*-* } .-1 } */
      29    /* Initializers must not attempt to initialize outside the object
      30       declared.  */
      31    (int [1]) { [1] = 2 }; /* { dg-bogus "warning" "warning in place of error" } */
      32    /* { dg-error "init" "value outside array" { target *-*-* } .-1 } */
      33    (int [1]) { [-1] = 2 }; /* { dg-bogus "warning" "warning in place of error" } */
      34    /* { dg-error "init" "value outside array" { target *-*-* } .-1 } */
      35    (int [1]) { 0, 1 }; /* { dg-bogus "warning" "warning in place of error" } */
      36    /* { dg-error "init" "value outside array" { target *-*-* } .-1 } */
      37  }
      38  
      39  int z;
      40  
      41  /* Outside a function, initializers must be constant.  */
      42  struct s *s0 = &(struct s) { 0, z }; /* { dg-bogus "warning" "warning in place of error" } */
      43  /* { dg-error "init" "non-const" { target *-*-* } .-1 } */
      44  int sz = sizeof((struct s) { 0, z }); /* { dg-bogus "warning" "warning in place of error" } */
      45  /* { dg-error "init" "non-const" { target *-*-* } .-1 } */
      46  
      47  /* Compound literals aren't themselves constant expressions.  */
      48  int x = (int) { 0 }; /* { dg-bogus "warning" "warning in place of error" } */
      49  /* { dg-error "init" "non-const" { target *-*-* } .-1 } */
      50  
      51  /* Nor are they suitable structure or union initializers
      52     outside a function.  */
      53  struct s s1 = (struct s) { 0, 1 }; /* { dg-bogus "warning" "warning in place of error" } */
      54  /* { dg-error "init" "struct bad init" { target *-*-* } .-1 } */
      55  union u u1 = (union u) { 0 }; /* { dg-bogus "warning" "warning in place of error" } */
      56  /* { dg-error "init" "union bad init" { target *-*-* } .-1 } */
      57  
      58  /* They aren't suitable for array initializers, either inside or outside
      59     a function.  */
      60  int y[2] = (int [2]) { 0, 1 }; /* { dg-bogus "warning" "warning in place of error" } */
      61  /* { dg-error "init" "array bad init" { target *-*-* } .-1 } */
      62  
      63  void
      64  bar (void)
      65  {
      66    struct s s2 = (struct s) { 0, 1 };
      67    union u u2 = (union u) { 0 };
      68    int z[2] = (int [2]) { 0, 1 }; /* { dg-bogus "warning" "warning in place of error" } */
      69    /* { dg-error "init" "array bad init" { target *-*-* } .-1 } */
      70  }