Hi! tree-object-size.c has been written with C-family FEs in mind (which is where it has been solely used for initially), and as the testcases show it doesn't work well with Fortran or other FE when it started to be used everywhere recently.
Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2016-12-10 Jakub Jelinek <ja...@redhat.com> PR fortran/78758 * tree-object-size.c (compute_object_offset) <case ARRAY_REF>: Handle non-zero low bound or non-standard element sizes. * gfortran.dg/pr78758.f90: New test. * gfortran.dg/pr38868.f: Remove again bogus warning. --- gcc/tree-object-size.c.jj 2016-09-02 18:17:33.000000000 +0200 +++ gcc/tree-object-size.c 2016-12-09 23:21:25.653372199 +0100 @@ -138,13 +138,18 @@ compute_object_offset (const_tree expr, return base; t = TREE_OPERAND (expr, 1); + tree low_bound, unit_size; + low_bound = array_ref_low_bound (CONST_CAST_TREE (expr)); + unit_size = array_ref_element_size (CONST_CAST_TREE (expr)); + if (! integer_zerop (low_bound)) + t = fold_build2 (MINUS_EXPR, TREE_TYPE (t), t, low_bound); if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0) { code = MINUS_EXPR; t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t); } t = fold_convert (sizetype, t); - off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); + off = size_binop (MULT_EXPR, unit_size, t); break; case MEM_REF: --- gcc/testsuite/gfortran.dg/pr78758.f90.jj 2016-12-09 23:23:47.036584374 +0100 +++ gcc/testsuite/gfortran.dg/pr78758.f90 2016-12-09 23:24:14.176240933 +0100 @@ -0,0 +1,11 @@ +! PR fortran/78758 +! { dg-do compile } +! { dg-options "-O2 -Wall" } + +integer function pr78758 (x) + character(len=*), intent(in) :: x + character(len=16) :: y + integer, external :: z + y(2:) = " " // adjustl (x(2:)) + pr78758 = z (y) +end function pr78758 --- gcc/testsuite/gfortran.dg/pr38868.f.jj 2016-12-08 09:17:10.649955170 +0100 +++ gcc/testsuite/gfortran.dg/pr38868.f 2016-12-10 01:34:53.445284388 +0100 @@ -9,7 +9,7 @@ ANER(1)='A ' ANER(2)=' ' LINE=' ' - LINE(78:80)='xyz' ! { dg-warning "writing 3 bytes into a region of size 2" } + LINE(78:80)='xyz' WRITE(*,'(A82)') "'"//LINE//"'" END Jakub