(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Warray-bounds-92.c
       1  /* PR middle-end/103215 - bogus -Warray-bounds with two pointers with
       2     different offsets each
       3     Test for accesses into distinct arrays through pointers with different
       4     offsets each.
       5  
       6     If/when -Warray-bounds is enhanced to issue "maybe" kinds of warnings
       7     some of the accesses here will trigger those and will need updating.
       8  
       9     { dg-do compile }
      10     { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
      11  
      12  #define NOIPA __attribute__ ((noipa))
      13  
      14  #define A(p, off) ((p)[off] = __COUNTER__)
      15  
      16  extern int a4[4], a8[8];
      17  
      18  
      19  NOIPA void a4_p1_a8_p3 (int i)
      20  {
      21    int *a4_p1 = a4 + 1;
      22    int *a8_p3 = a8 + 3;
      23    int *q = i ? a4_p1 : a8_p3;
      24    A (q, -4);      // { dg-warning "-Warray-bounds" }
      25    /* Because -3 is a valid offset into a8 but not a4, q must point
      26       to the former and so subscripts between -3 and +4 refer to its
      27       elements.  */
      28    A (q, -3); A (q, -2); A (q, -1); A (q, 0);
      29    A (q,  1); A (q,  2); A (q,  3); A (q, 4);
      30    A (q, 5);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      31    /* Both of the following are definitely out of bounds but the first isn't
      32       diagnosed because the code conservatively merges the offsets into A4
      33       and A8.  */
      34    A (q, 7);       // { dg-warning "-Warray-bounds" }
      35  }
      36  
      37  
      38  NOIPA void a4_p1_a8_p5 (int i)
      39  {
      40    int *a4_p1 = a4 + 1;
      41    int *a8_p5 = a8 + 5;
      42    int *q = i ? a4_p1 : a8_p5;
      43    A (q, -6);     // { dg-warning "-Warray-bounds" }
      44    /* Similarly to the above, because -5 is a valid offset into a8 but
      45       not a4, q must point to the former and so subscripts between -5
      46       and +2 refer to its elements.  */
      47    A (q, -5); A (q, -4); A (q, -3); A (q, -2);
      48    A (q, -1); A (q,  0); A (q,  1); A (q,  2);
      49    A (q, 3);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      50    A (q, 7);       // { dg-warning "-Warray-bounds" }
      51  }
      52  
      53  
      54  NOIPA void a4_p1_a8_p7 (int i)
      55  {
      56    int *a4_p1 = a4 + 1;
      57    int *a8_p7 = a8 + 7;
      58    int *q = i ? a4_p1 : a8_p7;
      59    A (q, -8);     // { dg-warning "-Warray-bounds" }
      60    A (q, -7); A (q, -6); A (q, -5); A (q, -4);
      61    A (q, -3); A (q, -2); A (q, -1); A (q,  0);
      62    /* Since -7 is valid, q must point to a8 and the last valid subscript
      63       must be 0.  */
      64    A (q, 1);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      65    A (q, 2);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      66    A (q, 3);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      67    A (q, 7);       // { dg-warning "-Warray-bounds" }
      68  }
      69  
      70  
      71  NOIPA void mp_1_a4_p1_a8_p7 (int i, int j)
      72  {
      73    int *a4_p1 = a4 + 1;
      74    int *a8_p7 = a8 + 7;
      75    int *p = i ? a4_p1 : a8_p7;
      76    int *q = j ? p + 1 : p - 1;
      77  
      78    A (q, -9);      // { dg-warning "-Warray-bounds" }
      79  
      80    /* q points either to a8 + [6, 8] or a4 + [0, 2].  */
      81    A (q, -8); A (q, -7); A (q, -6); A (q, -5);
      82    A (q, -4); A (q, -3); A (q, -2); A (q, -1);
      83  
      84    /* Since all the above are valid, q must point to a8 + 8. But as
      85       mentioned above, the warning for each subscript is independent
      86       of prior subscripts into the same object so the access below
      87       aren't diagnosed.  */
      88    A (q, 0);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      89    A (q, 1);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      90    A (q, 2);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      91    A (q, 3);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      92    A (q, 4);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
      93    A (q, 8);       // { dg-warning "-Warray-bounds" }
      94  }
      95  
      96  
      97  NOIPA void mp1_a4_p1_a8_p5 (int i, int j)
      98  {
      99    int *a4_p1 = a4 + 1;
     100    int *a8_p5 = a8 + 5;
     101    int *p = i ? a4_p1 : a8_p5;
     102  
     103    int *q = j ? p + 1 : p - 1;
     104  
     105    // q is assumed to point to a8 + 6
     106    A (q, -7);      // { dg-warning "-Warray-bounds" }
     107    A (q, -6); A (q, -5); A (q, -4); A (q, -3);
     108    A (q, -2); A (q, -1); A (q,  0); A (q,  1);
     109    /* Even though the above accesses rule it out, q is now assumed
     110       to point to either a4 + [0, 2] or a8 + [4, 5].  */
     111    A (q, 2);
     112    /* q is now assumed to point tp a4.  Given that, only the first store
     113       is valid.  */
     114    A (q, 3);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     115    A (q, 4);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     116    A (q, 5);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     117    A (q, 6);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     118    A (q, 7);       // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     119    A (q, 8);       // { dg-warning "-Warray-bounds" }
     120  }
     121  
     122  
     123  NOIPA void mp1_a4_p1_a8_p4 (int i, int j)
     124  {
     125    int *a4_p1 = a4 + 1;
     126    int *a8_p4 = a8 + 4;
     127    int *p = i ? a4_p1 : a8_p4;
     128  
     129    int *q = j ? p + 1 : p - 1;
     130  
     131    // q is assumed to point to a8 + 5
     132    A (q, -6);      // { dg-warning "-Warray-bounds" }
     133    A (q, -5);
     134    A (q, -4);
     135    A (q, -3);
     136    A (q, -2);
     137    A (q, -1);
     138    A (q,  0);
     139    A (q,  1);
     140    A (q,  2);
     141    /* Even though the above accesses rule it out, q is now assumed
     142       to point tp a4.  Given that, only the first store is valid.  */
     143    A (q,  3);      // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     144    A (q,  4);      // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     145    A (q,  5);      // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     146    A (q,  6);      // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     147    A (q,  7);      // { dg-warning "-Warray-bounds" "pr??????" { xfail *-*-* } }
     148    A (q,  8);      // { dg-warning "-Warray-bounds" }
     149  }