https://gcc.gnu.org/g:14cd10815a39cc131662d4b6759ff6712ddd0b6d

commit r15-3880-g14cd10815a39cc131662d4b6759ff6712ddd0b6d
Author: Hans-Peter Nilsson <h...@axis.com>
Date:   Mon Sep 23 18:44:11 2024 +0200

    gfortran testsuite: Remove unit-files in files having open-statements, 
PR116701
    
    PR testsuite/116701 shows that left-behind files from
    unnamed gfortran open statements (named unit.N, where N =
    unit number) can interfere with the result of a subsequent
    run.  While that's unlikely to happen for a "real" fortran
    target or a test with a deleting close-statement, test-cases
    should not rely on previous test-cases passing and not
    execute along different execution paths depending on earlier
    runs, even if the difference is benevolent.
    
    Most but not all fortran test-cases go through
    gfortran-dg-runtest (gfortran.dg) or fortran-torture-execute
    (gfortran.fortran-torture).  However, the exceptions, with
    more complex framework and call-chains, either don't run or
    don't have open-statements, so a more complex solution
    doesn't seem worthwhile.  If test-cases with open-statements
    are added later to those parts of the test-suite, calls to
    fortran-delete-unit-files at the right spot may be added or
    worst case, "manual" cleanup-calls added, like:
    ! { dg-final { remote_file target delete "fort.10" } }
    Put the new proc in fortran-modules.exp since that's where other
    common fortran-testsuite dejagnu-library functions are located.
    
            PR testsuite/116701
            * lib/fortran-modules.exp (fortran-delete-unit-files): New proc.
            * lib/gfortran-dg.exp (gfortran-dg-runtest): Call
            fortran-delete-unit-files after executing test.
            * lib/fortran-torture.exp (fortran-torture-execute): Ditto.

Diff:
---
 gcc/testsuite/lib/fortran-modules.exp | 21 +++++++++++++++++++++
 gcc/testsuite/lib/fortran-torture.exp |  2 ++
 gcc/testsuite/lib/gfortran-dg.exp     |  1 +
 3 files changed, 24 insertions(+)

diff --git a/gcc/testsuite/lib/fortran-modules.exp 
b/gcc/testsuite/lib/fortran-modules.exp
index 158b16bada91..a7196f13ed22 100644
--- a/gcc/testsuite/lib/fortran-modules.exp
+++ b/gcc/testsuite/lib/fortran-modules.exp
@@ -172,3 +172,24 @@ proc igrep { args } {
     }
     return $grep_out
 }
+
+# If the code has any "open" statements for numbered units, make sure
+# no corresponding output file remains.  Redundant remove operations
+# are ok, but duplicate removals look sloppy, so track for uniqueness.
+proc fortran-delete-unit-files { src } {  
+    set openpat {open *\( *(?:unit *= *)?([0-9]+)}
+    set openmatches [igrep $src $openpat]
+    if {![string match "" $openmatches]} {
+       # verbose -log "Found \"$openmatches\""
+       set deleted_units {}
+       foreach openmatch $openmatches {
+           regexp -nocase -- "$openpat" $openmatch match unit
+           if {[lsearch $deleted_units $unit] < 0} {
+               set rmfile "fort.$unit"
+               verbose -log "Deleting $rmfile"
+               remote_file target delete "fort.$unit"
+               lappend deleted_units $unit
+           }
+       }
+    }
+}
diff --git a/gcc/testsuite/lib/fortran-torture.exp 
b/gcc/testsuite/lib/fortran-torture.exp
index 66f5bc822232..0727fb4fb0a6 100644
--- a/gcc/testsuite/lib/fortran-torture.exp
+++ b/gcc/testsuite/lib/fortran-torture.exp
@@ -332,6 +332,8 @@ proc fortran-torture-execute { src } {
            catch { remote_file build delete $executable }
         }
        $status "$testcase execution, $option"
+
+       fortran-delete-unit-files $src
     }
     cleanup-modules ""
 }
diff --git a/gcc/testsuite/lib/gfortran-dg.exp 
b/gcc/testsuite/lib/gfortran-dg.exp
index fcba95dc3961..2edc09e5c995 100644
--- a/gcc/testsuite/lib/gfortran-dg.exp
+++ b/gcc/testsuite/lib/gfortran-dg.exp
@@ -160,6 +160,7 @@ proc gfortran-dg-runtest { testcases flags 
default-extra-flags } {
        foreach flags_t $option_list {
            verbose "Testing $nshort, $flags $flags_t" 1
            dg-test $test "$flags $flags_t" ${default-extra-flags}
+           fortran-delete-unit-files $test
            cleanup-modules ""
        }
     }

Reply via email to