public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: "Martin Liška" <mliska@suse.cz>
Cc: binutils@sourceware.org, gdb-patches@sourceware.org
Subject: Re: [PATCH] Add startswith function and use it instead of CONST_STRNEQ.
Date: Sat, 20 Mar 2021 17:30:37 +1030	[thread overview]
Message-ID: <20210320070037.GR6791@bubble.grove.modra.org> (raw)
In-Reply-To: <b0e46fde-465a-8884-d3e7-2c441a4a62c1@suse.cz>

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

  parent reply	other threads:[~2021-03-20  7:00 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18 14:20 [PATCH] [RFC] Come up with startswith function Martin Liška
2021-03-18 14:54 ` Tom Tromey
2021-03-18 15:26   ` Martin Liška
2021-03-18 18:00     ` Tom Tromey
2021-03-18 18:29 ` Hans-Peter Nilsson
2021-03-19  6:37   ` Alan Modra
2021-03-19 12:44     ` [PATCH] Add startswith function and use it instead of CONST_STRNEQ Martin Liška
2021-03-19 17:55       ` Hans-Peter Nilsson
2021-03-20  7:00       ` Alan Modra [this message]
2021-03-20 18:58         ` Tom Tromey
2021-03-21 13:12           ` Alan Modra
2021-03-22  2:13             ` Tom Tromey
2021-03-22 12:06               ` Alan Modra
2021-03-22 16:13                 ` Luis Machado
2021-03-22 22:56                   ` Alan Modra
2021-03-25 10:53                     ` Luis Machado
2021-03-25 11:54                       ` Alan Modra
2021-03-25 12:05                         ` Luis Machado
2021-03-25 19:47                         ` Luis Machado
2021-03-25 22:31                           ` Mike Frysinger
2021-03-26 11:44                             ` Luis Machado
2021-03-30 11:58                               ` Luis Machado
2021-03-31 13:12                                 ` Martin Liška
2021-03-31 13:44                                   ` Luis Machado
2021-03-22 16:42                 ` Martin Liška
2021-03-23  0:02                   ` Alan Modra
2021-03-23  4:49                     ` Mike Frysinger
2021-03-24  8:19                     ` [PATCH 0/5] Start using startswith instead of strncmp Martin Liska
2021-03-18 14:16                       ` [PATCH 2/5] Use startswith more for strncmp function calls Martin Liska
2021-03-22 11:12                       ` [PATCH 1/5] Replace const_strneq with startswith Martin Liska
2021-03-22 12:33                       ` [PATCH 3/5] Use startswith in gas subfolder Martin Liska
2021-03-22 13:56                       ` [PATCH 4/5] Remove strneq macro and use startswith Martin Liska
2021-03-23  9:02                       ` [PATCH 5/5] Use startswith in gdb subfolder Martin Liska
2021-03-31 20:26                         ` Tom Tromey
2021-04-01  5:21                       ` [PATCH 0/5] Start using startswith instead of strncmp Martin Liška
2021-04-01 12:47                         ` Alan Modra
2021-04-01 13:03                           ` Martin Liška
2021-03-31 20:18                     ` [PATCH] Add startswith function and use it instead of CONST_STRNEQ Tom Tromey
2021-03-22  6:57             ` Mike Frysinger
2021-03-21 13:19       ` Alan Modra
2021-03-22 10:04         ` Martin Liška

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210320070037.GR6791@bubble.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=gdb-patches@sourceware.org \
    --cc=mliska@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).