(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Warray-bounds-79.c
       1  /* PR tree-optimization/99475 - bogus -Warray-bounds accessing an array
       2     element of empty structs
       3     { dg-do compile }
       4     { dg-options "-O2 -Wall" } */
       5  
       6  struct S
       7  {
       8  #if SOME_CONFIG_MACRO
       9    /* Suppose the contents are empty in the development configuration
      10       but non-empty in others.  Out of bounds accesses to elements of
      11       the arrays below should be diagnosed in all configurations,
      12       including when S is empty, even if they are folded away.  */
      13    int member;
      14  #endif
      15  };
      16  
      17  extern struct S sa3[3];
      18  extern struct S sa2_3[2][3];
      19  extern struct S sa3_4_5[3][4][5];
      20  
      21  void sink (void*);
      22  
      23  
      24  void access_sa3 (void)
      25  {
      26    sa3[0] = (struct S){ };
      27    sa3[1] = (struct S){ };
      28    sa3[2] = (struct S){ };
      29    sa3[3] = (struct S){ };       // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
      30  }
      31  
      32  void access_sa3_ptr (void)
      33  {
      34    struct S *p = &sa3[0];
      35  
      36    p[0] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      37    p[1] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      38    p[2] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      39    p[3] = (struct S){ };         // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
      40  }
      41  
      42  void access_sa2_3_ptr (void)
      43  {
      44    struct S *p = &sa2_3[0][0];
      45  
      46    p[0] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      47    p[1] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      48    p[2] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      49    p[6] = (struct S){ };         // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
      50  }
      51  
      52  void access_sa3_4_5_ptr (struct S s, int i)
      53  {
      54    struct S *p = &sa3_4_5[0][0][0];
      55  
      56    p[0] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      57    p[1] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      58    p[2] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      59    p[60] = (struct S){ };        // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
      60  }
      61  
      62  
      63  void access_vla3 (struct S s, unsigned n)
      64  {
      65    struct S vla3[3 < n ? 3 : n];
      66  
      67    vla3[0] = (struct S){ };
      68    vla3[1] = (struct S){ };
      69    vla3[2] = (struct S){ };
      70    vla3[3] = (struct S){ };       // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
      71  
      72    sink (vla3);
      73  }
      74  
      75  void access_vla3_ptr (struct S s, unsigned n)
      76  {
      77    struct S vla3[3 < n ? 3 : n];
      78    struct S *p = &vla3[0];
      79  
      80    p[0] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      81    p[1] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      82    p[2] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      83    p[3] = (struct S){ };         // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
      84  
      85    sink (vla3);
      86  }
      87  
      88  void access_vla2_3_ptr (struct S s, unsigned n)
      89  {
      90    struct S vla2_3[2 < n ? 2 : n][3 < n ? 3 : n];
      91    struct S *p = &vla2_3[0][0];
      92  
      93    p[0] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      94    p[1] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      95    p[2] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
      96    p[6] = (struct S){ };         // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
      97  
      98    sink (vla2_3);
      99  }
     100  
     101  void access_vla3_4_5_ptr (struct S s, unsigned n)
     102  {
     103    struct S vla3_4_5[3 < n ? 3 : n][4 < n ? 4 : n][5 < n ? 5 : n];
     104    struct S *p = &vla3_4_5[0][0][0];
     105  
     106    p[0] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
     107    p[1] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
     108    p[2] = (struct S){ };         // { dg-bogus "\\\[-Warray-bounds" }
     109    p[60] = (struct S){ };        // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
     110  
     111    sink (vla3_4_5);
     112  }