+2010-09-06 Thomas Koenig <tkoenig@gcc.gnu.org>
+
+ PR fortran/34145
+ * trans-expr.c (gfc_conv_substring): If start and end
+ of the string reference are equal, set the length to one.
+
2010-09-06 Tobias Burnus <burnus@net-b.de>
PR fortran/45560
gfc_free (msg);
}
- tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
- end.expr, start.expr);
- tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
- build_int_cst (gfc_charlen_type_node, 1), tmp);
- tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node, tmp,
- build_int_cst (gfc_charlen_type_node, 0));
+ /* If the start and end expressions are equal, the length is one. */
+ if (ref->u.ss.end
+ && gfc_dep_compare_expr (ref->u.ss.start, ref->u.ss.end) == 0)
+ tmp = build_int_cst (gfc_charlen_type_node, 1);
+ else
+ {
+ tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
+ end.expr, start.expr);
+ tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
+ build_int_cst (gfc_charlen_type_node, 1), tmp);
+ tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
+ tmp, build_int_cst (gfc_charlen_type_node, 0));
+ }
+
se->string_length = tmp;
}
--- /dev/null
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+! PR 34145 - the length of the string should be simplified to one,
+! no library call for string comparison is necessary.
+program main
+ character (len=5) :: c
+ integer(kind=8) :: i
+ i = 3
+ c(i:i) = 'a'
+ c(i+1:i+1) = 'b'
+ if (c(i:i) /= 'a') call abort ()
+ if (c(i+1:i+1) /= 'b') call abort ()
+end program main
+! { dg-final { scan-tree-dump-times "gfortran_compare_string" 0 "original" } }
+! { dg-final { cleanup-tree-dump "original" } }