diff mbox

gfortran testsuite: implicitly cleanup-modules

Message ID 1330636193-25099-1-git-send-email-rep.dot.nop@gmail.com
State New
Headers show

Commit Message

Bernhard Reutner-Fischer March 1, 2012, 9:09 p.m. UTC
Hi,

By now we have quite some leftover modules in the testsuite, again.
Given that the previous suggestion in this thread -- to have a separate
script in contrib -- did not trigger any reaction, let me suggest the
patch below instead.

Teach the testsuite to cleanup the modules.
We do this by grepping for the module names and implicitly deleting them
when the test is finished. For the testcases that use modules from other
files we introduce a keep-modules procedure that keeps either all ("",
i.e. empty list) or the given modules of the source file.

A follow-up patch would remove the now superfluous cleanup-module calls
in the remaining testcases. Since that patch is pretty big (~360k) i do
not intend to send it in but want to apply it straight to the repo (it's
just a sed '/cleanup-modules/d' on the testcases that do not occur in
the below patch).

Is it ok to reference ../doc in the ChangeLog entry below to keep the
documentation change associated with the script itself?


The patch was bootstrapped and regression tested using tcl-8.5 on
x86_64-linux-gnu with no new regressions.

Ok for trunk?
Thanks,

gcc/testsuite/ChangeLog:

2012-02-28  Bernhard Reutner-Fischer  <aldot@gcc.gnu.org>

	* gfortran.fortran-torture/compile/compile.exp: Simplify.
	* gfortran.fortran-torture/execute/execute.exp: Likewise.
	* lib/gcc-dg.exp (cleanup-modules): Move to ..
	* lib/fortran-modules.exp: .. this new file. Adjust users.
	* ../doc/sourcebuild.texi (cleanup-modules, keep-modules): Update
	documentation.
	* lib/gfortran-dg.exp (gfortran-dg-runtest,
	gfortran-dg-debug-runtest): Call cleanup-modules.
	* lib/fortran-torture.exp (fortran-torture-execute,
	fortran-torture): Likewise.
	* gfortran.dg/coarray/caf.exp: Likewise.
	* lib/lto.exp: Likewise.
	* gfortran.dg/class_4a.f03: Adjust cleanup-modules and keep-modules.
	* gfortran.dg/class_4b.f03: Likewise.
	* gfortran.dg/class_4c.f03: Likewise.
	* gfortran.dg/class_45a.f03: Likewise.
	* gfortran.dg/binding_label_tests_10.f03: Likewise.
	* gfortran.dg/binding_label_tests_10_main.f03: Likewise.
	* gfortran.dg/binding_label_tests_11.f03: Likewise.
	* gfortran.dg/binding_label_tests_11_main.f03: Likewise.
	* gfortran.dg/binding_label_tests_13.f03: Likewise.
	* gfortran.dg/binding_label_tests_13_main.f03: Likewise.
	* gfortran.dg/test_common_binding_labels_2.f03: Likewise.
	* gfortran.dg/test_common_binding_labels_2_main.f03: Likewise.
	* gfortran.dg/test_common_binding_labels_3.f03: Likewise.
	* gfortran.dg/test_common_binding_labels_3_main.f03: Likewise.
	* gfortran.dg/whole_file_28.f90: Likewise.
	* gfortran.dg/whole_file_29.f90: Likewise.
	* gfortran.dg/whole_file_30.f90: Likewise.
	* gfortran.dg/whole_file_31.f90: Likewise.

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
---
 gcc/doc/sourcebuild.texi                           |   35 +++++++-
 .../gfortran.dg/binding_label_tests_10.f03         |    3 +-
 .../gfortran.dg/binding_label_tests_10_main.f03    |    3 +-
 .../gfortran.dg/binding_label_tests_11.f03         |    3 +-
 .../gfortran.dg/binding_label_tests_11_main.f03    |    3 +-
 .../gfortran.dg/binding_label_tests_13.f03         |    1 +
 .../gfortran.dg/binding_label_tests_13_main.f03    |    3 +-
 gcc/testsuite/gfortran.dg/class_45a.f03            |    1 +
 gcc/testsuite/gfortran.dg/class_4a.f03             |    1 +
 gcc/testsuite/gfortran.dg/class_4b.f03             |    1 +
 gcc/testsuite/gfortran.dg/class_4c.f03             |    3 +-
 gcc/testsuite/gfortran.dg/coarray/caf.exp          |    3 +
 .../gfortran.dg/test_common_binding_labels_2.f03   |    2 +-
 .../test_common_binding_labels_2_main.f03          |    3 +-
 .../gfortran.dg/test_common_binding_labels_3.f03   |    1 +
 .../test_common_binding_labels_3_main.f03          |    3 +-
 gcc/testsuite/gfortran.dg/whole_file_28.f90        |    1 +
 gcc/testsuite/gfortran.dg/whole_file_29.f90        |    2 +-
 gcc/testsuite/gfortran.dg/whole_file_30.f90        |    1 +
 gcc/testsuite/gfortran.dg/whole_file_31.f90        |    2 +-
 .../gfortran.fortran-torture/compile/compile.exp   |   65 +-------------
 .../gfortran.fortran-torture/execute/execute.exp   |   65 +-------------
 gcc/testsuite/lib/fortran-modules.exp              |   98 ++++++++++++++++++++
 gcc/testsuite/lib/fortran-torture.exp              |    5 +
 gcc/testsuite/lib/gcc-dg.exp                       |    8 +--
 gcc/testsuite/lib/gfortran-dg.exp                  |    4 +
 gcc/testsuite/lib/lto.exp                          |    9 ++-
 27 files changed, 172 insertions(+), 157 deletions(-)
 create mode 100644 gcc/testsuite/lib/fortran-modules.exp

