[RFA,3/8] Make print_command_trace varargs

Message ID 20180419191539.661-4-tom@tromey.com
State Superseded
Headers show
Series
  • Various command-related improvements
Related show

Commit Message

Tom Tromey April 19, 2018, 7:15 p.m.
I noticed some code in execute_control_command_1 that could be
simplified by making print_command_trace a printf-like function.  This
patch makes this change.

2018-04-19  Tom Tromey  <tom@tromey.com>

	* top.c (execute_command): Update.
	* cli/cli-script.h (print_command_lines): Now varargs.
	* cli/cli-script.c (print_command_lines): Now varargs.
	(execute_control_command_1) <case while_control, case if_control>:
	Update.
---
 gdb/ChangeLog        |  8 ++++++++
 gdb/cli/cli-script.c | 22 ++++++++++------------
 gdb/cli/cli-script.h |  3 ++-
 gdb/top.c            |  2 +-
 4 files changed, 21 insertions(+), 14 deletions(-)

-- 
2.13.6

Comments

Pedro Alves April 24, 2018, 4:43 p.m. | #1
On 04/19/2018 08:15 PM, Tom Tromey wrote:
> I noticed some code in execute_control_command_1 that could be

> simplified by making print_command_trace a printf-like function.  This

> patch makes this change.

> 

> 2018-04-19  Tom Tromey  <tom@tromey.com>

> 

> 	* top.c (execute_command): Update.

> 	* cli/cli-script.h (print_command_lines): Now varargs.

> 	* cli/cli-script.c (print_command_lines): Now varargs.

> 	(execute_control_command_1) <case while_control, case if_control>:

> 	Update.


Looks fine to me.

Thanks,
Pedro Alves

Patch

diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index b066da7d60..c7d405c0d0 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -426,8 +426,9 @@  reset_command_nest_depth (void)
    via while_command or if_command.  Inner levels of 'if' and 'while'
    are dealt with directly.  Therefore we can use these functions
    to determine whether the command has been printed already or not.  */
+ATTRIBUTE_PRINTF (1, 2)
 void
-print_command_trace (const char *cmd)
+print_command_trace (const char *fmt, ...)
 {
   int i;
 
@@ -443,7 +444,12 @@  print_command_trace (const char *cmd)
   for (i=0; i < command_nest_depth; i++)
     printf_filtered ("+");
 
-  printf_filtered ("%s\n", cmd);
+  va_list args;
+
+  va_start (args, fmt);
+  vprintf_filtered (fmt, args);
+  va_end (args);
+  puts_filtered ("\n");
 }
 
 /* Helper for execute_control_command.  */
@@ -490,11 +496,7 @@  execute_control_command_1 (struct command_line *cmd)
 
     case while_control:
       {
-	int len = strlen (cmd->line) + 7;
-	char *buffer = (char *) alloca (len);
-
-	xsnprintf (buffer, len, "while %s", cmd->line);
-	print_command_trace (buffer);
+	print_command_trace ("while %s", cmd->line);
 
 	/* Parse the loop control expression for the while statement.  */
 	std::string new_line = insert_user_defined_cmd_args (cmd->line);
@@ -555,11 +557,7 @@  execute_control_command_1 (struct command_line *cmd)
 
     case if_control:
       {
-	int len = strlen (cmd->line) + 4;
-	char *buffer = (char *) alloca (len);
-
-	xsnprintf (buffer, len, "if %s", cmd->line);
-	print_command_trace (buffer);
+	print_command_trace ("if %s", cmd->line);
 
 	/* Parse the conditional for the if statement.  */
 	std::string new_line = insert_user_defined_cmd_args (cmd->line);
diff --git a/gdb/cli/cli-script.h b/gdb/cli/cli-script.h
index 58dede2342..10b6c17789 100644
--- a/gdb/cli/cli-script.h
+++ b/gdb/cli/cli-script.h
@@ -148,7 +148,8 @@  extern std::string insert_user_defined_cmd_args (const char *line);
 
 /* Exported to top.c */
 
-extern void print_command_trace (const char *cmd);
+extern void print_command_trace (const char *cmd, ...)
+  ATTRIBUTE_PRINTF (1, 2);
 
 /* Exported to event-top.c */
 
diff --git a/gdb/top.c b/gdb/top.c
index 8903a92983..6f5abd5a3d 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -571,7 +571,7 @@  execute_command (const char *p, int from_tty)
       line = p;
 
       /* If trace-commands is set then this will print this command.  */
-      print_command_trace (p);
+      print_command_trace ("%s", p);
 
       c = lookup_cmd (&cmd, cmdlist, "", 0, 1);
       p = cmd;