Wattributes-10.c: Add -fno-common option on hppa*-*-hpux*.
[official-gcc.git] / gcc / testsuite / lib / gcc-dg.exp
blobc56e8e80ae0c63fec7a8c8aa9dcec1f024a6bba2
1 # Copyright (C) 1997-2019 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
17 load_lib dg.exp
18 load_lib file-format.exp
19 load_lib target-supports.exp
20 load_lib target-supports-dg.exp
21 load_lib scanasm.exp
22 load_lib scanrtl.exp
23 load_lib scantree.exp
24 load_lib scanltranstree.exp
25 load_lib scanipa.exp
26 load_lib scanwpaipa.exp
27 load_lib scanlang.exp
28 load_lib timeout.exp
29 load_lib timeout-dg.exp
30 load_lib prune.exp
31 load_lib libgloss.exp
32 load_lib target-libpath.exp
33 load_lib torture-options.exp
34 load_lib fortran-modules.exp
35 load_lib multiline.exp
37 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
38 setenv LC_ALL C
39 setenv LANG C
41 # Many hosts now default to a non-ASCII C locale, however, so
42 # they can set a charset encoding here if they need.
43 if { [ishost "*-*-cygwin*"] } {
44 setenv LC_ALL C.ASCII
45 setenv LANG C.ASCII
48 # Avoid sporadic data-losses with expect
49 match_max -d 10000
51 # Ensure GCC_COLORS is unset, for the rare testcases that verify
52 # how output is colorized.
53 if [info exists ::env(GCC_COLORS) ] {
54 unsetenv GCC_COLORS
57 global GCC_UNDER_TEST
58 if ![info exists GCC_UNDER_TEST] {
59 set GCC_UNDER_TEST "[find_gcc]"
62 # This file may be sourced, so don't override environment settings
63 # that have been previously setup.
64 if { $orig_environment_saved == 0 } {
65 append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
66 set_ld_library_path_env_vars
69 # Some torture-options cause intermediate code output, unusable for
70 # testing using e.g. scan-assembler. In this variable are the options
71 # how to force it, when needed.
72 global gcc_force_conventional_output
73 set gcc_force_conventional_output ""
75 set LTO_TORTURE_OPTIONS ""
76 if [info exists TORTURE_OPTIONS] {
77 set DG_TORTURE_OPTIONS $TORTURE_OPTIONS
78 } else {
79 # It is theoretically beneficial to group all of the O2/O3 options together,
80 # as in many cases the compiler will generate identical executables for
81 # all of them--and the c-torture testsuite will skip testing identical
82 # executables multiple times.
83 # Also note that -finline-functions is explicitly included in one of the
84 # items below, even though -O3 is also specified, because some ports may
85 # choose to disable inlining functions by default, even when optimizing.
86 set DG_TORTURE_OPTIONS [list \
87 { -O0 } \
88 { -O1 } \
89 { -O2 } \
90 { -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions } \
91 { -O3 -g } \
92 { -Os } ]
94 if [check_effective_target_lto] {
95 # When having plugin test both slim and fat LTO and plugin/nonplugin
96 # path.
97 if [check_linker_plugin_available] {
98 set LTO_TORTURE_OPTIONS [list \
99 { -O2 -flto -fno-use-linker-plugin -flto-partition=none } \
100 { -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects }
102 } else {
103 set LTO_TORTURE_OPTIONS [list \
104 { -O2 -flto -flto-partition=none } \
105 { -O2 -flto }
108 set gcc_force_conventional_output "-ffat-lto-objects"
112 if [info exists ADDITIONAL_TORTURE_OPTIONS] {
113 set DG_TORTURE_OPTIONS \
114 [concat $DG_TORTURE_OPTIONS $ADDITIONAL_TORTURE_OPTIONS]
117 proc dg-final { args } {
118 upvar dg-final-code final-code
120 if { [llength $args] > 2 } {
121 error "[lindex $args 0]: too many arguments"
123 set line [lindex $args 0]
124 set code [lindex $args 1]
125 set directive [lindex $code 0]
126 switch $directive {
127 gdb-test {
128 set code [linsert $code 1 $line]
131 append final-code "$code\n"
134 global orig_environment_saved
136 # Deduce generated files from tool flags, return finalcode string
137 proc schedule-cleanups { opts } {
138 global additional_sources
139 set finalcode ""
140 set testcases {}
141 lappend testcases [lindex [testname-for-summary] 0]
142 verbose "Cleanup testcases: $testcases" 4
143 if { [info exists additional_sources] && $additional_sources != "" } {
144 lappend testcases $additional_sources
145 verbose "Cleanup testcases, additional: $additional_sources" 4
147 verbose "Cleanup all options: $opts" 4
149 # First some fixups to transform stuff to something manageable ..
150 # --dump= should translate to -d with joined operand.
151 if [regexp -- {(^|\s+)--dump=[^\s]+(\s+|$)} $opts] {
152 regsub -all -- {--dump=} $opts {-d} opts
154 # -da and -dx are treated as shorthand for -fdump-rtl-all here
155 if [regexp -- {(^|\s+)-d[ax](\s+|$)} $opts] {
156 verbose "Cleanup -d seen" 4
157 lappend opts "-fdump-rtl-all"
159 # .. and don't question why there is --dump=? and -d?
161 # Then handle options that generate non-dump files
162 # TODO
163 # -fprofile-generate -> cleanup-coverage-files()
164 # -fstack-usage -> cleanup-stack-usage()
165 if [regexp -- {(^|\s+)-fstack-usage(\s+|$)} $opts] {
166 verbose "Cleanup -fstack-usage seen" 4
167 # append finalcode "cleanup-stack-usage\n"
169 global keep_saved_temps_suffixes
170 if [info exists keep_saved_temps_suffixes ] {
171 verbose "dg-keep-saved-temps ${keep_saved_temps_suffixes}" 2
173 # -save-temps -> cleanup-saved-temps()
174 if [regexp -- {(^|\s+)-save-temps(\s+|$)} $opts] {
175 verbose "Cleanup -save-temps seen" 4
176 if [info exists keep_saved_temps_suffixes] {
177 append finalcode "cleanup-saved-temps ${keep_saved_temps_suffixes}\n"
178 } else {
179 append finalcode "cleanup-saved-temps\n"
181 } else {
182 if [info exists keep_saved_temps_suffixes ] {
183 error "dg-keep-saved-temps specified but testcase does not -save-temps"
184 return
187 # Finally see if there are any dumps in opts, otherwise we are done
188 if [regexp -- {(?=(?:^|[ \t]+)?)-fdump-[^ \t]+(?=(?:$|[ \t]+)?)} $opts] {
189 # Lang, Ipa, Rtl, Tree for simplicity
190 set ptn "{l,i,r,t}"
191 } else {
192 return $finalcode
194 # stem.ext.<passnum><fam>.<passname><pass-instances>
195 # (tree)passes can have multiple instances, thus optional trailing *
196 set ptn "\[0-9\]\[0-9\]\[0-9\]$ptn.*"
197 # Handle ltrans files around -flto
198 if [regexp -- {(^|\s+)-flto(\s+|$)} $opts] {
199 verbose "Cleanup -flto seen" 4
200 set ltrans "{ltrans\[0-9\]*.,}"
201 } else {
202 set ltrans ""
204 set ptn "$ltrans$ptn"
205 verbose "Cleanup final ptn: $ptn" 4
206 set tfiles {}
207 foreach src $testcases {
208 set basename [file tail $src]
209 if { $ltrans != "" } {
210 # ??? should we use upvar 1 output_file instead of this (dup ?)
211 set stem [file rootname $basename]
212 set basename_ext [file extension $basename]
213 if {$basename_ext != ""} {
214 regsub -- {^.*\.} $basename_ext {} basename_ext
216 lappend tfiles "$stem.{$basename_ext,exe}"
217 unset basename_ext
218 } else {
219 lappend tfiles $basename
222 if { [llength $tfiles] > 1 } {
223 set tfiles [join $tfiles ","]
224 set tfiles "{$tfiles}"
226 verbose "Cleanup final testcases: $tfiles" 4
227 # We have to quote the regex
228 regsub -all {([][$^?+*()|\\{}])} "$tfiles.$ptn" {\\\1} ptn
229 set final ""
230 append final {remove-build-file }
231 append final "\"$ptn\""
232 verbose "Cleanup final: $final" 4
233 append finalcode "$final\n"
235 return $finalcode
238 # Define gcc callbacks for dg.exp.
240 proc gcc-dg-test-1 { target_compile prog do_what extra_tool_flags } {
241 # Set up the compiler flags, based on what we're going to do.
243 set options [list]
245 # Tests should be able to use "dg-do repo". However, the dg test
246 # driver checks the argument to dg-do against a list of acceptable
247 # options, and "repo" is not among them. Therefore, we resort to
248 # this ugly approach.
249 if [string match "*-frepo*" $extra_tool_flags] then {
250 set do_what "repo"
253 switch $do_what {
254 "preprocess" {
255 set compile_type "preprocess"
256 set output_file "[file rootname [file tail $prog]].i"
258 "compile" {
259 set compile_type "assembly"
260 set output_file "[file rootname [file tail $prog]].s"
262 "assemble" {
263 set compile_type "object"
264 set output_file "[file rootname [file tail $prog]].o"
266 "precompile" {
267 set compile_type "precompiled_header"
268 set output_file "[file tail $prog].gch"
270 "link" {
271 set compile_type "executable"
272 set output_file "[file rootname [file tail $prog]].exe"
273 # The following line is needed for targets like the i960 where
274 # the default output file is b.out. Sigh.
276 "repo" {
277 set compile_type "object"
278 set output_file "[file rootname [file tail $prog]].o"
280 "run" {
281 set compile_type "executable"
282 # FIXME: "./" is to cope with "." not being in $PATH.
283 # Should this be handled elsewhere?
284 # YES.
285 set output_file "./[file rootname [file tail $prog]].exe"
286 # This is the only place where we care if an executable was
287 # created or not. If it was, dg.exp will try to run it.
288 catch { remote_file build delete $output_file }
290 default {
291 perror "$do_what: not a valid dg-do keyword"
292 return ""
296 # Let { dg-final { action } } force options as returned by an
297 # optional proc ${action}_required_options.
298 upvar 2 dg-final-code finalcode
299 foreach x [split $finalcode "\n"] {
300 set finalcmd [lindex $x 0]
301 if { [info procs ${finalcmd}_required_options] != "" } {
302 foreach req [${finalcmd}_required_options] {
303 if { $req != ""
304 && [lsearch -exact $extra_tool_flags $req] == -1 } {
305 lappend extra_tool_flags $req
311 append finalcode [schedule-cleanups "$options $extra_tool_flags"]
312 if { $extra_tool_flags != "" } {
313 lappend options "additional_flags=$extra_tool_flags"
316 verbose "$target_compile $prog $output_file $compile_type $options" 4
317 set comp_output [$target_compile "$prog" "$output_file" "$compile_type" $options]
319 # Look for an internal compiler error, which sometimes masks the fact
320 # that we didn't get an expected error message. XFAIL an ICE via
321 # dg-xfail-if and use { dg-prune-output ".*internal compiler error.*" }
322 # to avoid a second failure for excess errors.
323 if [string match "*internal compiler error*" $comp_output] {
324 upvar 2 name name
325 fail "$name (internal compiler error)"
328 if { $do_what == "repo" } {
329 set object_file "$output_file"
330 set output_file "[file rootname [file tail $prog]].exe"
331 set comp_output \
332 [ concat $comp_output \
333 [$target_compile "$object_file" "$output_file" \
334 "executable" $options] ]
337 return [list $comp_output $output_file]
340 proc gcc-dg-test { prog do_what extra_tool_flags } {
341 return [gcc-dg-test-1 gcc_target_compile $prog $do_what $extra_tool_flags]
344 # Global: should blank lines be allowed in the output?
345 # By default, they should not be. (PR other/69006)
346 # However, there are some ways for them to validly occur.
347 # If this variable is 0, blank lines are not allowed in output,
348 # if it is 1, they are allowed for a single testcase only and gcc-dg-prune
349 # will clear it again after checking it, if it is 2, they are disabled
350 # for all tests.
351 set allow_blank_lines 0
353 if { [check_effective_target_llvm_binutils] } {
354 set allow_blank_lines 2
357 # A command for use by testcases to mark themselves as expecting
358 # blank lines in the output.
360 proc dg-allow-blank-lines-in-output { args } {
361 global allow_blank_lines
362 if { !$allow_blank_lines } {
363 set allow_blank_lines 1
367 proc gcc-dg-prune { system text } {
368 global additional_prunes
370 # Extra prune rules that will apply to tests defined in a .exp file.
371 # Always remember to clear it in .exp file after executed all tests.
372 global dg_runtest_extra_prunes
374 # Complain about blank lines in the output (PR other/69006)
375 global allow_blank_lines
376 if { !$allow_blank_lines } {
377 set num_blank_lines [llength [regexp -all -inline "\n\n" $text]]
378 if { $num_blank_lines } {
379 global testname_with_flags
380 fail "$testname_with_flags $num_blank_lines blank line(s) in output"
383 if { $allow_blank_lines == 1 } {
384 set allow_blank_lines 0
387 set text [prune_gcc_output $text]
389 foreach p "$additional_prunes $dg_runtest_extra_prunes" {
390 if { [string length $p] > 0 } {
391 # Following regexp matches a complete line containing $p.
392 regsub -all "(^|\n)\[^\n\]*$p\[^\n\]*" $text "" text
396 # If we see "region xxx is full" then the testcase is too big for ram.
397 # This is tricky to deal with in a large testsuite like c-torture so
398 # deal with it here. Just mark the testcase as unsupported.
399 if [regexp "(^|\n)\[^\n\]*: region \[^\n\]* is full" $text] {
400 # The format here is important. See dg.exp.
401 return "::unsupported::memory full"
404 if { [regexp "(^|\n)\[^\n\]*: relocation truncated to fit" $text]
405 && [check_effective_target_tiny] } {
406 return "::unsupported::memory full"
409 if [regexp "(^|\n)\[^\n\]* section.*will not fit in region" $text] {
410 return "::unsupported::memory full"
413 if [regexp "(^|\n)\[^\n\]* region.*overflowed by" $text] {
414 return "::unsupported::memory full"
417 # Likewise, if we see ".text exceeds local store range" or
418 # similar.
419 if {[string match "spu-*" $system] && \
420 [string match "*exceeds local store*" $text]} {
421 # The format here is important. See dg.exp.
422 return "::unsupported::memory full"
425 return $text
428 # Replace ${tool}_load with a wrapper to provide for an expected nonzero
429 # exit status. Multiple languages include this file so this handles them
430 # all, not just gcc.
431 if { [info procs ${tool}_load] != [list] \
432 && [info procs saved_${tool}_load] == [list] } {
433 rename ${tool}_load saved_${tool}_load
435 proc ${tool}_load { program args } {
436 global tool
437 global shouldfail
438 global set_target_env_var
440 set saved_target_env_var [list]
441 if { [info exists set_target_env_var] \
442 && [llength $set_target_env_var] != 0 } {
443 if { [is_remote target] } {
444 return [list "unsupported" ""]
446 set-target-env-var
448 set result [eval [list saved_${tool}_load $program] $args]
449 if { [info exists set_target_env_var] \
450 && [llength $set_target_env_var] != 0 } {
451 restore-target-env-var
453 if { $shouldfail != 0 } {
454 switch [lindex $result 0] {
455 "pass" { set status "fail" }
456 "fail" { set status "pass" }
457 default { set status [lindex $result 0] }
459 set result [list $status [lindex $result 1]]
462 set result [list [lindex $result 0] [prune_file_path [lindex $result 1]]]
463 return $result
467 proc dg-set-target-env-var { args } {
468 global set_target_env_var
469 if { [llength $args] != 3 } {
470 error "dg-set-target-env-var: need two arguments"
471 return
473 lappend set_target_env_var [list [lindex $args 1] [lindex $args 2]]
476 proc set-target-env-var { } {
477 global set_target_env_var
478 upvar 1 saved_target_env_var saved_target_env_var
479 foreach env_var $set_target_env_var {
480 set var [lindex $env_var 0]
481 set value [lindex $env_var 1]
482 if [info exists ::env($var)] {
483 lappend saved_target_env_var [list $var 1 $::env($var)]
484 } else {
485 lappend saved_target_env_var [list $var 0]
487 setenv $var $value
491 proc restore-target-env-var { } {
492 upvar 1 saved_target_env_var saved_target_env_var
493 for { set env_vari [llength $saved_target_env_var] } {
494 [incr env_vari -1] >= 0 } {} {
495 set env_var [lindex $saved_target_env_var $env_vari]
496 set var [lindex $env_var 0]
497 if [lindex $env_var 1] {
498 setenv $var [lindex $env_var 2]
499 } else {
500 unsetenv $var
505 proc dg-set-compiler-env-var { args } {
506 global set_compiler_env_var
507 global saved_compiler_env_var
508 if { [llength $args] != 3 } {
509 error "dg-set-compiler-env-var: need two arguments"
510 return
512 set var [lindex $args 1]
513 set value [lindex $args 2]
514 if [info exists ::env($var)] {
515 lappend saved_compiler_env_var [list $var 1 $::env($var)]
516 } else {
517 lappend saved_compiler_env_var [list $var 0]
519 setenv $var $value
520 lappend set_compiler_env_var [list $var $value]
523 proc restore-compiler-env-var { } {
524 global saved_compiler_env_var
525 for { set env_vari [llength $saved_compiler_env_var] } {
526 [incr env_vari -1] >= 0 } {} {
527 set env_var [lindex $saved_compiler_env_var $env_vari]
528 set var [lindex $env_var 0]
529 if [lindex $env_var 1] {
530 setenv $var [lindex $env_var 2]
531 } else {
532 unsetenv $var
537 # Utility routines.
540 # search_for -- looks for a string match in a file
542 proc search_for { file pattern } {
543 set fd [open $file r]
544 while { [gets $fd cur_line]>=0 } {
545 if [string match "*$pattern*" $cur_line] then {
546 close $fd
547 return 1
550 close $fd
551 return 0
554 # Modified dg-runtest that can cycle through a list of optimization options
555 # as c-torture does.
556 proc gcc-dg-runtest { testcases flags default-extra-flags } {
557 global runtests
559 # Some callers set torture options themselves; don't override those.
560 set existing_torture_options [torture-options-exist]
561 if { $existing_torture_options == 0 } {
562 global DG_TORTURE_OPTIONS LTO_TORTURE_OPTIONS
563 torture-init
564 set-torture-options $DG_TORTURE_OPTIONS [list {}] $LTO_TORTURE_OPTIONS
566 dump-torture-options
568 foreach test $testcases {
569 global torture_with_loops torture_without_loops
570 # If we're only testing specific files and this isn't one of
571 # them, skip it.
572 if ![runtest_file_p $runtests $test] {
573 continue
576 # Look for a loop within the source code - if we don't find one,
577 # don't pass -funroll[-all]-loops.
578 if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
579 set option_list $torture_with_loops
580 } else {
581 set option_list $torture_without_loops
584 set nshort [file tail [file dirname $test]]/[file tail $test]
586 foreach flags_t $option_list {
587 global torture_current_flags
588 set torture_current_flags "$flags_t"
589 verbose "Testing $nshort, $flags $flags_t" 1
590 dg-test $test "$flags $flags_t" ${default-extra-flags}
594 if { $existing_torture_options == 0 } {
595 torture-finish
599 proc gcc-dg-debug-runtest { target_compile trivial opt_opts testcases } {
600 global srcdir subdir
602 if ![info exists DEBUG_TORTURE_OPTIONS] {
603 set DEBUG_TORTURE_OPTIONS ""
604 foreach type {-gdwarf-2 -gstabs -gstabs+ -gxcoff -gxcoff+} {
605 set comp_output [$target_compile \
606 "$srcdir/$subdir/$trivial" "trivial.S" assembly \
607 "additional_flags=$type"]
608 if { ! [string match "*: target system does not support the * debug format*" \
609 $comp_output] } {
610 remove-build-file "trivial.S"
611 foreach level {1 "" 3} {
612 if { ($type == "-gdwarf-2") && ($level != "") } {
613 lappend DEBUG_TORTURE_OPTIONS [list "${type}" "-g${level}"]
614 foreach opt $opt_opts {
615 lappend DEBUG_TORTURE_OPTIONS \
616 [list "${type}" "-g${level}" "$opt" ]
618 } else {
619 lappend DEBUG_TORTURE_OPTIONS [list "${type}${level}"]
620 foreach opt $opt_opts {
621 lappend DEBUG_TORTURE_OPTIONS \
622 [list "${type}${level}" "$opt" ]
630 verbose -log "Using options $DEBUG_TORTURE_OPTIONS"
632 global runtests
634 foreach test $testcases {
635 # If we're only testing specific files and this isn't one of
636 # them, skip it.
637 if ![runtest_file_p $runtests $test] {
638 continue
641 set nshort [file tail [file dirname $test]]/[file tail $test]
643 foreach flags $DEBUG_TORTURE_OPTIONS {
644 set doit 1
646 # These tests check for information which may be deliberately
647 # suppressed at -g1.
648 if { ([string match {*/debug-[126].c} "$nshort"] \
649 || [string match {*/enum-1.c} "$nshort"] \
650 || [string match {*/enum-[12].C} "$nshort"]) \
651 && ([string match "*1" [lindex "$flags" 0] ]
652 || [lindex "$flags" 1] == "-g1") } {
653 set doit 0
656 # High optimization can remove the variable whose existence is tested.
657 # Dwarf debugging with commentary (-dA) preserves the symbol name in the
658 # assembler output, but stabs debugging does not.
659 # http://gcc.gnu.org/ml/gcc-regression/2003-04/msg00095.html
660 if { [string match {*/debug-[12].c} "$nshort"] \
661 && [string match "*O*" "$flags"] \
662 && ( [string match "*coff*" "$flags"] \
663 || [string match "*stabs*" "$flags"] ) } {
664 set doit 0
667 if { $doit } {
668 verbose -log "Testing $nshort, $flags" 1
669 dg-test $test $flags ""
675 # Prune any messages matching ARGS[1] (a regexp) from test output.
676 proc dg-prune-output { args } {
677 global additional_prunes
679 if { [llength $args] != 2 } {
680 error "[lindex $args 1]: need one argument"
681 return
684 lappend additional_prunes [lindex $args 1]
687 # Remove files matching the pattern from the build machine.
688 proc remove-build-file { pat } {
689 verbose "remove-build-file `$pat'" 2
690 set file_list "[glob -nocomplain $pat]"
691 verbose "remove-build-file `$file_list'" 2
692 foreach output_file $file_list {
693 if [is_remote host] {
694 # Ensure the host knows the file is gone by deleting there
695 # first.
696 remote_file host delete $output_file
698 remote_file build delete $output_file
702 # Remove runtime-generated profile file for the current test.
703 proc cleanup-profile-file { } {
704 remove-build-file "mon.out"
705 remove-build-file "gmon.out"
708 # Remove compiler-generated coverage files for the current test.
709 proc cleanup-coverage-files { } {
710 global additional_sources_used
711 set testcase [testname-for-summary]
712 # The name might include a list of options; extract the file name.
713 set testcase [lindex $testcase 0]
714 remove-build-file "[file rootname [file tail $testcase]].gc??"
716 # Clean up coverage files for additional source files.
717 if [info exists additional_sources_used] {
718 foreach srcfile $additional_sources_used {
719 remove-build-file "[file rootname [file tail $srcfile]].gc??"
724 # Remove compiler-generated files from -repo for the current test.
725 proc cleanup-repo-files { } {
726 global additional_sources_used
727 set testcase [testname-for-summary]
728 # The name might include a list of options; extract the file name.
729 set testcase [lindex $testcase 0]
730 remove-build-file "[file rootname [file tail $testcase]].o"
731 remove-build-file "[file rootname [file tail $testcase]].rpo"
733 # Clean up files for additional source files.
734 if [info exists additional_sources_used] {
735 foreach srcfile $additional_sources_used {
736 remove-build-file "[file rootname [file tail $srcfile]].o"
737 remove-build-file "[file rootname [file tail $srcfile]].rpo"
742 # Remove a final insns dump file for the current test.
743 proc cleanup-final-insns-dump { } {
744 set testcase [testname-for-summary]
745 # The name might include a list of options; extract the file name.
746 set testcase [lindex $testcase 0]
747 remove-build-file "[file rootname [file tail $testcase]].s.gkd"
749 # Clean up files for additional source files.
750 if [info exists additional_sources_used] {
751 foreach srcfile $additional_sources_used {
752 remove-build-file "[file rootname [file tail $srcfile]].s.gkd"
757 # Remove a stack usage file for the current test.
758 proc cleanup-stack-usage { } {
759 set testcase [testname-for-summary]
760 # The name might include a list of options; extract the file name.
761 set testcase [lindex $testcase 0]
762 remove-build-file "[file rootname [file tail $testcase]].su"
764 # Clean up files for additional source files.
765 if [info exists additional_sources_used] {
766 foreach srcfile $additional_sources_used {
767 remove-build-file "[file rootname [file tail $srcfile]].su"
772 # Remove an Ada spec file for the current test.
773 proc cleanup-ada-spec { } {
774 global additional_sources_used
775 set testcase [testname-for-summary]
776 remove-build-file "[get_ada_spec_filename $testcase]"
778 # Clean up files for additional source files.
779 if [info exists additional_sources_used] {
780 foreach srcfile $additional_sources_used {
781 remove-build-file "[get_ada_spec_filename $srcfile]"
786 # Remove files kept by --save-temps for the current test.
788 # Currently this is only .i, .ii, .s and .o files, but more can be added
789 # if there are tests generating them.
790 # ARGS is a list of suffixes to NOT delete.
791 proc cleanup-saved-temps { args } {
792 global additional_sources_used
793 set suffixes {}
795 # add the to-be-kept suffixes
796 foreach suffix {".mii" ".ii" ".i" ".s" ".o" ".gkd" ".res" ".ltrans.out"} {
797 if {[lsearch $args $suffix] < 0} {
798 lappend suffixes $suffix
802 set testcase [testname-for-summary]
803 # The name might include a list of options; extract the file name.
804 set testcase [lindex $testcase 0]
805 foreach suffix $suffixes {
806 remove-build-file "[file rootname [file tail $testcase]]$suffix"
807 remove-build-file "[file rootname [file tail $testcase]].exe$suffix"
808 remove-build-file "[file rootname [file tail $testcase]].exe.ltrans\[0-9\]*$suffix"
809 # -fcompare-debug dumps
810 remove-build-file "[file rootname [file tail $testcase]].gk$suffix"
813 # Clean up saved temp files for additional source files.
814 if [info exists additional_sources_used] {
815 foreach srcfile $additional_sources_used {
816 foreach suffix $suffixes {
817 remove-build-file "[file rootname [file tail $srcfile]]$suffix"
818 remove-build-file "[file rootname [file tail $srcfile]].exe$suffix"
819 remove-build-file "[file rootname [file tail $srcfile]].exe.ltrans\[0-9\]*$suffix"
821 # -fcompare-debug dumps
822 remove-build-file "[file rootname [file tail $srcfile]].gk$suffix"
829 # Files to be kept after cleanup of --save-temps for the current test.
830 # ARGS is a list of suffixes to NOT delete.
831 proc dg-keep-saved-temps { args } {
832 global keep_saved_temps_suffixes
833 set keep_saved_temps_suffixes {}
835 # add the to-be-kept suffixes
836 foreach suffix {".mii" ".ii" ".i" ".s" ".o" ".gkd" ".res" ".ltrans.out"} {
837 if {[lsearch $args $suffix] >= 0} {
838 lappend keep_saved_temps_suffixes $suffix
841 if { [llength keep_saved_temps_suffixes] < 1 } {
842 error "dg-keep-saved-temps ${args} did not match any known suffix"
846 # Scan Fortran modules for a given regexp.
848 # Argument 0 is the module name
849 # Argument 1 is the regexp to match
850 proc scan-module { args } {
851 set modfilename [string tolower [lindex $args 0]].mod
852 set fd [open [list | gzip -dc $modfilename] r]
853 set text [read $fd]
854 close $fd
856 set testcase [testname-for-summary]
857 if [regexp -- [lindex $args 1] $text] {
858 pass "$testcase scan-module [lindex $args 1]"
859 } else {
860 fail "$testcase scan-module [lindex $args 1]"
864 # Scan Fortran modules for absence of a given regexp.
866 # Argument 0 is the module name
867 # Argument 1 is the regexp to match
868 proc scan-module-absence { args } {
869 set modfilename [string tolower [lindex $args 0]].mod
870 set fd [open [list | gzip -dc $modfilename] r]
871 set text [read $fd]
872 close $fd
874 set testcase [testname-for-summary]
875 if [regexp -- [lindex $args 1] $text] {
876 fail "$testcase scan-module [lindex $args 1]"
877 } else {
878 pass "$testcase scan-module [lindex $args 1]"
882 # Verify that the compiler output file exists, invoked via dg-final.
883 proc output-exists { args } {
884 # Process an optional target or xfail list.
885 if { [llength $args] >= 1 } {
886 switch [dg-process-target [lindex $args 0]] {
887 "S" { }
888 "N" { return }
889 "F" { setup_xfail "*-*-*" }
890 "P" { }
894 set testcase [testname-for-summary]
895 # Access variable from gcc-dg-test-1.
896 upvar 2 output_file output_file
898 if [file exists $output_file] {
899 pass "$testcase output-exists $output_file"
900 } else {
901 fail "$testcase output-exists $output_file"
905 # Verify that the compiler output file does not exist, invoked via dg-final.
906 proc output-exists-not { args } {
907 # Process an optional target or xfail list.
908 if { [llength $args] >= 1 } {
909 switch [dg-process-target [lindex $args 0]] {
910 "S" { }
911 "N" { return }
912 "F" { setup_xfail "*-*-*" }
913 "P" { }
917 set testcase [testname-for-summary]
918 # Access variable from gcc-dg-test-1.
919 upvar 2 output_file output_file
921 if [file exists $output_file] {
922 fail "$testcase output-exists-not $output_file"
923 } else {
924 pass "$testcase output-exists-not $output_file"
928 # We need to make sure that additional_* are cleared out after every
929 # test. It is not enough to clear them out *before* the next test run
930 # because gcc-target-compile gets run directly from some .exp files
931 # (outside of any test). (Those uses should eventually be eliminated.)
933 # Because the DG framework doesn't provide a hook that is run at the
934 # end of a test, we must replace dg-test with a wrapper.
936 if { [info procs saved-dg-test] == [list] } {
937 rename dg-test saved-dg-test
939 # Helper function for cleanups that should happen after the call
940 # to the real dg-test, whether or not it returns normally, or
941 # fails with an error.
942 proc cleanup-after-saved-dg-test { } {
943 global additional_files
944 global additional_sources
945 global additional_sources_used
946 global additional_prunes
947 global compiler_conditional_xfail_data
948 global shouldfail
949 global testname_with_flags
950 global set_target_env_var
951 global set_compiler_env_var
952 global saved_compiler_env_var
953 global keep_saved_temps_suffixes
954 global multiline_expected_outputs
955 global freeform_regexps
956 global save_linenr_varnames
958 set additional_files ""
959 set additional_sources ""
960 set additional_sources_used ""
961 set additional_prunes ""
962 set shouldfail 0
963 if [info exists set_target_env_var] {
964 unset set_target_env_var
966 if [info exists set_compiler_env_var] {
967 restore-compiler-env-var
968 unset set_compiler_env_var
969 unset saved_compiler_env_var
971 if [info exists keep_saved_temps_suffixes] {
972 unset keep_saved_temps_suffixes
974 unset_timeout_vars
975 if [info exists compiler_conditional_xfail_data] {
976 unset compiler_conditional_xfail_data
978 if [info exists testname_with_flags] {
979 unset testname_with_flags
981 set multiline_expected_outputs []
982 set freeform_regexps []
984 if { [info exists save_linenr_varnames] } {
985 foreach varname $save_linenr_varnames {
986 # Cleanup varname
987 eval global $varname
988 eval unset $varname
990 # Cleanup varname_used, or generate defined-but-not-used
991 # warning.
992 set varname_used used_$varname
993 eval global $varname_used
994 eval set used [info exists $varname_used]
995 if { $used } {
996 eval unset $varname_used
997 } else {
998 regsub {^saved_linenr_} $varname "" org_varname
999 warning "dg-line var $org_varname defined, but not used"
1002 unset save_linenr_varnames
1006 proc dg-test { args } {
1007 global errorInfo
1009 if { [ catch { eval saved-dg-test $args } errmsg ] } {
1010 set saved_info $errorInfo
1011 cleanup-after-saved-dg-test
1012 error $errmsg $saved_info
1014 cleanup-after-saved-dg-test
1018 if { [info procs saved-dg-warning] == [list] \
1019 && [info exists gcc_warning_prefix] } {
1020 rename dg-warning saved-dg-warning
1022 proc dg-warning { args } {
1023 # Make this variable available here and to the saved proc.
1024 upvar dg-messages dg-messages
1025 global gcc_warning_prefix
1027 process-message saved-dg-warning "$gcc_warning_prefix" "$args"
1031 if { [info procs saved-dg-error] == [list] \
1032 && [info exists gcc_error_prefix] } {
1033 rename dg-error saved-dg-error
1035 proc dg-error { args } {
1036 # Make this variable available here and to the saved proc.
1037 upvar dg-messages dg-messages
1038 global gcc_error_prefix
1040 process-message saved-dg-error "$gcc_error_prefix" "$args"
1043 # Override dg-bogus at the same time. It doesn't handle a prefix
1044 # but its expression should include a column number. Otherwise the
1045 # line number can match the column number for other messages, leading
1046 # to insanity.
1047 rename dg-bogus saved-dg-bogus
1049 proc dg-bogus { args } {
1050 upvar dg-messages dg-messages
1051 process-message saved-dg-bogus "" $args
1055 # Set variable VARNAME to LINENR
1057 proc dg-line { linenr varname } {
1058 set org_varname $varname
1059 set varname "saved_linenr_$varname"
1060 eval global $varname
1062 # Generate defined-but-previously-defined error.
1063 eval set var_defined [info exists $varname]
1064 if { $var_defined } {
1065 eval set deflinenr \$$varname
1066 error "dg-line var $org_varname defined at line $linenr, but previously defined at line $deflinenr"
1067 return
1070 eval set $varname $linenr
1072 # Schedule cleanup of varname by cleanup-after-saved-dg-test
1073 global save_linenr_varnames
1074 if { [info exists save_linenr_varnames] } {
1075 lappend save_linenr_varnames $varname
1076 } else {
1077 set save_linenr_varnames [list $varname]
1081 # Get the absolute line number corresponding to:
1082 # - a relative line number (a non-null useline is required), or
1083 # - a line number variable reference.
1084 # Argument 0 is the line number on which line was used
1085 # Argument 1 is the relative line number or line number variable reference
1087 proc get-absolute-line { useline line } {
1088 if { "$line" == "." } {
1089 return $useline
1092 if { [regsub "^\.\[+-\](\[0-9\]+)$" $line "\\1" num] && $useline != "" } {
1093 # Handle relative line specification, .+1 or .-1 etc.
1094 set num [expr $useline [string index $line 1] $num]
1095 return $num
1098 if { ! [regsub "^(\[a-zA-Z\]\[a-zA-Z0-9_\]*)$" $line "\\1" varname] } {
1099 return $line
1102 # Handle linenr variable defined by dg-line
1103 set org_varname $varname
1104 set varname "saved_linenr_$varname"
1105 eval global $varname
1107 # Generate used-but-not-defined error.
1108 eval set var_defined [info exists $varname]
1109 if { ! $var_defined } {
1110 if { "$useline" != "" } {
1111 error "dg-line var $org_varname used at line $useline, but not defined"
1112 } else {
1113 error "dg-line var $org_varname used, but not defined"
1115 return
1118 # Note that varname has been used.
1119 set varname_used "used_$varname"
1120 eval global $varname_used
1121 eval set $varname_used 1
1123 # Get line number from var and use it.
1124 eval set num \$$varname
1125 set line $num
1128 # Modify the regular expression saved by a DejaGnu message directive to
1129 # include a prefix and to force the expression to match a single line.
1130 # MSGPROC is the procedure to call.
1131 # MSGPREFIX is the prefix to prepend.
1132 # DGARGS is the original argument list.
1134 proc process-message { msgproc msgprefix dgargs } {
1135 upvar dg-messages dg-messages
1137 if { [llength $dgargs] == 5 } {
1138 set num [get-absolute-line [lindex $dgargs 0] [lindex $dgargs 4]]
1139 set dgargs [lreplace $dgargs 4 4 $num]
1142 # Process the dg- directive, including adding the regular expression
1143 # to the new message entry in dg-messages.
1144 set msgcnt [llength ${dg-messages}]
1145 eval $msgproc $dgargs
1147 # If the target expression wasn't satisfied there is no new message.
1148 if { [llength ${dg-messages}] == $msgcnt } {
1149 return;
1152 # Get the entry for the new message. Prepend the message prefix to
1153 # the regular expression and make it match a single line.
1154 set newentry [lindex ${dg-messages} end]
1155 set expmsg [lindex $newentry 2]
1157 set column ""
1158 # Handle column numbers from the specified expression (if there is
1159 # one) and set up the search expression that will be used by DejaGnu.
1160 if [regexp {^-:} $expmsg] {
1161 # The expected column is -, so shouldn't appear.
1162 set expmsg [string range $expmsg 2 end]
1163 } elseif [regexp {^[0-9]+:} $expmsg column] {
1164 # The expression in the directive included a column number.
1165 # Remove it from the original expression and move it
1166 # to the proper place in the search expression.
1167 set expmsg [string range $expmsg [string length $column] end]
1168 set column "$column "
1169 } elseif [string match "" [lindex $newentry 0]] {
1170 # The specified line number is 0; don't expect a column number.
1171 } else {
1172 # There is no column number in the search expression, but we
1173 # should expect one in the message itself.
1174 set column {[0-9]+: }
1176 set expmsg "$column$msgprefix\[^\n\]*$expmsg"
1177 set newentry [lreplace $newentry 2 2 $expmsg]
1179 set dg-messages [lreplace ${dg-messages} end end $newentry]
1180 verbose "process-message:\n${dg-messages}" 3
1183 # Look for messages that don't have standard prefixes.
1185 proc dg-message { args } {
1186 upvar dg-messages dg-messages
1187 process-message saved-dg-warning "" $args
1190 # Look for a location marker of the form
1191 # file:line:column:
1192 # with no extra text (e.g. a line-span separator).
1194 proc dg-locus { args } {
1195 upvar dg-messages dg-messages
1197 # Process the dg- directive, including adding the regular expression
1198 # to the new message entry in dg-messages.
1199 set msgcnt [llength ${dg-messages}]
1200 eval saved-dg-warning $args
1202 # If the target expression wasn't satisfied there is no new message.
1203 if { [llength ${dg-messages}] == $msgcnt } {
1204 return;
1207 # Get the entry for the new message. Prepend the message prefix to
1208 # the regular expression and make it match a single line.
1209 set newentry [lindex ${dg-messages} end]
1210 set expmsg [lindex $newentry 2]
1212 set newentry [lreplace $newentry 2 2 $expmsg]
1213 set dg-messages [lreplace ${dg-messages} end end $newentry]
1214 verbose "process-message:\n${dg-messages}" 3
1217 # Handle output from -fopt-info for MSG_OPTIMIZED_LOCATIONS:
1218 # a successful optimization.
1220 proc dg-optimized { args } {
1221 # Make this variable available here and to the saved proc.
1222 upvar dg-messages dg-messages
1224 process-message saved-dg-error "optimized: " "$args"
1227 # Handle output from -fopt-info for MSG_MISSED_OPTIMIZATION:
1228 # a missed optimization.
1230 proc dg-missed { args } {
1231 # Make this variable available here and to the saved proc.
1232 upvar dg-messages dg-messages
1234 process-message saved-dg-error "missed: " "$args"
1237 # Check the existence of a gdb in the path, and return true if there
1238 # is one.
1240 # Set env(GDB_FOR_GCC_TESTING) accordingly.
1242 proc gdb-exists { args } {
1243 if ![info exists ::env(GDB_FOR_GCC_TESTING)] {
1244 global GDB
1245 if ![info exists ::env(GDB_FOR_GCC_TESTING)] {
1246 if [info exists GDB] {
1247 setenv GDB_FOR_GCC_TESTING "$GDB"
1248 } else {
1249 setenv GDB_FOR_GCC_TESTING "[transform gdb]"
1253 if { [which $::env(GDB_FOR_GCC_TESTING)] != 0 } {
1254 return 1;
1256 return 0;
1259 # Helper function for scan-symbol and scan-symbol-not. It scans a symbol in
1260 # the final executable and return 1 if present, otherwise fail.
1262 # Argument 0 is the regexp to match.
1263 # Argument 1 handles expected failures and the like
1264 proc scan-symbol-common { scan_directive args } {
1265 global nm
1266 global base_dir
1268 # Access variable from gcc-dg-test-1 or lto-execute.
1269 upvar 3 output_file output_file
1271 if { [llength $args] >= 2 } {
1272 switch [dg-process-target [lindex $args 1]] {
1273 "S" { }
1274 "N" { return }
1275 "F" { setup_xfail "*-*-*" }
1276 "P" { }
1280 # Find nm like we find g++ in g++.exp.
1281 if ![info exists nm] {
1282 set nm [findfile $base_dir/../../../binutils/nm \
1283 $base_dir/../../../binutils/nm \
1284 [findfile $base_dir/../../nm $base_dir/../../nm \
1285 [findfile $base_dir/nm $base_dir/nm \
1286 [transform nm]]]]
1287 verbose -log "nm is $nm"
1290 set output_file "[glob -nocomplain $output_file]"
1291 if { $output_file == "" } {
1292 fail "$scan_directive $args: output file does not exist"
1293 return
1296 set fd [open "| $nm $output_file" r]
1297 set text [read $fd]
1298 close $fd
1300 if [regexp -- [lindex $args 0] $text] {
1301 return 1
1302 } else {
1303 return 0
1307 # Utility for scanning a symbol in the final executable, invoked via dg-final.
1308 # Call pass if pattern is present, otherwise fail.
1310 # Argument 0 is the regexp to match.
1311 # Argument 1 handles expected failures and the like
1312 proc scan-symbol { args } {
1313 set testcase [testname-for-summary]
1314 if { [scan-symbol-common "scan-symbol" $args]} {
1315 pass "$testcase scan-symbol $args"
1316 } else {
1317 fail "$testcase scan-symbol $args"
1321 # Utility for scanning a symbol in the final executable, invoked via dg-final.
1322 # Call pass if pattern is absent, otherwise fail.
1324 # Argument 0 is the regexp to match.
1325 # Argument 1 handles expected failures and the like
1326 proc scan-symbol-not { args } {
1327 set testcase [testname-for-summary]
1328 if { [scan-symbol-common "scan-symbol-not" $args]} {
1329 fail "$testcase scan-symbol-not $args"
1330 } else {
1331 pass "$testcase scan-symbol-not $args"
1335 set additional_prunes ""
1336 set dg_runtest_extra_prunes ""