]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR fortran/31725 ([4.1 only] substring bound evaluated multiple times: wrong code...
authorFrançois-Xavier Coudert <fxcoudert@gcc.gnu.org>
Mon, 14 May 2007 19:29:33 +0000 (19:29 +0000)
committerFrançois-Xavier Coudert <fxcoudert@gcc.gnu.org>
Mon, 14 May 2007 19:29:33 +0000 (19:29 +0000)
PR fortran/31725

* trans-expr.c (gfc_conv_substring): Evaluate substring bounds
only once.

* gfortran.dg/substr_4.f: New test.

From-SVN: r124720

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

index f94b3c35ec0f8843e14ecab8d40b0df980b0c2f3..87947c269233df8091a6fdb0e99254194fbfabce 100644 (file)
@@ -1,3 +1,9 @@
+2007-05-14  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>
+
+       PR fortran/31725
+       * trans-expr.c (gfc_conv_substring): Evaluate substring bounds
+       only once.
+
 2007-05-14  Rafael Avila de Espindola  <espindola@google.com>
 
        * f95-lang.c (LANG_HOOKS_UNSIGNED_TYPE): Remove.
index 239e41e1f8b88ab7690f3163ebaf394fa4ae29c6..34be30c19a179dce739f8db44023cf871041b1f9 100644 (file)
@@ -261,6 +261,10 @@ gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
     gfc_conv_string_parameter (se);
   else
     {
+      /* Avoid multiple evaluation of substring start.  */
+      if (!CONSTANT_CLASS_P (start.expr) && !DECL_P (start.expr))
+       start.expr = gfc_evaluate_now (start.expr, &se->pre);
+
       /* Change the start of the string.  */
       if (TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
        tmp = se->expr;
@@ -279,6 +283,9 @@ gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
       gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
       gfc_add_block_to_block (&se->pre, &end.pre);
     }
+  if (!CONSTANT_CLASS_P (end.expr) && !DECL_P (end.expr))
+    end.expr = gfc_evaluate_now (end.expr, &se->pre);
+
   if (flag_bounds_check)
     {
       tree nonempty = fold_build2 (LE_EXPR, boolean_type_node,
index 0c3111793de653a1c4deb0702f3f3e3c42b32a4e..ddb8c1f348fdb9fc8a11d2d54f2c5ec6bf0807b8 100644 (file)
@@ -1,3 +1,8 @@
+2007-05-14  Francois-Xavier Coudert  <fxcoudert@gcc.gnu.org>
+
+       PR fortran/31725
+       * gfortran.dg/substr_4.f: New test.
+
 2007-05-14  Kazu Hirata  <kazu@codesourcery.com>
 
        * gcc.target/m68k/interrupt_thread-1.c,
@@ -5,7 +10,7 @@
        gcc.target/m68k/interrupt_thread-3.c: New.
        * gcc.target/m68k/m68k.exp: Accept fido.
 
-2007-05-13  Dominique d'Humières  <dominiq@lps.ens.fr>
+2007-05-13  Dominique d'Humieres  <dominiq@lps.ens.fr>
 
        * alloc_comp_basics_1.f90: Fix dg directive.
        * altreturn_3.f90: Likewise.
diff --git a/gcc/testsuite/gfortran.dg/substr_4.f b/gcc/testsuite/gfortran.dg/substr_4.f
new file mode 100644 (file)
index 0000000..fadd5b3
--- /dev/null
@@ -0,0 +1,69 @@
+! { dg-do run }
+      subroutine test_lower
+      implicit none
+      character(3), dimension(3) :: zsymel,zsymelr
+      common /xx/ zsymel, zsymelr
+      integer :: znsymelr
+      zsymel = (/ 'X', 'Y', ' ' /)
+      zsymelr= (/ 'X', 'Y', ' ' /)
+      znsymelr=2
+      call check_zsymel(zsymel,zsymelr,znsymelr)
+
+      contains
+
+      subroutine check_zsymel(zsymel,zsymelr,znsymelr)
+        implicit none
+        integer znsymelr, isym
+        character(*) zsymel(*),zsymelr(*)
+        character(len=80) buf
+        zsymel(3)(lenstr(zsymel(3))+1:)='X'
+        write (buf,10) (trim(zsymelr(isym)),isym=1,znsymelr)
+10      format(3(a,:,','))
+        if (trim(buf) /= 'X,Y') call abort
+      end subroutine check_zsymel
+
+      function lenstr(s)
+        character(len=*),intent(in) :: s
+        integer :: lenstr
+        if (len_trim(s) /= 0) call abort
+        lenstr = len_trim(s)
+      end function lenstr
+
+      end subroutine test_lower
+
+      subroutine test_upper
+      implicit none
+      character(3), dimension(3) :: zsymel,zsymelr
+      common /xx/ zsymel, zsymelr
+      integer :: znsymelr
+      zsymel = (/ 'X', 'Y', ' ' /)
+      zsymelr= (/ 'X', 'Y', ' ' /)
+      znsymelr=2
+      call check_zsymel(zsymel,zsymelr,znsymelr)
+
+      contains
+
+      subroutine check_zsymel(zsymel,zsymelr,znsymelr)
+        implicit none
+        integer znsymelr, isym
+        character(*) zsymel(*),zsymelr(*)
+        character(len=80) buf
+        zsymel(3)(:lenstr(zsymel(3))+1)='X'
+        write (buf,20) (trim(zsymelr(isym)),isym=1,znsymelr)
+20      format(3(a,:,','))
+        if (trim(buf) /= 'X,Y') call abort
+      end subroutine check_zsymel
+
+      function lenstr(s)
+        character(len=*),intent(in) :: s
+        integer :: lenstr
+        if (len_trim(s) /= 0) call abort
+        lenstr = len_trim(s)
+      end function lenstr
+
+      end subroutine test_upper
+
+      program test
+        call test_lower
+        call test_upper
+      end program test