Comments

Mikael Morin March 9, 2012, 12:02 a.m. UTC | #1
On 01/03/2012 22:09, Bernhard Reutner-Fischer wrote:
> Hi,
> 
> By now we have quite some leftover modules in the testsuite, again.
> Given that the previous suggestion in this thread -- to have a separate
> script in contrib -- did not trigger any reaction, let me suggest the
> patch below instead.
> 
> Teach the testsuite to cleanup the modules.
> We do this by grepping for the module names and implicitly deleting them
> when the test is finished. For the testcases that use modules from other
> files we introduce a keep-modules procedure that keeps either all ("",
> i.e. empty list) or the given modules of the source file.
> 
> A follow-up patch would remove the now superfluous cleanup-module calls
> in the remaining testcases. Since that patch is pretty big (~360k) i do
> not intend to send it in but want to apply it straight to the repo (it's
> just a sed '/cleanup-modules/d' on the testcases that do not occur in
> the below patch).
> 
> Is it ok to reference ../doc in the ChangeLog entry below to keep the
> documentation change associated with the script itself?
> 
No, the changes are associated by being in the same commit (and their
ChangeLog entries in the same subversion commit log).

> 
> The patch was bootstrapped and regression tested using tcl-8.5 on
> x86_64-linux-gnu with no new regressions.
> 
> Ok for trunk?

