]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb] Rewrite producer_is_gcc and producer_is_clang
authorTom de Vries <tdevries@suse.de>
Mon, 17 Nov 2025 16:33:03 +0000 (17:33 +0100)
committerTom de Vries <tdevries@suse.de>
Mon, 17 Nov 2025 16:33:03 +0000 (17:33 +0100)
Rewrite functions producer_is_gcc and producer_is_clang into an early-exit
style.

Tested on x86_64-linux.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
gdb/producer.c

index baac115c587564f773fd0f409ad92673706c0183..48c21775f568dec22b8b682329b7d89637aa2a9e 100644 (file)
@@ -42,40 +42,38 @@ producer_is_gcc_ge_4 (const char *producer)
 bool
 producer_is_gcc (const char *producer, int *major, int *minor)
 {
-  const char *cs;
+  if (producer == nullptr)
+    return false;
 
-  if (producer != nullptr && startswith (producer, "GNU "))
-    {
-      int maj, min;
-
-      if (major == nullptr)
-       major = &maj;
-      if (minor == nullptr)
-       minor = &min;
-
-      /* Skip GNU.  */
-      cs = &producer[strlen ("GNU ")];
-
-      /* Bail out for GNU AS.  */
-      if (startswith (cs, "AS "))
-       return false;
-
-      /* Skip any identifier after "GNU " - such as "C11" "C++" or "Java".
-        A full producer string might look like:
-        "GNU C 4.7.2"
-        "GNU Fortran 4.8.2 20140120 (Red Hat 4.8.2-16) -mtune=generic ..."
-        "GNU C++14 5.0.0 20150123 (experimental)"
-      */
-      while (*cs && !c_isspace (*cs))
-       cs++;
-      if (*cs && c_isspace (*cs))
-       cs++;
-      if (sscanf (cs, "%d.%d", major, minor) == 2)
-       return true;
-    }
+  const char gnu_prefix[] = "GNU ";
+  if (!startswith (producer, gnu_prefix))
+    return false;
 
-  /* Not recognized as GCC.  */
-  return false;
+  /* Skip "GNU " prefix.  */
+  const char *cs = &producer[strlen (gnu_prefix)];
+
+  /* Bail out for "GNU AS ".  */
+  if (startswith (cs, "AS "))
+    return false;
+
+  /* Skip any identifier after "GNU " - such as "C11" "C++" or "Java".
+     A full producer string might look like:
+     "GNU C 4.7.2"
+     "GNU Fortran 4.8.2 20140120 (Red Hat 4.8.2-16) -mtune=generic ..."
+     "GNU C++14 5.0.0 20150123 (experimental)".  */
+  while (*cs && !c_isspace (*cs))
+    cs++;
+  if (*cs && c_isspace (*cs))
+    cs++;
+
+  int maj, min;
+  if (major == nullptr)
+    major = &maj;
+  if (minor == nullptr)
+    minor = &min;
+  int matches = sscanf (cs, "%d.%d", major, minor);
+
+  return matches == 2;
 }
 
 /* See producer.h.  */
@@ -177,23 +175,24 @@ producer_is_llvm (const char *producer)
 bool
 producer_is_clang (const char *producer, int *major, int *minor)
 {
-  if (producer != nullptr && startswith (producer, "clang version "))
-    {
-      int maj, min;
-      if (major == nullptr)
-       major = &maj;
-      if (minor == nullptr)
-       minor = &min;
-
-      /* The full producer string will look something like
-        "clang version XX.X.X ..."
-        So we can safely ignore all characters before the first digit.  */
-      const char *cs = producer + strlen ("clang version ");
-
-      if (sscanf (cs, "%d.%d", major, minor) == 2)
-       return true;
-    }
-  return false;
+  if (producer == nullptr)
+    return false;
+
+  const char clang_prefix[] = "clang version ";
+  if (!startswith (producer, clang_prefix))
+    return false;
+
+  /* Skip "clang version " prefix.  */
+  const char *cs = producer + strlen (clang_prefix);
+
+  int maj, min;
+  if (major == nullptr)
+    major = &maj;
+  if (minor == nullptr)
+    minor = &min;
+  int matches = sscanf (cs, "%d.%d", major, minor);
+
+  return matches == 2;
 }
 
 /* See producer.h.  */