From dc181880947cbfb3d652c6d9530cea07cf8280d8 Mon Sep 17 00:00:00 2001
From: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Date: Fri, 18 Oct 2013 21:08:49 +0200
Subject: [PATCH] auto-wipe dump files

---
 gcc/config/arm/neon-testgen.ml        |   1 -
 gcc/doc/sourcebuild.texi              |  19 ++--
 gcc/testsuite/g++.dg/cdce3.C          |   5 +-
 gcc/testsuite/gcc.dg/cdce1.c          |   3 +-
 gcc/testsuite/gcc.dg/cdce2.c          |   3 +-
 gcc/testsuite/gcc.dg/strlenopt-22.c   |   3 +-
 gcc/testsuite/gcc.dg/strlenopt-24.c   |   3 +-
 gcc/testsuite/gcc.dg/tree-ssa/vrp26.c |   3 +-
 gcc/testsuite/gcc.dg/tree-ssa/vrp28.c |   3 +-
 gcc/testsuite/lib/dg-pch.exp          |  18 +++-
 gcc/testsuite/lib/gcc-dg.exp          | 160 ++++++++++++++++++++++++----------
 gcc/testsuite/lib/profopt.exp         |   3 +
 gcc/testsuite/obj-c++.dg/encode-2.mm  |   3 +-
 13 files changed, 151 insertions(+), 76 deletions(-)

diff --git a/gcc/config/arm/neon-testgen.ml b/gcc/config/arm/neon-testgen.ml
index 543318b..4734ac0 100644
--- a/gcc/config/arm/neon-testgen.ml
+++ b/gcc/config/arm/neon-testgen.ml
@@ -139,7 +139,6 @@ let emit_epilogue chan features regexps =
      else
        ()
     );
-    Printf.fprintf chan "/* { dg-final { cleanup-saved-temps } } */\n"
 
 (* Check a list of C types to determine which ones are pointers and which
    ones are const.  *)
diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index 1a70916..7e0ebd9 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -2145,13 +2145,17 @@ Check branch and/or call counts, in addition to line counts, in
 
 @subsubsection Clean up generated test files
 
+Usually the test-framework removes files that were generated during
+testing. If a testcase, for example, uses any dumping mechanism to
+inspect a passes dump file, the testsuite recognized the dumping option
+passed to the tool and schedules a final cleanup to remove these files.
+
+There are, however, following additional cleanup directives that can be
+used to annotate a testcase "manually".
 @table @code
 @item cleanup-coverage-files
 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 "@var{list-of-extra-modules}"
 Removes Fortran module files generated for this test, excluding the
 module names listed in keep-modules.
@@ -2192,15 +2196,6 @@ Removes profiling files generated for this test.
 @item cleanup-repo-files
 Removes files generated for this test for @option{-frepo}.
 
-@item cleanup-rtl-dump @var{suffix}
-Removes RTL dump files generated for this test.
-
-@item cleanup-saved-temps
-Removes files for the current test which were kept for @option{-save-temps}.
-
-@item cleanup-tree-dump @var{suffix}
-Removes tree dump files matching @var{suffix} which were generated for
-this test.
 @end table
 
 @node Ada Tests
diff --git a/gcc/testsuite/g++.dg/cdce3.C b/gcc/testsuite/g++.dg/cdce3.C
index 726e9ec..3937953 100644
--- a/gcc/testsuite/g++.dg/cdce3.C
+++ b/gcc/testsuite/g++.dg/cdce3.C
@@ -4,8 +4,9 @@
 /* { dg-additional-options "-DLARGE_LONG_DOUBLE" { target large_long_double } } */
 /* { dg-additional-options "-DGNU_EXTENSION" { target pow10 } } */
 /* { dg-add-options ieee } */
+/* { dg-final { scan-tree-dump  "cdce3.C:91: note: function call is shrink-wrapped into error conditions\." "cdce" { target pow10 } } } */
 /* { dg-final { scan-tree-dump  "cdce3.C:92: note: function call is shrink-wrapped into error conditions\." "cdce" { target pow10 } } } */
