[RFA,3/8] Use std::string in utils.c

Message ID 20171001040643.25162-4-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Oct. 1, 2017, 4:06 a.m. UTC
  This converts internal_vproblem and defaulted_query to use
std::string.

2017-09-30  Tom Tromey  <tom@tromey.com>

	* utils.c (internal_vproblem): Use std::string.
	(defaulted_query): Likewise.
---
 gdb/ChangeLog |  5 +++++
 gdb/utils.c   | 45 ++++++++++++++++++++-------------------------
 2 files changed, 25 insertions(+), 25 deletions(-)
  

Comments

Pedro Alves Oct. 3, 2017, 10:50 a.m. UTC | #1
On 10/01/2017 05:06 AM, Tom Tromey wrote:

>    /* Don't allow infinite error/warning recursion.  */
>    {
> @@ -429,18 +428,17 @@ internal_vproblem (struct internal_problem *problem,
>      char *msg;
>  
>      msg = xstrvprintf (fmt, ap);
> -    reason = xstrprintf ("%s:%d: %s: %s\n"
> -			 "A problem internal to GDB has been detected,\n"
> -			 "further debugging may prove unreliable.",
> -			 file, line, problem->name, msg);
> +    reason = string_printf ("%s:%d: %s: %s\n"
> +			    "A problem internal to GDB has been detected,\n"
> +			    "further debugging may prove unreliable.",
> +			    file, line, problem->name, msg);
>      xfree (msg);
> -    make_cleanup (xfree, reason);

Could you please make msg use string_vprintf too while at it, thus
getting rid of that bare xfree?

(Similarly to the defaulted_query change below, though there we
were using a cleanup.)

Looks good to me with that change.

Thanks,
Pedro Alves

> @@ -978,13 +975,12 @@ defaulted_query (const char *ctlstr, const char defchar, va_list args)
>      }
>  
>    /* Format the question outside of the loop, to avoid reusing args.  */
> -  question = xstrvprintf (ctlstr, args);
> -  old_chain = make_cleanup (xfree, question);
> -  prompt = xstrprintf (_("%s%s(%s or %s) %s"),
> -		      annotation_level > 1 ? "\n\032\032pre-query\n" : "",
> -		      question, y_string, n_string,
> -		      annotation_level > 1 ? "\n\032\032query\n" : "");
> -  make_cleanup (xfree, prompt);
> +  std::string question = string_vprintf (ctlstr, args);
> +  std::string prompt
> +    = string_printf (_("%s%s(%s or %s) %s"),
> +		     annotation_level > 1 ? "\n\032\032pre-query\n" : "",
> +		     question.c_str (), y_string, n_string,
> +		     annotation_level > 1 ? "\n\032\032query\n" : "");
>
  
Tom Tromey Oct. 3, 2017, 11:25 a.m. UTC | #2
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> Could you please make msg use string_vprintf too while at it, thus
Pedro> getting rid of that bare xfree?

Sure, I made this change.

Tom
  

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index a17a83c..6744ac0 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,10 @@ 
 2017-09-30  Tom Tromey  <tom@tromey.com>
 
+	* utils.c (internal_vproblem): Use std::string.
+	(defaulted_query): Likewise.
+
+2017-09-30  Tom Tromey  <tom@tromey.com>
+
 	* guile/scm-ports.c (ioscm_with_output_to_port_worker): Update.
 	* top.c (execute_command_to_string): Update.
 	* utils.c (make_cleanup_restore_page_info): Remove.
diff --git a/gdb/utils.c b/gdb/utils.c
index 0c59b4e..7595c21 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -392,8 +392,7 @@  internal_vproblem (struct internal_problem *problem,
   static int dejavu;
   int quit_p;
   int dump_core_p;
-  char *reason;
-  struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
+  std::string reason;
 
   /* Don't allow infinite error/warning recursion.  */
   {
@@ -429,18 +428,17 @@  internal_vproblem (struct internal_problem *problem,
     char *msg;
 
     msg = xstrvprintf (fmt, ap);
-    reason = xstrprintf ("%s:%d: %s: %s\n"
-			 "A problem internal to GDB has been detected,\n"
-			 "further debugging may prove unreliable.",
-			 file, line, problem->name, msg);
+    reason = string_printf ("%s:%d: %s: %s\n"
+			    "A problem internal to GDB has been detected,\n"
+			    "further debugging may prove unreliable.",
+			    file, line, problem->name, msg);
     xfree (msg);
-    make_cleanup (xfree, reason);
   }
 
   /* Fall back to abort_with_message if gdb_stderr is not set up.  */
   if (current_ui == NULL)
     {
-      fputs (reason, stderr);
+      fputs (reason.c_str (), stderr);
       abort_with_message ("\n");
     }
 
@@ -458,7 +456,7 @@  internal_vproblem (struct internal_problem *problem,
   if (problem->should_quit != internal_problem_ask
       || !confirm
       || !filtered_printing_initialized ())
-    fprintf_unfiltered (gdb_stderr, "%s\n", reason);
+    fprintf_unfiltered (gdb_stderr, "%s\n", reason.c_str ());
 
   if (problem->should_quit == internal_problem_ask)
     {
@@ -468,7 +466,8 @@  internal_vproblem (struct internal_problem *problem,
       if (!confirm || !filtered_printing_initialized ())
 	quit_p = 1;
       else
-        quit_p = query (_("%s\nQuit this debugging session? "), reason);
+        quit_p = query (_("%s\nQuit this debugging session? "),
+			reason.c_str ());
     }
   else if (problem->should_quit == internal_problem_yes)
     quit_p = 1;
@@ -485,7 +484,7 @@  internal_vproblem (struct internal_problem *problem,
 
   if (problem->should_dump_core == internal_problem_ask)
     {
-      if (!can_dump_core_warn (LIMIT_MAX, reason))
+      if (!can_dump_core_warn (LIMIT_MAX, reason.c_str ()))
 	dump_core_p = 0;
       else if (!filtered_printing_initialized ())
 	dump_core_p = 1;
@@ -494,11 +493,12 @@  internal_vproblem (struct internal_problem *problem,
 	  /* Default (yes/batch case) is to dump core.  This leaves a GDB
 	     `dropping' so that it is easier to see that something went
 	     wrong in GDB.  */
-	  dump_core_p = query (_("%s\nCreate a core file of GDB? "), reason);
+	  dump_core_p = query (_("%s\nCreate a core file of GDB? "),
+			       reason.c_str ());
 	}
     }
   else if (problem->should_dump_core == internal_problem_yes)
-    dump_core_p = can_dump_core_warn (LIMIT_MAX, reason);
+    dump_core_p = can_dump_core_warn (LIMIT_MAX, reason.c_str ());
   else if (problem->should_dump_core == internal_problem_no)
     dump_core_p = 0;
   else
@@ -523,7 +523,6 @@  internal_vproblem (struct internal_problem *problem,
     }
 
   dejavu = 0;
-  do_cleanups (cleanup);
 }
 
 static struct internal_problem internal_error_problem = {
@@ -915,8 +914,6 @@  defaulted_query (const char *ctlstr, const char defchar, va_list args)
   int def_value;
   char def_answer, not_def_answer;
   const char *y_string, *n_string;
-  char *question, *prompt;
-  struct cleanup *old_chain;
 
   /* Set up according to which answer is the default.  */
   if (defchar == '\0')
@@ -978,13 +975,12 @@  defaulted_query (const char *ctlstr, const char defchar, va_list args)
     }
 
   /* Format the question outside of the loop, to avoid reusing args.  */
-  question = xstrvprintf (ctlstr, args);
-  old_chain = make_cleanup (xfree, question);
-  prompt = xstrprintf (_("%s%s(%s or %s) %s"),
-		      annotation_level > 1 ? "\n\032\032pre-query\n" : "",
-		      question, y_string, n_string,
-		      annotation_level > 1 ? "\n\032\032query\n" : "");
-  make_cleanup (xfree, prompt);
+  std::string question = string_vprintf (ctlstr, args);
+  std::string prompt
+    = string_printf (_("%s%s(%s or %s) %s"),
+		     annotation_level > 1 ? "\n\032\032pre-query\n" : "",
+		     question.c_str (), y_string, n_string,
+		     annotation_level > 1 ? "\n\032\032query\n" : "");
 
   /* Used to add duration we waited for user to respond to
      prompt_for_continue_wait_time.  */
@@ -998,7 +994,7 @@  defaulted_query (const char *ctlstr, const char defchar, va_list args)
       char *response, answer;
 
       gdb_flush (gdb_stdout);
-      response = gdb_readline_wrapper (prompt);
+      response = gdb_readline_wrapper (prompt.c_str ());
 
       if (response == NULL)	/* C-d  */
 	{
@@ -1038,7 +1034,6 @@  defaulted_query (const char *ctlstr, const char defchar, va_list args)
 
   if (annotation_level > 1)
     printf_filtered (("\n\032\032post-query\n"));
-  do_cleanups (old_chain);
   return retval;
 }