This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch, fortran] Fix PR 34145, single char detection


Hello world,

the attached patch fixes PR 34145, where we failed to detect that a
string was in fact a single character.

Regression-tested.

OK for trunk?

	Thomas

2010-09-05  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-05  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/34145
	* char_length_17.f90:  New test.
Index: trans-expr.c
===================================================================
--- trans-expr.c	(Revision 163868)
+++ trans-expr.c	(Arbeitskopie)
@@ -463,12 +463,20 @@ gfc_conv_substring (gfc_se * se, gfc_ref * ref, in
       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;
 }
 
! { 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" } }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]