-/* { dg-final { scan-tree-dump  "cdce3.C:93: note: function call is shrink-wrapped into error conditions\." "cdce" { target pow10 } } } */
+/* { dg-final { scan-tree-dump  "cdce3.C:94: note: function call is shrink-wrapped into error conditions\." "cdce" } } */
 /* { dg-final { scan-tree-dump  "cdce3.C:95: note: function call is shrink-wrapped into error conditions\." "cdce" } } */
 /* { dg-final { scan-tree-dump  "cdce3.C:96: note: function call is shrink-wrapped into error conditions\." "cdce" } } */
 /* { dg-final { scan-tree-dump  "cdce3.C:97: note: function call is shrink-wrapped into error conditions\." "cdce" } } */
@@ -19,8 +20,6 @@
 /* { dg-final { scan-tree-dump  "cdce3.C:105: note: function call is shrink-wrapped into error conditions\." "cdce" } } */
 /* { dg-final { scan-tree-dump  "cdce3.C:106: note: function call is shrink-wrapped into error conditions\." "cdce" } } */
 /* { dg-final { scan-tree-dump  "cdce3.C:107: note: function call is shrink-wrapped into error conditions\." "cdce" } } */
-/* { dg-final { scan-tree-dump  "cdce3.C:108: note: function call is shrink-wrapped into error conditions\." "cdce" } } */
-/* { dg-final { cleanup-tree-dump "cdce" } } */
 
 #include <stdlib.h>
 #include <math.h>
diff --git a/gcc/testsuite/gcc.dg/cdce1.c b/gcc/testsuite/gcc.dg/cdce1.c
index 2123f7f..02b47c0 100644
--- a/gcc/testsuite/gcc.dg/cdce1.c
+++ b/gcc/testsuite/gcc.dg/cdce1.c
@@ -1,8 +1,7 @@
 /* { dg-do  run  } */
 /* { dg-options "-O2 -fmath-errno -fdump-tree-cdce-details  -lm" } */
 /* { dg-require-effective-target int32plus } */
-/* { dg-final { scan-tree-dump  "cdce1.c:17: note: function call is shrink-wrapped into error conditions\."  "cdce" } } */
-/* { dg-final { cleanup-tree-dump "cdce" } } */
+/* { dg-final { scan-tree-dump  "cdce1.c:16: note: function call is shrink-wrapped into error conditions\."  "cdce" } } */
 /* { dg-require-effective-target large_double } */
 
 #include <stdlib.h>
diff --git a/gcc/testsuite/gcc.dg/cdce2.c b/gcc/testsuite/gcc.dg/cdce2.c
index a461ce7..dafaeab 100644
--- a/gcc/testsuite/gcc.dg/cdce2.c
+++ b/gcc/testsuite/gcc.dg/cdce2.c
@@ -1,8 +1,7 @@
 /* { dg-do  run  } */
 /* { dg-skip-if "doubles are floats" { "avr-*-*" } { "*" } { "" } } */
 /* { dg-options "-O2 -fmath-errno -fdump-tree-cdce-details  -lm" } */
-/* { dg-final { scan-tree-dump  "cdce2.c:16: note: function call is shrink-wrapped into error conditions\." "cdce" } }*/
-/* { dg-final { cleanup-tree-dump "cdce" } } */
+/* { dg-final { scan-tree-dump  "cdce2.c:15: note: function call is shrink-wrapped into error conditions\." "cdce" } }*/
  
 #include <stdlib.h>
 #include <math.h>
diff --git a/gcc/testsuite/gcc.dg/strlenopt-22.c b/gcc/testsuite/gcc.dg/strlenopt-22.c
index d6fd4df..aa55f5e 100644
--- a/gcc/testsuite/gcc.dg/strlenopt-22.c
+++ b/gcc/testsuite/gcc.dg/strlenopt-22.c
@@ -36,5 +36,4 @@ main ()
 /* { dg-final { scan-tree-dump-times "strcpy \\(" 1 "strlen" } } */
 /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen" } } */
 /* { dg-final { scan-tree-dump-times "strchr \\(" 1 "strlen" } } */
