Attached is a much scaled-down patch that only adds a note to the -Warray-bounds warnings mentioning the declaration to which the out-of-bounds index or offset applies.
Printing the inlining context needs a bit more work but this small improvement can be made independently of it. On 07/23/2018 05:49 PM, Martin Sebor wrote:
(David, I'm hoping your your help here. Please see the end.) While looking into a recent -Warray-bounds instance in Glibc involving inlining of large functions it became apparent that GCC could do a better job of pinpointing the source of the problem. The attached patch makes a few adjustments to both the pretty printer infrastructure and to VRP to make this possible. The diagnostic pretty printer already has directives to print the inlining context for both tree and gcall* arguments, so most of the changes just adjust things to be able to pass in gimple* argument instead. The only slightly interesting change is to print the declaration to which the out-of-bounds array refers if one is known. Tested on x86_64-linux with one regression. The regression is in the gcc.dg/Warray-bounds.c test: the column numbers of the warnings are off. Adding the %G specifier to the array bounds warnings in VRP has the unexpected effect of expanding the extent of the underling. For instance, for a test case like this: int a[10]; void f (void) { a[-1] = 0; } from the expected: a[-1] = 0; ~^~~~ to this: a[-1] = 0; ~~~~~~^~~ David, do you have any idea how to avoid this? Martin
PR tree-optimization/86650 - -Warray-bounds missing inlining context gcc/ChangeLog: PR tree-optimization/86650 * tree-vrp.c (vrp_prop::check_array_ref): Print an inform message. (vrp_prop::check_mem_ref): Same. gcc/testsuite/ChangeLog: PR tree-optimization/86650 * gcc.dg/Warray-bounds-33.c: New test. diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index 7ab8898..e553f3a 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -4838,14 +4838,13 @@ vrp_prop::check_array_ref (location_t location, tree ref, tree artype = TREE_TYPE (TREE_OPERAND (ref, 0)); + bool warned = false; + /* Empty array. */ if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1)) - { - warning_at (location, OPT_Warray_bounds, - "array subscript %E is above array bounds of %qT", - low_bound, artype); - TREE_NO_WARNING (ref) = 1; - } + warned = warning_at (location, OPT_Warray_bounds, + "array subscript %E is above array bounds of %qT", + low_bound, artype); if (TREE_CODE (low_sub) == SSA_NAME) { @@ -4866,12 +4865,10 @@ vrp_prop::check_array_ref (location_t location, tree ref, : tree_int_cst_le (up_bound, up_sub)) && TREE_CODE (low_sub) == INTEGER_CST && tree_int_cst_le (low_sub, low_bound)) - { - warning_at (location, OPT_Warray_bounds, - "array subscript [%E, %E] is outside array bounds of %qT", - low_sub, up_sub, artype); - TREE_NO_WARNING (ref) = 1; - } + warned = warning_at (location, OPT_Warray_bounds, + "array subscript [%E, %E] is outside " + "array bounds of %qT", + low_sub, up_sub, artype); } else if (up_bound && TREE_CODE (up_sub) == INTEGER_CST @@ -4885,10 +4882,9 @@ vrp_prop::check_array_ref (location_t location, tree ref, dump_generic_expr (MSG_NOTE, TDF_SLIM, ref); fprintf (dump_file, "\n"); } - warning_at (location, OPT_Warray_bounds, - "array subscript %E is above array bounds of %qT", - up_sub, artype); - TREE_NO_WARNING (ref) = 1; + warned = warning_at (location, OPT_Warray_bounds, + "array subscript %E is above array bounds of %qT", + up_sub, artype); } else if (TREE_CODE (low_sub) == INTEGER_CST && tree_int_cst_lt (low_sub, low_bound)) @@ -4899,9 +4895,18 @@ vrp_prop::check_array_ref (location_t location, tree ref, dump_generic_expr (MSG_NOTE, TDF_SLIM, ref); fprintf (dump_file, "\n"); } - warning_at (location, OPT_Warray_bounds, - "array subscript %E is below array bounds of %qT", - low_sub, artype); + warned = warning_at (location, OPT_Warray_bounds, + "array subscript %E is below array bounds of %qT", + low_sub, artype); + } + + if (warned) + { + ref = TREE_OPERAND (ref, 0); + + if (DECL_P (ref)) + inform (DECL_SOURCE_LOCATION (ref), "while referencing %qD", ref); + TREE_NO_WARNING (ref) = 1; } } @@ -4916,7 +4921,8 @@ vrp_prop::check_array_ref (location_t location, tree ref, the address of the just-past-the-end element of an array). */ void -vrp_prop::check_mem_ref (location_t location, tree ref, bool ignore_off_by_one) +vrp_prop::check_mem_ref (location_t location, tree ref, + bool ignore_off_by_one) { if (TREE_NO_WARNING (ref)) return; @@ -5134,16 +5140,21 @@ vrp_prop::check_mem_ref (location_t location, tree ref, bool ignore_off_by_one) offrange[1] = offrange[1] / eltsize; } + bool warned; if (offrange[0] == offrange[1]) - warning_at (location, OPT_Warray_bounds, - "array subscript %wi is outside array bounds " - "of %qT", - offrange[0].to_shwi (), reftype); + warned = warning_at (location, OPT_Warray_bounds, + "array subscript %wi is outside array bounds " + "of %qT", + offrange[0].to_shwi (), reftype); else - warning_at (location, OPT_Warray_bounds, - "array subscript [%wi, %wi] is outside array bounds " - "of %qT", - offrange[0].to_shwi (), offrange[1].to_shwi (), reftype); + warned = warning_at (location, OPT_Warray_bounds, + "array subscript [%wi, %wi] is outside " + "array bounds of %qT", + offrange[0].to_shwi (), + offrange[1].to_shwi (), reftype); + if (warned && DECL_P (arg)) + inform (DECL_SOURCE_LOCATION (arg), "while referencing %qD", arg); + TREE_NO_WARNING (ref) = 1; return; } @@ -5183,60 +5194,69 @@ vrp_prop::search_for_addr_array (tree t, location_t location) } while (handled_component_p (t) || TREE_CODE (t) == MEM_REF); - if (TREE_CODE (t) == MEM_REF - && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR - && !TREE_NO_WARNING (t)) - { - tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0); - tree low_bound, up_bound, el_sz; - offset_int idx; - if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE - || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE - || !TYPE_DOMAIN (TREE_TYPE (tem))) - return; + if (TREE_CODE (t) != MEM_REF + || TREE_CODE (TREE_OPERAND (t, 0)) != ADDR_EXPR + || TREE_NO_WARNING (t)) + return; - low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem))); - up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem))); - el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem))); - if (!low_bound - || TREE_CODE (low_bound) != INTEGER_CST - || !up_bound - || TREE_CODE (up_bound) != INTEGER_CST - || !el_sz - || TREE_CODE (el_sz) != INTEGER_CST) - return; + tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0); + tree low_bound, up_bound, el_sz; + if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE + || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE + || !TYPE_DOMAIN (TREE_TYPE (tem))) + return; - if (!mem_ref_offset (t).is_constant (&idx)) - return; + low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem))); + up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem))); + el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem))); + if (!low_bound + || TREE_CODE (low_bound) != INTEGER_CST + || !up_bound + || TREE_CODE (up_bound) != INTEGER_CST + || !el_sz + || TREE_CODE (el_sz) != INTEGER_CST) + return; - idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz)); - if (idx < 0) + offset_int idx; + if (!mem_ref_offset (t).is_constant (&idx)) + return; + + bool warned = false; + idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz)); + if (idx < 0) + { + if (dump_file && (dump_flags & TDF_DETAILS)) { - if (dump_file && (dump_flags & TDF_DETAILS)) - { - fprintf (dump_file, "Array bound warning for "); - dump_generic_expr (MSG_NOTE, TDF_SLIM, t); - fprintf (dump_file, "\n"); - } - warning_at (location, OPT_Warray_bounds, - "array subscript %wi is below array bounds of %qT", - idx.to_shwi (), TREE_TYPE (tem)); - TREE_NO_WARNING (t) = 1; + fprintf (dump_file, "Array bound warning for "); + dump_generic_expr (MSG_NOTE, TDF_SLIM, t); + fprintf (dump_file, "\n"); } - else if (idx > (wi::to_offset (up_bound) - - wi::to_offset (low_bound) + 1)) + warned = warning_at (location, OPT_Warray_bounds, + "array subscript %wi is below " + "array bounds of %qT", + idx.to_shwi (), TREE_TYPE (tem)); + } + else if (idx > (wi::to_offset (up_bound) + - wi::to_offset (low_bound) + 1)) + { + if (dump_file && (dump_flags & TDF_DETAILS)) { - if (dump_file && (dump_flags & TDF_DETAILS)) - { - fprintf (dump_file, "Array bound warning for "); - dump_generic_expr (MSG_NOTE, TDF_SLIM, t); - fprintf (dump_file, "\n"); - } - warning_at (location, OPT_Warray_bounds, - "array subscript %wu is above array bounds of %qT", - idx.to_uhwi (), TREE_TYPE (tem)); - TREE_NO_WARNING (t) = 1; + fprintf (dump_file, "Array bound warning for "); + dump_generic_expr (MSG_NOTE, TDF_SLIM, t); + fprintf (dump_file, "\n"); } + warned = warning_at (location, OPT_Warray_bounds, + "array subscript %wu is above " + "array bounds of %qT", + idx.to_uhwi (), TREE_TYPE (tem)); + } + + if (warned) + { + if (DECL_P (t)) + inform (DECL_SOURCE_LOCATION (t), "while referencing %qD", t); + + TREE_NO_WARNING (t) = 1; } } diff --git a/gcc/testsuite/gcc.dg/Warray-bounds-33.c b/gcc/testsuite/gcc.dg/Warray-bounds-33.c new file mode 100644 index 0000000..cea7c4b --- /dev/null +++ b/gcc/testsuite/gcc.dg/Warray-bounds-33.c @@ -0,0 +1,18 @@ +/* PR tree-optimization/86650 - -Warray-bounds missing inlining context + { dg-do compile } + { dg-options "-O2 -Warray-bounds" } */ + +int a[3]; /* { dg-message "while referencing .a." } */ +int x; + +inline void foo (int i) +{ + a[i + 1] = 123; /* { dg-warning "\\\[-Warray-bounds]" } */ +} + +int bar (void) +{ + foo (3); + + return x; +}