(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
Waddress-4.c
       1  /* PR c/102103 - missing warning comparing array address to null
       2     { dg-do compile }
       3     { dg-options "-Wall" } */
       4  
       5  typedef __INTPTR_TYPE__ intptr_t;
       6  typedef __INTPTR_TYPE__ uintptr_t;
       7  
       8  extern char *ax[], *a2[][2];
       9  
      10  void T (int);
      11  
      12  void test_ax_plus_eq_0 (int i)
      13  {
      14    // Verify that casts to intptr_t suppress the warning.
      15    T ((intptr_t)(ax + 0) == 0);
      16    T ((uintptr_t)(ax + 1) == 0);
      17  
      18    T (ax + 0 == 0);      // { dg-warning "-Waddress" }
      19    T (&ax[0] == 0);      // { dg-warning "-Waddress" }
      20    T (ax - 1 == 0);      // { dg-warning "-Waddress" }
      21    T (0 == &ax[-1]);     // { dg-warning "-Waddress" }
      22    T ((void *)(&ax[0] + 2) == 0);  // { dg-warning "-Waddress" }
      23    T (&ax[0] + 2 == 0);  // { dg-warning "-Waddress" }
      24    T (ax + 3 == 0);      // { dg-warning "-Waddress" }
      25    T (0 == &ax[-4]);     // { dg-warning "-Waddress" }
      26    T (ax - i == 0);      // { dg-warning "-Waddress" }
      27    T (&ax[i] == 0);      // { dg-warning "-Waddress" }
      28    T (0 == &ax[1] + i);  // { dg-warning "-Waddress" }
      29  }
      30  
      31  void test_a2_plus_eq_0 (int i)
      32  {
      33    // Verify that casts to intptr_t suppress the warning.
      34    T ((intptr_t)(a2 + 0) == 0);
      35    T ((uintptr_t)(a2 + 1) == 0);
      36  
      37    T (a2 + 0 == 0);      // { dg-warning "-Waddress" }
      38    // Verify that a cast to another pointer type doesn't suppress it.
      39    T ((void*)(a2 + 0) == 0);       // { dg-warning "-Waddress" }
      40    T ((char*)a2 + 1 == 0);         // { dg-warning "-Waddress" }
      41    T (&a2[0] == 0);      // { dg-warning "-Waddress" }
      42    T (a2 - 1 == 0);      // { dg-warning "-Waddress" }
      43    T (0 == &a2[-1]);     // { dg-warning "-Waddress" }
      44    T (a2 + 2 == 0);      // { dg-warning "-Waddress" }
      45    T (0 == &a2[-2]);     // { dg-warning "-Waddress" }
      46    T (a2 - i == 0);      // { dg-warning "-Waddress" }
      47    T (&a2[i] == 0);      // { dg-warning "-Waddress" }
      48  }
      49  
      50  // Exercise a pointer.
      51  void test_p_plus_eq_0 (int *p, int i)
      52  {
      53    /* P + 0 and equivalently &P[0] are invalid for a null P but they're
      54       folded to p before the warning has a chance to trigger.  */
      55    T (p + 0 == 0);       // { dg-warning "-Waddress" "pr102555" { xfail *-*-* } }
      56    T (&p[0] == 0);       // { dg-warning "-Waddress" "pr102555" { xfail *-*-* } }
      57  
      58    T (p - 1 == 0);       // { dg-warning "-Waddress" }
      59    T (0 == &p[-1]);      // { dg-warning "-Waddress" }
      60    T (p + 2 == 0);       // { dg-warning "-Waddress" }
      61    T (0 == &p[-2]);      // { dg-warning "-Waddress" }
      62    T (p - i == 0);       // { dg-warning "-Waddress" }
      63    T (&p[i] == 0);       // { dg-warning "-Waddress" }
      64  }
      65  
      66  // Exercise pointer to array.
      67  void test_pa_plus_eq_0 (int (*p)[], int (*p2)[][2], int i)
      68  {
      69    // The array pointer may be null.
      70    T (*p == 0);
      71    /* &**P is equivalent to *P and might be the result od macro expansion.
      72       Verify it doesn't cause a warning.  */
      73    T (&**p == 0);
      74  
      75    /* *P + 0 is invalid but folded to *P before the warning has a chance
      76       to trigger.  */
      77    T (*p + 0 == 0);      // { dg-warning "-Waddress" "pr102555" { xfail *-*-* } }
      78  
      79    T (&(*p)[0] == 0);    // { dg-warning "-Waddress" }
      80    T (*p - 1 == 0);      // { dg-warning "-Waddress" }
      81    T (0 == &(*p)[-1]);   // { dg-warning "-Waddress" }
      82    T (*p + 2 == 0);      // { dg-warning "-Waddress" }
      83    T (0 == &(*p)[-2]);   // { dg-warning "-Waddress" }
      84    T (*p - i == 0);      // { dg-warning "-Waddress" }
      85    T (&(*p)[i] == 0);    // { dg-warning "-Waddress" }
      86  
      87  
      88    /* Similar to the above but for a pointer to a two-dimensional array,
      89       referring to the higher-level element (i.e., an array itself).  */
      90    T (*p2 == 0);
      91    T (**p2 == 0);         // { dg-warning "-Waddress" "pr102555" { xfail *-*-* } }
      92    T (&**p2 == 0);        // { dg-warning "-Waddress" "pr102555" { xfail *-*-* } }
      93    T (&***p2 == 0);       // { dg-warning "-Waddress" "pr102555" { xfail *-*-* } }
      94    T (&**p2 == 0);
      95  
      96    T (*p2 + 0 == 0);      // { dg-warning "-Waddress" "pr102555" { xfail *-*-* } }
      97    T (&(*p2)[0] == 0);    // { dg-warning "-Waddress" }
      98    T (&(*p2)[0][1] == 0); // { dg-warning "-Waddress" }
      99    T (*p2 - 1 == 0);      // { dg-warning "-Waddress" }
     100    T (0 == &(*p2)[-1]);   // { dg-warning "-Waddress" }
     101    T (0 == &(*p2)[1][2]); // { dg-warning "-Waddress" }
     102    T (*p2 + 2 == 0);      // { dg-warning "-Waddress" }
     103    T (0 == &(*p2)[-2]);   // { dg-warning "-Waddress" }
     104    T (*p2 - i == 0);      // { dg-warning "-Waddress" }
     105    T (&(*p2)[i] == 0);    // { dg-warning "-Waddress" }
     106  }