gdb: make use of std::string in utils.c
authorAndrew Burgess <andrew.burgess@embecosm.com>
Tue, 17 Aug 2021 12:29:22 +0000 (13:29 +0100)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Tue, 7 Sep 2021 14:10:37 +0000 (15:10 +0100)
Replace some of the manual string management (malloc/free) with
std::string when creating commands in utils.c.

Things are a little bit messy as, creating the prefix commands (using
add_basic_prefix_cmd and add_show_prefix_cmd), doesn't copy the doc
string, while creating the actual set/show commands (using
add_setshow_enum_cmd) does copy the doc string.

As a result, I have retained the use of xstrprintf when creating the
prefix command doc strings, but switched to using std::string when
creating the actual set/show commands.

There should be no user visible changes after this commit.

gdb/utils.c

index 0009cb10d87b1dcb67af253568486f6213e47c02..0a7c270b40db2646170a609b5189df4fee9fafaa 100644 (file)
@@ -508,19 +508,21 @@ add_internal_problem_command (struct internal_problem *problem)
 {
   struct cmd_list_element **set_cmd_list;
   struct cmd_list_element **show_cmd_list;
-  char *set_doc;
-  char *show_doc;
 
   set_cmd_list = XNEW (struct cmd_list_element *);
   show_cmd_list = XNEW (struct cmd_list_element *);
   *set_cmd_list = NULL;
   *show_cmd_list = NULL;
 
-  set_doc = xstrprintf (_("Configure what GDB does when %s is detected."),
-                       problem->name);
-
-  show_doc = xstrprintf (_("Show what GDB does when %s is detected."),
-                        problem->name);
+  /* The add_basic_prefix_cmd and add_show_prefix_cmd functions take
+     ownership of the string passed in, which is why we don't need to free
+     set_doc and show_doc in this function.  */
+  const char *set_doc
+    = xstrprintf (_("Configure what GDB does when %s is detected."),
+                 problem->name);
+  const char *show_doc
+    = xstrprintf (_("Show what GDB does when %s is detected."),
+                 problem->name);
 
   add_basic_prefix_cmd (problem->name, class_maintenance, set_doc,
                        set_cmd_list,
@@ -532,48 +534,42 @@ add_internal_problem_command (struct internal_problem *problem)
 
   if (problem->user_settable_should_quit)
     {
-      set_doc = xstrprintf (_("Set whether GDB should quit "
-                             "when an %s is detected."),
-                           problem->name);
-      show_doc = xstrprintf (_("Show whether GDB will quit "
-                              "when an %s is detected."),
-                            problem->name);
+      std::string set_quit_doc
+       = string_printf (_("Set whether GDB should quit when an %s is "
+                          "detected."), problem->name);
+      std::string show_quit_doc
+       = string_printf (_("Show whether GDB will quit when an %s is "
+                          "detected."), problem->name);
       add_setshow_enum_cmd ("quit", class_maintenance,
                            internal_problem_modes,
                            &problem->should_quit,
-                           set_doc,
-                           show_doc,
+                           set_quit_doc.c_str (),
+                           show_quit_doc.c_str (),
                            NULL, /* help_doc */
                            NULL, /* setfunc */
                            NULL, /* showfunc */
                            set_cmd_list,
                            show_cmd_list);
-
-      xfree (set_doc);
-      xfree (show_doc);
     }
 
   if (problem->user_settable_should_dump_core)
     {
-      set_doc = xstrprintf (_("Set whether GDB should create a core "
-                             "file of GDB when %s is detected."),
-                           problem->name);
-      show_doc = xstrprintf (_("Show whether GDB will create a core "
-                              "file of GDB when %s is detected."),
-                            problem->name);
+      std::string set_core_doc
+       = string_printf (_("Set whether GDB should create a core file of "
+                          "GDB when %s is detected."), problem->name);
+      std::string show_core_doc
+       = string_printf (_("Show whether GDB will create a core file of "
+                          "GDB when %s is detected."), problem->name);
       add_setshow_enum_cmd ("corefile", class_maintenance,
                            internal_problem_modes,
                            &problem->should_dump_core,
-                           set_doc,
-                           show_doc,
+                           set_core_doc.c_str (),
+                           show_core_doc.c_str (),
                            NULL, /* help_doc */
                            NULL, /* setfunc */
                            NULL, /* showfunc */
                            set_cmd_list,
                            show_cmd_list);
-
-      xfree (set_doc);
-      xfree (show_doc);
     }
 }