[GCC14] Extend check-function-bodies to allow label and directives

Message ID Z_642BJrZTBwjj5Q@gmail.com
State New
Headers
Series [GCC14] Extend check-function-bodies to allow label and directives |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap fail Patch failed to apply

Commit Message

H.J. Lu April 15, 2025, 8:10 p.m. UTC
  Hi,

I'd like to backport this testsuite enhancement to GCC 14 so that

https://gcc.gnu.org/pipermail/gcc-patches/2025-April/680896.html

can be backported to GCC 14 with testcases unchanged.


H.J.
---
As PR target/116174 shown, we may need to verify labels and the directive
order.  Extend check-function-bodies to support matched output lines to
allow label and directives.

gcc/

	* doc/sourcebuild.texi (check-function-bodies): Add an optional
	argument for matched output lines.

gcc/testsuite/

	* gcc.target/i386/pr116174.c: Use check-function-bodies.
	* lib/scanasm.exp (parse_function_bodies): Append the line if
	$up_config(matched) matches the line.
	(check-function-bodies): Add an argument for matched.  Set
	up_config(matched) to $matched.  Append the expected line without
	$config(line_prefix) to function_regexp if it starts with ".L".

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
(cherry picked from commit d6bb1e257fc414d21bc31faa7ddecbc93a197e3c)
---
 gcc/doc/sourcebuild.texi                 |  9 ++++++---
 gcc/testsuite/gcc.target/i386/pr116174.c | 18 +++++++++++++++---
 gcc/testsuite/lib/scanasm.exp            | 15 +++++++++++++--
 3 files changed, 34 insertions(+), 8 deletions(-)
  

Comments

Richard Biener April 16, 2025, 7:03 a.m. UTC | #1
On Tue, 15 Apr 2025, H.J. Lu wrote:

> Hi,
> 
> I'd like to backport this testsuite enhancement to GCC 14 so that
> 
> https://gcc.gnu.org/pipermail/gcc-patches/2025-April/680896.html
> 
> can be backported to GCC 14 with testcases unchanged.

OK.

