2018-03-08 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / testsuite / lib / c-torture.exp
blob3fb600942d507050f47c8b532a8ed1024ed4412b
1 # Copyright (C) 1992-2018 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 # This file was written by Rob Savoye. (rob@cygnus.com)
19 load_lib target-supports.exp
20 load_lib file-format.exp
21 load_lib target-libpath.exp
22 load_lib target-utils.exp
24 global GCC_UNDER_TEST
25 if ![info exists GCC_UNDER_TEST] {
26 set GCC_UNDER_TEST "[find_gcc]"
29 global orig_environment_saved
31 # This file may be sourced, so don't override environment settings
32 # that have been previously setup.
33 if { $orig_environment_saved == 0 } {
34 append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
35 set_ld_library_path_env_vars
38 # The default option list can be overridden by
39 # TORTURE_OPTIONS="{ list1 } ... { listN }"
41 set LTO_TORTURE_OPTIONS ""
42 if [info exists TORTURE_OPTIONS] {
43 set C_TORTURE_OPTIONS $TORTURE_OPTIONS
44 } else {
45 # It is theoretically beneficial to group all of the O2/O3 options together,
46 # as in many cases the compiler will generate identical executables for
47 # all of them--and the c-torture testsuite will skip testing identical
48 # executables multiple times.
49 # Also note that -finline-functions is explicitly included in one of the
50 # items below, even though -O3 is also specified, because some ports may
51 # choose to disable inlining functions by default, even when optimizing.
52 set C_TORTURE_OPTIONS [list \
53 { -O0 } \
54 { -O1 } \
55 { -O2 } \
56 { -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions } \
57 { -O3 -g } \
58 { -Os } \
59 { -Og -g } ]
61 if [check_effective_target_lto] {
62 # When having plugin test both slim and fat LTO and plugin/nonplugin
63 # path.
64 if [check_linker_plugin_available] {
65 set LTO_TORTURE_OPTIONS [list \
66 { -O2 -flto -fno-use-linker-plugin -flto-partition=none } \
67 { -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects }
69 } else {
70 set LTO_TORTURE_OPTIONS [list \
71 { -O2 -flto -flto-partition=none } \
72 { -O2 -flto }
78 if [info exists ADDITIONAL_TORTURE_OPTIONS] {
79 set C_TORTURE_OPTIONS \
80 [concat $C_TORTURE_OPTIONS $ADDITIONAL_TORTURE_OPTIONS]
84 # c-torture-compile -- runs the Tege C-torture test
86 # SRC is the full pathname of the testcase.
87 # OPTION is the specific compiler flag we're testing (eg: -O2).
89 proc c-torture-compile { src option } {
90 global output
91 global srcdir tmpdir
92 global host_triplet
94 set output "$tmpdir/[file tail [file rootname $src]].o"
96 regsub "(?q)$srcdir/" $src "" testcase
97 # If we couldn't rip $srcdir out of `src' then just do the best we can.
98 # The point is to reduce the unnecessary noise in the logs. Don't strip
99 # out too much because different testcases with the same name can confuse
100 # `test-tool'.
101 if [string match "/*" $testcase] {
102 set testcase "[file tail [file dirname $src]]/[file tail $src]"
105 verbose "Testing $testcase, $option" 1
107 # Run the compiler and analyze the results.
108 set options ""
109 lappend options "additional_flags=-w $option"
111 set comp_output [gcc_target_compile "$src" "$output" object $options]
112 gcc_check_compile $testcase $option $output $comp_output
113 file_on_host delete $output
117 # c-torture-execute -- utility to compile and execute a testcase
119 # SOURCES is a list of full pathnames to the test source files.
120 # The first filename in this list forms the "testcase".
122 # If the testcase has an associated .x file, we source that to run the
123 # test instead. We use .x so that we don't lengthen the existing filename
124 # to more than 14 chars.
126 proc c-torture-execute { sources args } {
127 global tmpdir tool srcdir output compiler_conditional_xfail_data
129 # Use the first source filename given as the filename under test.
130 set src [lindex $sources 0]
132 if { [llength $args] > 0 } {
133 set additional_flags [lindex $args 0]
134 } else {
135 set additional_flags ""
137 # Check for alternate driver.
138 if [file exists [file rootname $src].x] {
139 verbose "Using alternate driver [file rootname [file tail $src]].x" 2
140 set done_p 0
141 catch "set done_p \[source [file rootname $src].x\]"
142 if { $done_p } {
143 return
147 # Look for a loop within the source code - if we don't find one,
148 # don't pass -funroll[-all]-loops.
149 global torture_with_loops torture_without_loops
150 if [expr [search_for $src "for*("]+[search_for $src "while*("]] then {
151 set option_list $torture_with_loops
152 } else {
153 set option_list $torture_without_loops
156 set executable $tmpdir/[file tail [file rootname $src].x]
158 regsub "(?q)$srcdir/" $src "" testcase
159 # If we couldn't rip $srcdir out of `src' then just do the best we can.
160 # The point is to reduce the unnecessary noise in the logs. Don't strip
161 # out too much because different testcases with the same name can confuse
162 # `test-tool'.
163 if [string match "/*" $testcase] {
164 set testcase "[file tail [file dirname $src]]/[file tail $src]"
167 set count 0
168 set oldstatus "foo"
169 foreach option $option_list {
170 if { $count > 0 } {
171 set oldexec $execname
173 set execname "${executable}${count}"
174 incr count
176 # torture_{compile,execute}_xfail are set by the .x script
177 # (if present)
178 if [info exists torture_compile_xfail] {
179 setup_xfail $torture_compile_xfail
182 # torture_execute_before_{compile,execute} can be set by the .x script
183 # (if present)
184 if [info exists torture_eval_before_compile] {
185 set ignore_me [eval $torture_eval_before_compile]
188 file_on_host delete $execname
189 verbose "Testing $testcase, $option" 1
191 set options ""
192 lappend options "additional_flags=-w $option"
193 if { $additional_flags != "" } {
194 lappend options "additional_flags=$additional_flags"
196 set comp_output [gcc_target_compile "$sources" "${execname}" executable $options]
198 if ![gcc_check_compile "$testcase compilation" $option $execname $comp_output] {
199 unresolved "$testcase execution, $option"
200 file_on_host delete $execname
201 continue
204 # See if this source file uses "long long" types, if it does, and
205 # no_long_long is set, skip execution of the test.
206 if [target_info exists no_long_long] then {
207 if [expr [search_for $src "long long"]] then {
208 unsupported "$testcase execution, $option"
209 continue
213 if [info exists torture_execute_xfail] {
214 setup_xfail $torture_execute_xfail
217 if [info exists torture_eval_before_execute] {
218 set ignore_me [eval $torture_eval_before_execute]
222 # Sometimes we end up creating identical executables for two
223 # consecutive sets of different of compiler options.
225 # In such cases we know the result of this test will be identical
226 # to the result of the last test.
228 # So in cases where the time to load and run/simulate the test
229 # is relatively high, compare the two binaries and avoid rerunning
230 # tests if the executables are identical.
232 # Do not do this for native testing since the cost to load/execute
233 # the test is fairly small and the comparison step actually slows
234 # the entire process down because it usually does not "hit".
235 set skip 0
236 if { ![isnative] && [info exists oldexec] } {
237 if { [file_on_host cmp $oldexec $execname] == 0 } {
238 set skip 1
241 if { $skip == 0 } {
242 set result [gcc_load "$execname" "" ""]
243 set status [lindex $result 0]
244 set output [lindex $result 1]
246 if { $oldstatus == "pass" } {
247 file_on_host delete $oldexec
249 $status "$testcase execution, $option"
250 set oldstatus $status
252 if [info exists status] {
253 if { $status == "pass" } {
254 file_on_host delete $execname
260 # search_for -- looks for a string match in a file
262 proc search_for { file pattern } {
263 set fd [open $file r]
264 while { [gets $fd cur_line]>=0 } {
265 if [string match "*$pattern*" $cur_line] then {
266 close $fd
267 return 1
270 close $fd
271 return 0
275 # c-torture -- the c-torture testcase source file processor
277 # This runs compilation only tests (no execute tests).
278 # SRC is the full pathname of the testcase, or just a file name in which case
279 # we prepend $srcdir/$subdir.
281 # If the testcase has an associated .x file, we source that to run the
282 # test instead. We use .x so that we don't lengthen the existing filename
283 # to more than 14 chars.
285 proc c-torture { args } {
286 global srcdir subdir compiler_conditional_xfail_data
288 set src [lindex $args 0]
289 if { [llength $args] > 1 } {
290 set options [lindex $args 1]
291 } else {
292 set options ""
295 # Prepend $srdir/$subdir if missing.
296 if ![string match "*/*" $src] {
297 set src "$srcdir/$subdir/$src"
300 # Check for alternate driver.
301 if [file exists [file rootname $src].x] {
302 verbose "Using alternate driver [file rootname [file tail $src]].x" 2
303 set done_p 0
304 catch "set done_p \[source [file rootname $src].x\]"
305 if { $done_p } {
306 return
310 # Look for a loop within the source code - if we don't find one,
311 # don't pass -funroll[-all]-loops.
312 global torture_with_loops torture_without_loops
313 if [expr [search_for $src "for*("]+[search_for $src "while*("]] then {
314 set option_list $torture_with_loops
315 } else {
316 set option_list $torture_without_loops
319 # loop through all the options
320 foreach option $option_list {
321 # torture_compile_xfail is set by the .x script (if present)
322 if [info exists torture_compile_xfail] {
323 setup_xfail $torture_compile_xfail
326 # torture_execute_before_compile is set by the .x script (if present)
327 if [info exists torture_eval_before_compile] {
328 set ignore_me [eval $torture_eval_before_compile]
331 c-torture-compile $src "$option $options"