OSDN Git Service

gcc/
[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     # Likewise, if we see ".text exceeds local store range" or
187     # similar.
188     if {[string match "spu-*" $system] && \
189             [string match "*exceeds local store range*" $text]} {
190         # The format here is important.  See dg.exp.
191         return "::unsupported::memory full"
192     }
193
194     return $text
195 }
196
197 # Replace ${tool}_load with a wrapper to provide for an expected nonzero
198 # exit status.  Multiple languages include this file so this handles them
199 # all, not just gcc.
200 if { [info procs ${tool}_load] != [list] \
201       && [info procs saved_${tool}_load] == [list] } {
202     rename ${tool}_load saved_${tool}_load
203
204     proc ${tool}_load { program args } {
205         global tool
206         global shouldfail
207         set result [eval [list saved_${tool}_load $program] $args]
208         if { $shouldfail != 0 } {
209             switch [lindex $result 0] {
210                 "pass" { set status "fail" }
211                 "fail" { set status "pass" }
212             }
213             set result [list $status [lindex $result 1]]
214         }
215         return $result
216     }
217 }
218
219 # Utility routines.
220
221 #
222 # search_for -- looks for a string match in a file
223 #
224 proc search_for { file pattern } {
225     set fd [open $file r]
226     while { [gets $fd cur_line]>=0 } {
227         if [string match "*$pattern*" $cur_line] then {
228             close $fd
229             return 1
230         }
231     }
232     close $fd
233     return 0
234 }
235
236 # Modified dg-runtest that can cycle through a list of optimization options
237 # as c-torture does.
238 proc gcc-dg-runtest { testcases default-extra-flags } {
239     global runtests
240
241     foreach test $testcases {
242         # If we're only testing specific files and this isn't one of
243         # them, skip it.
244         if ![runtest_file_p $runtests $test] {
245             continue
246         }
247
248         # Look for a loop within the source code - if we don't find one,
249         # don't pass -funroll[-all]-loops.
250         global torture_with_loops torture_without_loops
251         if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
252             set option_list $torture_with_loops
253         } else {
254             set option_list $torture_without_loops
255         }
256
257         set nshort [file tail [file dirname $test]]/[file tail $test]
258
259         foreach flags $option_list {
260             verbose "Testing $nshort, $flags" 1
261             dg-test $test $flags ${default-extra-flags}
262         }
263     }
264 }
265
266 proc gcc-dg-debug-runtest { target_compile trivial opt_opts testcases } {
267     global srcdir subdir
268
269     if ![info exists DEBUG_TORTURE_OPTIONS] {
270         set DEBUG_TORTURE_OPTIONS ""
271         foreach type {-gdwarf-2 -gstabs -gstabs+ -gxcoff -gxcoff+ -gcoff} {
272             set comp_output [$target_compile \
273                     "$srcdir/$subdir/$trivial" "trivial.S" assembly \
274                     "additional_flags=$type"]
275             if { ! [string match "*: target system does not support the * debug format*" \
276                     $comp_output] } {
277                 remove-build-file "trivial.S"
278                 foreach level {1 "" 3} {
279                     lappend DEBUG_TORTURE_OPTIONS [list "${type}${level}"]
280                     foreach opt $opt_opts {
281                         lappend DEBUG_TORTURE_OPTIONS \
282                                 [list "${type}${level}" "$opt" ]
283                     }
284                 }
285             }
286         }
287     }
288
289     verbose -log "Using options $DEBUG_TORTURE_OPTIONS"
290
291     global runtests
292
293     foreach test $testcases {
294         # If we're only testing specific files and this isn't one of
295         # them, skip it.
296         if ![runtest_file_p $runtests $test] {
297             continue
298         }
299
300         set nshort [file tail [file dirname $test]]/[file tail $test]
301
302         foreach flags $DEBUG_TORTURE_OPTIONS {
303             set doit 1
304
305             # These tests check for information which may be deliberately
306             # suppressed at -g1.
307             if { ([string match {*/debug-[126].c} "$nshort"] \
308                    || [string match {*/enum-1.c} "$nshort"] \
309                    || [string match {*/enum-[12].C} "$nshort"]) \
310                     && [string match "*1" [lindex "$flags" 0] ] } {
311                 set doit 0
312             }
313
314     # High optimization can remove the variable whose existence is tested.
315     # Dwarf debugging with commentary (-dA) preserves the symbol name in the
316     # assembler output, but stabs debugging does not.
317     # http://gcc.gnu.org/ml/gcc-regression/2003-04/msg00095.html
318             if { [string match {*/debug-[12].c} "$nshort"] \
319                     && [string match "*O*" "$flags"] \
320                     && ( [string match "*coff*" "$flags"] \
321                          || [string match "*stabs*" "$flags"] ) } {
322                 set doit 0
323             }
324
325             if { $doit } {
326                 verbose -log "Testing $nshort, $flags" 1
327                 dg-test $test $flags ""
328             }
329         }
330     }
331 }
332
333 # Prune any messages matching ARGS[1] (a regexp) from test output.
334 proc dg-prune-output { args } {
335     global additional_prunes
336
337     if { [llength $args] != 2 } {
338         error "[lindex $args 1]: need one argument"
339         return
340     }
341
342     lappend additional_prunes [lindex $args 1]
343 }
344
345 # Remove files matching the pattern from the build machine.
346 proc remove-build-file { pat } {
347     verbose "remove-build-file `$pat'" 2
348     set file_list "[glob -nocomplain $pat]"
349     verbose "remove-build-file `$file_list'" 2
350     foreach output_file $file_list {
351         remote_file build delete $output_file
352     }
353 }
354
355 # Remove compiler-generated coverage files for the current test.
356 proc cleanup-coverage-files { } {
357     # This assumes that we are two frames down from dg-test or some other proc
358     # that stores the filename of the testcase in a local variable "name".
359     # A cleaner solution would require a new DejaGnu release.
360     upvar 2 name testcase
361     remove-build-file "[file rootname [file tail $testcase]].gc??"
362
363     # Clean up coverage files for additional source files.
364     if [info exists additional_sources] {
365         foreach srcfile $additional_sources {
366             remove-build-file "[file rootname [file tail $srcfile]].gc??"
367         }
368     }
369 }
370
371 # Remove compiler-generated files from -repo for the current test.
372 proc cleanup-repo-files { } {
373     # This assumes that we are two frames down from dg-test or some other proc
374     # that stores the filename of the testcase in a local variable "name".
375     # A cleaner solution would require a new DejaGnu release.
376     upvar 2 name testcase
377     remove-build-file "[file rootname [file tail $testcase]].o"
378     remove-build-file "[file rootname [file tail $testcase]].rpo"
379
380     # Clean up files for additional source files.
381     if [info exists additional_sources] {
382         foreach srcfile $additional_sources {
383             remove-build-file "[file rootname [file tail $srcfile]].o"
384             remove-build-file "[file rootname [file tail $srcfile]].rpo"
385         }
386     }
387 }
388
389 # Remove compiler-generated RTL dump files for the current test.
390 #
391 # SUFFIX is the filename suffix pattern.
392 proc cleanup-rtl-dump { suffix } {
393   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]r.$suffix"
394 }
395
396 # Remove a specific tree dump file for the current test.
397 #
398 # SUFFIX is the tree dump file suffix pattern.
399 proc cleanup-tree-dump { suffix } {
400   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]t.$suffix"
401 }
402
403 # Remove a specific ipa dump file for the current test.
404 #
405 # SUFFIX is the ipa dump file suffix pattern.
406 proc cleanup-ipa-dump { suffix } {
407   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]i.$suffix"
408 }
409
410 # Remove all dump files with the provided suffix.
411 proc cleanup-dump { suffix } {
412     # This assumes that we are three frames down from dg-test or some other
413     # proc that stores the filename of the testcase in a local variable
414     # "name".  A cleaner solution would require a new DejaGnu release.
415     upvar 3 name testcase
416     # The name might include a list of options; extract the file name.
417     set src [file tail [lindex $testcase 0]]
418     remove-build-file "[file tail $src].$suffix"
419
420     # Clean up dump files for additional source files.
421     if [info exists additional_sources] {
422         foreach srcfile $additional_sources {
423             remove-build-file "[file tail $srcfile].$suffix"
424         }
425     }
426 }
427
428 # Remove files kept by --save-temps for the current test.
429 #
430 # Currently this is only .i files, but more can be added if there are
431 # tests generating them.
432 proc cleanup-saved-temps { } {
433     global additional_sources
434
435     # This assumes that we are two frames down from dg-test or some other proc
436     # that stores the filename of the testcase in a local variable "name".
437     # A cleaner solution would require a new DejaGnu release.
438     upvar 2 name testcase
439     remove-build-file "[file rootname [file tail $testcase]].ii"
440     remove-build-file "[file rootname [file tail $testcase]].i"
441
442     # Clean up saved temp files for additional source files.
443     if [info exists additional_sources] {
444         foreach srcfile $additional_sources {
445             remove-build-file "[file rootname [file tail $srcfile]].ii"
446             remove-build-file "[file rootname [file tail $srcfile]].i"
447         }
448     }
449 }
450
451 # Remove files for specified Fortran modules.
452 proc cleanup-modules { modlist } {
453     foreach modname $modlist {
454         remove-build-file [string tolower $modname].mod
455     }
456 }
457
458 # Scan Fortran modules for a given regexp.
459 #
460 # Argument 0 is the module name
461 # Argument 1 is the regexp to match
462 proc scan-module { args } {
463     set modfilename [string tolower [lindex $args 0]].mod
464     set fd [open $modfilename r]
465     set text [read $fd]
466     close $fd
467
468     upvar 2 name testcase
469     if [regexp -- [lindex $args 1] $text] {
470       pass "$testcase scan-module [lindex $args 1]"
471     } else {
472       fail "$testcase scan-module [lindex $args 1]"
473     }
474 }
475
476 # Verify that the compiler output file exists, invoked via dg-final.
477 proc output-exists { args } {
478     # Process an optional target or xfail list.
479     if { [llength $args] >= 1 } {
480         switch [dg-process-target [lindex $args 0]] {
481             "S" { }
482             "N" { return }
483             "F" { setup_xfail "*-*-*" }
484             "P" { }
485         }
486     }
487
488     # Access variables from gcc-dg-test-1.
489     upvar 2 name testcase
490     upvar 2 output_file output_file
491
492     if [file exists $output_file] {
493         pass "$testcase output-exists $output_file"
494     } else {
495         fail "$testcase output-exists $output_file"
496     }
497 }
498
499 # Verify that the compiler output file does not exist, invoked via dg-final.
500 proc output-exists-not { args } {
501     # Process an optional target or xfail list.
502     if { [llength $args] >= 1 } {
503         switch [dg-process-target [lindex $args 0]] {
504             "S" { }
505             "N" { return }
506             "F" { setup_xfail "*-*-*" }
507             "P" { }
508         }
509     }
510
511     # Access variables from gcc-dg-test-1.
512     upvar 2 name testcase
513     upvar 2 output_file output_file
514
515     if [file exists $output_file] {
516         fail "$testcase output-exists-not $output_file"
517     } else {
518         pass "$testcase output-exists-not $output_file"
519     }
520 }
521
522 # We need to make sure that additional_* are cleared out after every
523 # test.  It is not enough to clear them out *before* the next test run
524 # because gcc-target-compile gets run directly from some .exp files
525 # (outside of any test).  (Those uses should eventually be eliminated.)
526
527 # Because the DG framework doesn't provide a hook that is run at the
528 # end of a test, we must replace dg-test with a wrapper.
529
530 if { [info procs saved-dg-test] == [list] } {
531     rename dg-test saved-dg-test
532
533     proc dg-test { args } {
534         global additional_files
535         global additional_sources
536         global additional_prunes
537         global errorInfo
538         global compiler_conditional_xfail_data
539         global shouldfail
540
541         if { [ catch { eval saved-dg-test $args } errmsg ] } {
542             set saved_info $errorInfo
543             set additional_files ""
544             set additional_sources ""
545             set additional_prunes ""
546             set shouldfail 0
547             if [info exists compiler_conditional_xfail_data] {
548                 unset compiler_conditional_xfail_data
549             }
550             error $errmsg $saved_info
551         }
552         set additional_files ""
553         set additional_sources ""
554         set additional_prunes ""
555         set shouldfail 0
556         if [info exists compiler_conditional_xfail_data] {
557             unset compiler_conditional_xfail_data
558         }
559     }
560 }
561
562 # Modify the regular expression saved by a DejaGnu message directive to
563 # include a prefix and to force the expression to match a single line.
564 # MSGPROC is the procedure to call.
565 # MSGPREFIX is the prefix to prepend.
566 # DGARGS is the original argument list.
567
568 proc process-message { msgproc msgprefix dgargs } {
569     upvar dg-messages dg-messages
570
571     # Process the dg- directive, including adding the regular expression
572     # to the new message entry in dg-messages.
573     set msgcnt [llength ${dg-messages}]
574     catch { eval $msgproc $dgargs }
575
576     # If the target expression wasn't satisfied there is no new message.
577     if { [llength ${dg-messages}] == $msgcnt } {
578         return;
579     }
580
581     # Prepend the message prefix to the regular expression and make
582     # it match a single line.
583     set newentry [lindex ${dg-messages} end]
584     set expmsg [lindex $newentry 2]
585     set expmsg "$msgprefix\[^\n]*$expmsg"
586     set newentry [lreplace $newentry 2 2 $expmsg]
587     set dg-messages [lreplace ${dg-messages} end end $newentry]
588     verbose "process-message:\n${dg-messages}" 2
589 }
590
591 # Look for messages that don't have standard prefixes.
592
593 proc dg-message { args } {
594     upvar dg-messages dg-messages
595     process-message dg-warning "" $args
596 }
597
598 set additional_prunes ""