Skip analyzer strndup test on hppa*-*-hpux*
[official-gcc.git] / gcc / testsuite / gcc.dg / Wstring-compare.c
blob239bbfe5148372e8e733635254cc7a911a4c26bd
1 /* PR tree-optimization/90879 - fold zero-equality of strcmp between
2 a longer string and a smaller array
3 { dg-do compile }
4 { dg-options "-O2 -Wall -Wextra -ftrack-macro-expansion=0" } */
6 #include "strlenopt.h"
8 #define T(a, b) sink (0 == strcmp (a, b), a, b)
10 void sink (int, ...);
12 struct S { char a4[4], c; };
14 extern char a4[4];
15 extern char a5[5];
16 extern char b4[4];
18 /* Verify that comparison of string literals with arrays with unknown
19 content but size that prevents them from comparing equal is diagnosed. */
21 void strcmp_array_lit (void)
23 if (strcmp (a4, "1234")) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
24 // { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 }
25 sink (0, a4);
27 int cmp;
28 cmp = strcmp (a4, "1234"); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
29 if (cmp) // { dg-message "in this expression" }
30 sink (0, a4);
32 T (a4, "4321"); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
33 T (a4, "12345"); // { dg-warning "length 5 and an array of size 4 " }
34 T (a4, "123456"); // { dg-warning "length 6 and an array of size 4 " }
35 T ("1234", a4); // { dg-warning "length 4 and an array of size 4 " }
36 T ("12345", a4); // { dg-warning "length 5 and an array of size 4 " }
37 T ("123456", a4); // { dg-warning "length 6 and an array of size 4 " }
41 void strcmp_array_pstr (void)
43 const char *s4 = "1234";
46 if (strcmp (a4, s4)) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
47 // { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 }
48 sink (1, a4);
49 else
50 sink (0, a4);
54 int c;
55 c = strcmp (a4, s4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
56 if (c) // { dg-message "in this expression" }
57 sink (1, a4);
58 else
59 sink (0, a4);
62 const char *t4 = "4321";
63 const char *s5 = "12345";
64 const char *s6 = "123456";
66 T (a4, t4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
67 T (a4, s5); // { dg-warning "length 5 and an array of size 4 " }
68 T (a4, s6); // { dg-warning "length 6 and an array of size 4 " }
69 T (s4, a4); // { dg-warning "length 4 and an array of size 4 " }
70 T (s5, a4); // { dg-warning "length 5 and an array of size 4 " }
71 T (s6, a4); // { dg-warning "length 6 and an array of size 4 " }
75 void strcmp_array_cond_pstr (int i)
77 const char *s4 = i ? "1234" : "4321";
78 T (a4, s4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
79 T (a5, s4);
82 void strcmp_array_copy (void)
84 char s[8];
87 strcpy (s, "1234");
88 if (strcmp (a4, s)) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
89 // { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 }
90 sink (1, a4);
91 else
92 sink (0, a4);
96 strcpy (s, "1234");
98 int c;
99 c = strcmp (a4, s); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
100 if (c) // { dg-message "in this expression" }
101 sink (1, a4);
102 else
103 sink (0, a4);
106 strcpy (s, "4321");
107 T (a4, s); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
108 strcpy (s, "12345");
109 T (a4, s); // { dg-warning "length 5 and an array of size 4 " }
110 strcpy (s, "123456");
111 T (a4, s); // { dg-warning "length 6 and an array of size 4 " }
112 strcpy (s, "4321");
113 T (s, a4); // { dg-warning "length 4 and an array of size 4 " }
114 strcpy (s, "54321");
115 T (s, a4); // { dg-warning "length 5 and an array of size 4 " }
116 strcpy (s, "654321");
117 T (s, a4); // { dg-warning "length 6 and an array of size 4 " }
121 void strcmp_member_array_lit (const struct S *p)
123 // Not handled due to the fix for PR 92756.
124 T (p->a4, "1234"); // { dg-warning "length 4 and an array of size 4 " "pr92765" { xfail *-*-* } }
128 #undef T
129 #define T(a, b, n) sink (0 == strncmp (a, b, n), a, b)
131 void strncmp_array_lit (void)
133 if (strncmp (a4, "12345", 5)) // { dg-warning "'strncmp' of a string of length 5, an array of size 4 and bound of 5 evaluates to nonzero" }
134 // { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 }
135 sink (0, a4);
137 int cmp;
138 cmp = strncmp (a4, "54321", 5); // { dg-warning "'strncmp' of a string of length 5, an array of size 4 and bound of 5 evaluates to nonzero" }
139 if (cmp) // { dg-message "in this expression" }
140 sink (0, a4);
142 // Verify no warning when the bound is the same as the array size.
143 T (a4, "4321", 4);
144 T (a4, "654321", 4);
146 T (a4, "12345", 5); // { dg-warning "length 5, an array of size 4 and bound of 5 " }
147 T (a4, "123456", 6); // { dg-warning "length 6, an array of size 4 and bound of 6" }
149 T ("1234", a4, 4);
150 T ("12345", a4, 4);
152 T ("12345", a4, 5); // { dg-warning "length 5, an array of size 4 and bound of 5 " }
153 T ("123456", a4, 6); // { dg-warning "length 6, an array of size 4 and bound of 6 " }
157 void strncmp_strarray_copy (void)
160 char a[] = "1234";
161 char b[6];
162 strcpy (b, "12345");
163 if (strncmp (a, b, 5)) // { dg-warning "'strncmp' of strings of length 4 and 5 and bound of 5 evaluates to nonzero" }
164 // { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 }
165 sink (0, a, b);
169 char a[] = "4321";
170 char b[6];
171 strcpy (b, "54321");
172 int cmp;
173 cmp = strncmp (a, b, 5); // { dg-warning "'strncmp' of strings of length 4 and 5 and bound of 5 evaluates to nonzero" }
174 if (cmp) // { dg-message "in this expression" }
175 sink (0, a, b);
178 strcpy (a4, "abc");
179 T (a4, "54321", 5); // { dg-warning "'strncmp' of strings of length 3 and 5 and bound of 5 evaluates to nonzero " }