(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Wrestrict-20.c
       1  /* PR c/84919 - bogus -Wrestrict on sprintf %p with destination as argument
       2     { dg-do compile }
       3     -O2 isn't strictly necessary but setting also verifies that the sprintf/
       4     strlen pass doesn't warn with non-constant arguments.
       5     { dg-options "-O2 -Wall" } */
       6  
       7  extern int sprintf (char* restrict, const char* restrict, ...);
       8  extern int snprintf (char* restrict, __SIZE_TYPE__, const char* restrict, ...);
       9  
      10  char a[32];
      11  
      12  void test_warn (char *p)
      13  {
      14    a[0] = 0;
      15    sprintf (a, "a=%s", a);     /* { dg-warning "-Wrestrict" } */
      16  
      17    p = a;
      18    char *q = p + 3;
      19    sprintf (p, "a=%s", q);     /* { dg-warning "-Wrestrict" } */
      20  }
      21  
      22  void test_nowarn_front_end (char *d)
      23  {
      24    sprintf (d, "%p", d);
      25    snprintf (d, 32, "%p", d);
      26  
      27    sprintf (a, "p=%p", a);
      28    snprintf (a, sizeof a, "%p", a);
      29  }
      30  
      31  void test_nowarn_sprintf_pass (char *d)
      32  {
      33    char *q = d;
      34  
      35    sprintf (d, "p=%p", q);
      36    snprintf (d, 32, "p=%p", q);
      37  
      38    q = a;
      39    sprintf (a, "a=%p", q);
      40    snprintf (a, sizeof a, "a=%p", q);
      41  }