PR middle-end/77357 - strlen of constant strings not folded
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / builtins / strlen-3.c
blob8d36fa72cd4159abe530b7236717e76fa57933dc
1 /* Copyright (C) 2004 Free Software Foundation.
3 Test strlen on const variables initialized to string literals.
5 Written by Jakub Jelinek, 9/14/2004.
7 { dg-do compile }
8 { dg-options "-O2 -Wall -fdump-tree-optimized" } */
10 extern void abort (void);
11 extern __SIZE_TYPE__ strlen (const char *);
12 extern char *strcpy (char *, const char *);
13 static const char bar[] = "Hello, World!";
14 static const char baz[] = "hello, world?";
15 static const char larger[20] = "short string";
17 int l1 = 1;
18 int x = 6;
20 void
21 main_test(void)
23 #ifdef __OPTIMIZE__
24 const char *foo;
25 int i;
26 #endif
28 if (strlen (bar) != 13)
29 abort ();
31 if (strlen (bar + 3) != 10)
32 abort ();
34 if (strlen (&bar[6]) != 7)
35 abort ();
37 if (strlen (bar + (x++ & 7)) != 7)
38 abort ();
39 if (x != 7)
40 abort ();
42 #ifdef __OPTIMIZE__
43 foo = bar;
44 for (i = 0; i < 4; ++i)
46 if (i == l1 - 1)
47 foo = "HELLO, WORLD!";
48 else if (i == l1)
49 foo = bar;
50 else if (i == l1 + 1)
51 foo = "hello, world!";
52 else
53 foo = baz;
55 if (strlen (foo) != 13)
56 abort ();
57 #endif
59 if (strlen (larger) != 12)
60 abort ();
61 if (strlen (&larger[10]) != 2)
62 abort ();
64 if (strlen (larger + (x++ & 7)) != 5)
65 abort ();
66 if (x != 8)
67 abort ();
70 /* { dg-final { scan-tree-dump-not "strlen" "optimized" } } */