https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62309

Fritz Reese <fritzoreese at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fritzoreese at gmail dot com

--- Comment #1 from Fritz Reese <fritzoreese at gmail dot com> ---
Created attachment 33418
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33418&action=edit
Patch and testcase

I believe this is a simple fix; to actually follow the specification set forth
in the man page, don't treat symbols in a RECURSIVE namespace as if they are
saved in resolve.c (apply_default_init_local):

2014-08-29  Fritz Reese  <reese-fr...@zai.com>

    * resolve.c (apply_default_init_local): Don't treat variables in
    RECURSIVE units as saved.


diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 43eb240..a428633 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -11104,6 +11104,7 @@ apply_default_init_local (gfc_symbol *sym)
      result variable, which are also nonstatic.  */
   if (sym->attr.save || sym->ns->save_all
       || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
+          && !sym->ns->proc_name->attr.recursive
          && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
     {
       /* Don't clobber an existing initializer!  */
diff --git a/gcc/testsuite/gfortran.dg/auto_save_2.f90
b/gcc/testsuite/gfortran.
new file mode 100644
index 0000000..0d39d48
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/auto_save_2.f90
@@ -0,0 +1,52 @@
+! { dg-do run }
+! { dg-options "-fno-automatic -finit-local-zero" }
+!
+! Make sure variables are saved with -fno-automatic except in
+! functions marked RECURSIVE, and that they are still initialized with
+! -finit-local-zero.
+!
+
+function f (x)
+implicit none
+  integer f, x
+  integer a ! should be SAVEd
+  a = a + x ! should increment by y every time
+  f = a
+  return
+endfunction
+
+recursive function g (x)
+implicit none
+  integer g, x
+  integer b ! should be automatic
+  b = b + x ! should be set to y every time
+  g = b
+  return
+endfunction
+
+implicit none
+integer f, g
+
+! Should return static value of a; accumulates y
+if ( f(3) .ne. 3 ) then
+  call abort ()
+endif
+if ( f(4) .ne. 7 ) then
+  call abort ()
+endif
+if ( f(2) .ne. 9 ) then
+  call abort ()
+endif
+
+! Should return automatic value of a; equal to y each time
+if ( g(3) .ne. 3 ) then
+  call abort ()
+endif
+if ( g(4) .ne. 4 ) then
+  call abort ()
+endif
+if ( g(2) .ne. 2 ) then
+  call abort ()
+endif
+
+end

Reply via email to