[gcc(refs/users/marxin/heads/str_starts_with)] RFC: str_starts_with

Martin Liska marxin@gcc.gnu.org
Wed Mar 17 15:38:46 GMT 2021


https://gcc.gnu.org/g:0b9a368ddf36867339edb611c17e6571b569ccbe

commit 0b9a368ddf36867339edb611c17e6571b569ccbe
Author: Martin Liska <mliska@suse.cz>
Date:   Wed Mar 17 16:36:44 2021 +0100

    RFC: str_starts_with

Diff:
---
 gcc/builtins.c          | 10 +++-------
 gcc/c-family/c-common.c |  2 +-
 gcc/c-family/c-format.c |  2 +-
 gcc/collect2.c          | 36 ++++++++++++++++++------------------
 include/libiberty.h     |  8 ++++++++
 5 files changed, 31 insertions(+), 27 deletions(-)

diff --git a/gcc/builtins.c b/gcc/builtins.c
index 196dda3fa5e..19cf74a1707 100644
--- a/gcc/builtins.c
+++ b/gcc/builtins.c
@@ -740,13 +740,9 @@ pointer_query::flush_cache ()
 static bool
 is_builtin_name (const char *name)
 {
-  if (strncmp (name, "__builtin_", 10) == 0)
-    return true;
-  if (strncmp (name, "__sync_", 7) == 0)
-    return true;
-  if (strncmp (name, "__atomic_", 9) == 0)
-    return true;
-  return false;
+  return (str_starts_with (name, "__builtin_")
+	  || str_starts_with (name, "__sync_")
+	  || str_starts_with (name, "__atomic_"));
 }
 
 /* Return true if NODE should be considered for inline expansion regardless
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index d227686a030..3592b3e7fec 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -4670,7 +4670,7 @@ static bool builtin_function_disabled_p (const char *);
 void
 disable_builtin_function (const char *name)
 {
-  if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
+  if (str_starts_with (name, "__builtin_"))
     error ("cannot disable built-in function %qs", name);
   else
     {
diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c
index 0a63cacb0d9..f50cf53f811 100644
--- a/gcc/c-family/c-format.c
+++ b/gcc/c-family/c-format.c
@@ -5104,7 +5104,7 @@ convert_format_name_to_system_name (const char *attr_name)
   int i;
 
   if (attr_name == NULL || *attr_name == 0
-      || strncmp (attr_name, "gcc_", 4) == 0)
+      || str_starts_with (attr_name, "gcc_"))
     return attr_name;
 #ifdef TARGET_OVERRIDES_FORMAT_INIT
   TARGET_OVERRIDES_FORMAT_INIT ();
diff --git a/gcc/collect2.c b/gcc/collect2.c
index bd371430ab7..11b3fc5a213 100644
--- a/gcc/collect2.c
+++ b/gcc/collect2.c
@@ -970,7 +970,7 @@ main (int argc, char **argv)
 	  selected_linker = USE_GOLD_LD;
 	else if (strcmp (argv[i], "-fuse-ld=lld") == 0)
 	  selected_linker = USE_LLD_LD;
-	else if (strncmp (argv[i], "-o", 2) == 0)
+	else if (str_starts_with (argv[i], "-o"))
 	  {
 	    /* Parse the output filename if it's given so that we can make
 	       meaningful temp filenames.  */
@@ -984,19 +984,19 @@ main (int argc, char **argv)
 	/* These flags are position independent, although their order
 	   is important - subsequent flags override earlier ones. */
 	else if (strcmp (argv[i], "-b64") == 0)
-	    aix64_flag = 1;
+	  aix64_flag = 1;
 	/* -bexport:filename always needs the :filename */
-	else if (strncmp (argv[i], "-bE:", 4) == 0
-	      || strncmp (argv[i], "-bexport:", 9) == 0)
-	    export_flag = 1;
+	else if (str_starts_with (argv[i], "-bE:")
+		 || str_starts_with (argv[i], "-bexport:"))
+	  export_flag = 1;
 	else if (strcmp (argv[i], "-brtl") == 0
 	      || strcmp (argv[i], "-bsvr4") == 0
 	      || strcmp (argv[i], "-G") == 0)
-	    aixrtl_flag = 1;
+	  aixrtl_flag = 1;
 	else if (strcmp (argv[i], "-bnortl") == 0)
-	    aixrtl_flag = 0;
+	  aixrtl_flag = 0;
 	else if (strcmp (argv[i], "-blazy") == 0)
-	    aixlazy_flag = 1;
+	  aixlazy_flag = 1;
 #endif
       }
 
@@ -1016,11 +1016,11 @@ main (int argc, char **argv)
 	const char *q = extract_string (&p);
 	if (*q == '-' && (q[1] == 'm' || q[1] == 'f'))
 	  num_c_args++;
-	if (strncmp (q, "-flto-partition=none", 20) == 0)
+	if (str_starts_with (q, "-flto-partition=none"))
 	  no_partition = true;
-	else if (strncmp (q, "-fno-lto", 8) == 0)
+	else if (str_starts_with (q, "-fno-lto"))
 	  lto_mode = LTO_MODE_NONE;
-	else if (strncmp (q, "-save-temps", 11) == 0)
+	else if (str_starts_with (q, "-save-temps"))
 	  /* FIXME: Honour =obj.  */
 	  save_temps = true;
 	else if (strcmp (q, "-dumpdir") == 0)
@@ -1254,7 +1254,7 @@ main (int argc, char **argv)
 	(void) extract_string (&p);
 #ifdef COLLECT_EXPORT_LIST
       /* Detect any invocation with -fvisibility.  */
-      if (strncmp (q, "-fvisibility", 12) == 0)
+      if (str_starts_with (q, "-fvisibility")
 	visibility_flag = 1;
 #endif
     }
@@ -1303,7 +1303,7 @@ main (int argc, char **argv)
 	      break;
 
             case 'f':
-	      if (strncmp (arg, "-flto", 5) == 0)
+	      if (str_starts_with (arg, "-flto"))
 		{
 #ifdef ENABLE_LTO
 		  /* Do not pass LTO flag to the linker. */
@@ -1315,13 +1315,13 @@ main (int argc, char **argv)
 #endif
 		}
 	      else if (!use_collect_ld
-		       && strncmp (arg, "-fuse-ld=", 9) == 0)
+		       && str_starts_with (arg, "-fuse-ld="))
 		{
 		  /* Do not pass -fuse-ld={bfd|gold|lld} to the linker. */
 		  ld1--;
 		  ld2--;
 		}
-	      else if (strncmp (arg, "-fno-lto", 8) == 0)
+	      else if (str_starts_with (arg, "-fno-lto"))
 		{
 		  /* Do not pass -fno-lto to the linker. */
 		  ld1--;
@@ -1462,7 +1462,7 @@ main (int argc, char **argv)
 		  ld2--;
 #endif
 		}
-	      else if (strncmp (arg, "--demangle", 10) == 0)
+	      else if (str_starts_with (arg, "--demangle"))
 		{
 #ifndef HAVE_LD_DEMANGLE
 		  no_demangle = 0;
@@ -1479,7 +1479,7 @@ main (int argc, char **argv)
 		  ld2--;
 #endif
 		}
-	      else if (strncmp (arg, "--sysroot=", 10) == 0)
+	      else if (str_starts_with (arg, "--sysroot="))
 		target_system_root = arg + 10;
 	      else if (strcmp (arg, "--version") == 0)
 		verbose = true;
@@ -2619,7 +2619,7 @@ scan_libraries (const char *prog_name)
 	continue;
 
       name = p;
-      if (strncmp (name, "not found", sizeof ("not found") - 1) == 0)
+      if (str_starts_with (name, "not found"))
 	fatal_error (input_location, "dynamic dependency %s not found", buf);
 
       /* Find the end of the symbol name.  */
diff --git a/include/libiberty.h b/include/libiberty.h
index f4c0fe11d6f..4e963f51fd4 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -753,6 +753,14 @@ extern unsigned long libiberty_len;
    (char *) memcpy (libiberty_nptr, libiberty_optr, libiberty_len))
 #endif
 
+/* Return 1 if STR string starts with PREFIX.  */
+
+static inline int
+str_starts_with (const char *str, const char *prefix)
+{
+  return __builtin_strncmp (str, prefix, __builtin_strlen (prefix)) == 0;
+}
+
 #ifdef __cplusplus
 }
 #endif


More information about the Gcc-cvs mailing list