Simplify print_doc_line

Message ID 20250408021437.1754876-1-tom@tromey.com
State New
Headers
Series Simplify print_doc_line |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 fail Patch failed to apply

Commit Message

Tom Tromey April 8, 2025, 2:14 a.m. UTC
  print_doc_line uses a static buffer and manually manages memory.  I
think neither of these is really needed, so this patch rewrites the
function to use std::string.  The new implementation tries to avoid
copying when possible.

Regression tested on x86-64 Fedora 40.
---
 gdb/cli/cli-decode.c | 46 ++++++++++++++++----------------------------
 1 file changed, 17 insertions(+), 29 deletions(-)
  

Comments

Keith Seitz April 8, 2025, 5:07 p.m. UTC | #1
On 4/7/25 7:14 PM, Tom Tromey wrote:
> print_doc_line uses a static buffer and manually manages memory.  I
> think neither of these is really needed, so this patch rewrites the
> function to use std::string.  The new implementation tries to avoid
> copying when possible.

Nice cleanup. I find that much more readable. Thank you.

Reviewed-By: Keith Seitz <keiths@redhat.com>

Keith

> Regression tested on x86-64 Fedora 40.
> ---
>   gdb/cli/cli-decode.c | 46 ++++++++++++++++----------------------------
>   1 file changed, 17 insertions(+), 29 deletions(-)
> 
> diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
> index 5008b4881fb..911085a55ed 100644
> --- a/gdb/cli/cli-decode.c
> +++ b/gdb/cli/cli-decode.c
> @@ -2041,40 +2041,28 @@ void
>   print_doc_line (struct ui_file *stream, const char *str,
>   		bool for_value_prefix)
>   {
> -  static char *line_buffer = 0;
> -  static int line_size;
> -  const char *p;
> +  const char *p = strchr (str, '\n');
>   
> -  if (!line_buffer)
> -    {
> -      line_size = 80;
> -      line_buffer = (char *) xmalloc (line_size);
> -    }
> +  /* Only copy the input string if we really need to.  */
> +  std::optional<std::string> line_buffer;
> +  if (p != nullptr)
> +    line_buffer = std::string (str, p);
> +  else if (for_value_prefix)
> +    line_buffer = str;
>   
> -  /* Searches for the first end of line or the end of STR.  */
> -  p = str;
> -  while (*p && *p != '\n')
> -    p++;
> -  if (p - str > line_size - 1)
> -    {
> -      line_size = p - str + 1;
> -      xfree (line_buffer);
> -      line_buffer = (char *) xmalloc (line_size);
> -    }
> -  strncpy (line_buffer, str, p - str);
>     if (for_value_prefix)
>       {
> -      if (islower (line_buffer[0]))
> -	line_buffer[0] = toupper (line_buffer[0]);
> -      gdb_assert (p > str);
> -      if (line_buffer[p - str - 1] == '.')
> -	line_buffer[p - str - 1] = '\0';
> -      else
> -	line_buffer[p - str] = '\0';
> +      char &c = (*line_buffer)[0];
> +      if (islower (c))
> +	c = toupper (c);
> +      if (line_buffer->back () == '.')
> +	line_buffer->pop_back ();
>       }
> -  else
> -    line_buffer[p - str] = '\0';
> -  gdb_puts (line_buffer, stream);
> +
> +  gdb_puts (line_buffer.has_value ()
> +	    ? line_buffer->c_str ()
> +	    : str,
> +	    stream);
>   }
>   
>   /* Print one-line help for command C.
  

Patch

diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 5008b4881fb..911085a55ed 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -2041,40 +2041,28 @@  void
 print_doc_line (struct ui_file *stream, const char *str,
 		bool for_value_prefix)
 {
-  static char *line_buffer = 0;
-  static int line_size;
-  const char *p;
+  const char *p = strchr (str, '\n');
 
-  if (!line_buffer)
-    {
-      line_size = 80;
-      line_buffer = (char *) xmalloc (line_size);
-    }
+  /* Only copy the input string if we really need to.  */
+  std::optional<std::string> line_buffer;
+  if (p != nullptr)
+    line_buffer = std::string (str, p);
+  else if (for_value_prefix)
+    line_buffer = str;
 
-  /* Searches for the first end of line or the end of STR.  */
-  p = str;
-  while (*p && *p != '\n')
-    p++;
-  if (p - str > line_size - 1)
-    {
-      line_size = p - str + 1;
-      xfree (line_buffer);
-      line_buffer = (char *) xmalloc (line_size);
-    }
-  strncpy (line_buffer, str, p - str);
   if (for_value_prefix)
     {
-      if (islower (line_buffer[0]))
-	line_buffer[0] = toupper (line_buffer[0]);
-      gdb_assert (p > str);
-      if (line_buffer[p - str - 1] == '.')
-	line_buffer[p - str - 1] = '\0';
-      else
-	line_buffer[p - str] = '\0';
+      char &c = (*line_buffer)[0];
+      if (islower (c))
+	c = toupper (c);
+      if (line_buffer->back () == '.')
+	line_buffer->pop_back ();
     }
-  else
-    line_buffer[p - str] = '\0';
-  gdb_puts (line_buffer, stream);
+
+  gdb_puts (line_buffer.has_value ()
+	    ? line_buffer->c_str ()
+	    : str,
+	    stream);
 }
 
 /* Print one-line help for command C.