[RFA(configure)] c++: provide strchrnul on targets without it [PR107781]

Message ID 20221121233147.523576-1-jason@redhat.com
State New
Headers
Series [RFA(configure)] c++: provide strchrnul on targets without it [PR107781] |

Commit Message

Jason Merrill Nov. 21, 2022, 11:31 p.m. UTC
  Tested x86_64-pc-linux-gnu, and also manually changing the HAVE_DECL_STRCHRNUL
flag.  OK for trunk?

-- 8< --

The Contracts implementation uses strchrnul, which is a glibc extension, so
bootstrap broke on non-glibc targets.  I considered unconditionally using a
local definition, but I guess we might as well use the libc version if it
exists.

	PR c++/107781

gcc/cp/ChangeLog:

	* contracts.cc (strchrnul): Define if needed.

gcc/ChangeLog:

	* configure.ac: Check for strchrnul.
	* config.in, configure: Regenerate.
---
 gcc/cp/contracts.cc | 12 ++++++++++++
 gcc/config.in       |  7 +++++++
 gcc/configure.ac    |  2 +-
 3 files changed, 20 insertions(+), 1 deletion(-)


base-commit: 5c0d171f67d082c353ddc319859111d3b9126c17
prerequisite-patch-id: 275d90e1bd8b940c1cca2840bc38dc4fafa0797b
  

Comments

Jakub Jelinek Nov. 22, 2022, 8:41 a.m. UTC | #1
On Mon, Nov 21, 2022 at 06:31:47PM -0500, Jason Merrill via Gcc-patches wrote:
> Tested x86_64-pc-linux-gnu, and also manually changing the HAVE_DECL_STRCHRNUL
> flag.  OK for trunk?
> 
> -- 8< --
> 
> The Contracts implementation uses strchrnul, which is a glibc extension, so
> bootstrap broke on non-glibc targets.  I considered unconditionally using a
> local definition, but I guess we might as well use the libc version if it
> exists.
> 
> 	PR c++/107781
> 
> gcc/cp/ChangeLog:
> 
> 	* contracts.cc (strchrnul): Define if needed.
> 
> gcc/ChangeLog:
> 
> 	* configure.ac: Check for strchrnul.
> 	* config.in, configure: Regenerate.

Normally we'd add such a local definition to libiberty, shouldn't we do it
in this case too?

	Jakub
  
Jakub Jelinek Nov. 22, 2022, 11:12 a.m. UTC | #2
On Tue, Nov 22, 2022 at 09:41:24AM +0100, Jakub Jelinek via Gcc-patches wrote:
> On Mon, Nov 21, 2022 at 06:31:47PM -0500, Jason Merrill via Gcc-patches wrote:
> > Tested x86_64-pc-linux-gnu, and also manually changing the HAVE_DECL_STRCHRNUL
> > flag.  OK for trunk?
> > 
> > -- 8< --
> > 
> > The Contracts implementation uses strchrnul, which is a glibc extension, so
> > bootstrap broke on non-glibc targets.  I considered unconditionally using a
> > local definition, but I guess we might as well use the libc version if it
> > exists.
> > 
> > 	PR c++/107781
> > 
> > gcc/cp/ChangeLog:
> > 
> > 	* contracts.cc (strchrnul): Define if needed.
> > 
> > gcc/ChangeLog:
> > 
> > 	* configure.ac: Check for strchrnul.
> > 	* config.in, configure: Regenerate.
> 
> Normally we'd add such a local definition to libiberty, shouldn't we do it
> in this case too?

Or use strcspn as Jonathan posted in the PR, at least glibc will handle
it as strchrnul (start, reject[0]) - start early in the strcspn
implementation.

	Jakub
  

Patch

diff --git a/gcc/cp/contracts.cc b/gcc/cp/contracts.cc
index 26396439361..8b11f26ca27 100644
--- a/gcc/cp/contracts.cc
+++ b/gcc/cp/contracts.cc
@@ -204,6 +204,18 @@  lookup_concrete_semantic (const char *name)
   return CCS_INVALID;
 }
 
+#if !HAVE_DECL_STRCHRNUL
+/* strchrnul is a glibc extension.  */
+
+static const char *
+strchrnul (const char *s, char c)
+{
+  if (auto p = strchr (s, c))
+    return p;
+  return strchr (s, '\0');
+}
+#endif
+
 /* Compare role and name up to either the NUL terminator or the first
    occurrence of colon.  */
 
diff --git a/gcc/config.in b/gcc/config.in
index 38ef792bd67..4a5dfb4151c 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -1126,6 +1126,13 @@ 
 #endif
 
 
+/* Define to 1 if we found a declaration for 'strchrnul', otherwise define to 0.
+   */
+#ifndef USED_FOR_TARGET
+#undef HAVE_DECL_STRCHRNUL
+#endif
+
+
 /* Define to 1 if we found a declaration for 'strnlen', otherwise define to 0.
    */
 #ifndef USED_FOR_TARGET
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 7c55bff6cb0..1124ecfa218 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -1581,7 +1581,7 @@  CXXFLAGS="$CXXFLAGS -I${srcdir} -I${srcdir}/../include $GMPINC"
 # normal autoconf function for these.  But force definition of
 # HAVE_DECL_BASENAME like gcc_AC_CHECK_DECLS does, to suppress the bizarre
 # basename handling in libiberty.h.
-AC_CHECK_DECLS([basename(const char*), strstr(const char*,const char*)], , ,[
+AC_CHECK_DECLS([basename(const char*), strchrnul(const char*, int), strstr(const char*,const char*)], , ,[
 #undef HAVE_DECL_BASENAME
 #define HAVE_DECL_BASENAME 1
 #include "ansidecl.h"