PR middle-end/77357 - strlen of constant strings not folded
[official-gcc.git] / gcc / testsuite / gcc.dg / strlenopt-15.c
blob827ea07b6ea30b51ee95cf5736a34012c4f51f19
1 /* { dg-do run } */
2 /* { dg-options "-O2 -fdump-tree-strlen" } */
4 #include "strlenopt.h"
6 __attribute__((noinline, noclone)) size_t
7 fn1 (char *p, size_t l)
9 memcpy (p, "abcdef", l);
10 /* This strlen can't be optimized, as l is unknown. */
11 return strlen (p);
14 __attribute__((noinline, noclone)) size_t
15 fn2 (char *p, const char *q, size_t *lp)
17 size_t l = strlen (q), l2;
18 memcpy (p, q, 7);
19 /* This strlen can't be optimized, as l might be bigger than 7. */
20 l2 = strlen (p);
21 *lp = l;
22 return l2;
25 __attribute__((noinline, noclone)) char *
26 fn3 (char *p)
28 *p = 0;
29 return p + 1;
32 int
33 main ()
35 char buf[64];
36 const char *volatile q = "ABCDEFGH";
37 const char *volatile q2 = "IJ\0KLMNOPQRS";
38 size_t l;
39 memset (buf, '\0', sizeof buf);
40 memset (buf + 2, 'a', 7);
41 if (fn1 (buf, 3) != 9 || memcmp (buf, "abcaaaaaa", 10) != 0)
42 abort ();
43 if (fn1 (buf, 7) != 6 || memcmp (buf, "abcdef\0aa", 10) != 0)
44 abort ();
45 if (fn2 (buf, q, &l) != 9 || l != 8 || memcmp (buf, "ABCDEFGaa", 10) != 0)
46 abort ();
47 if (fn2 (buf, q2, &l) != 2 || l != 2 || memcmp (buf, "IJ\0KLMNaa", 10) != 0)
48 abort ();
49 if (fn3 (buf) != buf + 1 || memcmp (buf, "\0J\0KLMNaa", 10) != 0)
50 abort ();
51 return 0;
54 /* { dg-final { scan-tree-dump-times "strlen \\(" 3 "strlen" } } */
55 /* { dg-final { scan-tree-dump-times "memcpy \\(" 2 "strlen" } } */
56 /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen" } } */
57 /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen" } } */
58 /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen" } } */
59 /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen" } } */