(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Warray-bounds-78.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 -Wno-strict-aliasing" } */
       5  
       6  typedef _Bool bool;
       7  
       8  #define NOIPA __attribute__ ((noipa))
       9  
      10  struct S { };
      11  
      12  extern struct S sa3[3];
      13  extern struct S sa2_3[2][3];
      14  extern struct S sa3_4_5[3][4][5];
      15  
      16  void sink (void*);
      17  
      18  
      19  NOIPA void access_sa3 (void)
      20  {
      21    ((bool*)sa3)[0] = __LINE__;     // { dg-warning "\\\[-Warray-bounds" }
      22    ((bool*)sa3)[1] = __LINE__;     // { dg-warning "\\\[-Warray-bounds" }
      23    ((bool*)sa3)[2] = __LINE__;     // { dg-warning "\\\[-Warray-bounds" }
      24    ((bool*)sa3)[3] = __LINE__;     // { dg-warning "\\\[-Warray-bounds" }
      25  }
      26  
      27  NOIPA void access_sa3_ptr (void)
      28  {
      29    bool *p = (bool*)&sa3[0];
      30  
      31    p[0] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      32    p[1] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      33    p[2] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      34    p[3] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      35  }
      36  
      37  NOIPA void access_sa2_3_ptr (void)
      38  {
      39    bool *p = (bool*)&sa2_3[0][0];
      40  
      41    p[0] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      42    p[1] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      43    p[2] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      44    p[6] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      45  }
      46  
      47  NOIPA void access_sa3_4_5_ptr (struct S s, int i)
      48  {
      49    bool *p = (bool*)&sa3_4_5[0][0][0];
      50  
      51    p[0] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      52    p[1] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      53    p[2] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      54    p[60] = __LINE__;       // { dg-warning "\\\[-Warray-bounds" }
      55  }
      56  
      57  
      58  NOIPA void access_vla3 (struct S s, unsigned n)
      59  {
      60    struct S vla3[3 < n ? 3 : n];
      61  
      62    ((bool*)vla3)[0] = __LINE__;    // { dg-warning "\\\[-Warray-bounds" }
      63    ((bool*)vla3)[1] = __LINE__;    // { dg-warning "\\\[-Warray-bounds" }
      64    ((bool*)vla3)[2] = __LINE__;    // { dg-warning "\\\[-Warray-bounds" }
      65    ((bool*)vla3)[3] = __LINE__;    // { dg-warning "\\\[-Warray-bounds" }
      66  
      67    sink (vla3);
      68  }
      69  
      70  NOIPA void access_vla3_ptr (struct S s, unsigned n)
      71  {
      72    struct S vla3[3 < n ? 3 : n];
      73    bool *p = (bool*)&vla3[0];
      74  
      75    p[0] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      76    p[1] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      77    p[2] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      78    p[3] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      79  
      80    sink (vla3);
      81  }
      82  
      83  NOIPA void access_vla2_3_ptr (struct S s, unsigned n)
      84  {
      85    struct S vla2_3[2 < n ? 2 : n][3 < n ? 3 : n];
      86    bool *p = (bool*)&vla2_3[0][0];
      87  
      88    p[0] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      89    p[1] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      90    p[2] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      91    p[6] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
      92  
      93    sink (vla2_3);
      94  }
      95  
      96  NOIPA void access_vla3_4_5_ptr (struct S s, unsigned n)
      97  {
      98    struct S vla3_4_5[3 < n ? 3 : n][4 < n ? 4 : n][5 < n ? 5 : n];
      99    bool *p = (bool*)&vla3_4_5[0][0][0];
     100  
     101    p[0] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
     102    p[1] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
     103    p[2] = __LINE__;        // { dg-warning "\\\[-Warray-bounds" }
     104    p[60] = __LINE__;       // { dg-warning "\\\[-Warray-bounds" }
     105  
     106    sink (vla3_4_5);
     107  }
     108  
     109  // { dg-prune-output "empty struct has size 0 in C" }