(root)/
gcc-13.2.0/
gcc/
testsuite/
c-c++-common/
attr-nonstring-5.c
       1  /* PR middle-end/84725 - enable attribute nonstring for all narrow character
       2     types
       3     Verify that using attribute nonstring with all three narrow character
       4     types is accepted and using arrays and pointers to characters of all
       5     three types (including their qualified forms) declared with the
       6     attributes doesn't trigger -Wstringop-truncation warnings.
       7     { dg-do compile }
       8     { dg-options "-O -Wall -Wstringop-truncation" } */
       9  
      10  #if __cplusplus
      11  extern "C"
      12  #endif
      13  char* strncpy (char*, const char*, __SIZE_TYPE__);
      14  
      15  #define NONSTR __attribute__ ((nonstring))
      16  
      17  #define S "1234"
      18  
      19  struct Arrays
      20  {
      21    char NONSTR a[4];
      22    signed char NONSTR b[4];
      23    unsigned char NONSTR c[4];
      24  };
      25  
      26  void test_arrays (struct Arrays *p, const char *s)
      27  {
      28    strncpy (p->a, s, sizeof p->a);
      29    strncpy ((char*)p->b, s, sizeof p->b);
      30    strncpy ((char*)p->c, s, sizeof p->c);
      31  }
      32  
      33  struct Pointers
      34  {
      35    char NONSTR *p;
      36    signed char NONSTR *q;
      37    unsigned char NONSTR *r;
      38  };
      39  
      40  void test_pointers (struct Pointers *p)
      41  {
      42    strncpy (p->p, S, sizeof S - 1);
      43    strncpy ((char*)p->q, S, sizeof S - 1);
      44    strncpy ((char*)p->r, S, sizeof S - 1);
      45  }
      46  
      47  struct ConstArrays
      48  {
      49    const char NONSTR a[4];
      50    const signed char NONSTR b[4];
      51    const unsigned char NONSTR c[4];
      52  };
      53  
      54  void test_const_arrays (struct ConstArrays *p, const char *s)
      55  {
      56    strncpy ((char*)p->a, s, sizeof p->a);
      57    strncpy ((char*)p->b, s, sizeof p->b);
      58    strncpy ((char*)p->c, s, sizeof p->c);
      59  }
      60  
      61  struct ConstPointers
      62  {
      63    const char NONSTR *p;
      64    const signed char NONSTR *q;
      65    const unsigned char NONSTR *r;
      66  };
      67  
      68  void test_const_pointers (struct ConstPointers *p)
      69  {
      70    strncpy ((char*)p->p, S, sizeof S - 1);
      71    strncpy ((char*)p->q, S, sizeof S - 1);
      72    strncpy ((char*)p->r, S, sizeof S - 1);
      73  }
      74  
      75  struct VolatileArrays
      76  {
      77    volatile char NONSTR a[4];
      78    volatile signed char NONSTR b[4];
      79    volatile unsigned char NONSTR c[4];
      80  };
      81  
      82  void test_volatile_arrays (struct VolatileArrays *p, const char *s)
      83  {
      84    strncpy ((char*)p->a, s, sizeof p->a);
      85    strncpy ((char*)p->b, s, sizeof p->b);
      86    strncpy ((char*)p->c, s, sizeof p->c);
      87  }
      88  
      89  struct VolatilePointers
      90  {
      91    volatile char NONSTR *p;
      92    volatile signed char NONSTR *q;
      93    volatile unsigned char NONSTR *r;
      94  };
      95  
      96  void test_volatile_pointers (struct VolatilePointers *p)
      97  {
      98    strncpy ((char*)p->p, S, sizeof S - 1);
      99    strncpy ((char*)p->q, S, sizeof S - 1);
     100    strncpy ((char*)p->r, S, sizeof S - 1);
     101  }
     102  
     103  struct ConstVolatileArrays
     104  {
     105    const volatile char NONSTR a[4];
     106    const volatile signed char NONSTR b[4];
     107    const volatile unsigned char NONSTR c[4];
     108  };
     109  
     110  void test_const_volatile_arrays (struct ConstVolatileArrays *p, const char *s)
     111  {
     112    strncpy ((char*)p->a, s, sizeof p->a);
     113    strncpy ((char*)p->b, s, sizeof p->b);
     114    strncpy ((char*)p->c, s, sizeof p->c);
     115  }
     116  
     117  struct ConstVolatilePointers
     118  {
     119    const volatile char NONSTR *p;
     120    const volatile signed char NONSTR *q;
     121    const volatile unsigned char NONSTR *r;
     122  };
     123  
     124  void test_const_volatile_pointers (struct ConstVolatilePointers *p)
     125  {
     126    strncpy ((char*)p->p, S, sizeof S - 1);
     127    strncpy ((char*)p->q, S, sizeof S - 1);
     128    strncpy ((char*)p->r, S, sizeof S - 1);
     129  }
     130  
     131  /* { dg-prune-output "-Wdiscarded-qualifiers" } */