| 1 |
// PR tree-optimization/71625 - missing strlen optimization on different |
| 2 |
// array initialization style |
| 3 |
// |
| 4 |
// Verify that strlen() calls with constant character array arguments |
| 5 |
// initialized with string constants are folded. (This is a small |
| 6 |
// subset of pr71625). |
| 7 |
// { dg-do compile } |
| 8 |
// { dg-options "-O0 -Wno-error=narrowing -fdump-tree-gimple" } |
| 9 |
|
| 10 |
#define A(expr) do { typedef char A[-1 + 2 * !!(expr)]; } while (0) |
| 11 |
|
| 12 |
/* This is undefined but accepted without -Wpedantic. Verify that |
| 13 |
the size is zero. */ |
| 14 |
const char ax[] = { }; |
| 15 |
|
| 16 |
void size0 () |
| 17 |
{ |
| 18 |
A (sizeof ax == 0); |
| 19 |
} |
| 20 |
|
| 21 |
const char a0[] = { 'a', 'b', 'c', '\0' }; |
| 22 |
|
| 23 |
int len0 () |
| 24 |
{ |
| 25 |
return __builtin_strlen (a0); |
| 26 |
} |
| 27 |
|
| 28 |
// Verify that narrowing warnings are preserved. |
| 29 |
const signed char |
| 30 |
sa0[] = { 'a', 'b', 255, '\0' }; // { dg-warning "\\\[\(-Wnarrowing|-Woverflow\)" "" { target { ! c++98_only } } } |
| 31 |
|
| 32 |
int lens0 () |
| 33 |
{ |
| 34 |
return __builtin_strlen ((const char*)sa0); |
| 35 |
} |
| 36 |
|
| 37 |
const unsigned char |
| 38 |
ua0[] = { 'a', 'b', -1, '\0' }; // { dg-warning "\\\[\(-Wnarrowing|-Woverflow\)" "" { target { ! c++98_only } } } |
| 39 |
|
| 40 |
int lenu0 () |
| 41 |
{ |
| 42 |
return __builtin_strlen ((const char*)ua0); |
| 43 |
} |
| 44 |
|
| 45 |
const char c = 0; |
| 46 |
const char a1[] = { 'a', 'b', 'c', c }; |
| 47 |
|
| 48 |
int len1 () |
| 49 |
{ |
| 50 |
return __builtin_strlen (a1); |
| 51 |
} |
| 52 |
|
| 53 |
template <class T> |
| 54 |
int tmplen () |
| 55 |
{ |
| 56 |
static const T |
| 57 |
a[] = { 1, 2, 333, 0 }; // { dg-warning "\\\[\(-Wnarrowing|-Woverflow\)" } |
| 58 |
return __builtin_strlen (a); |
| 59 |
} |
| 60 |
|
| 61 |
template int tmplen<char>(); |
| 62 |
|
| 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 }; |
| 66 |
|
| 67 |
void wsize () |
| 68 |
{ |
| 69 |
A (sizeof ws4 == 4 * sizeof *ws4); |
| 70 |
A (ws4[0] == 1 && ws4[1] == 2 && ws4[2] == 3 && ws4[3] == 4); |
| 71 |
|
| 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]); |
| 75 |
|
| 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]); |
| 79 |
} |
| 80 |
|
| 81 |
#if 0 |
| 82 |
|
| 83 |
// The following aren't handled. |
| 84 |
|
| 85 |
const char &cref = c; |
| 86 |
const char a2[] = { 'a', 'b', 'c', cref }; |
| 87 |
|
| 88 |
int len2 () |
| 89 |
{ |
| 90 |
return __builtin_strlen (a2); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
const char* const cptr = &cref; |
| 95 |
const char a3[] = { 'a', 'b', 'c', *cptr }; |
| 96 |
|
| 97 |
int len3 () |
| 98 |
{ |
| 99 |
return __builtin_strlen (a3); |
| 100 |
} |
| 101 |
|
| 102 |
#endif |
| 103 |
|
| 104 |
// { dg-final { scan-tree-dump-times "strlen" 0 "gimple" } } |