> 
> H.J.
> ---
> As PR target/116174 shown, we may need to verify labels and the directive
> order.  Extend check-function-bodies to support matched output lines to
> allow label and directives.
> 
> gcc/
> 
> 	* doc/sourcebuild.texi (check-function-bodies): Add an optional
> 	argument for matched output lines.
> 
> gcc/testsuite/
> 
> 	* gcc.target/i386/pr116174.c: Use check-function-bodies.
> 	* lib/scanasm.exp (parse_function_bodies): Append the line if
> 	$up_config(matched) matches the line.
> 	(check-function-bodies): Add an argument for matched.  Set
> 	up_config(matched) to $matched.  Append the expected line without
> 	$config(line_prefix) to function_regexp if it starts with ".L".
> 
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> (cherry picked from commit d6bb1e257fc414d21bc31faa7ddecbc93a197e3c)
> ---
>  gcc/doc/sourcebuild.texi                 |  9 ++++++---
>  gcc/testsuite/gcc.target/i386/pr116174.c | 18 +++++++++++++++---
>  gcc/testsuite/lib/scanasm.exp            | 15 +++++++++++++--
>  3 files changed, 34 insertions(+), 8 deletions(-)
> 
> diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
> index 23dedef4161..c8130dc1ba9 100644
> --- a/gcc/doc/sourcebuild.texi
> +++ b/gcc/doc/sourcebuild.texi
> @@ -3440,7 +3440,7 @@ assembly output.
>  Passes if @var{symbol} is not defined as a hidden symbol in the test's
>  assembly output.
>  
> -@item check-function-bodies @var{prefix} @var{terminator} [@var{options} [@{ target/xfail @var{selector} @}]]
> +@item check-function-bodies @var{prefix} @var{terminator} [@var{options} [@{ target/xfail @var{selector} @} [@var{matched}]]]
>  Looks through the source file for comments that give the expected assembly
>  output for selected functions.  Each line of expected output starts with the
>  prefix string @var{prefix} and the expected output for a function as a whole
> @@ -3467,8 +3467,11 @@ Depending on the configuration (see
>  @code{configure_check-function-bodies} in
>  @file{gcc/testsuite/lib/scanasm.exp}), the test may discard from the
>  compiler's assembly output directives such as @code{.cfi_startproc},
> -local label definitions such as @code{.LFB0}, and more.
> -It then matches the result against the expected
> +local label definitions such as @code{.LFB0}, and more.  This behavior
> +can be overridden using the optional @var{matched} argument, which
> +specifies a regexp for lines that should not be discarded in this way.
> +
> +The test then matches the result against the expected
>  output for a function as a single regular expression.  This means that
>  later lines can use backslashes to refer back to @samp{(@dots{})}
>  captures on earlier lines.  For example:
> diff --git a/gcc/testsuite/gcc.target/i386/pr116174.c b/gcc/testsuite/gcc.target/i386/pr116174.c
> index 8877d0b51af..686aeb9ff31 100644
> --- a/gcc/testsuite/gcc.target/i386/pr116174.c
> +++ b/gcc/testsuite/gcc.target/i386/pr116174.c
> @@ -1,6 +1,20 @@
>  /* { dg-do compile { target *-*-linux* } } */
> -/* { dg-options "-O2 -fcf-protection=branch" } */
> +/* { dg-options "-O2 -g0 -fcf-protection=branch" } */
> +/* Keep labels and directives ('.p2align', '.cfi_startproc').
> +/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.}  } } */
>  
> +/*
> +**foo:
> +**.LFB0:
> +**	.cfi_startproc
> +** (
> +**	endbr64
> +**	.p2align 5
> +** |
> +**	endbr32
> +** )
> +**...
> +*/
>  char *
>  foo (char *dest, const char *src)
>  {
> @@ -8,5 +22,3 @@ foo (char *dest, const char *src)
>      /* nothing */;
>    return --dest;
>  }
> -
> -/* { dg-final { scan-assembler "\t\.cfi_startproc\n\tendbr(32|64)\n" } } */
> diff --git a/gcc/testsuite/lib/scanasm.exp b/gcc/testsuite/lib/scanasm.exp
> index 6cf9997240d..d1c8e3b5079 100644
> --- a/gcc/testsuite/lib/scanasm.exp
> +++ b/gcc/testsuite/lib/scanasm.exp
> @@ -952,6 +952,9 @@ proc parse_function_bodies { config filename result } {
>  		verbose "parse_function_bodies: $function_name:\n$function_body"
>  		set up_result($function_name) $function_body
>  		set in_function 0
> +	    } elseif { $up_config(matched) ne "" \
> +		       && [regexp $up_config(matched) $line] } {
> +		append function_body $line "\n"
>  	    } elseif { [regexp $up_config(fluff) $line] } {
>  		verbose "parse_function_bodies: $function_name: ignoring fluff line: $line"
>  	    } else {
> @@ -982,7 +985,7 @@ proc check_function_body { functions name body_regexp } {
>  
>  # Check the implementations of functions against expected output.  Used as:
>  #
> -# { dg-do { check-function-bodies PREFIX TERMINATOR[ OPTION[ SELECTOR]] } }
> +# { dg-do { check-function-bodies PREFIX TERMINATOR[ OPTION[ SELECTOR [MATCHED]]] } }
>  #
>  # See sourcebuild.texi for details.
>  
> @@ -990,7 +993,7 @@ proc check-function-bodies { args } {
>      if { [llength $args] < 2 } {
>  	error "too few arguments to check-function-bodies"
>      }
> -    if { [llength $args] > 4 } {
> +    if { [llength $args] > 5 } {
>  	error "too many arguments to check-function-bodies"
>      }
>  
> @@ -1029,6 +1032,11 @@ proc check-function-bodies { args } {
>  	}
>      }
>  
> +    set matched ""
> +    if { [llength $args] >= 5 } {
> +	set matched [lindex $args 4]
> +    }
> +
>      set testcase [testname-for-summary]
>      # The name might include a list of options; extract the file name.
>      set filename [lindex $testcase 0]
> @@ -1048,6 +1056,7 @@ proc check-function-bodies { args } {
>      # (name in \1).  This may be different from '$config(start)'.
>      set start_expected {^(\S+):$}
>  
> +    set config(matched) $matched
>      configure_check-function-bodies config
>      set have_bodies 0
>      if { [is_remote host] } {
> @@ -1090,6 +1099,8 @@ proc check-function-bodies { args } {
>  		append function_regexp ")"
>  	    } elseif { [string equal $line "..."] } {
>  		append function_regexp ".*"
> +	    } elseif { [regexp {^\.L} $line] } {
> +		append function_regexp $line "\n"
>  	    } else {
>  		append function_regexp $config(line_prefix) $line "\n"
>  	    }
>
  

Patch

diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index 23dedef4161..c8130dc1ba9 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -3440,7 +3440,7 @@  assembly output.
 Passes if @var{symbol} is not defined as a hidden symbol in the test's
 assembly output.
 
-@item check-function-bodies @var{prefix} @var{terminator} [@var{options} [@{ target/xfail @var{selector} @}]]
+@item check-function-bodies @var{prefix} @var{terminator} [@var{options} [@{ target/xfail @var{selector} @} [@var{matched}]]]
 Looks through the source file for comments that give the expected assembly
 output for selected functions.  Each line of expected output starts with the
 prefix string @var{prefix} and the expected output for a function as a whole
@@ -3467,8 +3467,11 @@  Depending on the configuration (see
 @code{configure_check-function-bodies} in
 @file{gcc/testsuite/lib/scanasm.exp}), the test may discard from the
 compiler's assembly output directives such as @code{.cfi_startproc},
-local label definitions such as @code{.LFB0}, and more.
-It then matches the result against the expected
+local label definitions such as @code{.LFB0}, and more.  This behavior
+can be overridden using the optional @var{matched} argument, which
+specifies a regexp for lines that should not be discarded in this way.
+
+The test then matches the result against the expected
 output for a function as a single regular expression.  This means that
 later lines can use backslashes to refer back to @samp{(@dots{})}
 captures on earlier lines.  For example:
diff --git a/gcc/testsuite/gcc.target/i386/pr116174.c b/gcc/testsuite/gcc.target/i386/pr116174.c
index 8877d0b51af..686aeb9ff31 100644
--- a/gcc/testsuite/gcc.target/i386/pr116174.c
+++ b/gcc/testsuite/gcc.target/i386/pr116174.c
@@ -1,6 +1,20 @@ 
 /* { dg-do compile { target *-*-linux* } } */
-/* { dg-options "-O2 -fcf-protection=branch" } */
+/* { dg-options "-O2 -g0 -fcf-protection=branch" } */
+/* Keep labels and directives ('.p2align', '.cfi_startproc').
+/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.}  } } */
 
+/*
+**foo:
+**.LFB0:
+**	.cfi_startproc
+** (
+**	endbr64
+**	.p2align 5
+** |
+**	endbr32
+** )
+**...
+*/
 char *
 foo (char *dest, const char *src)
 {
@@ -8,5 +22,3 @@  foo (char *dest, const char *src)
     /* nothing */;
   return --dest;
 }
-
-/* { dg-final { scan-assembler "\t\.cfi_startproc\n\tendbr(32|64)\n" } } */
diff --git a/gcc/testsuite/lib/scanasm.exp b/gcc/testsuite/lib/scanasm.exp
index 6cf9997240d..d1c8e3b5079 100644
--- a/gcc/testsuite/lib/scanasm.exp
+++ b/gcc/testsuite/lib/scanasm.exp
@@ -952,6 +952,9 @@  proc parse_function_bodies { config filename result } {
 		verbose "parse_function_bodies: $function_name:\n$function_body"
 		set up_result($function_name) $function_body
 		set in_function 0
+	    } elseif { $up_config(matched) ne "" \
+		       && [regexp $up_config(matched) $line] } {
+		append function_body $line "\n"
 	    } elseif { [regexp $up_config(fluff) $line] } {
 		verbose "parse_function_bodies: $function_name: ignoring fluff line: $line"
 	    } else {
@@ -982,7 +985,7 @@  proc check_function_body { functions name body_regexp } {
 
 # Check the implementations of functions against expected output.  Used as:
 #
-# { dg-do { check-function-bodies PREFIX TERMINATOR[ OPTION[ SELECTOR]] } }
+# { dg-do { check-function-bodies PREFIX TERMINATOR[ OPTION[ SELECTOR [MATCHED]]] } }
 #
 # See sourcebuild.texi for details.
 
@@ -990,7 +993,7 @@  proc check-function-bodies { args } {
     if { [llength $args] < 2 } {
 	error "too few arguments to check-function-bodies"
     }
-    if { [llength $args] > 4 } {
+    if { [llength $args] > 5 } {
 	error "too many arguments to check-function-bodies"
     }
 
@@ -1029,6 +1032,11 @@  proc check-function-bodies { args } {
 	}
     }
 
+    set matched ""
+    if { [llength $args] >= 5 } {
+	set matched [lindex $args 4]
+    }
+
     set testcase [testname-for-summary]
     # The name might include a list of options; extract the file name.
     set filename [lindex $testcase 0]
@@ -1048,6 +1056,7 @@  proc check-function-bodies { args } {
     # (name in \1).  This may be different from '$config(start)'.
     set start_expected {^(\S+):$}
 
+    set config(matched) $matched
     configure_check-function-bodies config
     set have_bodies 0
     if { [is_remote host] } {
@@ -1090,6 +1099,8 @@  proc check-function-bodies { args } {
 		append function_regexp ")"
 	    } elseif { [string equal $line "..."] } {
 		append function_regexp ".*"
+	    } elseif { [regexp {^\.L} $line] } {
+		append function_regexp $line "\n"
 	    } else {
 		append function_regexp $config(line_prefix) $line "\n"
 	    }