Debian Codegraph @

gcc/testsuite/lib/lto.exp

raw
1 # Copyright (C) 2009-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 # Contributed by Diego Novillo <[email protected]>
18
19 # A subroutine of lto_handle_diagnostics: check TEXT for the expected
20 # diagnostics for one specific source file, issuing PASS/FAIL results.
21 # Return TEXT, stripped of any diagnostics that were handled.
22 #
23 # NAME is the testcase name to use when reporting PASS/FAIL results.
24 # FILENAME is the name (with full path) of the file we're interested in.
25 # MESSAGES_FOR_FILE is a list of expected messages, akin to DejaGnu's
26 # "dg-messages" variable.
27 # TEXT is the textual output from the LTO link.
28
29 proc lto_handle_diagnostics_for_file { name filename messages_for_file text } {
30 global dg-linenum-format
31
32 set filename_without_path [file tail $filename]
33
34 # This loop is adapted from the related part of DejaGnu's dg-test,
35 # with changes as detailed below to cope with the LTO case.
36
37 foreach i ${messages_for_file} {
38 verbose "Scanning for message: $i" 4
39
40 # Remove all error messages for the line [lindex $i 0]
41 # in the source file. If we find any, success!
42 set line [lindex $i 0]
43 set pattern [lindex $i 2]
44 set comment [lindex $i 3]
45 verbose "line: $line" 4
46 verbose "pattern: $pattern" 4
47 verbose "comment: $comment" 4
48 #send_user "Before:\n$text\n"
49
50 # Unlike dg-test, we use $filename_without_path in this pattern.
51 # This is to ensure that we have the correct file/line combination.
52 # This imposes the restriction that the filename can't contain
53 # any regexp control characters. We have to strip the path, since
54 # e.g. the '+' in "g++.dg" wouldn't be valid.
55 set pat "(^|\n)(\[^\n\]+$filename_without_path$line\[^\n\]*($pattern)\[^\n\]*\n?)+"
56 if {[regsub -all $pat $text "\n" text]} {
57 set text [string trimleft $text]
58 set ok pass
59 set uhoh fail
60 } else {
61 set ok fail
62 set uhoh pass
63 }
64 #send_user "After:\n$text\n"
65
66 # $line will either be a formatted line number or a number all by
67 # itself. Delete the formatting.
68 scan $line ${dg-linenum-format} line
69
70 # Unlike dg-test, add the filename to the PASS/FAIL message (rather
71 # than just the line number) so that the user can identify the
72 # pertinent directive.
73 set describe_where "$filename_without_path line $line"
74
75 # Issue the PASS/FAIL, adding "LTO" to the messages (e.g. "LTO errors")
76 # to distinguish them from the non-LTO case (in case we ever need to
77 # support both).
78 switch [lindex $i 1] {
79 "ERROR" {
80 $ok "$name $comment (test for LTO errors, $describe_where)"
81 }
82 "XERROR" {
83 x$ok "$name $comment (test for LTO errors, $describe_where)"
84 }
85 "WARNING" {
86 $ok "$name $comment (test for LTO warnings, $describe_where)"
87 }
88 "XWARNING" {
89 x$ok "$name $comment (test for LTO warnings, $describe_where)"
90 }
91 "BOGUS" {
92 $uhoh "$name $comment (test for LTO bogus messages, $describe_where)"
93 }
94 "XBOGUS" {
95 x$uhoh "$name $comment (test for LTO bogus messages, $describe_where)"
96 }
97 "BUILD" {
98 $uhoh "$name $comment (test for LTO build failure, $describe_where)"
99 }
100 "XBUILD" {
101 x$uhoh "$name $comment (test for LTO build failure, $describe_where)"
102 }
103 "EXEC" { }
104 "XEXEC" { }
105 }
106 }
107 return $text
108 }
109
110 # Support for checking for link-time diagnostics: check for
111 # the expected diagnostics within TEXT, issuing PASS/FAIL results.
112 # Return TEXT, stripped of any diagnostics that were handled.
113 #
114 # TEXT is the textual output from the LTO link.
115
116 proc lto_handle_diagnostics { text } {
117 global testcase
118
119 upvar dg-messages-by-file messages_by_file
120
121 verbose "lto_handle_diagnostics: entry: $text" 2
122
123 if { ![array exists messages_by_file] } {
124 error "lto_handle_diagnostics: messages_by_file not defined"
125 }
126
127 foreach src [lsort [array names messages_by_file]] {
128 set dg-messages $messages_by_file($src)
129 verbose " messages for $src: ${dg-messages}" 3
130 set text [lto_handle_diagnostics_for_file $testcase $src \
131 ${dg-messages} $text]
132 }
133
134 verbose "lto_handle_diagnostics: exit: $text" 2
135
136 return $text
137 }
138
139 # Prune messages that aren't useful.
140
141 proc lto_prune_warns { text } {
142
143 verbose "lto_prune_warns: entry: $text" 2
144
145 # Many tests that use visibility will still pass on platforms that don't support it.
146 regsub -all "(^|\n)\[^\n\]*: warning: visibility attribute not supported in this configuration; ignored\[^\n\]*" $text "" text
147
148 # Allow mixed-language LTO tests to pass with make check-c++0x
149 regsub -all "(^|\n)\[^\n\]*: warning: command line option '-std=\[^\n\]*" $text "" text
150
151 # And any stray location lines.
152 regsub -all "(^|\n)\[^\n\]*: In function \[^\n\]*" $text "" text
153 regsub -all "(^|\n)In file included from \[^\n\]*" $text "" text
154 regsub -all "(^|\n)\[ \t\]*from \[^\n\]*" $text "" text
155
156 # Sun ld warns about common symbols with differing sizes. Unlike GNU ld
157 # --warn-common (off by default), they cannot be disabled.
158 regsub -all "(^|\n)ld: warning: symbol \[`'\]\[^\n\]*' has differing sizes:" $text "" text
159 regsub -all "(^|\n)\[ \t\]*\[\(\]file \[^\n\]* value=\[^\n\]*; file \[^\n\]* value=\[^\n\]*\[)\];" $text "" text
160 regsub -all "(^|\n)\[ \t\]*\[^\n\]* definition taken" $text "" text
161
162 # Ignore missing jobserver for tests that do more than 1 LTRANS unit
163 regsub -all "(^|\n)\[^\n\]*: warning: using serial compilation of \[^\n\]*" $text "" text
164 regsub -all "(^|\n)\[^\n\]*: note: see the \[^\n\]*'-flto' option documentation\[^\n\]* for more information" $text "" text
165
166 global prune_notes
167 if { $prune_notes } {
168 # Ignore informational notes.
169 regsub -all "(^|\n)\[^\n\]*: note: \[^\n\]*" $text "" text
170 }
171
172 verbose "lto_prune_warns: exit: $text" 2
173
174 return $text
175 }
176
177 # lto_init -- called at the start of each subdir of tests
178
179 proc lto_init { args } {
180 global LTO_OPTIONS
181
182 if {[info exists args] && $args == "no-mathlib"} {
183 global board_info
184 global saved_mathlib
185
186 set dest [target_info name]
187 if [board_info $dest exists mathlib] {
188 set saved_mathlib [board_info $dest mathlib]
189 }
190 set board_info($dest,mathlib) " "
191 }
192
193 # Each test is run with the compiler options from this list.
194 # The default option lists can be overridden by LTO_OPTIONS="[list
195 # {opts_1} {opts_2}... {opts_n}]" where opts_i are lists of options.
196 # You can put this in the environment before site.exp is written or
197 # add it to site.exp directly.
198 if ![info exists LTO_OPTIONS] {
199 if [check_linker_plugin_available] {
200 set LTO_OPTIONS [list \
201 {-O0 -flto -flto-partition=none -fuse-linker-plugin} \
202 {-O2 -flto -flto-partition=none -fuse-linker-plugin -fno-fat-lto-objects } \
203 {-O0 -flto -flto-partition=1to1 -fno-use-linker-plugin } \
204 {-O2 -flto -flto-partition=1to1 -fno-use-linker-plugin } \
205 {-O0 -flto -fuse-linker-plugin -fno-fat-lto-objects } \
206 {-O2 -flto -fuse-linker-plugin} \
207 ]
208 } else {
209 set LTO_OPTIONS [list \
210 {-O0 -flto -flto-partition=none } \
211 {-O2 -flto -flto-partition=none } \
212 {-O0 -flto -flto-partition=1to1 } \
213 {-O2 -flto -flto-partition=1to1 } \
214 {-O0 -flto } \
215 {-O2 -flto} \
216 ]
217 }
218 global lto_init_set_LTO_OPTIONS
219 if [info exists lto_init_set_LTO_OPTIONS] {
220 error "lto_init_set_LTO_OPTIONS already set"
221 }
222 set lto_init_set_LTO_OPTIONS 1
223 }
224 }
225
226 #
227 # lto_finish -- called at the end of each subdir of tests if mathlib is
228 # changed.
229 #
230
231 proc lto_finish { } {
232 global board_info
233 global saved_mathlib
234
235 set dest [target_info name]
236 if [info exists saved_mathlib] {
237 set board_info($dest,mathlib) $saved_mathlib
238 } elseif [board_info $dest exists mathlib] {
239 unset board_info($dest,mathlib)
240 }
241
242 # Let the next 'lto_init' redetermine the default 'LTO_OPTIONS'.
243 global lto_init_set_LTO_OPTIONS
244 if [info exists lto_init_set_LTO_OPTIONS] {
245 global LTO_OPTIONS
246 unset LTO_OPTIONS
247 unset lto_init_set_LTO_OPTIONS
248 }
249 }
250
251 # Subsets of tests can be selectively disabled by members of this list:
252 # - ATTRIBUTE: disable all tests using the __attribute__ extension,
253 # - COMPLEX: disable all tests using the complex types feature,
254 # - COMPLEX_INT: disable all tests using the complex integral types extension,
255 # - VA: disable all tests using the variable number of arguments feature,
256 # - VLA_IN_STRUCT: disable all tests using the variable-length arrays as
257 # structure members extension,
258 # - ZERO_ARRAY: disable all tests using the zero-sized arrays extension.
259 # The default skip lists can be overriden by
260 # LTO_SKIPS="[list {skip_1}...{skip_n}]"
261 # where skip_i are skip identifiers. You can put this in the environment
262 # before site.exp is written or add it to site.exp directly.
263 if ![info exists LTO_SKIPS] {
264 set LTO_SKIPS [list {}]
265 }
266
267 global lto_skip_list
268 set lto_skip_list $LTO_SKIPS
269
270 load_lib dg.exp
271 load_lib gcc-dg.exp
272 load_lib gcc.exp
273
274 # lto-obj -- compile to an object file
275 #
276 # SOURCE is the source file
277 # DEST is the object file
278 # OPTALL is the list of compiler options to use with all tests
279 # OPTFILE is the list of compiler options to use with this file
280 # OPTSTR is the options to print with test messages
281 # XFAILDATA is the xfail data to be passed to the compiler
282 proc lto-obj { source dest optall optfile optstr xfaildata } {
283 global testcase
284 global tool
285 global compiler_conditional_xfail_data
286 global lto_skip_list
287
288 # Add the skip specifiers.
289 foreach skip $lto_skip_list {
290 if { ![string match $skip ""] } {
291 lappend optall "-DSKIP_$skip"
292 }
293 }
294
295 # Set up the options for compiling this file.
296 set options ""
297 lappend options "additional_flags=$optall $optfile"
298
299 set compiler_conditional_xfail_data $xfaildata
300
301 # Allow C source files to mix freely with other languages
302 if [ string match "*.c" $source ] then {
303 set comp_output [gcc_target_compile "$source" "$dest" object $options]
304 } else {
305 set comp_output [${tool}_target_compile "$source" "$dest" object $options]
306 }
307 # Prune unimportant visibility warnings before checking output.
308 set comp_output [lto_prune_warns $comp_output]
309 ${tool}_check_compile "$testcase $dest assemble" $optstr $dest $comp_output
310 }
311
312 proc lto-build-archive { testname objlist dest } {
313 global testcase
314 global tool
315 global GCC_UNDER_TEST
316 global GCC_AR_UNDER_TEST
317
318 upvar dg-messages-by-file dg-messages-by-file
319
320 verbose "lto-build-archive" 2
321 file_on_host delete $dest
322
323 # Check that all of the objects were built successfully.
324 foreach obj [split $objlist] {
325 if ![file_on_host exists $obj] then {
326 unresolved "$testcase $testname build-archive"
327 return
328 }
329 }
330
331 # For now find gcc-ar in the same directory as $GCC_UNDER_TEST, unless
332 # GCC_AR_UNDER_TEST is defined.
333
334 if ![info exists GCC_AR_UNDER_TEST] {
335 if { ![is_remote host] && [string match "*/*" [lindex $GCC_UNDER_TEST 0]] } {
336 set ar_cmd [file dirname [lindex $GCC_UNDER_TEST 0]]/[gcc-transform-out-of-tree gcc-ar]
337 } else {
338 set ar_cmd [gcc-transform-out-of-tree gcc-ar]
339 }
340 set ar_cmd "$ar_cmd [lrange $GCC_UNDER_TEST 1 end]"
341 } else {
342 set ar_cmd $GCC_AR_UNDER_TEST
343 }
344
345 set ar_output [remote_exec host "$ar_cmd rcs $dest $objlist"]
346 set retval [lindex $ar_output 0]
347 set retmsg [lindex $ar_output 1]
348
349 # If any message remains, we fail. Don't bother overriding tool since
350 # we're not really looking to match any specific error or warning patterns
351 # here.
352 if ![string match "0" $retval] then {
353 ${tool}_fail $testcase "ar returned $retval: $retmsg"
354 return 0
355 } else {
356 ${tool}_pass $testcase "archive"
357 return 0
358 }
359 }
360
361 # lto-link-and-maybe-run -- link the object files and run the executable
362 # if compile_type is set to "run"
363 #
364 # TESTNAME is the mixture of object files to link
365 # OBJLIST is the list of object files to link
366 # DEST is the name of the executable
367 # OPTALL is a list of compiler and linker options to use for all tests
368 # OPTFILE is a list of compiler and linker options to use for this test
369 # OPTSTR is the list of options to list in messages
370 proc lto-link-and-maybe-run { testname objlist dest optall optfile optstr } {
371 global testcase
372 global tool
373 global compile_type
374 global board_info
375
376 upvar dg-messages-by-file dg-messages-by-file
377
378 verbose "lto-link-and-maybe-run" 2
379
380 # Check that all of the objects were built successfully.
381 foreach obj [split $objlist] {
382 if ![file_on_host exists $obj] then {
383 unresolved "$testcase $testname link $optstr"
384 unresolved "$testcase $testname execute $optstr"
385 return
386 }
387 }
388
389 # Set up the options for linking this test.
390 set options ""
391 lappend options "additional_flags=$optall $optfile"
392
393 set target_board [target_info name]
394 set relocatable 0
395
396 # Some LTO tests do relocatable linking. Some target boards set
397 # a linker script which can't be used for relocatable linking.
398 # Use the default linker script instead.
399 if { [lsearch -exact [split "$optall $optfile"] "-r"] >= 0 } {
400 set relocatable 1
401 }
402
403 if { $relocatable } {
404 set saved_ldscript [board_info $target_board ldscript]
405 set board_info($target_board,ldscript) ""
406 }
407
408 # Link the objects into an executable.
409 set comp_output [${tool}_target_compile "$objlist" $dest executable \
410 "$options"]
411
412 if { $relocatable } {
413 set board_info($target_board,ldscript) $saved_ldscript
414 }
415
416 # Check for diagnostics specified by directives
417 set comp_output [lto_handle_diagnostics $comp_output]
418
419 # Prune unimportant visibility warnings before checking output.
420 set comp_output [lto_prune_warns $comp_output]
421
422 if ![${tool}_check_compile "$testcase $testname link" $optstr \
423 $dest $comp_output] then {
424 if { ![string compare "execute" $compile_type] } {
425 unresolved "$testcase $testname execute $optstr"
426 }
427 return
428 }
429
430 # Return if we only needed to link.
431 if { ![string compare "link" $compile_type] \
432 || ![string compare "ar-link" $compile_type] } {
433 return
434 }
435
436 # Run the self-checking executable.
437 if ![string match "*/*" $dest] then {
438 set dest "./$dest"
439 }
440 set result [${tool}_load $dest "" ""]
441 set status [lindex $result 0]
442 if { $status == "pass" } then {
443 file_on_host delete $dest
444 }
445 $status "$testcase $testname execute $optstr"
446 }
447
448 # Potentially handle the given dg- directive (a list)
449 # Return true is the directive was handled, false otherwise.
450
451 proc lto-can-handle-directive { op } {
452 set cmd [lindex $op 0]
453
454 # dg-warning and dg-message append to dg-messages.
455 upvar dg-messages dg-messages
456
457 # A list of directives to recognize, and a list of directives
458 # to remap them to.
459 # For example, "dg-lto-warning" is implemented by calling "dg-warning".
460 set directives { dg-lto-warning dg-lto-message dg-lto-note }
461 set remapped_directives { dg-warning dg-message dg-note }
462
463 set idx [lsearch -exact $directives $cmd]
464 if { $idx != -1 } {
465 verbose "remapping from: $op" 4
466
467 set remapped_cmd [lindex $remapped_directives $idx]
468 set op [lreplace $op 0 0 $remapped_cmd]
469
470 verbose "remapped to: $op" 4
471
472 set status [catch "$op" errmsg]
473 if { $status != 0 } {
474 if { 0 && [info exists errorInfo] } {
475 # This also prints a backtrace which will just confuse
476 # testcase writers, so it's disabled.
477 perror "$name: $errorInfo\n"
478 } else {
479 perror "$name: $errmsg for \"$op\"\n"
480 }
481 # ??? The call to unresolved here is necessary to clear `errcnt'.
482 # What we really need is a proc like perror that doesn't set errcnt.
483 # It should also set exit_status to 1.
484 unresolved "$name: $errmsg for \"$op\""
485 }
486
487 return true
488 }
489
490 return false
491 }
492
493 # lto-get-options-main -- get target requirements for a test and
494 # options for the primary source file and the test as a whole
495 #
496 # SRC is the full pathname of the primary source file.
497 proc lto-get-options-main { src } {
498 global compile_type
499 global dg-extra-ld-options
500 global dg-suppress-ld-options
501
502 set dg-extra-ld-options ""
503 set dg-suppress-ld-options ""
504
505 # dg-options sets a variable called dg-extra-tool-flags.
506 set dg-extra-tool-flags ""
507
508 # dg-options sets a variable called tool_flags.
509 set tool_flags ""
510
511 # dg-require-* sets dg-do-what.
512 upvar dg-do-what dg-do-what
513 upvar dg-final-code dg-final-code
514 set dg-final-code ""
515
516 # dg-warning and dg-message append to dg-messages.
517 upvar dg-messages-by-file dg-messages-by-file
518 set dg-messages ""
519
520 set tmp [dg-get-options $src]
521 verbose "getting options for $src: $tmp"
522 foreach op $tmp {
523 set cmd [lindex $op 0]
524 verbose "cmd is $cmd"
525 if { [string match "dg-skip-if" $cmd] \
526 || [string match "dg-require-*" $cmd] } {
527 set status [catch "$op" errmsg]
528 if { $status != 0 } {
529 perror "src: $errmsg for \"$op\"\n"
530 unresolved "$src: $errmsg for \"$op\""
531 return
532 }
533 } elseif { [string match "dg-lto-options" $cmd] } {
534 set op [lreplace $op 0 0 "dg-options"]
535 set status [catch "$op" errmsg]
536 if { $status != 0 } {
537 perror "src: $errmsg for \"$op\"\n"
538 unresolved "$src: $errmsg for \"$op\""
539 return
540 }
541 } elseif { ![string compare "dg-xfail-if" $cmd] \
542 || ![string compare "dg-options" $cmd] } {
543 warning "lto.exp does not support $cmd in primary source file"
544 } elseif { ![string compare "dg-lto-do" $cmd] } {
545 if { [llength $op] > 3 } {
546 set kw [lindex [lindex $op 3] 0]
547 if [string match "target" $kw] {
548 perror "$src: dg-lto-do does not support \"target\""
549 } elseif [string match "xfail" $kw] {
550 perror "$src: dg-lto-do does not support \"xfail\""
551 } else {
552 perror "$src: dg-lto-do takes a single argument"
553 }
554 }
555 set dgdo [lindex $op 2]
556 verbose "dg-lto-do command for \"$op\" is $dgdo"
557 if { ![string compare "assemble" $dgdo] } {
558 set compile_type "assemble"
559 } elseif { ![string compare "run" $dgdo] } {
560 set compile_type "run"
561 } elseif { ![string compare "link" $dgdo] } {
562 set compile_type "link"
563 } elseif { ![string compare "ar-link" $dgdo] } {
564 set compile_type "ar-link"
565 } else {
566 warning "lto.exp does not support dg-lto-do $dgdo"
567 }
568 } elseif { ![string compare "dg-extra-ld-options" $cmd] } {
569 if { [llength $op] > 4 } {
570 error "[lindex $op 0]: too many arguments"
571 } else {
572 if { [llength $op] == 3
573 || ([llength $op] > 3
574 && [dg-process-target [lindex $op 3]] == "S") } {
575 set dg-extra-ld-options [lindex $op 2]
576 verbose \
577 "dg-extra-ld-options for main is ${dg-extra-ld-options}"
578 }
579 }
580 } elseif { ![string compare "dg-suppress-ld-options" $cmd] } {
581 if { [llength $op] > 4 } {
582 error "[lindex $op 0]: too many arguments"
583 } else {
584 if { [llength $op] == 3
585 || ([llength $op] > 3
586 && [dg-process-target [lindex $op 3]] == "S") } {
587 set dg-suppress-ld-options [lindex $op 2]
588 verbose \
589 "dg-suppress-ld-options for main is ${dg-suppress-ld-options}"
590 }
591 }
592 } elseif { ![string compare "dg-final" $cmd] } {
593 if { [llength $op] > 3 } {
594 error "[lindex $op 0]: too many arguments"
595 } else {
596 append dg-final-code "[lindex $op 2]\n"
597 }
598 } elseif { ![lto-can-handle-directive $op] } {
599 # Ignore unrecognized dg- commands, but warn about them.
600 warning "lto.exp does not support $cmd"
601 }
602 }
603
604 verbose "dg-messages: ${dg-messages}" 3
605 set dg-messages-by-file($src) ${dg-messages}
606
607 # Return flags to use for compiling the primary source file and for
608 # linking.
609 verbose "dg-extra-tool-flags for main is ${dg-extra-tool-flags}"
610 return ${dg-extra-tool-flags}
611 }
612
613
614 # lto-get-options -- get special tool flags to use for a secondary
615 # source file
616 #
617 # SRC is the full pathname of the source file.
618 # The result is a list of options to use.
619 #
620 # This code is copied from proc dg-test in dg.exp from DejaGNU.
621 proc lto-get-options { src } {
622 # dg-options sets a variable called dg-extra-tool-flags.
623 set dg-extra-tool-flags ""
624
625 # dg-xfail-if sets compiler_conditional_xfail_data.
626 global compiler_conditional_xfail_data
627 set compiler_conditional_xfail_data ""
628
629 # dg-xfail-if needs access to dg-do-what.
630 upvar dg-do-what dg-do-what
631
632 # dg-warning appends to dg-messages.
633 upvar dg-messages-by-file dg-messages-by-file
634 set dg-messages ""
635
636 set tmp [dg-get-options $src]
637 foreach op $tmp {
638 set cmd [lindex $op 0]
639 if { ![string compare "dg-options" $cmd] \
640 || ![string compare "dg-xfail-if" $cmd] } {
641 set status [catch "$op" errmsg]
642 if { $status != 0 } {
643 perror "src: $errmsg for \"$op\"\n"
644 unresolved "$src: $errmsg for \"$op\""
645 return
646 }
647 } elseif { [string match "dg-require-*" $cmd] } {
648 warning "lto.exp does not support $cmd in secondary source files"
649 } elseif { ![lto-can-handle-directive $op] } {
650 # Ignore unrecognized dg- commands, but warn about them.
651 warning "lto.exp does not support $cmd in secondary source files"
652 }
653 }
654
655 verbose "dg-messages: ${dg-messages}" 3
656 if { [info exists dg-messages-by-file($src)] } {
657 append dg-messages-by-file($src) ${dg-messages}
658 } else {
659 set dg-messages-by-file($src) ${dg-messages}
660 }
661
662 return ${dg-extra-tool-flags}
663 }
664
665 # lto-execute -- compile multi-file tests
666 #
667 # SRC1 is the full pathname of the main file of the testcase.
668 # SID identifies a test suite in the names of temporary files.
669 proc lto-execute-1 { src1 sid } {
670 global srcdir tmpdir
671 global lto_option_list
672 global tool
673 global verbose
674 global testcase
675 global gluefile
676 global compiler_conditional_xfail_data
677 global dg-do-what-default
678 global compile_type
679 global dg-extra-ld-options
680 global dg-suppress-ld-options
681 global LTO_OPTIONS
682 global dg-final-code
683 global testname_with_flags
684
685 # Get extra flags for this test from the primary source file, and
686 # process other dg-* options that this suite supports. Warn about
687 # unsupported flags.
688 verbose "lto-execute: $src1" 1
689 set compile_type "run"
690 set dg-do-what [list ${dg-do-what-default} "" P]
691 array set dg-messages-by-file [list]
692 set extra_flags(0) [lto-get-options-main $src1]
693 set compile_xfail(0) ""
694
695 # If the main file defines dg-options, those flags are used to
696 # overwrite the default lto_option_list taken from LTO_OPTIONS.
697 if { [string length $extra_flags(0)] > 0 } {
698 set lto_option_list $extra_flags(0)
699 set extra_flags(0) ""
700 } else {
701 set lto_option_list $LTO_OPTIONS
702 }
703
704 # Set up the names of the other source files.
705 set dir [file dirname $src1]
706 set base [file rootname $src1]
707 set base [string range $base [string length $dir] end]
708 regsub "_0" $base "" base
709 regsub "/" $base "" base
710 set src_list $src1
711 set i 1
712 set done 0
713 while { !$done } {
714 set prefix "${dir}/${base}_${i}"
715
716 # glob returns a case insensitive list if filesystem is case
717 # insensitive. To have a case sensitive list, fetch the extended list
718 # and then filter it to avoid duplicates.
719 regsub -all {([.^$*+?()$${}|])} $prefix {\\\1} pattern
720 set pattern [format {^%s.(c|C|ii|[fF](|90|95|03|08)|d|m|mm)$} $pattern]
721 set names [lsearch -inline -all -regexp \
722 [glob -nocomplain -types f -- "${prefix}.*"] $pattern]
723
724 if { [llength ${names}] > 1 } {
725 warning "lto-execute: more than one file matched $pattern"
726 verbose "matching files: $names"
727 }
728 if { [llength ${names}] == 1 } {
729 lappend src_list [lindex ${names} 0]
730 incr i
731 } else {
732 set num_srcs ${i}
733 set done 1
734 }
735 }
736
737 # Use the dg-options mechanism to specify extra flags for each
738 # of the secondary files.
739 # The extra flags in each file are used to compile that file, and the
740 # extra flags in *_0.* are also used for linking.
741 verbose "\tsrc_list is: $src_list"
742 for {set i 1} {$i < $num_srcs} {incr i} {
743 set extra_flags($i) [lto-get-options [lindex $src_list $i]]
744 set compile_xfail($i) $compiler_conditional_xfail_data
745 }
746
747 # Define the names of the object files.
748 set obj_list ""
749 for {set i 0} {$i < $num_srcs} {incr i} {
750 lappend obj_list "${sid}_${base}_${i}.o"
751 }
752
753 # Get the base name of this test, for use in messages.
754 set testcase [lindex ${src_list} 0]
755
756 # The test needs to build all but the main file into an archive and then
757 # link them all together.
758 if { ![string compare "ar-link" $compile_type] } {
759 set arname "${sid}_${base}.a"
760 }
761
762 # Remove the $srcdir and $tmpdir prefixes from $src1. (It would
763 # be possible to use "regsub" here, if we were careful to escape
764 # all regular expression characters in $srcdir and $tmpdir, but
765 # that would be more complicated that this approach.)
766 if {[string first "$srcdir/" "${testcase}"] == 0} {
767 set testcase [string range "${testcase}" [string length "$srcdir/"] end]
768 }
769 if {[string first "$tmpdir/" "$testcase"] == 0} {
770 set testcase [string range "$testcase" [string length "$tmpdir/"] end]
771 set testcase "tmpdir-$testcase"
772 }
773 # If we couldn't rip $srcdir out of `src1' then just do the best we can.
774 # The point is to reduce the unnecessary noise in the logs. Don't strip
775 # out too much because different testcases with the same name can confuse
776 # `test-tool'.
777 if [string match "/*" $testcase] then {
778 set testcase "[file tail [file dirname $src1]]/[file tail $src1]"
779 }
780
781 # Check whether this test is supported for this target.
782 if { [lindex ${dg-do-what} 1 ] == "N" } {
783 unsupported "$testcase"
784 verbose "$testcase not supported on this target, skipping it" 3
785 return
786 }
787 # Should be safe for non-fortran too but be paranoid..
788 if {$sid eq "f_lto"} {
789 list-module-names $src_list
790 }
791 regsub "_0.*" $testcase "" testcase
792
793 # Set up the base name of executable files so they'll be unique.
794 regsub -all "\[./\]" $testcase "-" execbase
795
796 # Loop through all of the option lists used for this test.
797 set count 0
798 foreach option $lto_option_list {
799 verbose "Testing $testcase, $option"
800
801 # There's a unique name for each executable we generate.
802 set execname "${execbase}-${count}1.exe"
803
804 # The LTO tests don't use dg-test, so testname_with_flags and
805 # output_file need to be defined explicitly for each file. scan-symbol
806 # directives rely on both of these to be defined to find the symbol to
807 # scan and for the text to print in the PASS/FAIL since they can also
808 # be called from dg-test. testname_with_flags is also used via
809 # testname-for-summary when calling into generic function below to
810 # clean temporary files.
811 set output_file $execname
812 set testname_with_flags $execname
813
814 incr count
815
816 file_on_host delete $execname
817
818 # Compile pieces with the compiler under test.
819 set i 0
820 foreach src $src_list obj $obj_list {
821 lto-obj $src $obj $option $extra_flags($i) $option \
822 $compile_xfail($i)
823 incr i
824 }
825
826 # Bundle all but the main file into an archive. Update objlist to only
827 # have the archive and the last file.
828 if { ![string compare "ar-link" $compile_type] } {
829 set mainsrc [lindex $obj_list 0]
830 set obj_list [lrange $obj_list 1 end]
831 lto-build-archive \
832 "[lindex $obj_list 1]-[lindex $obj_list end]" \
833 $obj_list $arname
834
835 set obj_list ""
836 lappend obj_list $mainsrc
837 lappend obj_list $arname
838 set num_srcs 2
839 }
840
841 # Link (using the compiler under test), run, and clean up tests.
842 if { ![string compare "run" $compile_type] \
843 || ![string compare "ar-link" $compile_type] \
844 || ![string compare "link" $compile_type] } {
845
846 # Filter out any link options we were asked to suppress.
847 set reduced {}
848 foreach x [split $option] {
849 if {[lsearch ${dg-suppress-ld-options} $x] == -1} {
850 lappend reduced $x
851 }
852 }
853 set filtered [join $reduced " "]
854
855 lto-link-and-maybe-run \
856 "[lindex $obj_list 0]-[lindex $obj_list end]" \
857 $obj_list $execname $filtered ${dg-extra-ld-options} \
858 $filtered
859
860 if (![string compare "ar-link" $compile_type]) {
861 file_on_host delete $arname
862 }
863 }
864
865
866 # Are there any further tests to perform?
867 # Note that if the program has special run-time requirements, running
868 # of the program can be delayed until here. Ditto for other situations.
869 # It would be a bit cumbersome though.
870
871 if ![string match ${dg-final-code} ""] {
872 regsub -all "\\\\(\[{}\])" ${dg-final-code} "\\1" dg-final-code
873 # Note that the use of `args' here makes this a varargs proc.
874 proc dg-final-proc { args } ${dg-final-code}
875 verbose "Running dg-final tests." 3
876 verbose "dg-final-proc:\n[info body dg-final-proc]" 4
877 if [catch "dg-final-proc $src1" errmsg] {
878 perror "$src1: error executing dg-final: $errmsg"
879 # ??? The call to unresolved here is necessary to clear
880 # `errcnt'. What we really need is a proc like perror that
881 # doesn't set errcnt. It should also set exit_status to 1.
882 unresolved "$src1: error executing dg-final: $errmsg"
883 }
884 }
885
886 # Clean up object files.
887 set files [glob -nocomplain ${sid}_*.o]
888 if { $files != "" } {
889 foreach objfile $files {
890 if { ![info exists gluefile] || $objfile != $gluefile } {
891 eval "file_on_host delete $objfile"
892 }
893 }
894 }
895
896 # Clean up after -save-temps.
897 eval "cleanup-saved-temps"
898
899 for {set i 0} {$i < $num_srcs} {incr i} {
900 set testname_with_flags "${base}_${i}"
901 eval "cleanup-saved-temps"
902 set testname_with_flags "${sid}_${base}_${i}"
903 eval "cleanup-saved-temps"
904 }
905
906 unset testname_with_flags
907
908 if { ![string compare "run" $compile_type] \
909 || ![string compare "ar-link" $compile_type] \
910 || ![string compare "link" $compile_type] } {
911 file_on_host delete $execname
912 }
913 # Should be safe for non-fortran too but be paranoid..
914 if {$sid eq "f_lto"} {
915 cleanup-modules ""
916 }
917 }
918 }
919
920 proc lto-execute { src1 sid } {
921 lto-execute-1 $src1 $sid
922
923 # LTO testing doesn't use 'dg-runtest'/'dg-test', and thus doesn't call
924 # 'cleanup-after-saved-dg-test', so we have to clean up manually.
925 initialize_prune_notes
926 }
927
928 # Call pass if object readelf is ok, otherwise fail.
929 # example: /* { dg-final { object-readelf Tag_ABI_enum_size int} } */
930 proc object-readelf { args } {
931 global readelf
932 global base_dir
933 upvar 2 execname execname
934
935 if { [llength $args] < 2 } {
936 error "object-readelf: too few arguments"
937 return
938 }
939 if { [llength $args] > 3 } {
940 error "object-readelf: too many arguments"
941 return
942 }
943 if { [llength $args] >= 3 } {
944 switch [dg-process-target [lindex $args 2]] {
945 "S" { }
946 "N" { return }
947 "F" { setup_xfail "*-*-*" }
948 "P" { }
949 }
950 }
951
952 # Find size like we find g++ in g++.exp.
953 if ![info exists readelf] {
954 set readelf [findfile $base_dir/../../../binutils/readelf \
955 $base_dir/../../../binutils/readelf \
956 [findfile $base_dir/../../readelf $base_dir/../../readelf \
957 [findfile $base_dir/readelf $base_dir/readelf \
958 [transform readelf]]]]
959 verbose -log "readelf is $readelf"
960 }
961
962 set what [lindex $args 0]
963 set with [lindex $args 1]
964
965 if ![file_on_host exists $execname] {
966 verbose -log "$execname does not exist"
967 unresolved "object-readelf $what "
968 return
969 }
970
971 set output [remote_exec host "$readelf -A" "$execname"]
972 set status [lindex $output 0]
973 if { $status != 0 } {
974 verbose -log "object-readelf: $readelf failed"
975 unresolved "object-readelf $what $execname"
976 return
977 }
978
979 set text [lindex $output 1]
980 set lines [split $text "\n"]
981
982 set done 0
983 set i 0
984 while { !$done } {
985 set line_tex [lindex $lines $i]
986 if { [llength ${line_tex}] > 1} {
987 incr i
988 if [regexp -- $what $line_tex] {
989 set match [regexp -- $with $line_tex]
990 set done 1
991 }
992 } else {
993 set done 1
994 }
995 }
996
997 verbose -log "$what size is $with;"
998 if { $match == 1 } {
999 pass "object-readelf $what size is correct."
1000 } else {
1001 fail "object-readelf $what size is incorrect."
1002 }
1003 }
1004
1005