diff mbox

Remove unnecessary calls to strchr.

Message ID 20141125121504.GA103744@msticlxl7.ims.intel.com
State New
Headers show

Commit Message

Ilya Tocar Nov. 25, 2014, 12:15 p.m. UTC
Hi,

As proposed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63853
this patch replaces some function calls with pointer arithmetic.
I didn't mention PR in Changelog, as they are not actually related.
Ok for trunk?


gcc/
	* gcc.c (handle_foffload_option): Remove unnecessary calls to strchr,
	strlen, strncpy.
	* lto-wrapper.c (append_offload_options): Likewise.

---
 gcc/gcc.c         | 24 +++++++++++++-----------
 gcc/lto-wrapper.c |  2 +-
 2 files changed, 14 insertions(+), 12 deletions(-)

Comments

Jakub Jelinek Nov. 25, 2014, 12:18 p.m. UTC | #1
On Tue, Nov 25, 2014 at 03:15:04PM +0300, Ilya Tocar wrote:
> As proposed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63853
> this patch replaces some function calls with pointer arithmetic.
> I didn't mention PR in Changelog, as they are not actually related.
> Ok for trunk?
> @@ -3408,8 +3408,7 @@ handle_foffload_option (const char *arg)
>  	  if (n == NULL)
>  	    n = strchr (c, '\0');
>  
> -	  if (strlen (target) == (size_t) (n - c)
> -	      && strncmp (target, c, n - c) == 0)
> +	  if (next - cur == n - c && strncmp (target, c, n - c) == 0)

I suppose you could use memcmp here, you know the string lengths.

> @@ -3431,8 +3433,7 @@ handle_foffload_option (const char *arg)
>  	      if (n == NULL)
>  		n = strchr (c, '\0');
>  
> -	      if (strlen (target) == (size_t) (n - c)
> -		  && strncmp (c, target, n - c) == 0)
> +	      if (next - cur == n - c && strncmp (c, target, n - c) == 0)
>  		break;

And here too.

Ok with or without those changes.

	Jakub
diff mbox

Patch

diff --git a/gcc/gcc.c b/gcc/gcc.c
index 653ca8d..4731eec 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -3384,11 +3384,11 @@  handle_foffload_option (const char *arg)
     {
       next = strchr (cur, ',');
       if (next == NULL)
-	next = strchr (cur, '\0');
+	next = end;
       next = (next > end) ? end : next;
 
       target = XNEWVEC (char, next - cur + 1);
-      strncpy (target, cur, next - cur);
+      memcpy (target, cur, next - cur);
       target[next - cur] = '\0';
 
       /* If 'disable' is passed to the option, stop parsing the option and clean
@@ -3408,8 +3408,7 @@  handle_foffload_option (const char *arg)
 	  if (n == NULL)
 	    n = strchr (c, '\0');
 
-	  if (strlen (target) == (size_t) (n - c)
-	      && strncmp (target, c, n - c) == 0)
+	  if (next - cur == n - c && strncmp (target, c, n - c) == 0)
 	    break;
 
 	  c = *n ? n + 1 : NULL;
@@ -3420,7 +3419,10 @@  handle_foffload_option (const char *arg)
 		     target);
 
       if (!offload_targets)
-	offload_targets = xstrdup (target);
+	{
+	  offload_targets = target;
+	  target = NULL;
+	}
       else
 	{
 	  /* Check that the target hasn't already presented in the list.  */
@@ -3431,8 +3433,7 @@  handle_foffload_option (const char *arg)
 	      if (n == NULL)
 		n = strchr (c, '\0');
 
-	      if (strlen (target) == (size_t) (n - c)
-		  && strncmp (c, target, n - c) == 0)
+	      if (next - cur == n - c && strncmp (c, target, n - c) == 0)
 		break;