[gcc r17-2490] testsuite: Add dg-lto-additional-options directive

Lewis Hyatt via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:8d1f9ea822060a4e77238a33d2eb6ef99991c3d1

commit r17-2490-g8d1f9ea822060a4e77238a33d2eb6ef99991c3d1
Author: Lewis Hyatt <[email protected]>
Date:   Sat Jul 11 14:21:12 2026 -0400

    testsuite: Add dg-lto-additional-options directive
    
    In the LTO testsuite, tests run by default with a variety of options. If
    the dg-lto-options directive is used, the default list is replaced with just
    the requested options, so there is no convenient way, for instance, to add a
    given option like -Wall to all of the options being tested.
    
    This will be handy for testing `#pragma GCC diagnostic' in the next patch in
    this series, so this patch adds a new directive dg-lto-additional-options
    for that purpose. This keeps the list of options to test unchanged, and
    just adds the requested options to all of them.
    
    gcc/ChangeLog:
    
            * doc/sourcebuild.texi: Document dg-lto-additional-options
            directive.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/lto/README: Document dg-lto-additional-options directive.
            * lib/lto.exp (lto-get-options-main): Implement
            dg-lto-additional-options directive.
            (lto-execute-1): Likewise.

Diff:
---
 gcc/doc/sourcebuild.texi        | 13 +++++++++++--
 gcc/testsuite/gcc.dg/lto/README |  2 ++
 gcc/testsuite/lib/lto.exp       | 22 ++++++++++++++++++----
 3 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index a377739edb66..6c0a3a4c74f9 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -4134,8 +4134,17 @@ Unlike @code{dg-do}, @code{dg-lto-do} does not support an optional
 
 @item @{ dg-lto-options @{ @{ @var{options} @} [@{ @var{options} @}] @} [@{ target @var{selector} @}]@}
 This directive provides a list of one or more sets of compiler options
-to override @var{LTO_OPTIONS}.  Each test will be compiled and run with
-each of these sets of options.
+to override @var{LTO_OPTIONS}.  Every test file will be compiled and
+run once for each of these sets of options.  Individual source files
+within a test may also use @code{dg-options} to specify additional
+options to include.
+
+@item @{ dg-lto-additional-options @{ @{ @var{options} @} [@{ @var{options} @}] @} [@{ target @var{selector} @}]@}
+This is similar to @code{dg-lto-options}, but rather than overriding
+@var{LTO_OPTIONS}, it appends the given options to each of the sets
+specified in @var{LTO_OPTIONS}.  This can be used to add a given list
+of options to every compilation of all source files in the test,
+without otherwise changing which combinations of options are tested.
 
 @item @{ dg-extra-ld-options @var{options} [@{ target @var{selector} @}]@}
 This directive adds @var{options} to the linker options used.
diff --git a/gcc/testsuite/gcc.dg/lto/README b/gcc/testsuite/gcc.dg/lto/README
index 6777c15766a3..1ee8b00dee8f 100644
--- a/gcc/testsuite/gcc.dg/lto/README
+++ b/gcc/testsuite/gcc.dg/lto/README
@@ -8,6 +8,8 @@ The LTO harness recognizes the following special DejaGnu directives:
  *  dg-lto-options - the equivalent of dg-options with additional syntax
       to support different sets of options for different files compiled
       as part of the same test case,
+ *  dg-lto-additional-options - the equivalent of dg-additional-options;
+      adds to the default options instead of overwriting them
  *  dg-lto-warning - the equivalent of dg-warning for diagnostics expected
       to be emitted at LTO link time,
  *  dg-lto-message - the equivakent of dg-message for informational notes
diff --git a/gcc/testsuite/lib/lto.exp b/gcc/testsuite/lib/lto.exp
index 036d7e6cc86c..2cff54b699d0 100644
--- a/gcc/testsuite/lib/lto.exp
+++ b/gcc/testsuite/lib/lto.exp
@@ -507,6 +507,7 @@ proc lto-get-options-main { src } {
 
     # dg-options sets a variable called dg-extra-tool-flags.
     set dg-extra-tool-flags ""
+    set lto-additional-tool-flags ""
 
     # dg-options sets a variable called tool_flags.
     set tool_flags ""
@@ -541,6 +542,18 @@ proc lto-get-options-main { src } {
 		unresolved "$src: $errmsg for \"$op\""
 		return
 	    }
+	} elseif { [string match "dg-lto-additional-options" $cmd] } {
+	    set op [lreplace $op 0 0 "dg-additional-options"]
+	    set saved-dg-extra-tool-flags ${dg-extra-tool-flags}
+	    set dg-extra-tool-flags ${lto-additional-tool-flags}
+	    set status [catch "$op" errmsg]
+	    if { $status != 0 } {
+		perror "src: $errmsg for \"$op\"\n"
+		unresolved "$src: $errmsg for \"$op\""
+		return
+	    }
+	    set lto-additional-tool-flags ${dg-extra-tool-flags}
+	    set dg-extra-tool-flags ${saved-dg-extra-tool-flags}
 	} elseif { ![string compare "dg-xfail-if" $cmd] \
 		   || ![string compare "dg-options" $cmd] } {
 	    warning "lto.exp does not support $cmd in primary source file"
@@ -610,7 +623,8 @@ proc lto-get-options-main { src } {
     # Return flags to use for compiling the primary source file and for
     # linking.
     verbose "dg-extra-tool-flags for main is ${dg-extra-tool-flags}"
-    return ${dg-extra-tool-flags}
+    verbose "lto-additional-tool-flags for main is ${lto-additional-tool-flags}"
+    return [list ${dg-extra-tool-flags} ${lto-additional-tool-flags}]
 }
 
 
@@ -692,7 +706,7 @@ proc lto-execute-1 { src1 sid } {
     set compile_type "run"
     set dg-do-what [list ${dg-do-what-default} "" P]
     array set dg-messages-by-file [list]
-    set extra_flags(0) [lto-get-options-main $src1]
+    lassign [lto-get-options-main $src1] extra_flags(0) additional-flags
     set compile_xfail(0) "" 
 
     # If the main file defines dg-options, those flags are used to
@@ -821,7 +835,7 @@ proc lto-execute-1 { src1 sid } {
 	# Compile pieces with the compiler under test.
 	set i 0
 	foreach src $src_list obj $obj_list {
-	    lto-obj $src $obj $option $extra_flags($i) $option \
+	    lto-obj $src $obj $option "$extra_flags($i) ${additional-flags}" $option \
 		    $compile_xfail($i)
 	    incr i
 	}
@@ -857,7 +871,7 @@ proc lto-execute-1 { src1 sid } {
 
 	    lto-link-and-maybe-run \
 		    "[lindex $obj_list 0]-[lindex $obj_list end]" \
-		    $obj_list $execname $filtered ${dg-extra-ld-options} \
+		$obj_list $execname $filtered "${additional-flags} ${dg-extra-ld-options}" \
 		    $filtered
 
 	    if (![string compare "ar-link" $compile_type]) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.