(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Wfree-nonheap-object-4.c
       1  /* PR middle-end/98664 - inconsistent --Wfree-nonheap-object for inlined
       2     calls to system headers
       3     { dg-do compile }
       4     { dg-options "-O2 -Wall" } */
       5  
       6  # 7 "Wfree-nonheap-object-4.h" 1 3
       7  
       8  struct A
       9  {
      10    void *p;
      11  };
      12  
      13  static void f0 (struct A *p, void *q) { p->p = q; }
      14  static void f1 (struct A *p, void *q) { f0 (p, q); }
      15  static void f2 (struct A *p, void *q) { f1 (p, q); }
      16  
      17  static void g0 (struct A *p)
      18  {
      19    __builtin_free (p->p);      // { dg-warning "\\\[-Wfree-nonheap-object" }
      20  }
      21  
      22  static void g1 (struct A *p) { g0 (p); }
      23  static void g2 (struct A *p) { g1 (p); }
      24  
      25  # 26 "Wfree-nonheap-object-4.c"
      26  
      27  #define NOIPA __attribute__ ((noipa))
      28  
      29  extern int array[];           // { dg-message "declared here" "note on line 29" }
      30  
      31  /* Verify the warning is issued even for calls in a system header inlined
      32     into a function outside the header.  */
      33  
      34  NOIPA void warn_g0 (struct A *p)
      35  {
      36    int *q = array + 1;
      37  
      38    f0 (p, q);
      39    g0 (p);
      40  }
      41  
      42  // { dg-message "inlined from 'warn_g0'" "note on line 42" { target *-*-* } 0 }
      43  
      44  
      45  /* Also verify the warning can be suppressed.  */
      46  
      47  NOIPA void nowarn_g0 (struct A *p)
      48  {
      49    int *q = array + 2;
      50  
      51    f0 (p, q);
      52  
      53  #pragma GCC diagnostic push
      54  #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
      55    g0 (p);
      56  #pragma GCC diagnostic pop
      57  }
      58  
      59  
      60  NOIPA void warn_g1 (struct A *p)
      61  {
      62    int *q = array + 3;
      63  
      64    f1 (p, q);
      65    g1 (p);
      66  }
      67  
      68  // { dg-message "inlined from 'g1'" "note on line 68" { target *-*-* } 0 }
      69  // { dg-message "inlined from 'warn_g1'" "note on line 69" { target *-*-* } 0 }
      70  
      71  
      72  NOIPA void nowarn_g1 (struct A *p)
      73  {
      74    int *q = array + 4;
      75  
      76    f1 (p, q);
      77  
      78  #pragma GCC diagnostic push
      79  #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
      80    g1 (p);
      81  #pragma GCC diagnostic pop
      82  }
      83  
      84  
      85  NOIPA void warn_g2 (struct A *p)
      86  {
      87    int *q = array + 5;
      88  
      89    f2 (p, q);
      90    g2 (p);
      91  }
      92  
      93  // { dg-message "inlined from 'g2'" "note on line 93" { target *-*-* } 0 }
      94  // { dg-message "inlined from 'warn_g2'" "note on line 94" { target *-*-* } 0 }
      95  
      96  
      97  NOIPA void nowarn_g2 (struct A *p)
      98  {
      99    int *q = array + 6;
     100  
     101    f2 (p, q);
     102  
     103  #pragma GCC diagnostic push
     104  #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
     105    g2 (p);
     106  #pragma GCC diagnostic pop
     107  }