libstdc++: Avoid conflicting declaration in eh_call.cc [PR112997]
[official-gcc.git] / libffi / testsuite / lib / libffi.exp
blobc1ae32d987d075835c0ff8355cb1077305d0a555
1 # Copyright (C) 2003, 2005, 2008, 2009, 2010, 2011, 2014, 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 this program; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
17 proc load_gcc_lib { filename } {
18 global srcdir loaded_libs
19 load_file $srcdir/../../gcc/testsuite/lib/$filename
20 set loaded_libs($filename) ""
23 load_lib dg.exp
24 load_lib libgloss.exp
25 load_gcc_lib target-supports.exp
26 load_gcc_lib target-supports-dg.exp
27 load_gcc_lib target-libpath.exp
28 load_gcc_lib wrapper.exp
30 proc check_effective_target_gccbug { } {
31 global has_gccbug
32 return $has_gccbug
35 # Return 1 if the target matches the effective target 'arg', 0 otherwise.
36 # This can be used with any check_* proc that takes no argument and
37 # returns only 1 or 0. It could be used with check_* procs that take
38 # arguments with keywords that pass particular arguments.
40 proc is-effective-target { arg } {
41 global et_index
42 set selected 0
43 if { ![info exists et_index] } {
44 # Initialize the effective target index that is used in some
45 # check_effective_target_* procs.
46 set et_index 0
48 if { [info procs check_effective_target_${arg}] != [list] } {
49 set selected [check_effective_target_${arg}]
50 } else {
51 error "unknown effective target keyword `$arg'"
53 verbose "is-effective-target: $arg $selected" 2
54 return $selected
57 proc is-effective-target-keyword { arg } {
58 if { [info procs check_effective_target_${arg}] != [list] } {
59 return 1
60 } else {
61 return 0
65 # Intercept the call to the DejaGnu version of dg-process-target to
66 # support use of an effective-target keyword in place of a list of
67 # target triplets to xfail or skip a test.
69 # The argument to dg-process-target is the keyword "target" or "xfail"
70 # followed by a selector:
71 # target-triplet-1 ...
72 # effective-target-keyword
73 # selector-expression
75 # For a target list the result is "S" if the target is selected, "N" otherwise.
76 # For an xfail list the result is "F" if the target is affected, "P" otherwise.
78 # In contexts that allow either "target" or "xfail" the argument can be
79 # target selector1 xfail selector2
80 # which returns "N" if selector1 is not selected, otherwise the result of
81 # "xfail selector2".
83 # A selector expression appears within curly braces and uses a single logical
84 # operator: !, &&, or ||. An operand is another selector expression, an
85 # effective-target keyword, or a list of target triplets within quotes or
86 # curly braces.
88 if { [info procs saved-dg-process-target] == [list] } {
89 rename dg-process-target saved-dg-process-target
91 # Evaluate an operand within a selector expression.
92 proc selector_opd { op } {
93 set selector "target"
94 lappend selector $op
95 set answer [ expr { [dg-process-target $selector] == "S" } ]
96 verbose "selector_opd: `$op' $answer" 2
97 return $answer
100 # Evaluate a target triplet list within a selector expression.
101 # Unlike other operands, this needs to be expanded from a list to
102 # the same string as "target".
103 proc selector_list { op } {
104 set selector "target [join $op]"
105 set answer [ expr { [dg-process-target $selector] == "S" } ]
106 verbose "selector_list: `$op' $answer" 2
107 return $answer
110 # Evaluate a selector expression.
111 proc selector_expression { exp } {
112 if { [llength $exp] == 2 } {
113 if [string match "!" [lindex $exp 0]] {
114 set op1 [lindex $exp 1]
115 set answer [expr { ! [selector_opd $op1] }]
116 } else {
117 # Assume it's a list of target triplets.
118 set answer [selector_list $exp]
120 } elseif { [llength $exp] == 3 } {
121 set op1 [lindex $exp 0]
122 set opr [lindex $exp 1]
123 set op2 [lindex $exp 2]
124 if [string match "&&" $opr] {
125 set answer [expr { [selector_opd $op1] && [selector_opd $op2] }]
126 } elseif [string match "||" $opr] {
127 set answer [expr { [selector_opd $op1] || [selector_opd $op2] }]
128 } else {
129 # Assume it's a list of target triplets.
130 set answer [selector_list $exp]
132 } else {
133 # Assume it's a list of target triplets.
134 set answer [selector_list $exp]
137 verbose "selector_expression: `$exp' $answer" 2
138 return $answer
141 # Evaluate "target selector" or "xfail selector".
143 proc dg-process-target-1 { args } {
144 verbose "dg-process-target-1: `$args'" 2
146 # Extract the 'what' keyword from the argument list.
147 set selector [string trim [lindex $args 0]]
148 if [regexp "^xfail " $selector] {
149 set what "xfail"
150 } elseif [regexp "^target " $selector] {
151 set what "target"
152 } else {
153 error "syntax error in target selector \"$selector\""
156 # Extract the rest of the list, which might be a keyword.
157 regsub "^${what}" $selector "" rest
158 set rest [string trim $rest]
160 if [is-effective-target-keyword $rest] {
161 # The selector is an effective target keyword.
162 if [is-effective-target $rest] {
163 return [expr { $what == "xfail" ? "F" : "S" }]
164 } else {
165 return [expr { $what == "xfail" ? "P" : "N" }]
169 if [string match "{*}" $rest] {
170 if [selector_expression [lindex $rest 0]] {
171 return [expr { $what == "xfail" ? "F" : "S" }]
172 } else {
173 return [expr { $what == "xfail" ? "P" : "N" }]
177 # The selector is not an effective-target keyword, so process
178 # the list of target triplets.
179 return [saved-dg-process-target $selector]
182 # Intercept calls to the DejaGnu function. In addition to
183 # processing "target selector" or "xfail selector", handle
184 # "target selector1 xfail selector2".
186 proc dg-process-target { args } {
187 verbose "replacement dg-process-target: `$args'" 2
189 set selector [string trim [lindex $args 0]]
191 # If the argument list contains both 'target' and 'xfail',
192 # process 'target' and, if that succeeds, process 'xfail'.
193 if [regexp "^target .* xfail .*" $selector] {
194 set xfail_index [string first "xfail" $selector]
195 set xfail_selector [string range $selector $xfail_index end]
196 set target_selector [string range $selector 0 [expr $xfail_index-1]]
197 set target_selector [string trim $target_selector]
198 if { [dg-process-target-1 $target_selector] == "N" } {
199 return "N"
201 return [dg-process-target-1 $xfail_selector]
204 return [dg-process-target-1 $selector]
208 # Define libffi callbacks for dg.exp.
210 proc libffi-dg-test-1 { target_compile prog do_what extra_tool_flags } {
212 # To get all \n in dg-output test strings to match printf output
213 # in a system that outputs it as \015\012 (i.e. not just \012), we
214 # need to change all \n into \r?\n. As there is no dejagnu flag
215 # or hook to do that, we simply change the text being tested.
216 # Unfortunately, we have to know that the variable is called
217 # dg-output-text and lives in the caller of libffi-dg-test, which
218 # is two calls up. Overriding proc dg-output would be longer and
219 # would necessarily have the same assumption.
220 upvar 2 dg-output-text output_match
222 if { [llength $output_match] > 1 } {
223 regsub -all "\n" [lindex $output_match 1] "\r?\n" x
224 set output_match [lreplace $output_match 1 1 $x]
227 # Set up the compiler flags, based on what we're going to do.
229 set options [list]
230 switch $do_what {
231 "compile" {
232 set compile_type "assembly"
233 set output_file "[file rootname [file tail $prog]].s"
235 "link" {
236 set compile_type "executable"
237 set output_file "[file rootname [file tail $prog]].exe"
238 # The following line is needed for targets like the i960 where
239 # the default output file is b.out. Sigh.
241 "run" {
242 set compile_type "executable"
243 # FIXME: "./" is to cope with "." not being in $PATH.
244 # Should this be handled elsewhere?
245 # YES.
246 set output_file "./[file rootname [file tail $prog]].exe"
247 # This is the only place where we care if an executable was
248 # created or not. If it was, dg.exp will try to run it.
249 remote_file build delete $output_file;
251 default {
252 perror "$do_what: not a valid dg-do keyword"
253 return ""
257 if { $extra_tool_flags != "" } {
258 lappend options "additional_flags=$extra_tool_flags"
261 set comp_output [libffi_target_compile "$prog" "$output_file" "$compile_type" $options];
264 return [list $comp_output $output_file]
268 proc libffi-dg-test { prog do_what extra_tool_flags } {
269 return [libffi-dg-test-1 target_compile $prog $do_what $extra_tool_flags]
272 proc libffi-dg-prune { target_triplet text } {
273 # We get this with some qemu emulated systems (eg. ppc64le-linux-gnu)
274 regsub -all "(^|\n)\[^\n\]*unable to perform all requested operations" $text "" text
275 return $text
278 proc libffi-init { args } {
279 global gluefile wrap_flags;
280 global srcdir
281 global blddirffi
282 global objdir
283 global blddircxx
284 global TOOL_OPTIONS
285 global tool
286 global libffi_include
287 global libffi_link_flags
288 global tool_root_dir
289 global ld_library_path
290 global compiler_vendor
292 set blddirffi [lookfor_file [get_multilibs] libffi]
293 verbose "libffi $blddirffi"
294 set blddircxx [lookfor_file [get_multilibs] libstdc++-v3]
295 verbose "libstdc++ $blddircxx"
297 set compiler_vendor "gnu"
299 if { [string match $compiler_vendor "gnu"] } {
300 set gccdir [lookfor_file $tool_root_dir gcc/libgcc.a]
301 if {$gccdir != ""} {
302 set gccdir [file dirname $gccdir]
304 verbose "gccdir $gccdir"
306 set ld_library_path "."
307 append ld_library_path ":${gccdir}"
309 set compiler "${gccdir}/xgcc"
310 if { [is_remote host] == 0 && [which $compiler] != 0 } {
311 foreach i "[exec $compiler --print-multi-lib]" {
312 set mldir ""
313 regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
314 set mldir [string trimright $mldir "\;@"]
315 if { "$mldir" == "." } {
316 continue
318 if { [llength [glob -nocomplain ${gccdir}/${mldir}/libgcc_s*.so.*]] >= 1 } {
319 append ld_library_path ":${gccdir}/${mldir}"
325 # add the library path for libffi.
326 append ld_library_path ":${blddirffi}/.libs"
327 # add the library path for libstdc++ as well.
328 append ld_library_path ":${blddircxx}/src/.libs"
330 verbose "ld_library_path: $ld_library_path"
332 # Point to the Libffi headers in libffi.
333 set libffi_include "${blddirffi}/include"
334 verbose "libffi_include $libffi_include"
336 set libffi_dir "${blddirffi}/.libs"
337 verbose "libffi_dir $libffi_dir"
338 if { $libffi_dir != "" } {
339 set libffi_dir [file dirname ${libffi_dir}]
340 if [istarget *-*-darwin*] {
341 set libffi_link_flags "-B${libffi_dir}/.libs"
342 lappend libffi_link_flags "-B${blddircxx}/src/.libs"
343 } else {
344 set libffi_link_flags "-L${libffi_dir}/.libs"
345 lappend libffi_link_flags "-L${blddircxx}/src/.libs"
349 set_ld_library_path_env_vars
350 libffi_maybe_build_wrapper "${objdir}/testglue.o"
353 proc libffi_exit { } {
354 global gluefile;
356 if [info exists gluefile] {
357 file_on_build delete $gluefile;
358 unset gluefile;
362 proc libffi_target_compile { source dest type options } {
363 global gluefile wrap_flags;
364 global srcdir
365 global blddirffi
366 global TOOL_OPTIONS
367 global libffi_link_flags
368 global libffi_include
369 global target_triplet
370 global compiler_vendor
372 if { [target_info needs_status_wrapper]!="" && [info exists gluefile] } {
373 lappend options "libs=${gluefile}"
374 lappend options "ldflags=$wrap_flags"
377 if { $blddirffi != "" } {
378 # If '--with-build-sysroot=[...]' was specified, use it for build-tree
379 # testing.
380 global SYSROOT_CFLAGS_FOR_TARGET
381 lappend options "additional_flags=${SYSROOT_CFLAGS_FOR_TARGET}"
384 # TOOL_OPTIONS must come first, so that it doesn't override testcase
385 # specific options.
386 if [info exists TOOL_OPTIONS] {
387 lappend options "additional_flags=$TOOL_OPTIONS"
390 # search for ffi_mips.h in srcdir, too
391 lappend options "additional_flags=-I${libffi_include} -I${srcdir}/../include -I${libffi_include}/.."
392 lappend options "additional_flags=${libffi_link_flags}"
394 # Darwin needs a stack execution allowed flag.
396 if { [istarget "*-*-darwin9*"] || [istarget "*-*-darwin1*"]
397 || [istarget "x86_64-*-darwin2*"] } {
398 lappend options "additional_flags=-Wl,-allow_stack_execute"
399 lappend options "additional_flags=-Wl,-search_paths_first"
402 # If you're building the compiler with --prefix set to a place
403 # where it's not yet installed, then the linker won't be able to
404 # find the libgcc used by libffi.dylib. We could pass the
405 # -dylib_file option, but that's complicated, and it's much easier
406 # to just make the linker find libgcc using -L options.
407 if { [string match "*-*-darwin*" $target_triplet] } {
408 lappend options "libs= -shared-libgcc"
411 if { [string match "*-*-openbsd*" $target_triplet] } {
412 lappend options "libs= -lpthread"
415 lappend options "libs= -lffi"
417 if { [string match "aarch64*-*-linux*" $target_triplet] } {
418 lappend options "libs= -lpthread"
421 if { [string match "*.cc" $source] } {
422 lappend options "ldflags=-shared-libgcc -lstdc++"
425 if { [string match "arc*-*-linux*" $target_triplet] } {
426 lappend options "libs= -lpthread"
429 verbose "options: $options"
430 return [target_compile $source $dest $type $options]
433 # TEST should be a preprocessor condition. Returns true if it holds.
434 proc libffi_feature_test { test } {
435 set src "ffitest[pid].c"
437 set f [open $src "w"]
438 puts $f "#include <ffi.h>"
439 puts $f $test
440 puts $f "/* OK */"
441 puts $f "#else"
442 puts $f "# error Failed $test"
443 puts $f "#endif"
444 close $f
446 set lines [libffi_target_compile $src /dev/null assembly ""]
447 file delete $src
449 return [string match "" $lines]
452 # Utility routines.
455 # search_for -- looks for a string match in a file
457 proc search_for { file pattern } {
458 set fd [open $file r]
459 while { [gets $fd cur_line]>=0 } {
460 if [string match "*$pattern*" $cur_line] then {
461 close $fd
462 return 1
465 close $fd
466 return 0
469 # Modified dg-runtest that can cycle through a list of optimization options
470 # as c-torture does.
471 proc libffi-dg-runtest { testcases default-extra-flags } {
472 global runtests
474 foreach test $testcases {
475 # If we're only testing specific files and this isn't one of
476 # them, skip it.
477 if ![runtest_file_p $runtests $test] {
478 continue
481 # Look for a loop within the source code - if we don't find one,
482 # don't pass -funroll[-all]-loops.
483 global torture_with_loops torture_without_loops
484 if [expr [search_for $test "for*("]+[search_for $test "while*("]] {
485 set option_list $torture_with_loops
486 } else {
487 set option_list $torture_without_loops
490 set nshort [file tail [file dirname $test]]/[file tail $test]
492 foreach flags $option_list {
493 verbose "Testing $nshort, $flags" 1
494 dg-test $test $flags ${default-extra-flags}
499 proc run-many-tests { testcases extra_flags } {
500 global compiler_vendor
501 global has_gccbug
502 global env
503 switch $compiler_vendor {
504 "clang" {
505 set common "-W -Wall"
506 if [info exists env(LIBFFI_TEST_OPTIMIZATION)] {
507 set optimizations [ list $env(LIBFFI_TEST_OPTIMIZATION) ]
508 } else {
509 set optimizations { "-O0" "-O2" }
512 "gnu" {
513 set common "-W -Wall -Wno-psabi"
514 if [info exists env(LIBFFI_TEST_OPTIMIZATION)] {
515 set optimizations [ list $env(LIBFFI_TEST_OPTIMIZATION) ]
516 } else {
517 set optimizations { "-O0" "-O2" }
520 default {
521 # Assume we are using the vendor compiler.
522 set common ""
523 if [info exists env(LIBFFI_TEST_OPTIMIZATION)] {
524 set optimizations [ list $env(LIBFFI_TEST_OPTIMIZATION) ]
525 } else {
526 set optimizations { "" }
531 info exists env(LD_LIBRARY_PATH)
533 set targetabis { "" }
534 if [string match $compiler_vendor "gnu"] {
535 if [libffi_feature_test "#ifdef __i386__"] {
536 set targetabis {
538 "-DABI_NUM=FFI_STDCALL -DABI_ATTR=__STDCALL__"
539 "-DABI_NUM=FFI_THISCALL -DABI_ATTR=__THISCALL__"
540 "-DABI_NUM=FFI_FASTCALL -DABI_ATTR=__FASTCALL__"
542 } elseif { [istarget "x86_64-*-*"] \
543 && [libffi_feature_test "#if !defined __ILP32__ \
544 && !defined __i386__"] } {
545 set targetabis {
547 "-DABI_NUM=FFI_GNUW64 -DABI_ATTR=__MSABI__"
552 set common [ concat $common $extra_flags ]
553 foreach test $testcases {
554 set testname [file tail $test]
555 if [search_for $test "ABI_NUM"] {
556 set abis $targetabis
557 } else {
558 set abis { "" }
560 foreach opt $optimizations {
561 foreach abi $abis {
562 set options [concat $common $opt $abi]
563 set has_gccbug false;
564 if { [string match $compiler_vendor "gnu"] \
565 && [string match "*MSABI*" $abi] \
566 && ( ( [string match "*DGTEST=57 *" $common] \
567 && [string match "*call.c*" $testname] ) \
568 || ( [string match "*DGTEST=54 *" $common] \
569 && [string match "*callback*" $testname] ) \
570 || [string match "*DGTEST=55 *" $common] \
571 || [string match "*DGTEST=56 *" $common] ) } then {
572 if [libffi_feature_test "#if (__GNUC__ < 9) || ((__GNUC__ == 9) && (__GNUC_MINOR__ < 3))"] {
573 set has_gccbug true;
576 verbose "Testing $testname, $options" 1
577 verbose "has_gccbug = $has_gccbug" 1
578 dg-test $test $options ""
584 # Like check_conditional_xfail, but callable from a dg test.
586 proc dg-xfail-if { args } {
587 set args [lreplace $args 0 0]
588 set selector "target [join [lindex $args 1]]"
589 if { [dg-process-target $selector] == "S" } {
590 global compiler_conditional_xfail_data
591 set compiler_conditional_xfail_data $args
595 proc check-flags { args } {
597 # The args are within another list; pull them out.
598 set args [lindex $args 0]
600 # The next two arguments are optional. If they were not specified,
601 # use the defaults.
602 if { [llength $args] == 2 } {
603 lappend $args [list "*"]
605 if { [llength $args] == 3 } {
606 lappend $args [list ""]
609 # If the option strings are the defaults, or the same as the
610 # defaults, there is no need to call check_conditional_xfail to
611 # compare them to the actual options.
612 if { [string compare [lindex $args 2] "*"] == 0
613 && [string compare [lindex $args 3] "" ] == 0 } {
614 set result 1
615 } else {
616 # The target list might be an effective-target keyword, so replace
617 # the original list with "*-*-*", since we already know it matches.
618 set result [check_conditional_xfail [lreplace $args 1 1 "*-*-*"]]
621 return $result
624 proc dg-skip-if { args } {
625 # Verify the number of arguments. The last two are optional.
626 set args [lreplace $args 0 0]
627 if { [llength $args] < 2 || [llength $args] > 4 } {
628 error "dg-skip-if 2: need 2, 3, or 4 arguments"
631 # Don't bother if we're already skipping the test.
632 upvar dg-do-what dg-do-what
633 if { [lindex ${dg-do-what} 1] == "N" } {
634 return
637 set selector [list target [lindex $args 1]]
638 if { [dg-process-target $selector] == "S" } {
639 if [check-flags $args] {
640 upvar dg-do-what dg-do-what
641 set dg-do-what [list [lindex ${dg-do-what} 0] "N" "P"]
646 # We need to make sure that additional_files and additional_sources
647 # are both cleared out after every test. It is not enough to clear
648 # them out *before* the next test run because gcc-target-compile gets
649 # run directly from some .exp files (outside of any test). (Those
650 # uses should eventually be eliminated.)
652 # Because the DG framework doesn't provide a hook that is run at the
653 # end of a test, we must replace dg-test with a wrapper.
655 if { [info procs saved-dg-test] == [list] } {
656 rename dg-test saved-dg-test
658 proc dg-test { args } {
659 global additional_files
660 global additional_sources
661 global errorInfo
663 if { [ catch { eval saved-dg-test $args } errmsg ] } {
664 set saved_info $errorInfo
665 set additional_files ""
666 set additional_sources ""
667 error $errmsg $saved_info
669 set additional_files ""
670 set additional_sources ""
674 # Local Variables:
675 # tcl-indent-level:4
676 # End: