1 # Copyright (C) 1997, 1999, 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
2 # 2010 Free Software Foundation, Inc.
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.
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.
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/>.
19 load_lib file-format.exp
20 load_lib target-supports.exp
21 load_lib target-supports-dg.exp
27 load_lib timeout-dg.exp
30 load_lib target-libpath.exp
31 load_lib torture-options.exp
33 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
37 # Many hosts now default to a non-ASCII C locale, however, so
38 # they can set a charset encoding here if they need.
39 if { [ishost "*-*-cygwin*"] } {
44 if [info exists TORTURE_OPTIONS] {
45 set DG_TORTURE_OPTIONS $TORTURE_OPTIONS
47 # It is theoretically beneficial to group all of the O2/O3 options together,
48 # as in many cases the compiler will generate identical executables for
49 # all of them--and the c-torture testsuite will skip testing identical
50 # executables multiple times.
51 # Also note that -finline-functions is explicitly included in one of the
52 # items below, even though -O3 is also specified, because some ports may
53 # choose to disable inlining functions by default, even when optimizing.
54 set DG_TORTURE_OPTIONS [list \
58 { -O3 -fomit-frame-pointer } \
59 { -O3 -fomit-frame-pointer -funroll-loops } \
60 { -O3 -fomit-frame-pointer -funroll-all-loops -finline-functions } \
65 if [info exists ADDITIONAL_TORTURE_OPTIONS] {
66 set DG_TORTURE_OPTIONS \
67 [concat $DG_TORTURE_OPTIONS $ADDITIONAL_TORTURE_OPTIONS]
70 set LTO_TORTURE_OPTIONS ""
71 if [check_effective_target_lto] {
72 set LTO_TORTURE_OPTIONS [list \
73 { -O2 -flto -flto-partition=none } \
80 if ![info exists GCC_UNDER_TEST] {
81 set GCC_UNDER_TEST "[find_gcc]"
84 global orig_environment_saved
86 # This file may be sourced, so don't override environment settings
87 # that have been previously setup.
88 if { $orig_environment_saved == 0 } {
89 append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
90 set_ld_library_path_env_vars
93 # Define gcc callbacks for dg.exp.
95 proc gcc-dg-test-1 { target_compile prog do_what extra_tool_flags } {
96 # Set up the compiler flags, based on what we're going to do.
100 # Tests should be able to use "dg-do repo". However, the dg test
101 # driver checks the argument to dg-do against a list of acceptable
102 # options, and "repo" is not among them. Therefore, we resort to
103 # this ugly approach.
104 if [string match "*-frepo*" $extra_tool_flags] then {
110 set compile_type "preprocess"
111 set output_file "[file rootname [file tail $prog]].i"
114 set compile_type "assembly"
115 set output_file "[file rootname [file tail $prog]].s"
118 set compile_type "object"
119 set output_file "[file rootname [file tail $prog]].o"
122 set compile_type "precompiled_header"
123 set output_file "[file tail $prog].gch"
126 set compile_type "executable"
127 set output_file "[file rootname [file tail $prog]].exe"
128 # The following line is needed for targets like the i960 where
129 # the default output file is b.out. Sigh.
132 set compile_type "object"
133 set output_file "[file rootname [file tail $prog]].o"
136 set compile_type "executable"
137 # FIXME: "./" is to cope with "." not being in $PATH.
138 # Should this be handled elsewhere?
140 set output_file "./[file rootname [file tail $prog]].exe"
141 # This is the only place where we care if an executable was
142 # created or not. If it was, dg.exp will try to run it.
143 catch { remote_file build delete $output_file }
146 perror "$do_what: not a valid dg-do keyword"
151 if { $extra_tool_flags != "" } {
152 lappend options "additional_flags=$extra_tool_flags"
155 set comp_output [$target_compile "$prog" "$output_file" "$compile_type" $options]
157 # Look for an internal compiler error, which sometimes masks the fact
158 # that we didn't get an expected error message. XFAIL an ICE via
159 # dg-xfail-if and use { dg-prune-output ".*internal compiler error.*" }
160 # to avoid a second failure for excess errors.
161 if [string match "*internal compiler error*" $comp_output] {
163 fail "$name (internal compiler error)"
166 if { $do_what == "repo" } {
167 set object_file "$output_file"
168 set output_file "[file rootname [file tail $prog]].exe"
170 [ concat $comp_output \
171 [$target_compile "$object_file" "$output_file" \
172 "executable" $options] ]
175 return [list $comp_output $output_file]
178 proc gcc-dg-test { prog do_what extra_tool_flags } {
179 return [gcc-dg-test-1 gcc_target_compile $prog $do_what $extra_tool_flags]
182 proc gcc-dg-prune { system text } {
183 global additional_prunes
185 set text [prune_gcc_output $text]
187 foreach p $additional_prunes {
188 if { [string length $p] > 0 } {
189 # Following regexp matches a complete line containing $p.
190 regsub -all "(^|\n)\[^\n\]*$p\[^\n\]*" $text "" text
194 # If we see "region xxx is full" then the testcase is too big for ram.
195 # This is tricky to deal with in a large testsuite like c-torture so
196 # deal with it here. Just mark the testcase as unsupported.
197 if [regexp "(^|\n)\[^\n\]*: region \[^\n\]* is full" $text] {
198 # The format here is important. See dg.exp.
199 return "::unsupported::memory full"
202 # Likewise, if we see ".text exceeds local store range" or
204 if {[string match "spu-*" $system] && \
205 [string match "*exceeds local store*" $text]} {
206 # The format here is important. See dg.exp.
207 return "::unsupported::memory full"
213 # Replace ${tool}_load with a wrapper to provide for an expected nonzero
214 # exit status. Multiple languages include this file so this handles them
216 if { [info procs ${tool}_load] != [list] \
217 && [info procs saved_${tool}_load] == [list] } {
218 rename ${tool}_load saved_${tool}_load
220 proc ${tool}_load { program args } {
223 set result [eval [list saved_${tool}_load $program] $args]
224 if { $shouldfail != 0 } {
225 switch [lindex $result 0] {
226 "pass" { set status "fail" }
227 "fail" { set status "pass" }
229 set result [list $status [lindex $result 1]]
238 # search_for -- looks for a string match in a file
240 proc search_for { file pattern } {
241 set fd [open $file r]
242 while { [gets $fd cur_line]>=0 } {
243 if [string match "*$pattern*" $cur_line] then {
252 # Modified dg-runtest that can cycle through a list of optimization options
254 proc gcc-dg-runtest { testcases default-extra-flags } {
257 # Some callers set torture options themselves; don't override those.
258 set existing_torture_options [torture-options-exist]
259 if { $existing_torture_options == 0 } {
260 global DG_TORTURE_OPTIONS LTO_TORTURE_OPTIONS
262 set-torture-options $DG_TORTURE_OPTIONS [list {}] $LTO_TORTURE_OPTIONS
266 foreach test $testcases {
267 global torture_with_loops torture_without_loops
268 # If we're only testing specific files and this isn't one of
270 if ![runtest_file_p $runtests $test] {
274 # Look for a loop within the source code - if we don't find one,
275 # don't pass -funroll[-all]-loops.
276 if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
277 set option_list $torture_with_loops
279 set option_list $torture_without_loops
282 set nshort [file tail [file dirname $test]]/[file tail $test]
284 foreach flags $option_list {
285 verbose "Testing $nshort, $flags" 1
286 dg-test $test $flags ${default-extra-flags}
290 if { $existing_torture_options == 0 } {
295 proc gcc-dg-debug-runtest { target_compile trivial opt_opts testcases } {
298 if ![info exists DEBUG_TORTURE_OPTIONS] {
299 set DEBUG_TORTURE_OPTIONS ""
300 foreach type {-gdwarf-2 -gstabs -gstabs+ -gxcoff -gxcoff+ -gcoff} {
301 set comp_output [$target_compile \
302 "$srcdir/$subdir/$trivial" "trivial.S" assembly \
303 "additional_flags=$type"]
304 if { ! [string match "*: target system does not support the * debug format*" \
306 remove-build-file "trivial.S"
307 foreach level {1 "" 3} {
308 if { ($type == "-gdwarf-2") && ($level != "") } {
309 lappend DEBUG_TORTURE_OPTIONS [list "${type}" "-g${level}"]
310 foreach opt $opt_opts {
311 lappend DEBUG_TORTURE_OPTIONS \
312 [list "${type}" "-g${level}" "$opt" ]
315 lappend DEBUG_TORTURE_OPTIONS [list "${type}${level}"]
316 foreach opt $opt_opts {
317 lappend DEBUG_TORTURE_OPTIONS \
318 [list "${type}${level}" "$opt" ]
326 verbose -log "Using options $DEBUG_TORTURE_OPTIONS"
330 foreach test $testcases {
331 # If we're only testing specific files and this isn't one of
333 if ![runtest_file_p $runtests $test] {
337 set nshort [file tail [file dirname $test]]/[file tail $test]
339 foreach flags $DEBUG_TORTURE_OPTIONS {
342 # These tests check for information which may be deliberately
344 if { ([string match {*/debug-[126].c} "$nshort"] \
345 || [string match {*/enum-1.c} "$nshort"] \
346 || [string match {*/enum-[12].C} "$nshort"]) \
347 && ([string match "*1" [lindex "$flags" 0] ]
348 || [lindex "$flags" 1] == "-g1") } {
352 # High optimization can remove the variable whose existence is tested.
353 # Dwarf debugging with commentary (-dA) preserves the symbol name in the
354 # assembler output, but stabs debugging does not.
355 # http://gcc.gnu.org/ml/gcc-regression/2003-04/msg00095.html
356 if { [string match {*/debug-[12].c} "$nshort"] \
357 && [string match "*O*" "$flags"] \
358 && ( [string match "*coff*" "$flags"] \
359 || [string match "*stabs*" "$flags"] ) } {
364 verbose -log "Testing $nshort, $flags" 1
365 dg-test $test $flags ""
371 # Prune any messages matching ARGS[1] (a regexp) from test output.
372 proc dg-prune-output { args } {
373 global additional_prunes
375 if { [llength $args] != 2 } {
376 error "[lindex $args 1]: need one argument"
380 lappend additional_prunes [lindex $args 1]
383 # Remove files matching the pattern from the build machine.
384 proc remove-build-file { pat } {
385 verbose "remove-build-file `$pat'" 2
386 set file_list "[glob -nocomplain $pat]"
387 verbose "remove-build-file `$file_list'" 2
388 foreach output_file $file_list {
389 if [is_remote host] {
390 # Ensure the host knows the file is gone by deleting there
392 remote_file host delete $output_file
394 remote_file build delete $output_file
398 # Remove runtime-generated profile file for the current test.
399 proc cleanup-profile-file { } {
400 remove-build-file "mon.out"
401 remove-build-file "gmon.out"
404 # Remove compiler-generated coverage files for the current test.
405 proc cleanup-coverage-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 # The name might include a list of options; extract the file name.
411 set testcase [lindex $testcase 0]
412 remove-build-file "[file rootname [file tail $testcase]].gc??"
414 # Clean up coverage files for additional source files.
415 if [info exists additional_sources] {
416 foreach srcfile $additional_sources {
417 remove-build-file "[file rootname [file tail $srcfile]].gc??"
422 # Remove compiler-generated files from -repo for the current test.
423 proc cleanup-repo-files { } {
424 # This assumes that we are two frames down from dg-test or some other proc
425 # that stores the filename of the testcase in a local variable "name".
426 # A cleaner solution would require a new DejaGnu release.
427 upvar 2 name testcase
428 # The name might include a list of options; extract the file name.
429 set testcase [lindex $testcase 0]
430 remove-build-file "[file rootname [file tail $testcase]].o"
431 remove-build-file "[file rootname [file tail $testcase]].rpo"
433 # Clean up files for additional source files.
434 if [info exists additional_sources] {
435 foreach srcfile $additional_sources {
436 remove-build-file "[file rootname [file tail $srcfile]].o"
437 remove-build-file "[file rootname [file tail $srcfile]].rpo"
442 # Remove compiler-generated RTL dump files for the current test.
444 # SUFFIX is the filename suffix pattern.
445 proc cleanup-rtl-dump { suffix } {
446 cleanup-dump "\[0-9\]\[0-9\]\[0-9\]r.$suffix"
449 # Remove a specific tree dump file for the current test.
451 # SUFFIX is the tree dump file suffix pattern.
452 proc cleanup-tree-dump { suffix } {
453 cleanup-dump "\[0-9\]\[0-9\]\[0-9\]t.$suffix"
456 # Remove a specific ipa dump file for the current test.
458 # SUFFIX is the ipa dump file suffix pattern.
459 proc cleanup-ipa-dump { suffix } {
460 cleanup-dump "\[0-9\]\[0-9\]\[0-9\]i.$suffix"
463 # Remove a stack usage file for the current test.
464 proc cleanup-stack-usage { } {
465 # This assumes that we are two frames down from dg-test or some other proc
466 # that stores the filename of the testcase in a local variable "name".
467 # A cleaner solution would require a new DejaGnu release.
468 upvar 2 name testcase
469 # The name might include a list of options; extract the file name.
470 set testcase [lindex $testcase 0]
471 remove-build-file "[file rootname [file tail $testcase]].su"
473 # Clean up files for additional source files.
474 if [info exists additional_sources] {
475 foreach srcfile $additional_sources {
476 remove-build-file "[file rootname [file tail $srcfile]].su"
481 # Remove all dump files with the provided suffix.
482 proc cleanup-dump { suffix } {
483 # This assumes that we are three frames down from dg-test or some other
484 # proc that stores the filename of the testcase in a local variable
485 # "name". A cleaner solution would require a new DejaGnu release.
486 upvar 3 name testcase
487 # The name might include a list of options; extract the file name.
488 set src [file tail [lindex $testcase 0]]
489 remove-build-file "[file tail $src].$suffix"
490 remove-build-file "[file rootname [file tail $src]].exe.$suffix"
491 remove-build-file "[file rootname [file tail $src]].exe.ltrans\[0-9\]*.$suffix"
492 # -fcompare-debug dumps
493 remove-build-file "[file tail $src].gk.$suffix"
495 # Clean up dump files for additional source files.
496 if [info exists additional_sources] {
497 foreach srcfile $additional_sources {
498 remove-build-file "[file tail $srcfile].$suffix"
499 remove-build-file "[file rootname [file tail $srcfile]].exe.$suffix"
500 remove-build-file "[file rootname [file tail $srcfile]].exe.ltrans\[0-9\]*.$suffix"
501 # -fcompare-debug dumps
502 remove-build-file "[file tail $srcfile].gk.$suffix"
507 # Remove files kept by --save-temps for the current test.
509 # Currently this is only .i, .ii, .s and .o files, but more can be added
510 # if there are tests generating them.
511 # ARGS is a list of suffixes to NOT delete.
512 proc cleanup-saved-temps { args } {
513 global additional_sources
516 # add the to-be-kept suffixes
517 foreach suffix {".ii" ".i" ".s" ".o" ".gkd"} {
518 if {[lsearch $args $suffix] < 0} {
519 lappend suffixes $suffix
523 # This assumes that we are two frames down from dg-test or some other proc
524 # that stores the filename of the testcase in a local variable "name".
525 # A cleaner solution would require a new DejaGnu release.
526 upvar 2 name testcase
527 # The name might include a list of options; extract the file name.
528 set testcase [lindex $testcase 0]
529 foreach suffix $suffixes {
530 remove-build-file "[file rootname [file tail $testcase]]$suffix"
531 # -fcompare-debug dumps
532 remove-build-file "[file rootname [file tail $testcase]].gk$suffix"
535 # Clean up saved temp files for additional source files.
536 if [info exists additional_sources] {
537 foreach srcfile $additional_sources {
538 foreach suffix $suffixes {
539 remove-build-file "[file rootname [file tail $srcfile]]$suffix"
540 # -fcompare-debug dumps
541 remove-build-file "[file rootname [file tail $srcfile]].gk$suffix"
547 # Remove files for specified Fortran modules.
548 proc cleanup-modules { modlist } {
549 foreach modname $modlist {
550 remove-build-file [string tolower $modname].mod
554 # Scan Fortran modules for a given regexp.
556 # Argument 0 is the module name
557 # Argument 1 is the regexp to match
558 proc scan-module { args } {
559 set modfilename [string tolower [lindex $args 0]].mod
560 set fd [open $modfilename r]
564 upvar 2 name testcase
565 if [regexp -- [lindex $args 1] $text] {
566 pass "$testcase scan-module [lindex $args 1]"
568 fail "$testcase scan-module [lindex $args 1]"
572 # Verify that the compiler output file exists, invoked via dg-final.
573 proc output-exists { args } {
574 # Process an optional target or xfail list.
575 if { [llength $args] >= 1 } {
576 switch [dg-process-target [lindex $args 0]] {
579 "F" { setup_xfail "*-*-*" }
584 # Access variables from gcc-dg-test-1.
585 upvar 2 name testcase
586 upvar 2 output_file output_file
588 if [file exists $output_file] {
589 pass "$testcase output-exists $output_file"
591 fail "$testcase output-exists $output_file"
595 # Verify that the compiler output file does not exist, invoked via dg-final.
596 proc output-exists-not { args } {
597 # Process an optional target or xfail list.
598 if { [llength $args] >= 1 } {
599 switch [dg-process-target [lindex $args 0]] {
602 "F" { setup_xfail "*-*-*" }
607 # Access variables from gcc-dg-test-1.
608 upvar 2 name testcase
609 upvar 2 output_file output_file
611 if [file exists $output_file] {
612 fail "$testcase output-exists-not $output_file"
614 pass "$testcase output-exists-not $output_file"
618 # We need to make sure that additional_* are cleared out after every
619 # test. It is not enough to clear them out *before* the next test run
620 # because gcc-target-compile gets run directly from some .exp files
621 # (outside of any test). (Those uses should eventually be eliminated.)
623 # Because the DG framework doesn't provide a hook that is run at the
624 # end of a test, we must replace dg-test with a wrapper.
626 if { [info procs saved-dg-test] == [list] } {
627 rename dg-test saved-dg-test
629 proc dg-test { args } {
630 global additional_files
631 global additional_sources
632 global additional_prunes
634 global compiler_conditional_xfail_data
637 if { [ catch { eval saved-dg-test $args } errmsg ] } {
638 set saved_info $errorInfo
639 set additional_files ""
640 set additional_sources ""
641 set additional_prunes ""
643 if [info exists compiler_conditional_xfail_data] {
644 unset compiler_conditional_xfail_data
647 error $errmsg $saved_info
649 set additional_files ""
650 set additional_sources ""
651 set additional_prunes ""
654 if [info exists compiler_conditional_xfail_data] {
655 unset compiler_conditional_xfail_data
660 if { [info procs saved-dg-warning] == [list] \
661 && [info exists gcc_warning_prefix] } {
662 rename dg-warning saved-dg-warning
664 proc dg-warning { args } {
665 # Make this variable available here and to the saved proc.
666 upvar dg-messages dg-messages
667 global gcc_warning_prefix
669 process-message saved-dg-warning "$gcc_warning_prefix" "$args"
673 if { [info procs saved-dg-error] == [list] \
674 && [info exists gcc_error_prefix] } {
675 rename dg-error saved-dg-error
677 proc dg-error { args } {
678 # Make this variable available here and to the saved proc.
679 upvar dg-messages dg-messages
680 global gcc_error_prefix
682 process-message saved-dg-error "$gcc_error_prefix" "$args"
685 # Override dg-bogus at the same time. It doesn't handle a prefix
686 # but its expression should include a column number. Otherwise the
687 # line number can match the column number for other messages, leading
689 rename dg-bogus saved-dg-bogus
691 proc dg-bogus { args } {
692 upvar dg-messages dg-messages
693 process-message saved-dg-bogus "" $args
697 # Modify the regular expression saved by a DejaGnu message directive to
698 # include a prefix and to force the expression to match a single line.
699 # MSGPROC is the procedure to call.
700 # MSGPREFIX is the prefix to prepend.
701 # DGARGS is the original argument list.
703 proc process-message { msgproc msgprefix dgargs } {
704 upvar dg-messages dg-messages
706 # Process the dg- directive, including adding the regular expression
707 # to the new message entry in dg-messages.
708 set msgcnt [llength ${dg-messages}]
709 catch { eval $msgproc $dgargs }
711 # If the target expression wasn't satisfied there is no new message.
712 if { [llength ${dg-messages}] == $msgcnt } {
716 # Get the entry for the new message. Prepend the message prefix to
717 # the regular expression and make it match a single line.
718 set newentry [lindex ${dg-messages} end]
719 set expmsg [lindex $newentry 2]
721 # Handle column numbers from the specified expression (if there is
722 # one) and set up the search expression that will be used by DejaGnu.
723 if [regexp "^(\[0-9\]+):" $expmsg "" column] {
724 # The expression in the directive included a column number.
725 # Remove "COLUMN:" from the original expression and move it
726 # to the proper place in the search expression.
727 regsub "^\[0-9\]+:" $expmsg "" expmsg
728 set expmsg "$column: $msgprefix\[^\n\]*$expmsg"
729 } elseif [string match "" [lindex $newentry 0]] {
730 # The specified line number is 0; don't expect a column number.
731 set expmsg "$msgprefix\[^\n\]*$expmsg"
733 # There is no column number in the search expression, but we
734 # should expect one in the message itself.
735 set expmsg "\[0-9\]+: $msgprefix\[^\n\]*$expmsg"
738 set newentry [lreplace $newentry 2 2 $expmsg]
739 set dg-messages [lreplace ${dg-messages} end end $newentry]
740 verbose "process-message:\n${dg-messages}" 2
743 # Look for messages that don't have standard prefixes.
745 proc dg-message { args } {
746 upvar dg-messages dg-messages
747 process-message saved-dg-warning "" $args
750 # Check the existence of a gdb in the path, and return true if there
753 # Set env(GDB_FOR_GCC_TESTING) accordingly.
755 proc gdb-exists { args } {
756 if ![info exists ::env(GDB_FOR_GCC_TESTING)] {
758 if ![info exists ::env(GDB_FOR_GCC_TESTING)] {
759 if [info exists GDB] {
760 setenv GDB_FOR_GCC_TESTING "$GDB"
762 setenv GDB_FOR_GCC_TESTING "[transform gdb]"
766 if { [which $::env(GDB_FOR_GCC_TESTING)] != 0 } {
772 set additional_prunes ""