OSDN Git Service

d790396ab94eb88a90e1b2c624a710dea10ea12a
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / gcc-dg.exp
1 #   Copyright (C) 1997, 1999, 2000, 2003, 2004, 2005, 2006
2 #   Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 load_lib dg.exp
19 load_lib file-format.exp
20 load_lib target-supports.exp
21 load_lib target-supports-dg.exp
22 load_lib scanasm.exp
23 load_lib scanrtl.exp
24 load_lib scantree.exp
25 load_lib scanipa.exp
26 load_lib prune.exp
27 load_lib libgloss.exp
28 load_lib target-libpath.exp
29
30 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
31 setenv LC_ALL C
32 setenv LANG C
33
34 if ![info exists TORTURE_OPTIONS] {
35     # It is theoretically beneficial to group all of the O2/O3 options together,
36     # as in many cases the compiler will generate identical executables for
37     # all of them--and the c-torture testsuite will skip testing identical
38     # executables multiple times.
39     # Also note that -finline-functions is explicitly included in one of the
40     # items below, even though -O3 is also specified, because some ports may
41     # choose to disable inlining functions by default, even when optimizing.
42     set TORTURE_OPTIONS [list \
43         { -O0 } \
44         { -O1 } \
45         { -O2 } \
46         { -O3 -fomit-frame-pointer } \
47         { -O3 -fomit-frame-pointer -funroll-loops } \
48         { -O3 -fomit-frame-pointer -funroll-all-loops -finline-functions } \
49         { -O3 -g } \
50         { -Os } ]
51 }
52
53 global GCC_UNDER_TEST
54 if ![info exists GCC_UNDER_TEST] {
55     set GCC_UNDER_TEST "[find_gcc]"
56 }
57
58 global orig_environment_saved
59
60 # This file may be sourced, so don't override environment settings
61 # that have been previously setup.
62 if { $orig_environment_saved == 0 } {
63     append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
64     set_ld_library_path_env_vars
65 }
66
67 # Split TORTURE_OPTIONS into two choices: one for testcases with loops and
68 # one for testcases without loops.
69
70 set torture_with_loops $TORTURE_OPTIONS
71 set torture_without_loops ""
72 foreach option $TORTURE_OPTIONS {
73     if ![string match "*loop*" $option] {
74         lappend torture_without_loops $option
75     }
76 }
77
78 # Define gcc callbacks for dg.exp.
79
80 proc gcc-dg-test-1 { target_compile prog do_what extra_tool_flags } {
81     # Set up the compiler flags, based on what we're going to do.
82
83     set options [list]
84
85     # Tests should be able to use "dg-do repo".  However, the dg test
86     # driver checks the argument to dg-do against a list of acceptable
87     # options, and "repo" is not among them.  Therefore, we resort to
88     # this ugly approach.
89     if [string match "*-frepo*" $extra_tool_flags] then {
90         set do_what "repo"
91     }
92
93     switch $do_what {
94         "preprocess" {
95             set compile_type "preprocess"
96             set output_file "[file rootname [file tail $prog]].i"
97         }
98         "compile" {
99             set compile_type "assembly"
100             set output_file "[file rootname [file tail $prog]].s"
101         }
102         "assemble" {
103             set compile_type "object"
104             set output_file "[file rootname [file tail $prog]].o"
105         }
106         "precompile" {
107             set compile_type "precompiled_header"
108             set output_file "[file tail $prog].gch"
109         }
110         "link" {
111             set compile_type "executable"
112             set output_file "[file rootname [file tail $prog]].exe"
113             # The following line is needed for targets like the i960 where
114             # the default output file is b.out.  Sigh.
115         }
116         "repo" {
117             set compile_type "object"
118             set output_file "[file rootname [file tail $prog]].o"
119         }
120         "run" {
121             set compile_type "executable"
122             # FIXME: "./" is to cope with "." not being in $PATH.
123             # Should this be handled elsewhere?
124             # YES.
125             set output_file "./[file rootname [file tail $prog]].exe"
126             # This is the only place where we care if an executable was
127             # created or not.  If it was, dg.exp will try to run it.
128             catch { remote_file build delete $output_file }
129         }
130         default {
131             perror "$do_what: not a valid dg-do keyword"
132             return ""
133         }
134     }
135
136     if { $extra_tool_flags != "" } {
137         lappend options "additional_flags=$extra_tool_flags"
138     }
139
140     set comp_output [$target_compile "$prog" "$output_file" "$compile_type" $options]
141
142     # Look for an internal compiler error, which sometimes masks the fact
143     # that we didn't get an expected error message.  An ICE always fails,
144     # there's no way to XFAIL it.
145     if [string match "*internal compiler error*" $comp_output] {
146         upvar 2 name name
147         fail "$name (internal compiler error)"
148     }
149
150     if { $do_what == "repo" } {
151         set object_file "$output_file"
152         set output_file "[file rootname [file tail $prog]].exe"
153         set comp_output \
154             [ concat $comp_output \
155                   [$target_compile "$object_file" "$output_file" \
156                        "executable" $options] ]
157     }
158
159     return [list $comp_output $output_file]
160 }
161
162 proc gcc-dg-test { prog do_what extra_tool_flags } {
163     return [gcc-dg-test-1 gcc_target_compile $prog $do_what $extra_tool_flags]
164 }
165
166 proc gcc-dg-prune { system text } {
167     global additional_prunes
168
169     set text [prune_gcc_output $text]
170
171     foreach p $additional_prunes {
172         if { [string length $p] > 0 } {
173             # Following regexp matches a complete line containing $p.
174             regsub -all "(^|\n)\[^\n\]*$p\[^\n\]*" $text "" text
175         }
176     }
177
178     # If we see "region xxx is full" then the testcase is too big for ram.
179     # This is tricky to deal with in a large testsuite like c-torture so
180     # deal with it here.  Just mark the testcase as unsupported.
181     if [regexp "(^|\n)\[^\n\]*: region \[^\n\]* is full" $text] {
182         # The format here is important.  See dg.exp.
183         return "::unsupported::memory full"
184     }
185
186     return $text
187 }
188
189 # Utility routines.
190
191 #
192 # search_for -- looks for a string match in a file
193 #
194 proc search_for { file pattern } {
195     set fd [open $file r]
196     while { [gets $fd cur_line]>=0 } {
197         if [string match "*$pattern*" $cur_line] then {
198             close $fd
199             return 1
200         }
201     }
202     close $fd
203     return 0
204 }
205
206 # Modified dg-runtest that can cycle through a list of optimization options
207 # as c-torture does.
208 proc gcc-dg-runtest { testcases default-extra-flags } {
209     global runtests
210
211     foreach test $testcases {
212         # If we're only testing specific files and this isn't one of
213         # them, skip it.
214         if ![runtest_file_p $runtests $test] {
215             continue
216         }
217
218         # Look for a loop within the source code - if we don't find one,
219         # don't pass -funroll[-all]-loops.
220         global torture_with_loops torture_without_loops
221         if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
222             set option_list $torture_with_loops
223         } else {
224             set option_list $torture_without_loops
225         }
226
227         set nshort [file tail [file dirname $test]]/[file tail $test]
228
229         foreach flags $option_list {
230             verbose "Testing $nshort, $flags" 1
231             dg-test $test $flags ${default-extra-flags}
232         }
233     }
234 }
235
236 proc gcc-dg-debug-runtest { target_compile trivial opt_opts testcases } {
237     global srcdir subdir
238
239     if ![info exists DEBUG_TORTURE_OPTIONS] {
240         set DEBUG_TORTURE_OPTIONS ""
241         foreach type {-gdwarf-2 -gstabs -gstabs+ -gxcoff -gxcoff+ -gcoff} {
242             set comp_output [$target_compile \
243                     "$srcdir/$subdir/$trivial" "trivial.S" assembly \
244                     "additional_flags=$type"]
245             if { ! [string match "*: target system does not support the * debug format*" \
246                     $comp_output] } {
247                 foreach level {1 "" 3} {
248                     lappend DEBUG_TORTURE_OPTIONS [list "${type}${level}"]
249                     foreach opt $opt_opts {
250                         lappend DEBUG_TORTURE_OPTIONS \
251                                 [list "${type}${level}" "$opt" ]
252                     }
253                 }
254             }
255         }
256     }
257
258     verbose -log "Using options $DEBUG_TORTURE_OPTIONS"
259
260     global runtests
261
262     foreach test $testcases {
263         # If we're only testing specific files and this isn't one of
264         # them, skip it.
265         if ![runtest_file_p $runtests $test] {
266             continue
267         }
268
269         set nshort [file tail [file dirname $test]]/[file tail $test]
270
271         foreach flags $DEBUG_TORTURE_OPTIONS {
272             set doit 1
273             if { [string match {*/debug-[126].c} "$nshort"] \
274                     && [string match "*1" [lindex "$flags" 0] ] } {
275                 set doit 0
276             }
277
278     # High optimization can remove the variable whose existence is tested.
279     # Dwarf debugging with commentary (-dA) preserves the symbol name in the
280     # assembler output, but stabs debugging does not.
281     # http://gcc.gnu.org/ml/gcc-regression/2003-04/msg00095.html
282             if { [string match {*/debug-[12].c} "$nshort"] \
283                     && [string match "*O*" "$flags"] \
284                     && ( [string match "*coff*" "$flags"] \
285                          || [string match "*stabs*" "$flags"] ) } {
286                 set doit 0
287             }
288
289             if { $doit } {
290                 verbose -log "Testing $nshort, $flags" 1
291                 dg-test $test $flags ""
292             }
293         }
294     }
295 }
296
297 # Prune any messages matching ARGS[1] (a regexp) from test output.
298 proc dg-prune-output { args } {
299     global additional_prunes
300
301     if { [llength $args] != 2 } {
302         error "[lindex $args 1]: need one argument"
303         return
304     }
305
306     lappend additional_prunes [lindex $args 1]
307 }
308
309 # Remove files matching the pattern from the build machine.
310 proc remove-build-file { pat } {
311     verbose "remove-build-file `$pat'" 2
312     set file_list "[glob -nocomplain $pat]"
313     verbose "remove-build-file `$file_list'" 2
314     foreach output_file $file_list {
315         remote_file build delete $output_file
316     }
317 }
318
319 # Remove compiler-generated coverage files for the current test.
320 proc cleanup-coverage-files { } {
321     # This assumes that we are two frames down from dg-test or some other proc
322     # that stores the filename of the testcase in a local variable "name".
323     # A cleaner solution would require a new DejaGnu release.
324     upvar 2 name testcase
325     remove-build-file "[file rootname [file tail $testcase]].gc??"
326
327     # Clean up coverage files for additional source files.
328     if [info exists additional_sources] {
329         foreach srcfile $additional_sources {
330             remove-build-file "[file rootname [file tail $srcfile]].gc??"
331         }
332     }
333 }
334
335 # Remove compiler-generated files from -repo for the current test.
336 proc cleanup-repo-files { } {
337     # This assumes that we are two frames down from dg-test or some other proc
338     # that stores the filename of the testcase in a local variable "name".
339     # A cleaner solution would require a new DejaGnu release.
340     upvar 2 name testcase
341     remove-build-file "[file rootname [file tail $testcase]].o"
342     remove-build-file "[file rootname [file tail $testcase]].rpo"
343
344     # Clean up files for additional source files.
345     if [info exists additional_sources] {
346         foreach srcfile $additional_sources {
347             remove-build-file "[file rootname [file tail $srcfile]].o"
348             remove-build-file "[file rootname [file tail $srcfile]].rpo"
349         }
350     }
351 }
352
353 # Remove compiler-generated RTL dump files for the current test.
354 #
355 # SUFFIX is the filename suffix pattern.
356 proc cleanup-rtl-dump { suffix } {
357   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]r.$suffix"
358 }
359
360 # Remove a specific tree dump file for the current test.
361 #
362 # SUFFIX is the tree dump file suffix pattern.
363 proc cleanup-tree-dump { suffix } {
364   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]t.$suffix"
365 }
366
367 # Remove a specific ipa dump file for the current test.
368 #
369 # SUFFIX is the ipa dump file suffix pattern.
370 proc cleanup-ipa-dump { suffix } {
371   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]i.$suffix"
372 }
373
374 # Remove all dump files with the provided suffix.
375 proc cleanup-dump { suffix } {
376     # This assumes that we are three frames down from dg-test or some other
377     # proc that stores the filename of the testcase in a local variable
378     # "name".  A cleaner solution would require a new DejaGnu release.
379     upvar 3 name testcase
380     # The name might include a list of options; extract the file name.
381     set src [file tail [lindex $testcase 0]]
382     remove-build-file "[file tail $src].$suffix"
383
384     # Clean up dump files for additional source files.
385     if [info exists additional_sources] {
386         foreach srcfile $additional_sources {
387             remove-build-file "[file tail $srcfile].$suffix"
388         }
389     }
390 }
391
392 # Remove files kept by --save-temps for the current test.
393 #
394 # Currently this is only .i files, but more can be added if there are
395 # tests generating them.
396 proc cleanup-saved-temps { } {
397     global additional_sources
398
399     # This assumes that we are two frames down from dg-test or some other proc
400     # that stores the filename of the testcase in a local variable "name".
401     # A cleaner solution would require a new DejaGnu release.
402     upvar 2 name testcase
403     remove-build-file "[file rootname [file tail $testcase]].ii"
404     remove-build-file "[file rootname [file tail $testcase]].i"
405
406     # Clean up saved temp files for additional source files.
407     if [info exists additional_sources] {
408         foreach srcfile $additional_sources {
409             remove-build-file "[file rootname [file tail $srcfile]].ii"
410             remove-build-file "[file rootname [file tail $srcfile]].i"
411         }
412     }
413 }
414
415 # Remove files for specified Fortran modules.
416 proc cleanup-modules { modlist } {
417     foreach modname $modlist {
418         remove-build-file [string tolower $modname].mod
419     }
420 }
421
422 # We need to make sure that additional_* are cleared out after every
423 # test.  It is not enough to clear them out *before* the next test run
424 # because gcc-target-compile gets run directly from some .exp files
425 # (outside of any test).  (Those uses should eventually be eliminated.)
426
427 # Because the DG framework doesn't provide a hook that is run at the
428 # end of a test, we must replace dg-test with a wrapper.
429
430 if { [info procs saved-dg-test] == [list] } {
431     rename dg-test saved-dg-test
432
433     proc dg-test { args } {
434         global additional_files
435         global additional_sources
436         global additional_prunes
437         global errorInfo
438         global compiler_conditional_xfail_data
439
440         if { [ catch { eval saved-dg-test $args } errmsg ] } {
441             set saved_info $errorInfo
442             set additional_files ""
443             set additional_sources ""
444             set additional_prunes ""
445             if [info exists compiler_conditional_xfail_data] {
446                 unset compiler_conditional_xfail_data
447             }
448             error $errmsg $saved_info
449         }
450         set additional_files ""
451         set additional_sources ""
452         set additional_prunes ""
453         if [info exists compiler_conditional_xfail_data] {
454             unset compiler_conditional_xfail_data
455         }
456     }
457 }
458
459 set additional_prunes ""