-/* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen" } } *
-/* { dg-final { cleanup-tree-dump "strlen" } } */
+/* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen" } } */
diff --git a/gcc/testsuite/gcc.dg/strlenopt-24.c b/gcc/testsuite/gcc.dg/strlenopt-24.c
index 962e04f..639501a 100644
--- a/gcc/testsuite/gcc.dg/strlenopt-24.c
+++ b/gcc/testsuite/gcc.dg/strlenopt-24.c
@@ -13,5 +13,4 @@ main ()
   return 0;
 }
 
-/* { dg-final { scan-tree-dump-times "strlen \\(" 0 "strlen" } } *
-/* { dg-final { cleanup-tree-dump "strlen" } } */
+/* { dg-final { scan-tree-dump-times "strlen \\(" 0 "strlen" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp26.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp26.c
index 6215416..021d2de 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/vrp26.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp26.c
@@ -9,8 +9,7 @@ foo(int a)
 }
 
 /* VRP should optimize this to a trivial "return 1".   */
-/* { dg-final { scan-tree-dump-times "return 1" 1 "vrp1" } } * /
-/* { dg-final { cleanup-tree-dump "vrp1" } } */
+/* { dg-final { scan-tree-dump-times "return 1" 1 "vrp1" } } */
 
 
 
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp28.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp28.c
index 6b2a1fd..8c2a72b 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/vrp28.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp28.c
@@ -26,7 +26,6 @@ int f3 (unsigned char c)
     return 0;
 }
 
-/* { dg-final { scan-tree-dump-times "if " 0 "vrp1" } } * /
-/* { dg-final { cleanup-tree-dump "vrp1" } } */
+/* { dg-final { scan-tree-dump-times "if " 0 "vrp1" } } */
 
 
diff --git a/gcc/testsuite/lib/gcc-dg.exp b/gcc/testsuite/lib/gcc-dg.exp
index f64f4a1..1f96226 100644
--- a/gcc/testsuite/lib/gcc-dg.exp
+++ b/gcc/testsuite/lib/gcc-dg.exp
@@ -106,6 +106,121 @@ if { $orig_environment_saved == 0 } {
     set_ld_library_path_env_vars
 }
 
