OSDN Git Service

[toplevel]
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / gcc-dg.exp
1 #   Copyright (C) 1997, 1999, 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3 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 GCC; see the file COPYING3.  If not see
16 # <http://www.gnu.org/licenses/>.
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 timeout.exp
27 load_lib timeout-dg.exp
28 load_lib prune.exp
29 load_lib libgloss.exp
30 load_lib target-libpath.exp
31 load_lib torture-options.exp
32
33 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
34 setenv LC_ALL C
35 setenv LANG C
36
37 if [info exists TORTURE_OPTIONS] {
38     set DG_TORTURE_OPTIONS $TORTURE_OPTIONS
39 } else {
40     # It is theoretically beneficial to group all of the O2/O3 options together,
41     # as in many cases the compiler will generate identical executables for
42     # all of them--and the c-torture testsuite will skip testing identical
43     # executables multiple times.
44     # Also note that -finline-functions is explicitly included in one of the
45     # items below, even though -O3 is also specified, because some ports may
46     # choose to disable inlining functions by default, even when optimizing.
47     set DG_TORTURE_OPTIONS [list \
48         { -O0 } \
49         { -O1 } \
50         { -O2 } \
51         { -O3 -fomit-frame-pointer } \
52         { -O3 -fomit-frame-pointer -funroll-loops } \
53         { -O3 -fomit-frame-pointer -funroll-all-loops -finline-functions } \
54         { -O3 -g } \
55         { -Os } ]
56 }
57
58 if [info exists ADDITIONAL_TORTURE_OPTIONS] {
59     set DG_TORTURE_OPTIONS \
60         [concat $DG_TORTURE_OPTIONS $ADDITIONAL_TORTURE_OPTIONS]
61 }
62
63 global GCC_UNDER_TEST
64 if ![info exists GCC_UNDER_TEST] {
65     set GCC_UNDER_TEST "[find_gcc]"
66 }
67
68 global orig_environment_saved
69
70 # This file may be sourced, so don't override environment settings
71 # that have been previously setup.
72 if { $orig_environment_saved == 0 } {
73     append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
74     set_ld_library_path_env_vars
75 }
76
77 # Define gcc callbacks for dg.exp.
78
79 proc gcc-dg-test-1 { target_compile prog do_what extra_tool_flags } {
80     # Set up the compiler flags, based on what we're going to do.
81
82     set options [list]
83
84     # Tests should be able to use "dg-do repo".  However, the dg test
85     # driver checks the argument to dg-do against a list of acceptable
86     # options, and "repo" is not among them.  Therefore, we resort to
87     # this ugly approach.
88     if [string match "*-frepo*" $extra_tool_flags] then {
89         set do_what "repo"
90     }
91
92     switch $do_what {
93         "preprocess" {
94             set compile_type "preprocess"
95             set output_file "[file rootname [file tail $prog]].i"
96         }
97         "compile" {
98             set compile_type "assembly"
99             set output_file "[file rootname [file tail $prog]].s"
100         }
101         "assemble" {
102             set compile_type "object"
103             set output_file "[file rootname [file tail $prog]].o"
104         }
105         "precompile" {
106             set compile_type "precompiled_header"
107             set output_file "[file tail $prog].gch"
108         }
109         "link" {
110             set compile_type "executable"
111             set output_file "[file rootname [file tail $prog]].exe"
112             # The following line is needed for targets like the i960 where
113             # the default output file is b.out.  Sigh.
114         }
115         "repo" {
116             set compile_type "object"
117             set output_file "[file rootname [file tail $prog]].o"
118         }
119         "run" {
120             set compile_type "executable"
121             # FIXME: "./" is to cope with "." not being in $PATH.
122             # Should this be handled elsewhere?
123             # YES.
124             set output_file "./[file rootname [file tail $prog]].exe"
125             # This is the only place where we care if an executable was
126             # created or not.  If it was, dg.exp will try to run it.
127             catch { remote_file build delete $output_file }
128         }
129         default {
130             perror "$do_what: not a valid dg-do keyword"
131             return ""
132         }
133     }
134
135     if { $extra_tool_flags != "" } {
136         lappend options "additional_flags=$extra_tool_flags"
137     }
138
139     set comp_output [$target_compile "$prog" "$output_file" "$compile_type" $options]
140
141     # Look for an internal compiler error, which sometimes masks the fact
142     # that we didn't get an expected error message.  XFAIL an ICE via
143     # dg-xfail-if and use { dg-prune-output ".*internal compiler error.*" }
144     # to avoid a second failure for excess errors.
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*" $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     # Some callers set torture options themselves; don't override those.
242     set existing_torture_options [torture-options-exist]
243     if { $existing_torture_options == 0 } {
244         global DG_TORTURE_OPTIONS
245         torture-init
246         set-torture-options $DG_TORTURE_OPTIONS
247     }
248     dump-torture-options
249
250     foreach test $testcases {
251         global torture_with_loops torture_without_loops
252         # If we're only testing specific files and this isn't one of
253         # them, skip it.
254         if ![runtest_file_p $runtests $test] {
255             continue
256         }
257
258         # Look for a loop within the source code - if we don't find one,
259         # don't pass -funroll[-all]-loops.
260         if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
261             set option_list $torture_with_loops
262         } else {
263             set option_list $torture_without_loops
264         }
265
266         set nshort [file tail [file dirname $test]]/[file tail $test]
267
268         foreach flags $option_list {
269             verbose "Testing $nshort, $flags" 1
270             dg-test $test $flags ${default-extra-flags}
271         }
272     }
273
274     if { $existing_torture_options == 0 } {
275         torture-finish
276     }
277 }
278
279 proc gcc-dg-debug-runtest { target_compile trivial opt_opts testcases } {
280     global srcdir subdir
281
282     if ![info exists DEBUG_TORTURE_OPTIONS] {
283         set DEBUG_TORTURE_OPTIONS ""
284         foreach type {-gdwarf-2 -gstabs -gstabs+ -gxcoff -gxcoff+ -gcoff} {
285             set comp_output [$target_compile \
286                     "$srcdir/$subdir/$trivial" "trivial.S" assembly \
287                     "additional_flags=$type"]
288             if { ! [string match "*: target system does not support the * debug format*" \
289                     $comp_output] } {
290                 remove-build-file "trivial.S"
291                 foreach level {1 "" 3} {
292                     if { ($type == "-gdwarf-2") && ($level != "") } {
293                         lappend DEBUG_TORTURE_OPTIONS [list "${type}" "-g${level}"]
294                         foreach opt $opt_opts {
295                             lappend DEBUG_TORTURE_OPTIONS \
296                                     [list "${type}" "-g${level}" "$opt" ]
297                         }
298                     } else {
299                         lappend DEBUG_TORTURE_OPTIONS [list "${type}${level}"]
300                         foreach opt $opt_opts {
301                             lappend DEBUG_TORTURE_OPTIONS \
302                                     [list "${type}${level}" "$opt" ]
303                         }
304                     }
305                 }
306             }
307         }
308     }
309
310     verbose -log "Using options $DEBUG_TORTURE_OPTIONS"
311
312     global runtests
313
314     foreach test $testcases {
315         # If we're only testing specific files and this isn't one of
316         # them, skip it.
317         if ![runtest_file_p $runtests $test] {
318             continue
319         }
320
321         set nshort [file tail [file dirname $test]]/[file tail $test]
322
323         foreach flags $DEBUG_TORTURE_OPTIONS {
324             set doit 1
325
326             # These tests check for information which may be deliberately
327             # suppressed at -g1.
328             if { ([string match {*/debug-[126].c} "$nshort"] \
329                    || [string match {*/enum-1.c} "$nshort"] \
330                    || [string match {*/enum-[12].C} "$nshort"]) \
331                     && ([string match "*1" [lindex "$flags" 0] ]
332                         || [lindex "$flags" 1] == "-g1") } {
333                 set doit 0
334             }
335
336     # High optimization can remove the variable whose existence is tested.
337     # Dwarf debugging with commentary (-dA) preserves the symbol name in the
338     # assembler output, but stabs debugging does not.
339     # http://gcc.gnu.org/ml/gcc-regression/2003-04/msg00095.html
340             if { [string match {*/debug-[12].c} "$nshort"] \
341                     && [string match "*O*" "$flags"] \
342                     && ( [string match "*coff*" "$flags"] \
343                          || [string match "*stabs*" "$flags"] ) } {
344                 set doit 0
345             }
346
347             if { $doit } {
348                 verbose -log "Testing $nshort, $flags" 1
349                 dg-test $test $flags ""
350             }
351         }
352     }
353 }
354
355 # Prune any messages matching ARGS[1] (a regexp) from test output.
356 proc dg-prune-output { args } {
357     global additional_prunes
358
359     if { [llength $args] != 2 } {
360         error "[lindex $args 1]: need one argument"
361         return
362     }
363
364     lappend additional_prunes [lindex $args 1]
365 }
366
367 # Remove files matching the pattern from the build machine.
368 proc remove-build-file { pat } {
369     verbose "remove-build-file `$pat'" 2
370     set file_list "[glob -nocomplain $pat]"
371     verbose "remove-build-file `$file_list'" 2
372     foreach output_file $file_list {
373         if [is_remote host] {
374             # Ensure the host knows the file is gone by deleting there
375             # first.
376             remote_file host delete $output_file
377         }
378         remote_file build delete $output_file
379     }
380 }
381
382 # Remove runtime-generated profile file for the current test.
383 proc cleanup-profile-file { } {
384     remove-build-file "mon.out"
385     remove-build-file "gmon.out"
386 }
387
388 # Remove compiler-generated coverage files for the current test.
389 proc cleanup-coverage-files { } {
390     # This assumes that we are two frames down from dg-test or some other proc
391     # that stores the filename of the testcase in a local variable "name".
392     # A cleaner solution would require a new DejaGnu release.
393     upvar 2 name testcase
394     remove-build-file "[file rootname [file tail $testcase]].gc??"
395
396     # Clean up coverage files for additional source files.
397     if [info exists additional_sources] {
398         foreach srcfile $additional_sources {
399             remove-build-file "[file rootname [file tail $srcfile]].gc??"
400         }
401     }
402 }
403
404 # Remove compiler-generated files from -repo for the current test.
405 proc cleanup-repo-files { } {
406     # This assumes that we are two frames down from dg-test or some other proc
407     # that stores the filename of the testcase in a local variable "name".
408     # A cleaner solution would require a new DejaGnu release.
409     upvar 2 name testcase
410     remove-build-file "[file rootname [file tail $testcase]].o"
411     remove-build-file "[file rootname [file tail $testcase]].rpo"
412
413     # Clean up files for additional source files.
414     if [info exists additional_sources] {
415         foreach srcfile $additional_sources {
416             remove-build-file "[file rootname [file tail $srcfile]].o"
417             remove-build-file "[file rootname [file tail $srcfile]].rpo"
418         }
419     }
420 }
421
422 # Remove compiler-generated RTL dump files for the current test.
423 #
424 # SUFFIX is the filename suffix pattern.
425 proc cleanup-rtl-dump { suffix } {
426   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]r.$suffix"
427 }
428
429 # Remove a specific tree dump file for the current test.
430 #
431 # SUFFIX is the tree dump file suffix pattern.
432 proc cleanup-tree-dump { suffix } {
433   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]t.$suffix"
434 }
435
436 # Remove a specific ipa dump file for the current test.
437 #
438 # SUFFIX is the ipa dump file suffix pattern.
439 proc cleanup-ipa-dump { suffix } {
440   cleanup-dump "\[0-9\]\[0-9\]\[0-9\]i.$suffix"
441 }
442
443 # Remove all dump files with the provided suffix.
444 proc cleanup-dump { suffix } {
445     # This assumes that we are three frames down from dg-test or some other
446     # proc that stores the filename of the testcase in a local variable
447     # "name".  A cleaner solution would require a new DejaGnu release.
448     upvar 3 name testcase
449     # The name might include a list of options; extract the file name.
450     set src [file tail [lindex $testcase 0]]
451     remove-build-file "[file tail $src].$suffix"
452
453     # Clean up dump files for additional source files.
454     if [info exists additional_sources] {
455         foreach srcfile $additional_sources {
456             remove-build-file "[file tail $srcfile].$suffix"
457         }
458     }
459 }
460
461 # Remove files kept by --save-temps for the current test.
462 #
463 # Currently this is only .i, .ii, .s and .o files, but more can be added
464 # if there are tests generating them.
465 # ARGS is a list of suffixes to NOT delete.
466 proc cleanup-saved-temps { args } {
467     global additional_sources
468     set suffixes {}
469
470     # add the to-be-kept suffixes
471     foreach suffix {".ii" ".i" ".s" ".o"} {
472         if {[lsearch $args $suffix] < 0} {
473             lappend suffixes $suffix
474         }
475     }
476
477     # This assumes that we are two frames down from dg-test or some other proc
478     # that stores the filename of the testcase in a local variable "name".
479     # A cleaner solution would require a new DejaGnu release.
480     upvar 2 name testcase
481     foreach suffix $suffixes {
482         remove-build-file "[file rootname [file tail $testcase]]$suffix"
483     }
484
485     # Clean up saved temp files for additional source files.
486     if [info exists additional_sources] {
487         foreach srcfile $additional_sources {
488             foreach suffix $suffixes {
489                 remove-build-file "[file rootname [file tail $srcfile]]$suffix"
490             }
491         }
492     }
493 }
494
495 # Remove files for specified Fortran modules.
496 proc cleanup-modules { modlist } {
497     foreach modname $modlist {
498         remove-build-file [string tolower $modname].mod
499     }
500 }
501
502 # Scan Fortran modules for a given regexp.
503 #
504 # Argument 0 is the module name
505 # Argument 1 is the regexp to match
506 proc scan-module { args } {
507     set modfilename [string tolower [lindex $args 0]].mod
508     set fd [open $modfilename r]
509     set text [read $fd]
510     close $fd
511
512     upvar 2 name testcase
513     if [regexp -- [lindex $args 1] $text] {
514       pass "$testcase scan-module [lindex $args 1]"
515     } else {
516       fail "$testcase scan-module [lindex $args 1]"
517     }
518 }
519
520 # Verify that the compiler output file exists, invoked via dg-final.
521 proc output-exists { args } {
522     # Process an optional target or xfail list.
523     if { [llength $args] >= 1 } {
524         switch [dg-process-target [lindex $args 0]] {
525             "S" { }
526             "N" { return }
527             "F" { setup_xfail "*-*-*" }
528             "P" { }
529         }
530     }
531
532     # Access variables from gcc-dg-test-1.
533     upvar 2 name testcase
534     upvar 2 output_file output_file
535
536     if [file exists $output_file] {
537         pass "$testcase output-exists $output_file"
538     } else {
539         fail "$testcase output-exists $output_file"
540     }
541 }
542
543 # Verify that the compiler output file does not exist, invoked via dg-final.
544 proc output-exists-not { args } {
545     # Process an optional target or xfail list.
546     if { [llength $args] >= 1 } {
547         switch [dg-process-target [lindex $args 0]] {
548             "S" { }
549             "N" { return }
550             "F" { setup_xfail "*-*-*" }
551             "P" { }
552         }
553     }
554
555     # Access variables from gcc-dg-test-1.
556     upvar 2 name testcase
557     upvar 2 output_file output_file
558
559     if [file exists $output_file] {
560         fail "$testcase output-exists-not $output_file"
561     } else {
562         pass "$testcase output-exists-not $output_file"
563     }
564 }
565
566 # We need to make sure that additional_* are cleared out after every
567 # test.  It is not enough to clear them out *before* the next test run
568 # because gcc-target-compile gets run directly from some .exp files
569 # (outside of any test).  (Those uses should eventually be eliminated.)
570
571 # Because the DG framework doesn't provide a hook that is run at the
572 # end of a test, we must replace dg-test with a wrapper.
573
574 if { [info procs saved-dg-test] == [list] } {
575     rename dg-test saved-dg-test
576
577     proc dg-test { args } {
578         global additional_files
579         global additional_sources
580         global additional_prunes
581         global errorInfo
582         global compiler_conditional_xfail_data
583         global shouldfail
584
585         if { [ catch { eval saved-dg-test $args } errmsg ] } {
586             set saved_info $errorInfo
587             set additional_files ""
588             set additional_sources ""
589             set additional_prunes ""
590             set shouldfail 0
591             if [info exists compiler_conditional_xfail_data] {
592                 unset compiler_conditional_xfail_data
593             }
594             unset_timeout_vars
595             error $errmsg $saved_info
596         }
597         set additional_files ""
598         set additional_sources ""
599         set additional_prunes ""
600         set shouldfail 0
601         unset_timeout_vars
602         if [info exists compiler_conditional_xfail_data] {
603             unset compiler_conditional_xfail_data
604         }
605     }
606 }
607
608 if { [info procs saved-dg-warning] == [list] \
609      && [info exists gcc_warning_prefix] } {
610     rename dg-warning saved-dg-warning
611
612     proc dg-warning { args } {
613         # Make this variable available here and to the saved proc.
614         upvar dg-messages dg-messages
615         global gcc_warning_prefix
616
617         process-message saved-dg-warning "$gcc_warning_prefix" "$args"
618     }
619 }
620
621 if { [info procs saved-dg-error] == [list] \
622      && [info exists gcc_error_prefix] } {
623     rename dg-error saved-dg-error
624
625     proc dg-error { args } {
626         # Make this variable available here and to the saved proc.
627         upvar dg-messages dg-messages
628         global gcc_error_prefix
629
630         process-message saved-dg-error "$gcc_error_prefix" "$args"
631     }
632
633     # Override dg-bogus at the same time.  It doesn't handle a prefix
634     # but its expression should include a column number.  Otherwise the
635     # line number can match the column number for other messages, leading
636     # to insanity.
637     rename dg-bogus saved-dg-bogus
638
639     proc dg-bogus { args } {
640         upvar dg-messages dg-messages
641         process-message saved-dg-bogus "" $args
642     }
643 }
644
645 # Modify the regular expression saved by a DejaGnu message directive to
646 # include a prefix and to force the expression to match a single line.
647 # MSGPROC is the procedure to call.
648 # MSGPREFIX is the prefix to prepend.
649 # DGARGS is the original argument list.
650
651 proc process-message { msgproc msgprefix dgargs } {
652     upvar dg-messages dg-messages
653
654     # Process the dg- directive, including adding the regular expression
655     # to the new message entry in dg-messages.
656     set msgcnt [llength ${dg-messages}]
657     catch { eval $msgproc $dgargs }
658
659     # If the target expression wasn't satisfied there is no new message.
660     if { [llength ${dg-messages}] == $msgcnt } {
661         return;
662     }
663
664     # Get the entry for the new message.  Prepend the message prefix to
665     # the regular expression and make it match a single line.
666     set newentry [lindex ${dg-messages} end]
667     set expmsg [lindex $newentry 2]
668
669     # Handle column numbers from the specified expression (if there is
670     # one) and set up the search expression that will be used by DejaGnu.
671     if [regexp "^(\[0-9\]+):" $expmsg "" column] {
672         # The expression in the directive included a column number.
673         # Remove "COLUMN:" from the original expression and move it
674         # to the proper place in the search expression.
675         regsub "^\[0-9\]+:" $expmsg "" expmsg
676         set expmsg "$column: $msgprefix\[^\n\]*$expmsg"
677     } elseif [string match "" [lindex $newentry 0]] {
678         # The specified line number is 0; don't expect a column number.
679         set expmsg "$msgprefix\[^\n\]*$expmsg"
680     } else {
681         # There is no column number in the search expression, but we
682         # should expect one in the message itself.
683         set expmsg "\[0-9\]+: $msgprefix\[^\n\]*$expmsg"
684     }
685
686     set newentry [lreplace $newentry 2 2 $expmsg]
687     set dg-messages [lreplace ${dg-messages} end end $newentry]
688     verbose "process-message:\n${dg-messages}" 2
689 }
690
691 # Look for messages that don't have standard prefixes.
692
693 proc dg-message { args } {
694     upvar dg-messages dg-messages
695     process-message saved-dg-warning "" $args
696 }
697
698 set additional_prunes ""