Debian Codegraph @

gcc/testsuite/lib/gcc-defs.exp

raw
1 # Copyright (C) 2001-2025 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 3 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 GCC; see the file COPYING3. If not see
15 # <http://www.gnu.org/licenses/>.
16
17 load_lib target-libpath.exp
18
19 load_lib wrapper.exp
20
21 load_lib target-utils.exp
22
23 #
24 # ${tool}_check_compile -- Reports and returns pass/fail for a compilation
25 #
26
27 proc ${tool}_check_compile {testcase option objname gcc_output} {
28 global tool
29 set fatal_signal "*cc: Internal compiler error: program*got fatal signal"
30
31 if [string match "$fatal_signal 6" $gcc_output] then {
32 ${tool}_fail $testcase "Got Signal 6, $option"
33 return 0
34 }
35
36 if [string match "$fatal_signal 11" $gcc_output] then {
37 ${tool}_fail $testcase "Got Signal 11, $option"
38 return 0
39 }
40
41 if [regexp -line -- "internal compiler error.*" $gcc_output ice] then {
42 ${tool}_fail $testcase "$option ($ice)"
43 return 0
44 }
45
46 # We shouldn't get these because of -w, but just in case.
47 if [string match "*cc:*warning:*" $gcc_output] then {
48 warning "$testcase: (with warnings) $option"
49 send_log "$gcc_output\n"
50 unresolved "$testcase, $option"
51 return 0
52 }
53
54 set gcc_output [prune_warnings $gcc_output]
55
56 if { [info proc ${tool}-dg-prune] != "" } {
57 global target_triplet
58 set gcc_output [${tool}-dg-prune $target_triplet $gcc_output]
59 if [string match "*::unsupported::*" $gcc_output] then {
60 regsub -- "::unsupported::" $gcc_output "" gcc_output
61 unsupported "$testcase: $gcc_output"
62 return 0
63 }
64 } else {
65 set unsupported_message [${tool}_check_unsupported_p $gcc_output]
66 if { $unsupported_message != "" } {
67 unsupported "$testcase: $unsupported_message"
68 return 0
69 }
70 }
71
72 # remove any leftover LF/CR to make sure any output is legit
73 regsub -all -- "\[\r\n\]*" $gcc_output "" gcc_output
74
75 # If any message remains, we fail.
76 if ![string match "" $gcc_output] then {
77 ${tool}_fail $testcase $option
78 return 0
79 }
80
81 # fail if the desired object file doesn't exist.
82 # FIXME: there's no way of checking for existence on a remote host.
83 if {$objname != "" && ![is3way] && ![file exists $objname]} {
84 ${tool}_fail $testcase $option
85 return 0
86 }
87
88 ${tool}_pass $testcase $option
89 return 1
90 }
91
92 #
93 # ${tool}_pass -- utility to record a testcase passed
94 #
95
96 proc ${tool}_pass { testcase cflags } {
97 if { "$cflags" == "" } {
98 pass "$testcase"
99 } else {
100 pass "$testcase, $cflags"
101 }
102 }
103
104 #
105 # ${tool}_fail -- utility to record a testcase failed
106 #
107
108 proc ${tool}_fail { testcase cflags } {
109 if { "$cflags" == "" } {
110 fail "$testcase"
111 } else {
112 fail "$testcase, $cflags"
113 }
114 }
115
116 #
117 # ${tool}_finish -- called at the end of every script that calls ${tool}_init
118 #
119 # Hide all quirks of the testing environment from the testsuites. Also
120 # undo anything that ${tool}_init did that needs undoing.
121 #
122
123 proc ${tool}_finish { } {
124 # The testing harness apparently requires this.
125 global errorInfo
126
127 if [info exists errorInfo] then {
128 unset errorInfo
129 }
130
131 # Might as well reset these (keeps our caller from wondering whether
132 # s/he has to or not).
133 global prms_id bug_id
134 set prms_id 0
135 set bug_id 0
136 }
137
138 #
139 # ${tool}_exit -- Does final cleanup when testing is complete
140 #
141
142 proc ${tool}_exit { } {
143 global gluefile
144
145 if [info exists gluefile] {
146 file_on_build delete $gluefile
147 unset gluefile
148 }
149 }
150
151 #
152 # runtest_file_p -- Provide a definition for older dejagnu releases
153 # and assume the old syntax: foo1.exp bar1.c foo2.exp bar2.c.
154 # (delete after next dejagnu release).
155 #
156
157 if { [info procs runtest_file_p] == "" } then {
158 proc runtest_file_p { runtests testcase } {
159 if { $runtests != "" && [regexp "\[.\]\[cC\]" $runtests] } then {
160 if { [lsearch $runtests [file tail $testcase]] >= 0 } then {
161 return 1
162 } else {
163 return 0
164 }
165 }
166 return 1
167 }
168 }
169
170 if { [info exists env(GCC_RUNTEST_PARALLELIZE_DIR)] \
171 && [info procs runtest_file_p] != [list] \
172 && [info procs gcc_parallelize_saved_runtest_file_p] == [list] } then {
173 global gcc_runtest_parallelize_counter
174 global gcc_runtest_parallelize_counter_minor
175 global gcc_runtest_parallelize_limit_minor
176 global gcc_runtest_parallelize_enable
177 global gcc_runtest_parallelize_dir
178 global gcc_runtest_parallelize_last
179
180 # GCC testsuite is parallelised by starting N runtest processes -- each
181 # with its own test directory. These N runtest processes ALL go through
182 # the relevant .exp and ALL attempt to run every test. And they go
183 # through the tests the same order -- this is important, and if there is
184 # a bug that causes different runtest processes to enumerate the tests
185 # differently, then things will break and some tests will be skipped, while
186 # others will be ran several times.
187 # So, just before a runtest processes runs a specific test it asks
188 # "runtest_file_p" routine whether a particular test is part of
189 # the requested testsuite. We override this function so that it
190 # returns "yes" to the first-arrived runtest process, and "no" to all
191 # subsequent runtest processes -- this is implemented by creating a marker
192 # file, which persist till the end of the test run. We optimize this
193 # a bit by batching 10 tests and using a single marker file for the batch.
194 #
195 # Note that the runtest processes all race each other to get to the next
196 # test batch. This means that batch allocation between testsuite runs
197 # is very likely to change.
198 #
199 # To confirm or deny suspicion that tests are skipped or executed
200 # multiple times due to runtest processes enumerating tests differently ...
201 # 1. Uncomment the three below "verbose -log gcc_parallel_test_run_p ..."
202 # debug print-outs.
203 # 2. Run the testsuite with "-v" added to RUNTESTFLAGS
204 # 3. Extract debug print-outs with something like:
205 # for i in $(find -name "*.log.sep"); do
206 # grep gcc_parallel_test_run_p $i \
207 # | sed -e "s/\([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\)/\3 \2/" \
208 # | sed -e "s#\(/testsuite/[a-z+]*\)[0-9]*/#\1N/#" > $i.order
209 # done
210 # 4. Compare debug print-outs produced by individual runtest processes:
211 # find -name "*.log.sep.order" | xargs md5sum | sort
212 # 5. Check that MD5 hashes of all .order files of the same testsuite match
213 # and investigate if they don't.
214 set gcc_runtest_parallelize_counter 0
215 set gcc_runtest_parallelize_counter_minor 0
216 set gcc_runtest_parallelize_limit_minor 10
217 set gcc_runtest_parallelize_enable 1
218 set gcc_runtest_parallelize_dir [getenv GCC_RUNTEST_PARALLELIZE_DIR]
219 set gcc_runtest_parallelize_last 0
220
221 proc gcc_parallel_test_run_p { testcase } {
222 global gcc_runtest_parallelize_counter
223 global gcc_runtest_parallelize_counter_minor
224 global gcc_runtest_parallelize_limit_minor
225 global gcc_runtest_parallelize_enable
226 global gcc_runtest_parallelize_dir
227 global gcc_runtest_parallelize_last
228
229 if { $gcc_runtest_parallelize_enable == 0 } {
230 return 1
231 }
232
233 # Only test the filesystem every 10th iteration
234 if { $gcc_runtest_parallelize_counter_minor >= $gcc_runtest_parallelize_limit_minor } {
235 set gcc_runtest_parallelize_counter_minor 0
236 }
237 incr gcc_runtest_parallelize_counter_minor
238 if { $gcc_runtest_parallelize_counter_minor != 1 } {
239 #verbose -log "gcc_parallel_test_run_p $testcase $gcc_runtest_parallelize_counter $gcc_runtest_parallelize_last"
240 return $gcc_runtest_parallelize_last
241 }
242
243 set path $gcc_runtest_parallelize_dir/$gcc_runtest_parallelize_counter
244
245 if {![catch {open $path {RDWR CREAT EXCL} 0600} fd]} {
246 close $fd
247 set gcc_runtest_parallelize_last 1
248 #verbose -log "gcc_parallel_test_run_p $testcase $gcc_runtest_parallelize_counter 1"
249 incr gcc_runtest_parallelize_counter
250 return 1
251 }
252 set gcc_runtest_parallelize_last 0
253 #verbose -log "gcc_parallel_test_run_p $testcase $gcc_runtest_parallelize_counter 0"
254 incr gcc_runtest_parallelize_counter
255 return 0
256 }
257
258 proc gcc_parallel_test_enable { val } {
259 global gcc_runtest_parallelize_enable
260 set gcc_runtest_parallelize_enable $val
261 }
262
263 rename runtest_file_p gcc_parallelize_saved_runtest_file_p
264 proc runtest_file_p { runtests testcase } {
265 if ![gcc_parallelize_saved_runtest_file_p $runtests $testcase] {
266 return 0
267 }
268 return [gcc_parallel_test_run_p $testcase]
269 }
270
271 } else {
272
273 proc gcc_parallel_test_run_p { testcase } {
274 return 1
275 }
276
277 proc gcc_parallel_test_enable { val } {
278 }
279
280 }
281
282 # Like dg-options, but adds to the default options rather than replacing them.
283
284 proc dg-additional-options { args } {
285 upvar dg-extra-tool-flags extra-tool-flags
286 global srcdir
287
288 if { [llength $args] > 3 } {
289 error "[lindex $args 0]: too many arguments"
290 return
291 }
292
293 if { [llength $args] >= 3 } {
294 switch [dg-process-target [lindex $args 2]] {
295 "S" { eval lappend extra-tool-flags [lindex $args 1] }
296 "N" { }
297 "F" { error "[lindex $args 0]: `xfail' not allowed here" }
298 "P" { error "[lindex $args 0]: `xfail' not allowed here" }
299 }
300 } else {
301 eval lappend extra-tool-flags [lindex $args 1]
302 }
303 }
304
305 # Record additional sources files that must be compiled along with the
306 # main source file.
307
308 set additional_sources ""
309 set additional_sources_omit_on_compile ""
310 set additional_sources_used ""
311
312 proc dg-additional-sources { args } {
313 global additional_sources
314 global additional_sources_omit_on_compile
315
316 if { [llength $args] > 3 } {
317 error "[lindex $args 0]: too many arguments"
318 return
319 }
320
321 set target [lindex $args 2]
322 if { [llength $args] >= 3 && [lindex $target 0] == "linkonly" } {
323 append additional_sources_omit_on_compile " [lindex $args 1]"
324 set target [lreplace $target 0 1]
325 }
326
327 if { [llength $args] >= 3 && $target != ""} {
328 switch [dg-process-target $target] {
329 "S" { append additional_sources " [lindex $args 1]" }
330 "N" { }
331 "F" { error "[lindex $args 0]: `xfail' not allowed here" }
332 "P" { error "[lindex $args 0]: `xfail' not allowed here" }
333 }
334 } else {
335 append additional_sources " [lindex $args 1]"
336 }
337 }
338
339 # Record additional files -- other than source files -- that must be
340 # present on the system where the compiler runs.
341
342 set additional_files ""
343
344 proc dg-additional-files { args } {
345 global additional_files
346 set additional_files [lindex $args 1]
347 }
348
349 set gcc_adjusted_linker_flags 0
350
351 # Add -Wl, before any file names in $opts. Return the modified list.
352
353 proc gcc_adjust_linker_flags_list { args } {
354 set opts [lindex $args 0]
355 set nopts {}
356 set skip ""
357 foreach opt [split $opts " "] {
358 if { $opt == "" } then {
359 continue
360 } elseif { $skip != "" } then {
361 set skip ""
362 } elseif { $opt == "-Xlinker" || $opt == "-T" } then {
363 set skip $opt
364 } elseif { ![string match "-*" $opt] \
365 && [file isfile $opt] } {
366 set opt "-Wl,$opt"
367 }
368 lappend nopts $opt
369 }
370 return $nopts
371 }
372
373 # Add -Wl, before any file names in the target board's ldflags, libs,
374 # and ldscript, as well as in global testglue and wrap_flags, so that
375 # default object files or libraries do not change the names of gcc
376 # auxiliary outputs.
377
378 proc gcc_adjust_linker_flags {} {
379 global gcc_adjusted_linker_flags
380 if {$gcc_adjusted_linker_flags} {
381 return
382 }
383 set gcc_adjusted_linker_flags 1
384
385 if {![is_remote host]} {
386 set dest [target_info name]
387 foreach i { ldflags libs ldscript } {
388 if {[board_info $dest exists $i]} {
389 set opts [board_info $dest $i]
390 set nopts [gcc_adjust_linker_flags_list $opts]
391 if { $nopts != $opts } {
392 unset_currtarget_info $i
393 set_currtarget_info $i "$nopts"
394 }
395 }
396 }
397 foreach i { gluefile wrap_flags } {
398 global $i
399 if {[info exists $i]} {
400 set opts [set $i]
401 set nopts [gcc_adjust_linker_flags_list $opts]
402 if { $nopts != $opts } {
403 set $i $nopts
404 }
405 }
406 }
407 }
408 }
409
410 # Return an updated version of OPTIONS that mentions any additional
411 # source files registered with dg-additional-sources. SOURCE is the
412 # name of the test case. If DEST is given and TYPE does not require
413 # linking, additional sources are noted as unsupported rather than
414 # added, because the compiler rejects a single output for multiple
415 # sources.
416
417 proc dg-additional-files-options { options source dest type } {
418 gcc_adjust_linker_flags
419
420 global additional_sources
421 global additional_sources_omit_on_compile
422 global additional_sources_used
423 global additional_files
424 set to_download [list]
425 if { $additional_sources_omit_on_compile != "" \
426 && $additional_sources != "" \
427 && $type != "executable" && $dest != "" } then {
428 set linkonly ""
429 foreach s $additional_sources {
430 foreach s2 $additional_sources_omit_on_compile {
431 if { $s == $s2 } {
432 unsupported "$s: additional-source will not be used to build $dest"
433 set s ""
434 break
435 }
436 }
437 if { $s != "" } {
438 append linkonly " $s"
439 }
440 }
441 set additional_sources "$linkonly"
442 }
443 if { $additional_sources == "" } then {
444 } else {
445 if [is_remote host] {
446 lappend options "additional_flags=$additional_sources"
447 }
448 regsub -all {^ *| *} $additional_sources " [file dirname $source]/" additional_sources
449 if ![is_remote host] {
450 lappend options "additional_flags=$additional_sources"
451 }
452 set to_download [concat $to_download $additional_sources]
453 set additional_sources_used "$additional_sources"
454 set additional_sources ""
455 # This option restores naming of aux and dump output files
456 # after input files when multiple input files are named,
457 # instead of getting them combined with the output name.
458 lappend options "additional_flags=-dumpbase \"\""
459 }
460 if { $additional_files != "" } then {
461 regsub -all "^| " $additional_files " [file dirname $source]/" additional_files
462 set to_download [concat $to_download $additional_files]
463 set additional_files ""
464 }
465 if [is_remote host] {
466 foreach file $to_download {
467 remote_download host $file
468 }
469 }
470
471 return $options
472 }
473
474 # Return a colon-separate list of directories to search for libraries
475 # for COMPILER, including multilib directories.
476
477 proc gcc-set-multilib-library-path { compiler } {
478 set shlib_ext [get_shlib_extension]
479 set options [lrange $compiler 1 end]
480 set compiler [lindex $compiler 0]
481
482 set libgcc_s_x [remote_exec host "$compiler" \
483 "$options -print-file-name=libgcc_s.${shlib_ext}"]
484 if { [lindex $libgcc_s_x 0] == 0 \
485 && [set libgcc_s_dir [file dirname [lindex $libgcc_s_x 1]]] != "" } {
486 set libpath ":${libgcc_s_dir}"
487 } else {
488 return ""
489 }
490
491 set multi_dir_x [remote_exec host "$compiler" \
492 "$options -print-multi-directory"]
493 set multi_lib_x [remote_exec host "$compiler" \
494 "$options -print-multi-lib"]
495 if { [lindex $multi_dir_x 0] == 0 && [lindex $multi_lib_x 0] == 0 } {
496 set multi_dir [string trim [lindex $multi_dir_x 1]]
497 set multi_lib [string trim [lindex $multi_lib_x 1]]
498 if { "$multi_dir" == "." } {
499 set multi_root "$libgcc_s_dir"
500 } else {
501 set multi_match [string last "/$multi_dir" "$libgcc_s_dir"]
502 if { "$multi_match" < 0 } {
503 return $libpath
504 }
505 set multi_root [string range "$libgcc_s_dir" \
506 0 [expr $multi_match - 1]]
507 }
508 foreach i "$multi_lib" {
509 set mldir ""
510 regexp -- "\[a-z0-9=_/\.-\]*;" $i mldir
511 set mldir [string trimright $mldir "\;@"]
512 if { "$mldir" == "$multi_dir" } {
513 continue
514 }
515 append libpath ":${multi_root}/${mldir}"
516 }
517 }
518
519 return $libpath
520 }
521
522 # A list of all uses of dg-regexp, each entry of the form:
523 # line-number regexp
524 # This is cleared at the end of each test by gcc-dg.exp's wrapper for dg-test.
525 set freeform_regexps []
526
527 # Directive for looking for a regexp, without any line numbers or other
528 # prefixes.
529
530 proc dg-regexp { args } {
531 verbose "dg-regexp: args: $args" 2
532
533 global freeform_regexps
534 lappend freeform_regexps $args
535 }
536
537 # Hook to be called by prune.exp's prune_gcc_output to
538 # look for the expected dg-regexp expressions, pruning them,
539 # reporting PASS for those that are found, and FAIL for
540 # those that weren't found.
541 #
542 # It returns a pruned version of its output.
543
544 proc handle-dg-regexps { text } {
545 global freeform_regexps
546 global testname_with_flags
547
548 foreach entry $freeform_regexps {
549 verbose " entry: $entry" 3
550
551 set linenum [lindex $entry 0]
552 set rexp [lindex $entry 1]
553
554 # Escape newlines in $rexp so that we can print them in
555 # pass/fail results.
556 set escaped_regex [string map {"\n" "\\n"} $rexp]
557 verbose "escaped_regex: ${escaped_regex}" 4
558
559 set title "$testname_with_flags dg-regexp $linenum"
560
561 # Use "regsub" to attempt to prune the pattern from $text
562 if {[regsub -line $rexp $text "" text]} {
563 # Success; the multiline pattern was pruned.
564 pass "$title was found: \"$escaped_regex\""
565 } else {
566 fail "$title not found: \"$escaped_regex\""
567 }
568 }
569
570 return $text
571 }
572
573 # Verify that the initial arg is a valid .dot file
574 # (by running dot -Tpng on it, and verifying the exit code is 0).
575
576 proc dg-check-dot { args } {
577 verbose "dg-check-dot: args: $args" 2
578
579 set testcase [testname-for-summary]
580
581 set dotfile [lindex $args 0]
582 verbose " dotfile: $dotfile" 2
583
584 set status [remote_exec host "dot" "-O -Tpng $dotfile"]
585 verbose " status: $status" 2
586 if { [lindex $status 0] != 0 } {
587 fail "$testcase dg-check-dot $dotfile"
588 return 0
589 }
590
591 pass "$testcase dg-check-dot $dotfile"
592 }
593
594 # Used by aarch64-with-arch-dg-options to intercept dg-options and make
595 # the changes required. See there for details.
596 proc aarch64-arch-dg-options { args } {
597 upvar dg-do-what do_what
598 global aarch64_default_testing_arch
599
600 set add_arch 1
601 set add_tune 1
602 set checks_output [string equal [lindex $do_what 0] "compile"]
603 set options [lindex $args 1]
604
605 foreach option [split $options] {
606 switch -glob -- $option {
607 -march=* { set add_arch 0 }
608 -mcpu=* { set add_arch 0; set add_tune 0 }
609 -mtune=* { set add_tune 0 }
610 -moverride=* { set add_tune 0 }
611 -save-temps { set checks_output 1 }
612 --save-temps { set checks_output 1 }
613 -fdump* { set checks_output 1 }
614 }
615 }
616
617 if { $add_arch && ![string equal $aarch64_default_testing_arch ""] } {
618 # Force SVE if we're not testing it already.
619 append options " $aarch64_default_testing_arch"
620 }
621
622 if { $add_tune && $checks_output } {
623 # Turn off any default tuning and codegen tweaks.
624 append options " -mtune=generic -moverride=tune=none"
625 }
626
627 uplevel 1 aarch64-old-dg-options [lreplace $args 1 1 $options]
628 }
629
630 # Run Tcl code CODE with dg-options modified to work better for some
631 # AArch64 tests. In particular:
632 #
633 # - If the dg-options do not specify an -march or -mcpu option,
634 # use the architecture options in ARCH (which might be empty).
635 #
636 # - If the dg-options do not specify an -mcpu, -mtune or -moverride option,
637 # and if the test appears to be checking assembly or dump output,
638 # force the test to use generic tuning.
639 #
640 # The idea is to handle toolchains that are configured with a default
641 # CPU or architecture that's different from the norm.
642 proc aarch64-with-arch-dg-options { arch code } {
643 global aarch64_default_testing_arch
644
645 set aarch64_default_testing_arch $arch
646
647 rename dg-options aarch64-old-dg-options
648 rename aarch64-arch-dg-options dg-options
649
650 uplevel 1 $code
651
652 rename dg-options aarch64-arch-dg-options
653 rename aarch64-old-dg-options dg-options
654 }