Skip to content

Commit 7da93d4

Browse files
committed
testsuite: Add dg-lto-error directive support to lto.exp.
The LTO test framework lacked support for testing expected link-time errors. Tests could use dg-lto-warning and dg-lto-message for diagnostics emitted during compilation, but there was no way to verify that certain code patterns correctly fail at link time under LTO. This patch adds dg-lto-error directive support, allowing tests to: 1. Mark expected link failures with dg-lto-error directives 2. Pass when the link fails as expected (no executable created or linker errors present) 3. Fail when the link succeeds but should have failed 4. Skip execution tests when link failure is expected The implementation tracks dg-lto-error presence via a global flag and performs custom link result checking when link failure is expected, rather than using the standard check_compile reporting. Signed-off-by: Luis Silva <[email protected]>
1 parent 84843c7 commit 7da93d4

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

gcc/testsuite/lib/lto.exp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ proc lto-link-and-maybe-run { testname objlist dest optall optfile optstr } {
323323
global tool
324324
global compile_type
325325
global board_info
326+
global dg_lto_has_error
326327

327328
upvar dg-messages-by-file dg-messages-by-file
328329

@@ -370,8 +371,31 @@ proc lto-link-and-maybe-run { testname objlist dest optall optfile optstr } {
370371
# Prune unimportant visibility warnings before checking output.
371372
set comp_output [lto_prune_warns $comp_output]
372373

373-
if ![${tool}_check_compile "$testcase $testname link" $optstr \
374-
$dest $comp_output] then {
374+
# Check if link succeeded
375+
# If we expect errors, check manually without reporting
376+
if { [info exists dg_lto_has_error] && $dg_lto_has_error } {
377+
verbose "lto.exp: dg-lto-error present, checking for expected link failure" 2
378+
# Check if link failed (executable not created or output has errors)
379+
set link_failed [expr {![file_on_host exists $dest] || $comp_output != ""}]
380+
if { $link_failed } {
381+
# Link failure is expected due to dg-lto-error - pass the test
382+
verbose "lto.exp: link failed as expected" 2
383+
pass "$testcase $testname link $optstr"
384+
} else {
385+
# Link succeeded when it should have failed
386+
fail "$testcase $testname link $optstr (expected failure)"
387+
}
388+
if { ![string compare "execute" $compile_type] } {
389+
unresolved "$testcase $testname execute $optstr"
390+
}
391+
return
392+
}
393+
394+
# Normal link checking (no dg-lto-error)
395+
set link_ok [${tool}_check_compile "$testcase $testname link" $optstr \
396+
$dest $comp_output]
397+
398+
if { !$link_ok } {
375399
if { ![string compare "execute" $compile_type] } {
376400
unresolved "$testcase $testname execute $optstr"
377401
}
@@ -403,17 +427,25 @@ proc lto-can-handle-directive { op } {
403427

404428
# dg-warning and dg-message append to dg-messages.
405429
upvar dg-messages dg-messages
430+
# Track if we have dg-lto-error to expect link failure
431+
global dg_lto_has_error
406432

407433
# A list of directives to recognize, and a list of directives
408434
# to remap them to.
409435
# For example, "dg-lto-warning" is implemented by calling "dg-warning".
410-
set directives { dg-lto-warning dg-lto-message dg-lto-note }
411-
set remapped_directives { dg-warning dg-message dg-note }
436+
set directives { dg-lto-warning dg-lto-message dg-lto-note dg-lto-error }
437+
set remapped_directives { dg-warning dg-message dg-note dg-error }
412438

413439
set idx [lsearch -exact $directives $cmd]
414440
if { $idx != -1 } {
415441
verbose "remapping from: $op" 4
416442

443+
# Mark that we have a dg-lto-error directive
444+
if { $cmd == "dg-lto-error" } {
445+
set dg_lto_has_error 1
446+
verbose "lto.exp: dg-lto-error detected, expecting link failure" 2
447+
}
448+
417449
set remapped_cmd [lindex $remapped_directives $idx]
418450
set op [lreplace $op 0 0 $remapped_cmd]
419451

@@ -629,6 +661,7 @@ proc lto-execute-1 { src1 sid } {
629661
global LTO_OPTIONS
630662
global dg-final-code
631663
global testname_with_flags
664+
global dg_lto_has_error
632665

633666
# Get extra flags for this test from the primary source file, and
634667
# process other dg-* options that this suite supports. Warn about
@@ -637,6 +670,7 @@ proc lto-execute-1 { src1 sid } {
637670
set compile_type "run"
638671
set dg-do-what [list ${dg-do-what-default} "" P]
639672
array set dg-messages-by-file [list]
673+
set dg_lto_has_error 0
640674
set extra_flags(0) [lto-get-options-main $src1]
641675
set compile_xfail(0) ""
642676

0 commit comments

Comments
 (0)