libstdc++: Fix -Wsign-compare warning in std::string::resize_for_overwrite
[official-gcc.git] / gcc / testsuite / gcc.dg / strlenopt-65.c
blob521d7ac2b42e41a0fb6b948b218f3ae3749a299a
1 /* PRE tree-optimization/90626 - fold strcmp(a, b) == 0 to zero when
2 one string length is exact and the other is unequal
3 { dg-do compile }
4 { dg-options "-O2 -Wall -Wno-string-compare -fdump-tree-optimized -ftrack-macro-expansion=0" } */
6 #include "strlenopt.h"
8 #define CAT(x, y) x ## y
9 #define CONCAT(x, y) CAT (x, y)
10 #define FAILNAME(name) CONCAT (call_ ## name ##_on_line_, __LINE__)
12 #define FAIL(name) do { \
13 extern void FAILNAME (name) (void); \
14 FAILNAME (name)(); \
15 } while (0)
17 /* Macro to emit a call to function named
18 call_in_true_branch_not_eliminated_on_line_NNN()
19 for each call that's expected to be eliminated. The dg-final
20 scan-tree-dump-time directive at the bottom of the test verifies
21 that no such call appears in output. */
22 #define ELIM_IF_TRUE(expr) \
23 if (!(expr)) FAIL (in_true_branch_not_eliminated); else (void)0
25 /* Macro to emit a call to a function named
26 call_made_in_{true,false}_branch_on_line_NNN()
27 for each call that's expected to be retained. The dg-final
28 scan-tree-dump-time directive at the bottom of the test verifies
29 that the expected number of both kinds of calls appears in output
30 (a pair for each line with the invocation of the KEEP() macro. */
31 #define TEST_KEEP(expr) \
32 if (expr) \
33 FAIL (made_in_true_branch); \
34 else \
35 FAIL (made_in_false_branch)
37 #define FOLD(init1, init2, arg1, arg2, bnd) \
38 do { \
39 char a[8], b[8]; \
40 sink (a, b); \
41 memcpy (a, init1, sizeof init1 - 1); \
42 memcpy (b, init2, sizeof init2 - 1); \
43 ELIM_IF_TRUE (0 != CMPFUNC (arg1, arg2, bnd)); \
44 } while (0)
46 #define KEEP(init1, init2, arg1, arg2, bnd) \
47 do { \
48 char a[8], b[8]; \
49 sink (a, b); \
50 memcpy (a, init1, sizeof init1 - 1); \
51 memcpy (b, init2, sizeof init2 - 1); \
52 TEST_KEEP (0 == CMPFUNC (arg1, arg2, bnd)); \
53 } while (0)
55 const char s0[1] = "";
56 const char s00[2] = "\0";
57 const char s10[2] = "1";
58 const char s20[2] = "2";
60 void sink (void*, ...);
62 void test_strcmp_elim (void)
64 #undef CMPFUNC
65 #define CMPFUNC(a, b, dummy) strcmp (a, b)
67 FOLD (s00, s10, "\0", "1", -1);
68 FOLD (s00, s10, "\0", b, -1);
69 FOLD (s00, s10, "\0", s10, -1);
71 FOLD (s00, s10, s0, "1", -1);
72 FOLD (s00, s10, s0, b, -1);
73 FOLD (s00, s10, s0, s10, -1);
75 FOLD ("\0", "1", s0, "1", -1);
76 FOLD ("\0", "1", s0, b, -1);
77 FOLD ("\0", "1", s0, s10, -1);
79 FOLD ("2", "\0", "2", "\0", -1);
80 FOLD ("2", "\0", s20, s0, -1);
82 FOLD ("\0", "1", a, b, -1);
83 FOLD ("2", "\0", a, b, -1);
85 FOLD ("4\0", "44", a, b, -1);
86 FOLD ("55", "5\0", a, b, -1);
88 FOLD ("666\0", "6666", a, "6666", -1);
89 FOLD ("666\0", "6666", a, b, -1);
90 FOLD ("7777", "777\0", a, b, -1);
92 /* Avoid testing substrings of equal length with different characters.
93 The optimization doesn't have access to the contents of the strings
94 so it can't determine whether they are equal.
96 FOLD ("111\0", "112", a, b, -1);
97 FOLD ("112", "111\0", a, b, -1); */
100 const char s123[] = "123";
101 const char s1230[] = "123\0";
103 const char s1234[] = "1234";
104 const char s12340[] = "1234\0";
106 void test_strncmp_elim (void)
108 #undef CMPFUNC
109 #define CMPFUNC(a, b, n) strncmp (a, b, n)
111 FOLD (s1230, s1234, "123", "1234", 4);
112 FOLD (s1234, s1230, "1234", "123", 4);
114 FOLD (s1230, s1234, "123", s1234, 4);
115 FOLD (s1234, s1230, "1234", s123, 4);
117 FOLD (s1230, s1234, s123, "1234", 4);
118 FOLD (s1234, s1230, s1234, "123", 4);
120 FOLD (s1230, s1234, s123, b, 4);
121 FOLD (s1234, s1230, s1234, b, 4);
123 FOLD (s1230, s1234, a, b, 4);
124 FOLD (s1234, s1230, a, b, 4);
126 FOLD ("123\0", "1234", a, b, 5);
127 FOLD ("1234", "123\0", a, b, 5);
131 #line 1000
133 void test_strcmp_keep (const char *s, const char *t)
135 #undef CMPFUNC
136 #define CMPFUNC(a, b, dummy) strcmp (a, b)
138 KEEP ("123", "123\0", a, b, /* bnd = */ -1);
139 KEEP ("123\0", "123", a, b, -1);
142 char a[8], b[8];
143 sink (a, b);
144 strcpy (a, s);
145 strcpy (b, t);
146 TEST_KEEP (0 == strcmp (a, b));
151 void test_strncmp_keep (const char *s, const char *t)
153 #undef CMPFUNC
154 #define CMPFUNC(a, b, n) strncmp (a, b, n)
156 KEEP ("1", "1", a, b, 2);
158 KEEP ("1\0", "1", a, b, 2);
159 KEEP ("1", "1\0", a, b, 2);
161 KEEP ("12\0", "12", a, b, 2);
162 KEEP ("12", "12\0", a, b, 2);
164 KEEP ("111\0", "111", a, b, 3);
165 KEEP ("112", "112\0", a, b, 3);
168 char a[8], b[8];
169 sink (a, b);
170 strcpy (a, s);
171 strcpy (b, t);
172 TEST_KEEP (0 == strncmp (a, b, sizeof a));
176 /* { dg-final { scan-tree-dump-times "call_in_true_branch_not_eliminated_" 0 "optimized" } }
178 { dg-final { scan-tree-dump-times "call_made_in_true_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 11 "optimized" } }
179 { dg-final { scan-tree-dump-times "call_made_in_false_branch_on_line_1\[0-9\]\[0-9\]\[0-9\]" 11 "optimized" } } */