[PATCH] Add startswith function and use it instead of CONST_STRNEQ.

Alan Modra amodra@gmail.com
Sat Mar 20 07:00:37 GMT 2021


On Fri, Mar 19, 2021 at 01:44:38PM +0100, Martin Liška wrote:
> @@ -73,6 +64,13 @@ extern "C" {
>  #define LITMEMCPY(DEST,STR2) memcpy ((DEST), (STR2), sizeof (STR2) - 1)
>  #define LITSTRCPY(DEST,STR2) memcpy ((DEST), (STR2), sizeof (STR2))
>  
> +/* Return 1 if STR string starts with PREFIX.  */
> +
> +static inline int
> +startswith (const char *str, const char *prefix)
> +{
> +  return __builtin_strncmp (str, prefix, __builtin_strlen (prefix)) == 0;
> +}
>  
>  #define BFD_SUPPORTS_PLUGINS @supports_plugins@
>  

In binutils, we haven't yet made a policy that the project requires
gcc, so builtins can't be used without a fallback.  I tried building
with the following but it runs into a compilation failure in gdb.  I
expect your patch would do the same..

In file included from /home/alan/src/binutils-gdb/gdb/defs.h:37,
                 from /home/alan/src/binutils-gdb/gdb/gdb.c:19:
../bfd/bfd.h:85:1: error: ambiguating new declaration of ‘int startswith(const char*, const char*)’
 startswith (const char *str, const char *prefix)
 ^~~~~~~~~~
In file included from /home/alan/src/binutils-gdb/gdb/../gdbsupport/common-defs.h:125,
                 from /home/alan/src/binutils-gdb/gdb/defs.h:28,
                 from /home/alan/src/binutils-gdb/gdb/gdb.c:19:
/home/alan/src/binutils-gdb/gdb/../gdbsupport/common-utils.h:122:1: note: old declaration ‘bool startswith(const char*, const char*)’
 startswith (const char *string, const char *pattern)
 ^~~~~~~~~~

Forcing gdb to remove their startswith is a bit rude.  Should I use
#ifndef __cplusplus or #ifndef gdb_assert around the bfd version?


bfd/
	* bfd-in.h (startswith): New inline.
	(CONST_STRNEQ): Use startswith.
	* bfd-in2.h: Regenerate.
libctf/
	* ctf-impl.h: Include string.h.

diff --git a/bfd/bfd-in.h b/bfd/bfd-in.h
index c9a7673147..4d99c21d3e 100644
--- a/bfd/bfd-in.h
+++ b/bfd/bfd-in.h
@@ -65,7 +65,6 @@ extern "C" {
    definition of strncmp is provided here.
 
    Note - these macros do NOT work if STR2 is not a constant string.  */
-#define CONST_STRNEQ(STR1,STR2) (strncmp ((STR1), (STR2), sizeof (STR2) - 1) == 0)
   /* strcpy() can have a similar problem, but since we know we are
      copying a constant string, we can use memcpy which will be faster
      since there is no need to check for a NUL byte inside STR.  We
@@ -73,6 +72,15 @@ extern "C" {
 #define LITMEMCPY(DEST,STR2) memcpy ((DEST), (STR2), sizeof (STR2) - 1)
 #define LITSTRCPY(DEST,STR2) memcpy ((DEST), (STR2), sizeof (STR2))
 
+/* Return 1 if STR string starts with PREFIX.  */
+
+static inline int
+startswith (const char *str, const char *prefix)
+{
+  return strncmp (str, prefix, strlen (prefix)) == 0;
+}
+#define CONST_STRNEQ(STR1,STR2) startswith (STR1, STR2)
+
 
 #define BFD_SUPPORTS_PLUGINS @supports_plugins@
 
diff --git a/libctf/ctf-impl.h b/libctf/ctf-impl.h
index ad4af32e7e..342d2ff23e 100644
--- a/libctf/ctf-impl.h
+++ b/libctf/ctf-impl.h
@@ -32,6 +32,7 @@
 #include <stddef.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <string.h>
 #include <limits.h>
 #include <ctype.h>
 #include <elf.h>


-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list