1 // PR tree-optimization/71625 - missing strlen optimization on different
2 // array initialization style
4 // Verify that strlen() calls with constant character array arguments
5 // initialized with string constants are folded. (This is a small
8 // { dg-options "-O0 -Wno-error=narrowing -fdump-tree-gimple" }
10 #define A(expr) do { typedef char A[-1 + 2 * !!(expr)]; } while (0)
12 /* This is undefined but accepted without -Wpedantic. Verify that
14 const char ax[] = { };
21 const char a0[] = { 'a', 'b', 'c', '\0' };
25 return __builtin_strlen (a0);
28 // Verify that narrowing warnings are preserved.
30 sa0[] = { 'a', 'b', 255, '\0' }; // { dg-warning "\\\[\(-Wnarrowing|-Woverflow\)" "" { target { ! c++98_only } } }
34 return __builtin_strlen ((const char*)sa0);
38 ua0[] = { 'a', 'b', -1, '\0' }; // { dg-warning "\\\[\(-Wnarrowing|-Woverflow\)" "" { target { ! c++98_only } } }
42 return __builtin_strlen ((const char*)ua0);
46 const char a1[] = { 'a', 'b', 'c', c };
50 return __builtin_strlen (a1);
57 a[] = { 1, 2, 333, 0 }; // { dg-warning "\\\[\(-Wnarrowing|-Woverflow\)" }
58 return __builtin_strlen (a);
61 template int tmplen<char>();
63 const wchar_t ws4[] = { 1, 2, 3, 4 };
64 const wchar_t ws7[] = { 1, 2, 3, 4, 0, 0, 0 };
65 const wchar_t ws9[9] = { 1, 2, 3, 4, 0 };
69 A (sizeof ws4 == 4 * sizeof *ws4);
70 A (ws4[0] == 1 && ws4[1] == 2 && ws4[2] == 3 && ws4[3] == 4);
72 A (sizeof ws7 == 7 * sizeof *ws7);
73 A (ws7[0] == 1 && ws7[1] == 2 && ws7[2] == 3 && ws7[4] == 4
74 && !ws7[5] && !ws7[6]);
76 A (sizeof ws9 == 9 * sizeof *ws9);
77 A (ws9[0] == 1 && ws9[1] == 2 && ws9[2] == 3 && ws9[4] == 4
78 && !ws9[5] && !ws9[6] && !ws9[7] && !ws9[8]);
83 // The following aren't handled.
86 const char a2[] = { 'a', 'b', 'c', cref };
90 return __builtin_strlen (a2);
94 const char* const cptr = &cref;
95 const char a3[] = { 'a', 'b', 'c', *cptr };
99 return __builtin_strlen (a3);
104 // { dg-final { scan-tree-dump-times "strlen" 0 "gimple" } }