+# Deduce generated files from tool flags, return finalcode string
+proc schedule-cleanups { opts } {
+    global additional_sources
+    set finalcode ""
+    set testcases {}
+    lappend testcases [lindex [testname-for-summary] 0]
+    verbose "Cleanup testcases: $testcases" 4
+    if { [info exists additional_sources] && $additional_sources != "" } {
+	lappend testcases $additional_sources
+	verbose "Cleanup testcases, additional: $additional_sources" 4
+    }
+    # tuple of <fam>.<pass> where <pass> might be '*' for all
+    set fams {}
+    verbose "Cleanup all options: $opts" 4
+
+    # fixups
+    # --dump= should translate to -d with joined operand.
+    if [regexp -- {(^|\s+)--dump=[^\s]+(\s+|$)} $opts] {
+	regsub -all -- {--dump=} $opts {-d} opts
+    }
+    # ??? -fdump-class-hierarchy misnomer: missing -tree
+    if [regexp -- {(^|\s+)-fdump-class-hierarchy(\s+|$)} $opts] {
+	regsub -all -- {-fdump-class-hierarchy} $opts {-fdump-tree-class-hierarchy} opts
+    }
+
+    # -fprofile-generate -> cleanup-coverage-files()
+    # -fstack-usage -> cleanup-stack-usage()
+    if [regexp -- {(^|\s+)-fstack-usage(\s+|$)} $opts] {
+	verbose "Cleanup -fstack-usage seen" 4
+#	append finalcode "cleanup-stack-usage\n"
+    }
+    # -save-temps -> cleanup-saved-temps()
+    if [regexp -- {(^|\s+)-save-temps(\s+|$)} $opts] {
+	verbose "Cleanup -save-temps seen" 4
+	append finalcode "cleanup-saved-temps\n"
+    }
+    if [regexp -- {(^|\s+)-da(\s+|$)} $opts] { # shorthand for -fdump-rtl-all
+	verbose "Cleanup -da seen" 4
+	lappend fams "r"
+    }
+    if [regexp -- {(^|\s+)-flto(\s+|$)} $opts] {
+	verbose "Cleanup -flto seen" 4
+	set ltrans "{ltrans\[0-9\]*.,}"
+    } else {
+	set ltrans ""
+    }
+
+    # -fdump-<family>-<passname>-<detail> (plus exceptions)
+    set dflags [regexp -all -inline -- {(?=(?:^|[ \t]+)?)-fdump-[^ \t]+(?=(?:$|[ \t]+)?)} $opts]
+    verbose "Cleanup -fdump-* flags: $dflags" 4
+    foreach d $dflags {
+	verbose "Cleanup examine $d" 4
+	foreach { _ fam pas lvl } \
+		[regexp -all -inline -- {[^-]+(?=(?:-)?)} $d] {
+	    regsub -- {^all$} $pas {*} pas
+	    # -fdump-rtl-<passname>-graph -> .dot and ignore rest
+	    set lvl [lindex [list "" ".dot"] [string equal "graph" $lvl]]
+	    verbose "Cleanup fam=$fam pas=$pas lvl=$lvl" 4
+	    if [regexp {(rtl|tree|ipa)} $fam] {
+		# per-pass in the light of -fdump-tree-local-pure-const1
+		# is a bit too much for the moment..
+                set f [string index $fam 0]
+                if { [lsearch -exact $fams $f] < 0 } {
+		    lappend fams $f
+		}
+	    }
+	}
+    }
+    if { [llength $fams] < 1 } {
+	return $finalcode
+    } elseif { [llength $fams] == 1 } {
+	set fams [lindex $fams 0]
+    } else {
+	set fams [join $fams ","]
+	set fams "{$fams}"
+    }
+    # stem.ext.<passnum><fam>.<passname><pass-instances>
+    # (tree)passes can have multiple instances, thus optional trailing [0-9]
+    # That will generate *{\[0-9\],} for -fdump-fam-all but is nicer otherwise
+    #set tuples "\[0-9\]\[0-9\]\[0-9\]$tuples{\[0-9\],}"
+    set fams "\[0-9\]\[0-9\]\[0-9\]$fams.*"
+    set fams "$ltrans$fams"
+    verbose "Cleanup final fams: $fams" 4
+    set tfiles {}
+    foreach src $testcases {
+	set basename [file tail $src]
+	if { $ltrans != "" } {
+	    # ??? should we use upvar 1 output_file instead of this (dup ?)
+	    set stem [file rootname $basename]
+	    set basename_ext [file extension $basename]
+	    if {$basename_ext != ""} {
+		regsub -- {^.*\.} $basename_ext {} basename_ext
+	    }
+	    lappend tfiles "$stem.{$basename_ext,exe}"
+	    unset basename_ext
+	} else {
+	    lappend tfiles $basename
+	}
+    }
+    if { [llength $tfiles] > 1 } {
+	set tfiles [join $tfiles ","]
+	set tfiles "{$tfiles}"
+    }
+    verbose "Cleanup final testcases: $tfiles" 4
+    # We have to quote the regex
+    regsub -all {([][$^?+*()|\\{}])} "$tfiles.$fams" {\\\1} ptn
+    set final ""
+    append final {remove-build-file }
+    append final "\"$ptn\""
+    verbose "Cleanup final: $final" 4
+    append finalcode "$final\n"
+
+    return $finalcode
+}
+
 # Define gcc callbacks for dg.exp.
 
 proc gcc-dg-test-1 { target_compile prog do_what extra_tool_flags } {
@@ -177,6 +292,7 @@ proc gcc-dg-test-1 { target_compile prog do_what extra_tool_flags } {
 	}
     }
 
+    append finalcode [schedule-cleanups "$options $extra_tool_flags"]
     if { $extra_tool_flags != "" } {
 	lappend options "additional_flags=$extra_tool_flags"
     }
@@ -518,27 +634,6 @@ proc cleanup-repo-files { } {
     }
 }
 