According to http://tmml.sourceforge.net/doc/tcl/regexp.html
you can use the -nocase option (= case insensitive) to simplify the
regexp patterns.
I noticed one whitespace inconsistency in keep-modules.
Otherwise it looks good to me (but I'm not a testsuite maintainer).

Mikael
diff mbox

Patch

diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index ea6fedb..fc6f3e6 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -2107,8 +2107,39 @@  Removes coverage data files generated for this test.
 @item cleanup-ipa-dump @var{suffix}
 Removes IPA dump files generated for this test.
 
-@item cleanup-modules
-Removes Fortran module files generated for this test.
+@item cleanup-modules "@var{list-of-extra-modules}"
+Removes Fortran module files generated for this test, excluding the
+module names listed in keep-modules.
+Cleaning up module files is usually done automatically by the testsuite
+by looking at the source files and removing the modules after the test
+has been executed.
+@smallexample
+module MoD1
+end module MoD1
+module Mod2
+end module Mod2
+module moD3
+end module moD3
+module mod4
+end module mod4
+! @{ dg-final @{ cleanup-modules "mod1 mod2" @} @} ! redundant
+! @{ dg-final @{ keep-modules "mod3 mod4" @} @}
+@end smallexample
+
+@item keep-modules "@var{list-of-modules-not-to-delete}"
+Whitespace separated list of module names that should not be deleted by
+cleanup-modules.
+If the list of modules is empty, all modules defined in this file are kept.
+@smallexample
+module maybe_unneeded
+end module maybe_unneeded
+module keep1
+end module keep1
+module keep2
+end module keep2
+! @{ dg-final @{ keep-modules "keep1 keep2" @} @} ! just keep these two
+! @{ dg-final @{ keep-modules "" @} @} ! keep all
+@end smallexample
 
 @item cleanup-profile-file
 Removes profiling files generated for this test.
diff --git a/gcc/testsuite/gfortran.dg/binding_label_tests_10.f03 b/gcc/testsuite/gfortran.dg/binding_label_tests_10.f03
index 99c9c52..e609d34 100644
--- a/gcc/testsuite/gfortran.dg/binding_label_tests_10.f03
+++ b/gcc/testsuite/gfortran.dg/binding_label_tests_10.f03
@@ -6,5 +6,4 @@  module binding_label_tests_10
   implicit none
   integer(c_int), bind(c,name="c_one") :: one
 end module binding_label_tests_10
-
-! Do not use dg-final to cleanup-modules
+! { dg-final { keep-modules "" } }
diff --git a/gcc/testsuite/gfortran.dg/binding_label_tests_10_main.f03 b/gcc/testsuite/gfortran.dg/binding_label_tests_10_main.f03
index aa24a6a..48e8e5d 100644
--- a/gcc/testsuite/gfortran.dg/binding_label_tests_10_main.f03
+++ b/gcc/testsuite/gfortran.dg/binding_label_tests_10_main.f03
@@ -11,5 +11,4 @@  program main
   use binding_label_tests_10 ! { dg-error "collides" }
   use binding_label_tests_10_main
 end program main
-
-! { dg-final { cleanup-modules "binding_label_tests_10_main binding_label_tests_10" } }
+! { dg-final { cleanup-modules "binding_label_tests_10" } }
diff --git a/gcc/testsuite/gfortran.dg/binding_label_tests_11.f03 b/gcc/testsuite/gfortran.dg/binding_label_tests_11.f03
index 5e889a7..8dcf998 100644
--- a/gcc/testsuite/gfortran.dg/binding_label_tests_11.f03
+++ b/gcc/testsuite/gfortran.dg/binding_label_tests_11.f03
@@ -10,5 +10,4 @@  contains
     one = 1
   end function one
 end module binding_label_tests_11
-
-! Do not use dg-final to cleanup-modules
+! { dg-final { keep-modules "" } }
diff --git a/gcc/testsuite/gfortran.dg/binding_label_tests_11_main.f03 b/gcc/testsuite/gfortran.dg/binding_label_tests_11_main.f03
index 53eac7c..ef7cfce 100644
--- a/gcc/testsuite/gfortran.dg/binding_label_tests_11_main.f03
+++ b/gcc/testsuite/gfortran.dg/binding_label_tests_11_main.f03
@@ -15,5 +15,4 @@  program main
   use binding_label_tests_11 ! { dg-error "collides" }
   use binding_label_tests_11_main
 end program main
-
-! { dg-final { cleanup-modules "binding_label_tests_11_main binding_label_tests_11" } }
+! { dg-final { cleanup-modules "binding_label_tests_11" } }
diff --git a/gcc/testsuite/gfortran.dg/binding_label_tests_13.f03 b/gcc/testsuite/gfortran.dg/binding_label_tests_13.f03
index 786945d..a8e3179 100644
--- a/gcc/testsuite/gfortran.dg/binding_label_tests_13.f03
+++ b/gcc/testsuite/gfortran.dg/binding_label_tests_13.f03
@@ -6,3 +6,4 @@  module binding_label_tests_13
   integer(c_int) :: c3
   bind(c) c3
 end module binding_label_tests_13
+! { dg-final { keep-modules "" } }
diff --git a/gcc/testsuite/gfortran.dg/binding_label_tests_13_main.f03 b/gcc/testsuite/gfortran.dg/binding_label_tests_13_main.f03
index 1addc9c..355f11a 100644
--- a/gcc/testsuite/gfortran.dg/binding_label_tests_13_main.f03
+++ b/gcc/testsuite/gfortran.dg/binding_label_tests_13_main.f03
@@ -12,5 +12,4 @@  contains
     use binding_label_tests_13 ! { dg-error "collides" }
   end subroutine c_sub
 end module binding_label_tests_13_main
-! { dg-final { cleanup-modules "binding_label_tests_13 binding_label_tests_13_main" } }
-
+! { dg-final { cleanup-modules "binding_label_tests_13" } }
diff --git a/gcc/testsuite/gfortran.dg/class_45a.f03 b/gcc/testsuite/gfortran.dg/class_45a.f03
index 91f11c4..c3c9ac2 100644
--- a/gcc/testsuite/gfortran.dg/class_45a.f03
+++ b/gcc/testsuite/gfortran.dg/class_45a.f03
@@ -26,3 +26,4 @@  contains
   end function basicGet
 
 end module G_Nodes
+! { dg-final { keep-modules "" } }
diff --git a/gcc/testsuite/gfortran.dg/class_4a.f03 b/gcc/testsuite/gfortran.dg/class_4a.f03
index 3cf0b7a..9441cc7 100644
--- a/gcc/testsuite/gfortran.dg/class_4a.f03
+++ b/gcc/testsuite/gfortran.dg/class_4a.f03
@@ -12,3 +12,4 @@  module m
   type t
   end type t
 end module m
+! { dg-final { keep-modules "m" } }
diff --git a/gcc/testsuite/gfortran.dg/class_4b.f03 b/gcc/testsuite/gfortran.dg/class_4b.f03
index 4658b8c..a5d914a 100644
--- a/gcc/testsuite/gfortran.dg/class_4b.f03
+++ b/gcc/testsuite/gfortran.dg/class_4b.f03
@@ -13,3 +13,4 @@  module m2
   type, extends(t) :: t2
   end type t2
 end module m2
+! { dg-final { keep-modules "m2" } }
diff --git a/gcc/testsuite/gfortran.dg/class_4c.f03 b/gcc/testsuite/gfortran.dg/class_4c.f03
index c28a32b..088acae 100644
--- a/gcc/testsuite/gfortran.dg/class_4c.f03
+++ b/gcc/testsuite/gfortran.dg/class_4c.f03
@@ -27,5 +27,4 @@ 
   end select
   print *, i
 end
-
-! { dg-final { cleanup-modules "m m2 m3" } }
+! { dg-final { cleanup-modules "m m2" } }
diff --git a/gcc/testsuite/gfortran.dg/coarray/caf.exp b/gcc/testsuite/gfortran.dg/coarray/caf.exp
index c7e46f6..52dff75 100644
--- a/gcc/testsuite/gfortran.dg/coarray/caf.exp
+++ b/gcc/testsuite/gfortran.dg/coarray/caf.exp
@@ -61,15 +61,18 @@  foreach test [lsort [glob -nocomplain $srcdir/$subdir/*.\[fF\]{,90,95,03,08} ]]
     set option_list [list { -O2 } ]
 
     set nshort [file tail [file dirname $test]]/[file tail $test]
+    list-module-names $test
 
     foreach flags $option_list {
 	verbose "Testing $nshort (single), $flags" 1
 	dg-test $test "-fcoarray=single $flags" "" 
+	cleanup-modules ""
     }
 
     foreach flags $option_list {
 	verbose "Testing $nshort (libcaf_single), $flags" 1
 	dg-test $test "-fcoarray=lib $flags -lcaf_single" ""
+	cleanup-modules ""
     }
 }
 torture-finish
diff --git a/gcc/testsuite/gfortran.dg/test_common_binding_labels_2.f03 b/gcc/testsuite/gfortran.dg/test_common_binding_labels_2.f03
index d14c9b1..ad654b3 100644
--- a/gcc/testsuite/gfortran.dg/test_common_binding_labels_2.f03
+++ b/gcc/testsuite/gfortran.dg/test_common_binding_labels_2.f03
@@ -12,4 +12,4 @@  module test_common_binding_labels_2
   integer(c_int) :: i
   bind(c, name="") /com2/
 end module test_common_binding_labels_2
-
+! { dg-final { keep-modules "" } }
diff --git a/gcc/testsuite/gfortran.dg/test_common_binding_labels_2_main.f03 b/gcc/testsuite/gfortran.dg/test_common_binding_labels_2_main.f03
index 1b4103e..eeb981e 100644
--- a/gcc/testsuite/gfortran.dg/test_common_binding_labels_2_main.f03
+++ b/gcc/testsuite/gfortran.dg/test_common_binding_labels_2_main.f03
@@ -21,5 +21,4 @@  program main
   use test_common_binding_labels_2 ! { dg-error "does not match" }
   use test_common_binding_labels_2_main
 end program main
-
-! { dg-final { cleanup-modules "test_common_binding_labels_2_main test_common_binding_labels_2" } }
+! { dg-final { cleanup-modules "test_common_binding_labels_2" } }
diff --git a/gcc/testsuite/gfortran.dg/test_common_binding_labels_3.f03 b/gcc/testsuite/gfortran.dg/test_common_binding_labels_3.f03
index 87d6c6b..d851b5e 100644
--- a/gcc/testsuite/gfortran.dg/test_common_binding_labels_3.f03
+++ b/gcc/testsuite/gfortran.dg/test_common_binding_labels_3.f03
@@ -8,3 +8,4 @@  module test_common_binding_labels_3
   real(c_double) :: s
   bind(c, name="my_common_block") :: /mycom/
 end module test_common_binding_labels_3
+! { dg-final { keep-modules "" } }
diff --git a/gcc/testsuite/gfortran.dg/test_common_binding_labels_3_main.f03 b/gcc/testsuite/gfortran.dg/test_common_binding_labels_3_main.f03
index d2c67f6..91fcff1 100644
--- a/gcc/testsuite/gfortran.dg/test_common_binding_labels_3_main.f03
+++ b/gcc/testsuite/gfortran.dg/test_common_binding_labels_3_main.f03
@@ -10,5 +10,4 @@  program main
   use test_common_binding_labels_3_main
   use test_common_binding_labels_3 ! { dg-error "collides" }
 end program main
-
-! { dg-final { cleanup-modules "test_common_binding_labels_3_main test_common_binding_labels_3" } }
+! { dg-final { cleanup-modules "test_common_binding_labels_3" } }
diff --git a/gcc/testsuite/gfortran.dg/whole_file_28.f90 b/gcc/testsuite/gfortran.dg/whole_file_28.f90
index 78c848e..ec9efb2 100644
--- a/gcc/testsuite/gfortran.dg/whole_file_28.f90
+++ b/gcc/testsuite/gfortran.dg/whole_file_28.f90
@@ -10,3 +10,4 @@  module iso_red
   end type varying_string
 end module iso_red
 ! DO NOT CLEAN UP THE MODULE FILE - whole_file_29.f90 does it.
+! { dg-final { keep-modules "" } }
diff --git a/gcc/testsuite/gfortran.dg/whole_file_29.f90 b/gcc/testsuite/gfortran.dg/whole_file_29.f90
index 2521dad..703754c 100644
--- a/gcc/testsuite/gfortran.dg/whole_file_29.f90
+++ b/gcc/testsuite/gfortran.dg/whole_file_29.f90
@@ -24,4 +24,4 @@  contains
   end subroutine syntax_init_from_ifile
 end module syntax_rules
 end
-! { dg-final { cleanup-modules "syntax_rules ifiles iso_red" } }
+! { dg-final { cleanup-modules "iso_red" } }
diff --git a/gcc/testsuite/gfortran.dg/whole_file_30.f90 b/gcc/testsuite/gfortran.dg/whole_file_30.f90
index 813ca06..d8e401e 100644
--- a/gcc/testsuite/gfortran.dg/whole_file_30.f90
+++ b/gcc/testsuite/gfortran.dg/whole_file_30.f90
@@ -13,3 +13,4 @@  module system_defs_m
   end type sysvector_t
 end module system_defs_m
 ! DO NOT CLEAN UP THE MODULE FILE - whole_file_31.f90 does it.
+! { dg-final { keep-modules "" } }
diff --git a/gcc/testsuite/gfortran.dg/whole_file_31.f90 b/gcc/testsuite/gfortran.dg/whole_file_31.f90
index 7ef0b9f..eb77055 100644
--- a/gcc/testsuite/gfortran.dg/whole_file_31.f90
+++ b/gcc/testsuite/gfortran.dg/whole_file_31.f90
@@ -18,4 +18,4 @@  program t
   type(sysvector_t), target :: sol
   solution => sol
 end program t
-! { dg-final { cleanup-modules "system_defs_m convecreac_m" } }
+! { dg-final { cleanup-modules "system_defs_m" } }
diff --git a/gcc/testsuite/gfortran.fortran-torture/compile/compile.exp b/gcc/testsuite/gfortran.fortran-torture/compile/compile.exp
index 5c56ec3..5bc8d7f 100644
--- a/gcc/testsuite/gfortran.fortran-torture/compile/compile.exp
+++ b/gcc/testsuite/gfortran.fortran-torture/compile/compile.exp
@@ -28,7 +28,7 @@  load_lib torture-options.exp
 torture-init
 set-torture-options [get-fortran-torture-options]
 
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f]] {
+foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.\[fF\]{,90,95,03,08} ]] {
     # If we're only testing specific files and this isn't one of them, skip it.
     if ![runtest_file_p $runtests $testcase] then {
 	continue
@@ -36,67 +36,4 @@  foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f]] {
     fortran-torture $testcase
 }
 
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f90]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F90]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f95]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F95]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f03]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F03]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f08]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F08]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture $testcase
-}
-
 torture-finish
diff --git a/gcc/testsuite/gfortran.fortran-torture/execute/execute.exp b/gcc/testsuite/gfortran.fortran-torture/execute/execute.exp
index 40b65f8..d9f0a1b 100644
--- a/gcc/testsuite/gfortran.fortran-torture/execute/execute.exp
+++ b/gcc/testsuite/gfortran.fortran-torture/execute/execute.exp
@@ -32,7 +32,7 @@  load_lib torture-options.exp
 torture-init
 set-torture-options [get-fortran-torture-options]
 
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f]] {
+foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.\[fF\]{,90,95,03,08} ]] {
     # If we're only testing specific files and this isn't one of them, skip it.
     if ![runtest_file_p $runtests $testcase] then {
 	continue
@@ -40,67 +40,4 @@  foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f]] {
     fortran-torture-execute $testcase
 }
 
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f90]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F90]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f95]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F95]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f03]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F03]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.f08]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
-foreach testcase [lsort [glob -nocomplain $srcdir/$subdir/*.F08]] {
-    if ![runtest_file_p $runtests $testcase] then {
-	continue
-    }
-    fortran-torture-execute $testcase
-}
-
 torture-finish
diff --git a/gcc/testsuite/lib/fortran-modules.exp b/gcc/testsuite/lib/fortran-modules.exp
new file mode 100644
index 0000000..9aa283b
--- /dev/null
+++ b/gcc/testsuite/lib/fortran-modules.exp
@@ -0,0 +1,98 @@ 
+#   Copyright (C) 2012 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# helper to deal with fortran modules
+
+# Remove files for specified Fortran modules.
+proc cleanup-modules { modlist } {
+    global clean
+    foreach mod [concat $modlist $clean] {
+	set m [string tolower $mod].mod
+	verbose "cleanup-module `$m'" 2
+	if [is_remote host] {
+	    remote_file host delete $m
+	}
+	remote_file build delete $m
+    }
+}
+
+proc keep-modules { modlist } {
+    global clean
+    # if the modlist is empty, keep everything
+    if {[llength $modlist] < 1} {
+	set clean {}
+    } else {
+        set cleansed {}
+	foreach cl $clean {
+	    if {[lsearch $cl $modlist] < 0} {
+		lappend cleansed $cl
+	    }
+	}
+	if {[llength $clean] == [llength $cleansed]} {
+	    warning "keep-modules had no effect?! Possible typo in module name."
+	}
+	set clean $cleansed
+    }
+}
+
+# collect all module names from a source-file
+proc list-module-names { files } {
+    global clean
+    set clean {}
+    foreach file $files {
+	foreach mod [list-module-names-1 $file] {
+	    if {[lsearch $clean $mod] < 0} {
+		lappend clean $mod
+	    }
+	}
+    }
+    return [join $clean " "]
+}
+
+proc list-module-names-1 { file } {
+    set result {}
+    set tmp [grep $file "^\[ \t\]*((#)?\[ \t\]*include|\[mM\]\[oO\]\[dD\]\[uU\]\[lL\]\[eE\](?!\[ \t\]+\[pP\]\[rR\]\[oO\]\[cC\]\[eE\]\[dD\]\[uU\]\[rR\]\[eE\]\[ \t\]+))\[ \t\]+.*" line]
+    if {![string match "" $tmp]} {
+	foreach i $tmp {
+	    regexp "(\[0-9\]+)\[ \t\]+(?:(?:#)?\[ \t\]*include\[ \t\]+)\[\"\](\[^\"\]*)\[\"\]" $i dummy lineno include_file
+	    if {[info exists include_file]} {
+		set dir [file dirname $file]
+		set inc "$dir/$include_file"
+		unset include_file
+		if {![file readable $inc]} {
+		    warning "Line $lineno includes unreadable file `$inc'"
+		    continue
+		}
+		verbose "Line $lineno includes `$inc'" 3
+		foreach mod [list-module-names-1 $inc] {
+		    if {[lsearch $result $mod] < 0} {
+			lappend result $mod
+		    }
+		}
+		continue
+	    }
+	    regexp "(\[0-9\]+)\[ \t\]+(?:(\[mM\]\[oO\]\[dD\]\[uU\]\[lL\]\[eE\]\[ \t\]+(?!\[pP\]\[rR\]\[oO\]\[cC\]\[eE\]\[dD\]\[uU\]\[rR\]\[eE\]\[ \t\]+)))(\[^ \t;\]*)" $i i lineno keyword mod
+	    if {![info exists lineno]} {
+		continue
+	    }
+	    verbose "Line $lineno mentions module `$mod'" 3
+	    if {[lsearch $result $mod] < 0} {
+		lappend result $mod
+	    }
+	}
+    }
+    return $result
+}
diff --git a/gcc/testsuite/lib/fortran-torture.exp b/gcc/testsuite/lib/fortran-torture.exp
index 8d6e2dd..1009576 100644
--- a/gcc/testsuite/lib/fortran-torture.exp
+++ b/gcc/testsuite/lib/fortran-torture.exp
@@ -21,6 +21,7 @@ 
 # based on f-torture.exp, which was written by Rob Savoye.
 
 load_lib target-supports.exp
+load_lib fortran-modules.exp
 
 # Return the list of options to use for fortran torture tests.
 # The default option list can be overridden by
@@ -218,6 +219,7 @@  proc fortran-torture-execute { src } {
     if [string match "/*" $testcase] {
 	set testcase "[file tail [file dirname $src]]/[file tail $src]"
     }
+    list-module-names $src
 
     # Walk the list of options and copmile and run the testcase for all
     # options that are not explicitly disabled by the .x script (if present).
@@ -332,6 +334,7 @@  proc fortran-torture-execute { src } {
         }
 	$status "$testcase execution, $option"
     }
+    cleanup-modules ""
 }
 
 
@@ -390,6 +393,7 @@  proc fortran-torture { args } {
 	    return
 	}
     }
+    list-module-names $src
    
     # loop through all the options
     set option_list $torture_with_loops
@@ -406,6 +410,7 @@  proc fortran-torture { args } {
 	}
 
 	fortran-torture-compile $src "$option $options"
+	cleanup-modules ""
     }
 }
 
diff --git a/gcc/testsuite/lib/gcc-dg.exp b/gcc/testsuite/lib/gcc-dg.exp
index 026a8a9..4666ede 100644
--- a/gcc/testsuite/lib/gcc-dg.exp
+++ b/gcc/testsuite/lib/gcc-dg.exp
@@ -29,6 +29,7 @@  load_lib prune.exp
 load_lib libgloss.exp
 load_lib target-libpath.exp
 load_lib torture-options.exp
+load_lib fortran-modules.exp
 
 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
 setenv LC_ALL C
@@ -573,13 +574,6 @@  proc cleanup-saved-temps { args } {
     }
 }
 
-# Remove files for specified Fortran modules.
-proc cleanup-modules { modlist } {
-    foreach modname $modlist {
-	remove-build-file [string tolower $modname].mod
-    }
-}
-
 # Scan Fortran modules for a given regexp.
 #
 # Argument 0 is the module name
diff --git a/gcc/testsuite/lib/gfortran-dg.exp b/gcc/testsuite/lib/gfortran-dg.exp
index 0fd96b3..50753df 100644
--- a/gcc/testsuite/lib/gfortran-dg.exp
+++ b/gcc/testsuite/lib/gfortran-dg.exp
@@ -111,10 +111,12 @@  proc gfortran-dg-runtest { testcases default-extra-flags } {
 	}
 
 	set nshort [file tail [file dirname $test]]/[file tail $test]
+	list-module-names $test
 
 	foreach flags $option_list {
 	    verbose "Testing $nshort, $flags" 1
 	    dg-test $test $flags ${default-extra-flags}
+	    cleanup-modules ""
 	}
     }
 
@@ -171,6 +173,7 @@  proc gfortran-dg-debug-runtest { target_compile trivial opt_opts testcases } {
        }
 
        set nshort [file tail [file dirname $test]]/[file tail $test]
+	list-module-names $test
 
        foreach flags $DEBUG_TORTURE_OPTIONS {
            set doit 1
@@ -179,6 +182,7 @@  proc gfortran-dg-debug-runtest { target_compile trivial opt_opts testcases } {
            if { $doit } {
                verbose -log "Testing $nshort, $flags" 1
                dg-test $test $flags ""
+		cleanup-modules ""
            }
        }
     }
diff --git a/gcc/testsuite/lib/lto.exp b/gcc/testsuite/lib/lto.exp
index 4e5d443..536d629 100644
--- a/gcc/testsuite/lib/lto.exp
+++ b/gcc/testsuite/lib/lto.exp
@@ -499,7 +499,10 @@  proc lto-execute { src1 sid } {
 	verbose "$testcase not supported on this target, skipping it" 3
 	return
     }
-
+    # Should be safe for non-fortran too but be paranoid..
+    if {$sid eq "f_lto"} {
+	list-module-names $src_list
+    }
     regsub "_0.*" $testcase "" testcase
 
     # Set up the base name of executable files so they'll be unique.
@@ -578,6 +581,10 @@  proc lto-execute { src1 sid } {
 	     || ![string compare "link" $compile_type] } {
 	    file_on_host delete $execname
 	}
+	# Should be safe for non-fortran too but be paranoid..
+	if {$sid eq "f_lto"} {
+	    cleanup-modules ""
+	}
     }
 }