(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Warray-bounds-68.c
       1  /* PR middle-end/97391 - bogus -Warray-bounds accessing a multidimensional
       2     array parameter
       3     { dg-do compile }
       4     { dg-options "-O2 -Wall" } */
       5  
       6  
       7  void nowarn_access_loop_idx (char a[3][5])
       8  {
       9    for (int i = 0; i < 3; i++)
      10      for (int j = 0; j < 5; j++)
      11        a[i][j] = 0;
      12  }
      13  
      14  void warn_access_loop_idx (char a[3][5])
      15  {
      16    for (int i = 0; i < 3; i++)
      17      for (int j = 0; j < 5; j++)
      18        a[j][i] = 0;            // { dg-warning "\\\[-Warray-bounds" }
      19  }
      20  
      21  
      22  void nowarn_access_cst_idx (int a[5][7][9])
      23  {
      24    a[0][0][0] = __LINE__;
      25    a[0][0][8] = __LINE__;
      26  
      27    a[0][6][0] = __LINE__;
      28    a[0][6][8] = __LINE__;
      29  
      30    a[4][0][0] = __LINE__;
      31    a[4][0][8] = __LINE__;
      32    a[4][6][8] = __LINE__;
      33  }
      34  
      35  
      36  void test_ptr_access_cst_idx (int a[5][7][9])
      37  {
      38    int *p = &a[0][0][0];
      39  
      40    p[0] = __LINE__;
      41    p[8] = __LINE__;
      42  
      43    /* The following access should trigger a warning but it's represented
      44       the same  as the valid access in
      45         p = a[0][1][0];
      46         p[1] = __LINE__;
      47       both as
      48         MEM[(int *)a_1(D) + 36B] = __LINE__;  */
      49  
      50    p[9] = __LINE__;            // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
      51  
      52    p[315] = __LINE__;
      53    // { dg-warning "subscript 315 is outside array bounds of 'int\\\[5]\\\[7]\\\[9]'" "pr97425" { xfail *-*-* } .-1 }
      54    // { dg-warning "subscript 315 is outside array bounds " "" { target *-*-* } .-2 }
      55  
      56    p = &a[0][6][0];
      57    p[0] = __LINE__;
      58    p[8] = __LINE__;
      59  
      60    p = &a[4][6][0];
      61    p[0] = __LINE__;
      62    p[8] = __LINE__;
      63  }
      64  
      65  
      66  void warn_access_cst_idx (int a[5][7][9])
      67  {
      68    a[0][0][9] = __LINE__;      // { dg-warning "subscript 9 is above array bounds of 'int\\\[9]'" }
      69    a[0][7][0] = __LINE__;      // { dg-warning "subscript 7 is above array bounds of 'int\\\[7]\\\[9]'" }
      70    a[5][0][0] = __LINE__;
      71    // { dg-warning "subscript 5 is outside array bounds of 'int\\\[5]\\\[7]\\\[9]'" "pr97425" { xfail *-*-* } .-1 }
      72    // { dg-warning "subscript \\d+ is outside array bounds" "" { target *-*-* } .-2 }
      73  }
      74  
      75  
      76  void test_ptrarray_access_cst_idx (int (*pa)[5][7][9])
      77  {
      78    (*pa)[0][0][0] = __LINE__;
      79    (*pa)[0][0][8] = __LINE__;
      80    (*pa)[0][0][9] = __LINE__;  // { dg-warning "subscript 9 is above array bounds of 'int\\\[9]'" }
      81  
      82    (*pa)[0][6][0] = __LINE__;
      83    (*pa)[0][7][0] = __LINE__;  // { dg-warning "subscript 7 is above array bounds of 'int\\\[7]\\\[9]'" }
      84    (*pa)[0][8][0] = __LINE__;  // { dg-warning "subscript 8 is above array bounds of 'int\\\[7]\\\[9]'" }
      85  
      86    (*pa)[4][6][8] = __LINE__;
      87    (*pa)[5][0][0] = __LINE__;  // { dg-warning "subscript 5 is above array bounds of 'int\\\[5]\\\[7]\\\[9]'" }
      88  }
      89  
      90  
      91  void test_ptr_ptrarray_access_cst_idx (int (*pa)[5][7][9])
      92  {
      93    int *p = &(*pa)[0][0][0];
      94  
      95    p[0] = __LINE__;
      96    p[8] = __LINE__;
      97  
      98    /* The following access should trigger a warning but it's represented
      99       the same  as the valid access in
     100         p = a[0][1][0];
     101         p[1] = __LINE__;
     102       both as
     103         MEM[(int *)a_1(D) + 36B] = __LINE__;  */
     104  
     105    p[9] = __LINE__;            // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
     106  
     107    p[315] = __LINE__;          // { dg-warning "\\\[-Warray-bounds" "pr97429" { xfail *-*-* } }
     108  
     109    p = &(*pa)[0][6][0];
     110    p[0] = __LINE__;
     111    p[8] = __LINE__;
     112  
     113    p = &(*pa)[4][6][0];
     114    p[0] = __LINE__;
     115    p[8] = __LINE__;
     116  }
     117  
     118