OSDN Git Service

1a08b355aa2308ac140b47336d8c654ddcb0ced4
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / lib / gcc-dg.exp
1 #   Copyright (C) 1997, 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
2
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 2 of the License, or
6 # (at your option) any later version.
7 #
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.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17 load_lib dg.exp
18 load_lib file-format.exp
19 load_lib target-supports.exp
20 load_lib scanasm.exp
21 load_lib scantree.exp
22 load_lib prune.exp
23 load_lib libgloss.exp
24 load_lib target-libpath.exp
25
26 # We set LC_ALL and LANG to C so that we get the same error messages as expected.
27 setenv LC_ALL C
28 setenv LANG C
29
30 if ![info exists TORTURE_OPTIONS] {
31     # It is theoretically beneficial to group all of the O2/O3 options together,
32     # as in many cases the compiler will generate identical executables for
33     # all of them--and the c-torture testsuite will skip testing identical
34     # executables multiple times.
35     # Also note that -finline-functions is explicitly included in one of the
36     # items below, even though -O3 is also specified, because some ports may
37     # choose to disable inlining functions by default, even when optimizing.
38     set TORTURE_OPTIONS [list \
39         { -O0 } \
40         { -O1 } \
41         { -O2 } \
42         { -O3 -fomit-frame-pointer } \
43         { -O3 -fomit-frame-pointer -funroll-loops } \
44         { -O3 -fomit-frame-pointer -funroll-all-loops -finline-functions } \
45         { -O3 -g } \
46         { -Os } ]
47 }
48
49 global GCC_UNDER_TEST
50 if ![info exists GCC_UNDER_TEST] {
51     set GCC_UNDER_TEST "[find_gcc]"
52 }
53
54 global rootme
55 global ld_library_path
56 global orig_environment_saved
57
58 # This file may be sourced, so don't override environment settings
59 # that have been previously setup.
60 if { $orig_environment_saved == 0 } {
61     set ld_library_path "${rootme}"
62     set compiler [lindex $GCC_UNDER_TEST 0]
63     if { [is_remote host] == 0 && [which $compiler] != 0 } {
64         foreach i "[exec $compiler --print-multi-lib]" {
65             set mldir ""
66             regexp -- "\[a-z0-9=/\.-\]*;" $i mldir
67             set mldir [string trimright $mldir "\;@"]
68             if { "$mldir" == "." } {
69                 continue
70             }
71             if { [llength [glob -nocomplain ${rootme}/${mldir}/libgcc_s*.so.*]] >= 1 } {
72                 append ld_library_path ":${rootme}/${mldir}"
73             }
74         }
75     }
76     set_ld_library_path_env_vars
77 }
78
79 # Split TORTURE_OPTIONS into two choices: one for testcases with loops and
80 # one for testcases without loops.
81
82 set torture_with_loops $TORTURE_OPTIONS
83 set torture_without_loops ""
84 foreach option $TORTURE_OPTIONS {
85     if ![string match "*loop*" $option] {
86         lappend torture_without_loops $option
87     }
88 }
89
90 # Define gcc callbacks for dg.exp.
91
92 proc gcc-dg-test-1 { target_compile prog do_what extra_tool_flags } {
93     # Set up the compiler flags, based on what we're going to do.
94
95     set options [list]
96
97     # Tests should be able to use "dg-do repo".  However, the dg test
98     # driver checks the argument to dg-do against a list of acceptable
99     # options, and "repo" is not among them.  Therefore, we resort to
100     # this ugly approach.
101     if [string match "*-frepo*" $extra_tool_flags] then {
102         set do_what "repo"
103     }
104
105     switch $do_what {
106         "preprocess" {
107             set compile_type "preprocess"
108             set output_file "[file rootname [file tail $prog]].i"
109         }
110         "compile" {
111             set compile_type "assembly"
112             set output_file "[file rootname [file tail $prog]].s"
113         }
114         "assemble" {
115             set compile_type "object"
116             set output_file "[file rootname [file tail $prog]].o"
117         }
118         "precompile" {
119             set compile_type "precompiled_header"
120             set output_file "[file tail $prog].gch"
121         }
122         "link" {
123             set compile_type "executable"
124             set output_file "[file rootname [file tail $prog]].exe"
125             # The following line is needed for targets like the i960 where
126             # the default output file is b.out.  Sigh.
127         }
128         "repo" {
129             set compile_type "object"
130             set output_file "[file rootname [file tail $prog]].o"
131         }
132         "run" {
133             set compile_type "executable"
134             # FIXME: "./" is to cope with "." not being in $PATH.
135             # Should this be handled elsewhere?
136             # YES.
137             set output_file "./[file rootname [file tail $prog]].exe"
138             # This is the only place where we care if an executable was
139             # created or not.  If it was, dg.exp will try to run it.
140             remote_file build delete $output_file;
141         }
142         default {
143             perror "$do_what: not a valid dg-do keyword"
144             return ""
145         }
146     }
147
148     if { $extra_tool_flags != "" } {
149         lappend options "additional_flags=$extra_tool_flags"
150     }
151
152     set comp_output [$target_compile "$prog" "$output_file" "$compile_type" $options];
153
154     if { $do_what == "repo" } {
155         set object_file "$output_file"
156         set output_file "[file rootname [file tail $prog]].exe"
157         set comp_output \
158             [ concat $comp_output \
159                   [$target_compile "$object_file" "$output_file" \
160                        "executable" $options] ]
161     }
162
163     return [list $comp_output $output_file]
164 }
165
166 proc gcc-dg-test { prog do_what extra_tool_flags } {
167     return [gcc-dg-test-1 gcc_target_compile $prog $do_what $extra_tool_flags]
168 }
169
170 proc gcc-dg-prune { system text } {
171     global additional_prunes
172
173     set text [prune_gcc_output $text]
174
175     foreach p $additional_prunes {
176         if { [string length $p] > 0 } {
177             # Following regexp matches a complete line containing $p.
178             regsub -all "(^|\n)\[^\n\]*$p\[^\n\]*" $text "" text
179         }
180     }
181
182     # If we see "region xxx is full" then the testcase is too big for ram.
183     # This is tricky to deal with in a large testsuite like c-torture so
184     # deal with it here.  Just mark the testcase as unsupported.
185     if [regexp "(^|\n)\[^\n\]*: region \[^\n\]* is full" $text] {
186         # The format here is important.  See dg.exp.
187         return "::unsupported::memory full"
188     }
189
190     return $text
191 }
192
193 # Utility routines.
194
195 #
196 # search_for -- looks for a string match in a file
197 #
198 proc search_for { file pattern } {
199     set fd [open $file r]
200     while { [gets $fd cur_line]>=0 } {
201         if [string match "*$pattern*" $cur_line] then {
202             close $fd
203             return 1
204         }
205     }
206     close $fd
207     return 0
208 }
209
210 # Modified dg-runtest that can cycle through a list of optimization options
211 # as c-torture does.
212 proc gcc-dg-runtest { testcases default-extra-flags } {
213     global runtests
214
215     foreach test $testcases {
216         # If we're only testing specific files and this isn't one of
217         # them, skip it.
218         if ![runtest_file_p $runtests $test] {
219             continue
220         }
221
222         # Look for a loop within the source code - if we don't find one,
223         # don't pass -funroll[-all]-loops.
224         global torture_with_loops torture_without_loops
225         if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
226             set option_list $torture_with_loops
227         } else {
228             set option_list $torture_without_loops
229         }
230
231         set nshort [file tail [file dirname $test]]/[file tail $test]
232
233         foreach flags $option_list {
234             verbose "Testing $nshort, $flags" 1
235             dg-test $test $flags ${default-extra-flags}
236         }
237     }
238 }
239
240 proc gcc-dg-debug-runtest { target_compile trivial opt_opts testcases } {
241     global srcdir subdir
242
243     if ![info exists DEBUG_TORTURE_OPTIONS] {
244         set DEBUG_TORTURE_OPTIONS ""
245         foreach type {-gdwarf-2 -gstabs -gstabs+ -gxcoff -gxcoff+ -gcoff} {
246             set comp_output [$target_compile \
247                     "$srcdir/$subdir/$trivial" "trivial.S" assembly \
248                     "additional_flags=$type"]
249             if { ! [string match "*: target system does not support the * debug format*" \
250                     $comp_output] } {
251                 foreach level {1 "" 3} {
252                     lappend DEBUG_TORTURE_OPTIONS [list "${type}${level}"]
253                     foreach opt $opt_opts {
254                         lappend DEBUG_TORTURE_OPTIONS \
255                                 [list "${type}${level}" "$opt" ]
256                     }
257                 }
258             }
259         }
260     }
261
262     verbose -log "Using options $DEBUG_TORTURE_OPTIONS"
263
264     global runtests
265
266     foreach test $testcases {
267         # If we're only testing specific files and this isn't one of
268         # them, skip it.
269         if ![runtest_file_p $runtests $test] {
270             continue
271         }
272
273         set nshort [file tail [file dirname $test]]/[file tail $test]
274
275         foreach flags $DEBUG_TORTURE_OPTIONS {
276             set doit 1
277             if { [string match {*/debug-[126].c} "$nshort"] \
278                     && [string match "*1" [lindex "$flags" 0] ] } {
279                 set doit 0
280             }
281
282     # High optimization can remove the variable whose existence is tested.
283     # Dwarf debugging with commentary (-dA) preserves the symbol name in the
284     # assembler output, but stabs debugging does not.
285     # http://gcc.gnu.org/ml/gcc-regression/2003-04/msg00095.html
286             if { [string match {*/debug-[12].c} "$nshort"] \
287                     && [string match "*O*" "$flags"] \
288                     && ( [string match "*coff*" "$flags"] \
289                          || [string match "*stabs*" "$flags"] ) } {
290                 set doit 0
291             }
292
293             if { $doit } {
294                 verbose -log "Testing $nshort, $flags" 1
295                 dg-test $test $flags ""
296             }
297         }
298     }
299 }
300
301 # Skip a test and clear all xfail flags that might have been set for it
302 # by dg-xfail-if or for the xfail list in dg-do.  A procedure that calls
303 # this must specify "upvar dg-do-what dg-do-what".
304
305 proc skip_test_and_clear_xfail { } {
306     global compiler_conditional_xfail_data
307     upvar dg-do-what dg-do-what
308
309     set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
310     if [info exists compiler_conditional_xfail_data] {
311         unset compiler_conditional_xfail_data
312     }
313 }
314
315 # If this target does not support weak symbols, skip this test.
316
317 proc dg-require-weak { args } {
318     set weak_available [ check_weak_available ]
319     if { $weak_available == -1 } {
320         upvar name name
321         unresolved "$name"
322     }
323     if { $weak_available != 1 } {
324         upvar dg-do-what dg-do-what
325         skip_test_and_clear_xfail
326     }
327 }
328
329 # If this target does not support the "visibility" attribute, skip this
330 # test.
331
332 proc dg-require-visibility { args } {
333     set visibility_available [ check_visibility_available ]
334     if { $visibility_available == -1 } {
335         upvar name name
336         unresolved "$name"
337     }
338     if { $visibility_available != 1 } {
339         upvar dg-do-what dg-do-what
340         skip_test_and_clear_xfail
341     }
342 }
343
344 # If this target does not support the "alias" attribute, skip this
345 # test.
346
347 proc dg-require-alias { args } {
348     set alias_available [ check_alias_available ]
349     if { $alias_available == -1 } {
350         upvar name name
351         unresolved "$name"
352     }
353     if { $alias_available < 2 } {
354         upvar dg-do-what dg-do-what
355         skip_test_and_clear_xfail
356     }
357 }
358
359 # If this target's linker does not support the --gc-sections flag,
360 # skip this test.
361
362 proc dg-require-gc-sections { args } {
363     if { ![ check_gc_sections_available ] } {
364         upvar dg-do-what dg-do-what
365         skip_test_and_clear_xfail
366     }
367 }
368
369 # If this target does not support profiling, skip this test.
370
371 proc dg-require-profiling { args } {
372     if { ![ check_profiling_available ${args} ] } {
373         upvar dg-do-what dg-do-what
374         skip_test_and_clear_xfail
375     }
376 }
377
378 # If this target does not support DLL attributes skip this test.
379
380 proc dg-require-dll { args } {
381     global target_triplet
382     # As a special case, the mcore-*-elf supports these attributes.
383     # All Symbian OS targets also support these attributes.
384     if { [string match "mcore-*-elf" $target_triplet]
385          || [string match "*-*-symbianelf" $target_triplet]} {
386         return
387     }
388     # PE/COFF targets support dllimport/dllexport.
389     if { [gcc_target_object_format] == "pe" } {
390         return
391     }
392
393     upvar dg-do-what dg-do-what
394     skip_test_and_clear_xfail
395 }
396
397 proc dg-require-iconv { args } {
398     if { ![ check_iconv_available ${args} ] } {
399         upvar dg-do-what dg-do-what
400         skip_test_and_clear_xfail
401     }
402 }
403
404 # If this target does not support named sections skip this test.
405
406 proc dg-require-named-sections { args } {
407     if { ![ check_named_sections_available ] } {
408         upvar dg-do-what dg-do-what
409         skip_test_and_clear_xfail
410     }
411 }
412
413 # If the target does not match the required effective target, skip this test.
414
415 proc dg-require-effective-target { args } {
416     set args [lreplace $args 0 0]
417     if { ![is-effective-target [lindex $args 0]] } {
418         upvar dg-do-what dg-do-what
419         skip_test_and_clear_xfail
420     }
421 }
422
423 # Skip the test (report it as UNSUPPORTED) if the target list and
424 # included flags are matched and the excluded flags are not matched.
425 #
426 # The first argument is the line number of the dg-skip-if directive
427 # within the test file.   Remaining arguments are as for xfail lists:
428 # message { targets } { include } { exclude }
429 #
430 # This tests against multilib flags plus either the default flags for this
431 # group of tests or flags specified with a previous dg-options command.
432
433 proc dg-skip-if { args } {
434     set args [lreplace $args 0 0]
435
436     set selector "target [join [lindex $args 1]]"
437     if { [dg-process-target $selector] == "S" } {
438         # The target list matched; now check the flags.  The DejaGnu proc
439         # check_conditional_xfail will look at the options in compiler_flags,
440         # so set that up for this test based on flags we know about.  Start
441         # the list with a dummy tool name so the list will match "*" if
442         # there are no flags.
443
444         global compiler_flags
445         upvar dg-extra-tool-flags extra_tool_flags
446
447         set compiler_flags " toolname "
448         append compiler_flags $extra_tool_flags
449         set dest [target_info name]
450         if [board_info $dest exists multilib_flags] {
451             append compiler_flags "[board_info $dest multilib_flags] "
452         }
453
454         # The target list might be an effective-target keyword, so replace
455         # the original list with "*-*-*".
456         if [check_conditional_xfail [lreplace $args 1 1 "*-*-*"]] {
457             upvar dg-do-what dg-do-what
458             skip_test_and_clear_xfail
459         }
460
461         # Any value in this variable was left over from an earlier test.
462         set compiler_flags ""
463     }
464 }
465
466 # Prune any messages matching ARGS[1] (a regexp) from test output.
467 proc dg-prune-output { args } {
468     global additional_prunes
469
470     if { [llength $args] != 2 } {
471         error "[lindex $args 1]: need one argument"
472         return
473     }
474
475     lappend additional_prunes [lindex $args 1]
476 }
477
478 # Like check_conditional_xfail, but callable from a dg test.
479
480 proc dg-xfail-if { args } {
481     # Don't change anything if we're already skipping the test.
482     upvar dg-do-what dg-do-what
483     if { [lindex ${dg-do-what} 1] == "N" } {
484       return
485     }
486
487     set args [lreplace $args 0 0]
488     set selector "target [join [lindex $args 1]]"
489     if { [dg-process-target $selector] == "S" } {
490         global compiler_conditional_xfail_data
491         set compiler_conditional_xfail_data [lreplace $args 1 1 "*-*-*"]
492     }
493 }
494
495 # We need to make sure that additional_* are cleared out after every
496 # test.  It is not enough to clear them out *before* the next test run
497 # because gcc-target-compile gets run directly from some .exp files
498 # (outside of any test).  (Those uses should eventually be eliminated.)
499
500 # Because the DG framework doesn't provide a hook that is run at the
501 # end of a test, we must replace dg-test with a wrapper.
502
503 if { [info procs saved-dg-test] == [list] } {
504     rename dg-test saved-dg-test
505
506     proc dg-test { args } {
507         global additional_files
508         global additional_sources
509         global additional_prunes
510         global errorInfo
511
512         if { [ catch { eval saved-dg-test $args } errmsg ] } {
513             set saved_info $errorInfo
514             set additional_files ""
515             set additional_sources ""
516             set additional_prunes ""
517             error $errmsg $saved_info
518         }
519         set additional_files ""
520         set additional_sources ""
521         set additional_prunes ""
522     }
523 }
524
525 # Intercept the call to the DejaGnu version of dg-process-target to
526 # support use of an effective-target keyword in place of a list of
527 # target triplets to xfail or skip a test.
528 #
529 # selector is one of:
530 #    xfail target-triplet-1 ...
531 #    xfail effective-target-keyword
532 #    target target-triplet-1 ...
533 #    target effective-target-keyword
534 #
535 # For a target list the result is "S" if the target is selected, "N" otherwise.
536 # For an xfail list the result is "F" if the target is affected, "P" otherwise.
537
538 if { [info procs saved-dg-process-target] == [list] } {
539     rename dg-process-target saved-dg-process-target
540
541     proc dg-process-target { args } {
542         verbose "replacement dg-process-target" 2
543         
544         # Extract the 'what' keyword from the argument list.
545         set selector [string trim [lindex $args 0]]
546         if [regexp "^xfail " $selector] {
547             set what "xfail"
548         } elseif [regexp "^target " $selector] {
549             set what "target"
550         } else {
551             error "syntax error in target selector \"$selector\""
552         }
553
554         # Extract the rest of the list, which might be a keyword.
555         regsub "^${what}" $selector "" rest
556         set rest [string trim $rest]
557
558         if [is-effective-target-keyword $rest] {
559             # The selector is an effective target keyword.
560             if [is-effective-target $rest] {
561                 return [expr { $what == "xfail" ? "F" : "S" }]
562             } else {
563                 return [expr { $what == "xfail" ? "P" : "N" }]
564             }
565         }
566
567         # The selector is not an effective-target keyword, so process
568         # the list of target triplets.
569         return [saved-dg-process-target $selector]
570     }
571 }
572
573 set additional_prunes ""