re PR fortran/27958 (assignments to and from zero-sized string selections not handled)
authorFrancois-Xavier Coudert <coudert@clipper.ens.fr>
Thu, 8 Jun 2006 21:48:05 +0000 (23:48 +0200)
committerFrançois-Xavier Coudert <fxcoudert@gcc.gnu.org>
Thu, 8 Jun 2006 21:48:05 +0000 (21:48 +0000)
PR fortran/27958

* trans-expr.c (gfc_conv_substring): If the substring start is
greater than its end, the length of the substring is zero, and
not negative.
(gfc_trans_string_copy): Don't generate a call to
_gfortran_copy_string when destination length is zero.

* gcc/testsuite/gfortran.dg/substr_2.f: New test.

From-SVN: r114496

gcc/fortran/ChangeLog
gcc/fortran/trans-expr.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/substr_2.f [new file with mode: 0644]

index e2a4aafa2434e3310ec0ceaa781da3de9bedb01d..0a3d2c12bee9f0e966f6356f24319ae07accaa0f 100644 (file)
@@ -1,3 +1,12 @@
+2006-06-08  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
+
+       PR fortran/27958
+       * trans-expr.c (gfc_conv_substring): If the substring start is
+       greater than its end, the length of the substring is zero, and
+       not negative.
+       (gfc_trans_string_copy): Don't generate a call to
+       _gfortran_copy_string when destination length is zero.
+
 2006-06-08  Asher Langton  <langton2@llnl.gov>
        
        PR fortran/27786
index c0422b1aaf89ba20b015769f44ed430990743be8..9e5524f09c387056d6a605a2f993f5dc0d5e587d 100644 (file)
@@ -275,6 +275,8 @@ gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind)
                     build_int_cst (gfc_charlen_type_node, 1),
                     start.expr);
   tmp = fold_build2 (PLUS_EXPR, gfc_charlen_type_node, end.expr, tmp);
+  tmp = fold_build2 (MAX_EXPR, gfc_charlen_type_node, tmp,
+                    build_int_cst (gfc_charlen_type_node, 0));
   se->string_length = tmp;
 }
 
@@ -2196,6 +2198,7 @@ gfc_trans_string_copy (stmtblock_t * block, tree dlen, tree dest,
   tree tmp;
   tree dsc;
   tree ssc;
+  tree cond;
 
   /* Deal with single character specially.  */
   dsc = gfc_to_single_character (dlen, dest);
@@ -2206,12 +2209,16 @@ gfc_trans_string_copy (stmtblock_t * block, tree dlen, tree dest,
       return;
     }
 
+  cond = fold_build2 (GT_EXPR, boolean_type_node, dlen,
+                     build_int_cst (gfc_charlen_type_node, 0));
+
   tmp = NULL_TREE;
   tmp = gfc_chainon_list (tmp, dlen);
   tmp = gfc_chainon_list (tmp, dest);
   tmp = gfc_chainon_list (tmp, slen);
   tmp = gfc_chainon_list (tmp, src);
   tmp = build_function_call_expr (gfor_fndecl_copy_string, tmp);
+  tmp = fold_build3 (COND_EXPR, void_type_node, cond, tmp, build_empty_stmt ());
   gfc_add_expr_to_block (block, tmp);
 }
 
index a5455d99d9a3f04512a4363f0333bc97452071cf..53c280a0dbf3514ce00bc497814c5d2b02c3a667 100644 (file)
@@ -1,3 +1,8 @@
+2006-06-08  Francois-Xavier Coudert  <coudert@clipper.ens.fr>
+
+       PR fortran/27958
+       * gcc/testsuite/gfortran.dg/substr_2.f: New test.
+
 2006-06-08  Asher Langton  <langton2@llnl.gov>
        
        PR fortran/27786
diff --git a/gcc/testsuite/gfortran.dg/substr_2.f b/gcc/testsuite/gfortran.dg/substr_2.f
new file mode 100644 (file)
index 0000000..a7e43b6
--- /dev/null
@@ -0,0 +1,24 @@
+! { dg-do run }
+! Check that substrings behave correctly even when zero-sized
+      implicit none
+      character(len=10) :: s, t
+      integer :: i, j
+
+      s = "abcdefghij"
+      t(:10) = s(1:)
+      s(6:5) = "foo"
+      if (s /= t) call abort
+      i = 2
+      j = -1
+      s(i:i+j) = "foo"
+      if (s /= t) call abort
+      i = 20
+      s(i+1:i) = "foo"
+      if (s /= t) call abort
+      s(6:5) = s(7:5)
+      if (s /= t) call abort
+      s = t(7:6)
+      if (len(trim(s)) /= 0) call abort
+      if (len(t(8:4)) /= 0) call abort
+      if (len(trim(t(8:4))) /= 0) call abort
+      end