aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wstring-compare.c
diff options
context:
space:
mode:
authorMartin Sebor <msebor@redhat.com>2019-10-09 21:35:11 +0000
committerMartin Sebor <msebor@gcc.gnu.org>2019-10-09 15:35:11 -0600
commita7160771da8b77a03317aab2c27706ba70fe3e9c (patch)
treed959d13c4cf04cf2f20c0cc56cd382c62d3b3b45 /gcc/testsuite/gcc.dg/Wstring-compare.c
parent89e0a492af5bec8ffa2ec5d99c4858df50d22c16 (diff)
downloadgcc-a7160771da8b77a03317aab2c27706ba70fe3e9c.zip
gcc-a7160771da8b77a03317aab2c27706ba70fe3e9c.tar.gz
gcc-a7160771da8b77a03317aab2c27706ba70fe3e9c.tar.bz2
PR tree-optimization/90879 - fold zero-equality of strcmp between a longer string and a smaller array
gcc/c-family/ChangeLog: PR tree-optimization/90879 * c.opt (-Wstring-compare): New option. gcc/testsuite/ChangeLog: PR tree-optimization/90879 * gcc.dg/Wstring-compare-2.c: New test. * gcc.dg/Wstring-compare.c: New test. * gcc.dg/strcmpopt_3.c: Scan the optmized dump instead of strlen. * gcc.dg/strcmpopt_6.c: New test. * gcc.dg/strlenopt-65.c: Remove uinnecessary declarations, add test cases. * gcc.dg/strlenopt-66.c: Run it. * gcc.dg/strlenopt-68.c: New test. gcc/ChangeLog: PR tree-optimization/90879 * builtins.c (check_access): Avoid using maxbound when null. * calls.c (maybe_warn_nonstring_arg): Adjust to get_range_strlen change. * doc/invoke.texi (-Wstring-compare): Document new warning option. * gimple-fold.c (get_range_strlen_tree): Make setting maxbound conditional. (get_range_strlen): Overwrite initial maxbound when non-null. * gimple-ssa-sprintf.c (get_string_length): Adjust to get_range_strlen changes. * tree-ssa-strlen.c (maybe_diag_stxncpy_trunc): Same. (used_only_for_zero_equality): New function. (handle_builtin_memcmp): Call it. (determine_min_objsize): Return an integer instead of tree. (get_len_or_size, strxcmp_eqz_result): New functions. (maybe_warn_pointless_strcmp): New function. (handle_builtin_string_cmp): Call it. Fold zero-equality of strcmp between a longer string and a smaller array. (get_range_strlen_dynamic): Overwrite initial maxbound when non-null. From-SVN: r276773
Diffstat (limited to 'gcc/testsuite/gcc.dg/Wstring-compare.c')
-rw-r--r--gcc/testsuite/gcc.dg/Wstring-compare.c181
1 files changed, 181 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.dg/Wstring-compare.c b/gcc/testsuite/gcc.dg/Wstring-compare.c
new file mode 100644
index 0000000..0ca492d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wstring-compare.c
@@ -0,0 +1,181 @@
+/* PR tree-optimization/90879 - fold zero-equality of strcmp between
+ a longer string and a smaller array
+ { dg-do compile }
+ { dg-options "-O2 -Wall -Wextra -ftrack-macro-expansion=0" } */
+
+#include "strlenopt.h"
+
+#define T(a, b) sink (0 == strcmp (a, b), a, b)
+
+void sink (int, ...);
+
+struct S { char a4[4], c; };
+
+extern char a4[4];
+extern char a5[5];
+extern char b4[4];
+
+/* Verify that comparison of string literals with arrays with unknown
+ content but size that prevents them from comparing equal is diagnosed. */
+
+void strcmp_array_lit (void)
+{
+ if (strcmp (a4, "1234")) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
+ // { dg-bogus "in this expreession" "unwanted note" { target *-*-* } .-1 }
+ sink (0, a4);
+
+ int cmp;
+ cmp = strcmp (a4, "1234"); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
+ if (cmp) // { dg-message "in this expression" }
+ sink (0, a4);
+
+ T (a4, "4321"); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
+ T (a4, "12345"); // { dg-warning "length 5 and an array of size 4 " }
+ T (a4, "123456"); // { dg-warning "length 6 and an array of size 4 " }
+ T ("1234", a4); // { dg-warning "length 4 and an array of size 4 " }
+ T ("12345", a4); // { dg-warning "length 5 and an array of size 4 " }
+ T ("123456", a4); // { dg-warning "length 6 and an array of size 4 " }
+}
+
+
+void strcmp_array_pstr (void)
+{
+ const char *s4 = "1234";
+
+ {
+ if (strcmp (a4, s4)) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
+ // { dg-bogus "in this expreession" "unwanted note" { target *-*-* } .-1 }
+ sink (1, a4);
+ else
+ sink (0, a4);
+ }
+
+ {
+ int c;
+ c = strcmp (a4, s4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
+ if (c) // { dg-message "in this expression" }
+ sink (1, a4);
+ else
+ sink (0, a4);
+ }
+
+ const char *t4 = "4321";
+ const char *s5 = "12345";
+ const char *s6 = "123456";
+
+ T (a4, t4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
+ T (a4, s5); // { dg-warning "length 5 and an array of size 4 " }
+ T (a4, s6); // { dg-warning "length 6 and an array of size 4 " }
+ T (s4, a4); // { dg-warning "length 4 and an array of size 4 " }
+ T (s5, a4); // { dg-warning "length 5 and an array of size 4 " }
+ T (s6, a4); // { dg-warning "length 6 and an array of size 4 " }
+}
+
+
+void strcmp_array_cond_pstr (int i)
+{
+ const char *s4 = i ? "1234" : "4321";
+ T (a4, s4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
+ T (a5, s4);
+}
+
+void strcmp_array_copy (void)
+{
+ char s[8];
+
+ {
+ strcpy (s, "1234");
+ if (strcmp (a4, s)) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
+ // { dg-bogus "in this expreession" "unwanted note" { target *-*-* } .-1 }
+ sink (1, a4);
+ else
+ sink (0, a4);
+ }
+
+ {
+ strcpy (s, "1234");
+
+ int c;
+ c = strcmp (a4, s); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" }
+ if (c) // { dg-message "in this expression" }
+ sink (1, a4);
+ else
+ sink (0, a4);
+ }
+
+ strcpy (s, "4321");
+ T (a4, s); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
+ strcpy (s, "12345");
+ T (a4, s); // { dg-warning "length 5 and an array of size 4 " }
+ strcpy (s, "123456");
+ T (a4, s); // { dg-warning "length 6 and an array of size 4 " }
+ strcpy (s, "4321");
+ T (s, a4); // { dg-warning "length 4 and an array of size 4 " }
+ strcpy (s, "54321");
+ T (s, a4); // { dg-warning "length 5 and an array of size 4 " }
+ strcpy (s, "654321");
+ T (s, a4); // { dg-warning "length 6 and an array of size 4 " }
+}
+
+
+void strcmp_member_array_lit (const struct S *p)
+{
+ T (p->a4, "1234"); // { dg-warning "length 4 and an array of size 4 " }
+}
+
+
+#undef T
+#define T(a, b, n) sink (0 == strncmp (a, b, n), a, b)
+
+void strncmp_array_lit (void)
+{
+ 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" }
+ // { dg-bogus "in this expreession" "unwanted note" { target *-*-* } .-1 }
+ sink (0, a4);
+
+ int cmp;
+ 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" }
+ if (cmp) // { dg-message "in this expression" }
+ sink (0, a4);
+
+ // Verify no warning when the bound is the same as the array size.
+ T (a4, "4321", 4);
+ T (a4, "654321", 4);
+
+ T (a4, "12345", 5); // { dg-warning "length 5, an array of size 4 and bound of 5 " }
+ T (a4, "123456", 6); // { dg-warning "length 6, an array of size 4 and bound of 6" }
+
+ T ("1234", a4, 4);
+ T ("12345", a4, 4);
+
+ T ("12345", a4, 5); // { dg-warning "length 5, an array of size 4 and bound of 5 " }
+ T ("123456", a4, 6); // { dg-warning "length 6, an array of size 4 and bound of 6 " }
+}
+
+
+void strncmp_strarray_copy (void)
+{
+ {
+ char a[] = "1234";
+ char b[6];
+ strcpy (b, "12345");
+ if (strncmp (a, b, 5)) // { dg-warning "'strncmp' of strings of length 4 and 5 and bound of 5 evaluates to nonzero" }
+ // { dg-bogus "in this expreession" "unwanted note" { target *-*-* } .-1 }
+ sink (0, a, b);
+ }
+
+ {
+ char a[] = "4321";
+ char b[6];
+ strcpy (b, "54321");
+ int cmp;
+ cmp = strncmp (a, b, 5); // { dg-warning "'strncmp' of strings of length 4 and 5 and bound of 5 evaluates to nonzero" }
+ if (cmp) // { dg-message "in this expression" }
+ sink (0, a, b);
+ }
+
+ strcpy (a4, "abc");
+ T (a4, "54321", 5); // { dg-warning "'strncmp' of strings of length 3 and 5 and bound of 5 evaluates to nonzero " }
+}
+
+