-# Remove compiler-generated RTL dump files for the current test.
-#
-# SUFFIX is the filename suffix pattern.
-proc cleanup-rtl-dump { suffix } {
-  cleanup-dump "\[0-9\]\[0-9\]\[0-9\]r.$suffix"
-}
-
-# Remove a specific tree dump file for the current test.
-#
-# SUFFIX is the tree dump file suffix pattern.
-proc cleanup-tree-dump { suffix } {
-  cleanup-dump "\[0-9\]\[0-9\]\[0-9\]t.$suffix"
-}
-
-# Remove a specific ipa dump file for the current test.
-#
-# SUFFIX is the ipa dump file suffix pattern.
-proc cleanup-ipa-dump { suffix } {
-  cleanup-dump "\[0-9\]\[0-9\]\[0-9\]i.$suffix"
-}
-
 # Remove a stack usage file for the current test.
 proc cleanup-stack-usage { } {
     set testcase [testname-for-summary]
@@ -567,29 +662,6 @@ proc cleanup-ada-spec { } {
     }
 }
 
-# Remove all dump files with the provided suffix.
-proc cleanup-dump { suffix } {
-    set testcase [testname-for-summary]
-    # The name might include a list of options; extract the file name.
-    set src [file tail [lindex $testcase 0]]
-    remove-build-file "[file tail $src].$suffix"
-    remove-build-file "[file rootname [file tail $src]].exe.$suffix"
-    remove-build-file "[file rootname [file tail $src]].exe.ltrans\[0-9\]*.$suffix"
-    # -fcompare-debug dumps
-    remove-build-file "[file tail $src].gk.$suffix"
-
-    # Clean up dump files for additional source files.
-    if [info exists additional_sources] {
-	foreach srcfile $additional_sources {
-	    remove-build-file "[file tail $srcfile].$suffix"
-	    remove-build-file "[file rootname [file tail $srcfile]].exe.$suffix"
-	    remove-build-file "[file rootname [file tail $srcfile]].exe.ltrans\[0-9\]*.$suffix"
-	    # -fcompare-debug dumps
-	    remove-build-file "[file tail $srcfile].gk.$suffix"
-	}
-    }
-}
-
 # Remove files kept by --save-temps for the current test.
 #
 # Currently this is only .i, .ii, .s and .o files, but more can be added
diff --git a/gcc/testsuite/lib/profopt.exp b/gcc/testsuite/lib/profopt.exp
index cdc6b00..a5aa84d 100644
--- a/gcc/testsuite/lib/profopt.exp
+++ b/gcc/testsuite/lib/profopt.exp
@@ -283,6 +283,9 @@ proc profopt-execute { src } {
 	    return
 	}
 
+	# schedule removal of dump files et al
+	# Do this before the call below destroys additional_sources..
+	append use_final_code [schedule-cleanups "$option $extra_flags"]
         set extra_options [dg-additional-files-options "" "$src"]
 
 	# Tree profiling requires TLS runtime support, which may need
diff --git a/gcc/testsuite/obj-c++.dg/encode-2.mm b/gcc/testsuite/obj-c++.dg/encode-2.mm
index 157bb52..77eb6ad 100644
--- a/gcc/testsuite/obj-c++.dg/encode-2.mm
+++ b/gcc/testsuite/obj-c++.dg/encode-2.mm
@@ -24,5 +24,4 @@ const char *enc3 = @encode(anonymous);
 
 /* { dg-final { scan-assembler "{Vec<float>=ffi}" } }  */
 /* { dg-final { scan-assembler "{Vec<double>=ddi}" } }  */
-/* { dg-final { scan-file "encode-2.o" "{?={Vec<double>=ddi}{Vec<float>=ffi}fd{Vec<signed char>=cci}i}" } }
-/* { dg-final cleanup-saved-temps } */
+/* { dg-final { scan-file "encode-2.o" "{?={Vec<double>=ddi}{Vec<float>=ffi}fd{Vec<signed char>=cci}i}" } } */
-- 
1.8.4.rc3

