(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
builtin-memchr.c
       1  /* PR tree-optimization/86711 - wrong folding of memchr
       2  
       3     Verify that memchr() of arrays initialized with 16-bit wide string
       4     literals finds the nul only when it is present in the wide string.
       5  
       6     { dg-do compile }
       7     { dg-options "-O1 -Wall -fshort-wchar -fdump-tree-optimized" } */
       8  
       9  typedef __SIZE_TYPE__  size_t;
      10  typedef __WCHAR_TYPE__ wchar_t;
      11  
      12  extern void* memchr (const void*, int, size_t);
      13  extern int printf (const char*, ...);
      14  extern void abort (void);
      15  
      16  #define A(expr)							\
      17    ((expr)							\
      18     ? (void)0							\
      19     : (printf ("assertion failed on line %i: %s\n",		\
      20  			__LINE__, #expr),			\
      21        abort ()))
      22  
      23  static const wchar_t wc = L'1';
      24  static const wchar_t ws1[] = L"1";
      25  static const wchar_t ws2[2] = L"\x1234\x5678";   /* no terminating nul */
      26  static const wchar_t ws4[] = L"\x0012\x1200\x1234";
      27  
      28  void test_wide (void)
      29  {
      30    int i0 = 0;
      31    int i1 = i0 + 1;
      32    int i2 = i1 + 1;
      33  
      34    A (sizeof (wchar_t) == 2);
      35  
      36    A (memchr (L"" + 1, 0, 0) == 0);
      37    A (memchr (&wc + 1, 0, 0) == 0);
      38    A (memchr (L"\x1234", 0, sizeof (wchar_t)) == 0);
      39  
      40    A (memchr (L"" + i1, i0, i0) == 0);
      41    A (memchr (&wc + i1, i0, i0) == 0);
      42    A (memchr (L"\x1234", i0, sizeof (wchar_t)) == 0);
      43  
      44    A (memchr (ws2, 0, sizeof ws2) == 0);
      45    A (memchr (ws2, i0, sizeof ws2) == 0);
      46  
      47    const size_t nb = sizeof ws4;
      48    const size_t nwb = sizeof (wchar_t);
      49  
      50    const char *pws1 = (const char*)ws1;
      51    const char *pws4 = (const char*)ws4;
      52  
      53  #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
      54    A (memchr (ws1, i0, sizeof ws1) == pws1 + 1);
      55  
      56    A (memchr (&ws4[0], i0, nb) == pws4 + i1);
      57    A (memchr (&ws4[1], i0, nb - i1 * nwb) == pws4 + i1 * nwb);
      58    A (memchr (&ws4[2], i0, nb - i2 * nwb) == pws4 + i2 * nwb + i2);
      59  #else
      60    A (memchr (ws1, i0, sizeof ws1) == pws1 + 0);
      61  
      62    A (memchr (&ws4[0], i0, nb) == pws4 + 0);
      63    A (memchr (&ws4[1], i0, nb - i1 * nwb) == pws4 + i1 * nwb + i1);
      64    A (memchr (&ws4[2], i0, nb - i2 * nwb) == pws4 + i2 * nwb + i2);
      65  #endif
      66  }
      67  
      68  /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */