This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Avoid duplicate -Warray-bounds warnings (PR tree-optimization/86614)
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Richard Biener <rguenther at suse dot de>, Jeff Law <law at redhat dot com>, Martin Sebor <msebor at gmail dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Thu, 22 Nov 2018 21:04:44 +0100
- Subject: [PATCH] Avoid duplicate -Warray-bounds warnings (PR tree-optimization/86614)
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
Hi!
On the following testcases, we warn twice, once in the FE array bounds
warning code and once later on.
The FE array bounds warning code sets TREE_NO_WARNING on the corresponding
MEM_REF, so it is easy to avoid the duplicate warning later.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2018-11-22 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/86614
* gimple-ssa-warn-restrict.c (maybe_diag_offset_bounds): Return early
if TREE_NO_WARNING is set on ref.ref.
* c-c++-common/Warray-bounds-2.c (wrap_strncpy_dstarray_diff_neg,
call_strncpy_dstarray_diff_neg): Don't expect late -Warray-bounds
warnings, just early ones from FE. Remove dg-prune-output.
* c-c++-common/Warray-bounds-6.c: New test.
--- gcc/gimple-ssa-warn-restrict.c.jj 2018-10-19 10:59:08.279393367 +0200
+++ gcc/gimple-ssa-warn-restrict.c 2018-11-22 18:13:33.813739648 +0100
@@ -1582,6 +1582,9 @@ maybe_diag_offset_bounds (location_t loc
if (!warn_array_bounds)
return false;
+ if (ref.ref && TREE_NO_WARNING (ref.ref))
+ return false;
+
offset_int ooboff[] = { ref.offrange[0], ref.offrange[1] };
tree oobref = ref.offset_out_of_bounds (strict, ooboff);
if (!oobref)
--- gcc/testsuite/c-c++-common/Warray-bounds-2.c.jj 2018-07-23 09:46:57.352997850 +0200
+++ gcc/testsuite/c-c++-common/Warray-bounds-2.c 2018-11-22 18:29:49.911602501 +0100
@@ -201,18 +201,16 @@ void call_strncpy_dst_diff_max (const ch
static void
wrap_strncpy_dstarray_diff_neg (char *d, const char *s, ptrdiff_t i, size_t n)
{
- strncpy (d + i, s, n); /* { dg-warning "offset -\[0-9\]+ is out of the bounds \\\[0, 90] of object .ar10. with type .(struct )?Array ?\\\[2]." "strncpy" } */
-}
+ strncpy (d + i, s, n); /* { dg-bogus "offset -\[0-9\]+ is out of the bounds \\\[0, 90] of object .ar10. with type .(struct )?Array ?\\\[2]." "strncpy" } */
+} /* { dg-warning "array subscript -1 is outside array bounds" "" { target *-*-* } .-1 } */
void call_strncpy_dstarray_diff_neg (const char *s, size_t n)
{
- struct Array ar10[2]; /* { dg-message ".ar10. declared here" } */
- sink (&ar10);
+ struct Array ar10[2]; /* { dg-bogus ".ar10. declared here" } */
+ sink (&ar10); /* { dg-message "while referencing" "" { target *-*-* } .-1 } */
int off = (char*)ar10[1].a17 - (char*)ar10 + 1;
wrap_strncpy_dstarray_diff_neg (ar10[1].a17, s, -off, n);
sink (&ar10);
}
-
-/* { dg-prune-output "outside array bounds" } */
--- gcc/testsuite/c-c++-common/Warray-bounds-6.c.jj 2018-11-22 18:26:00.286397043 +0100
+++ gcc/testsuite/c-c++-common/Warray-bounds-6.c 2018-11-22 18:31:55.479527492 +0100
@@ -0,0 +1,18 @@
+/* PR tree-optimization/86614 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Warray-bounds" } */
+
+extern char *strncpy (char *, const char *, __SIZE_TYPE__);
+
+void sink (void *);
+
+struct A { char b[17]; } a[2];
+
+void g (const char *s, unsigned n)
+{
+ int i = (char *)a[1].b - (char *)a + 1;
+ char *d = a[1].b;
+ /* Ensure the same bug is not diagnosed more than once. */
+ strncpy (d + i, s, n); /* { dg-warning "array subscript \[0-9]+ is outside array bounds of" } */
+ /* { dg-bogus "offset \[0-9]+ is out of the bounds \\\[0, \[0-9]+\\\] of object 'a' with type" "" { target *-*-* } .-1 } */
+}
Jakub