Make target_options_to_string return an std::string

Return an std::string instead of a char *, saving some manual freeing.

I only manually tested with "set debug target 1" and "set debug lin-lwp
1", since this only deals with debug output.

gdb/ChangeLog:

	* target.h (target_options_to_string): Return an std::string.
	* target.c (str_comma_list_concat_elem): Return void, use
	std::string.
	(do_option): Likewise.
	(target_options_to_string): Return an std::string.
	* linux-nat.c (linux_nat_target::wait): Adjust.
	* target-debug.h (target_debug_print_options): Adjust.
This commit is contained in:
Simon Marchi 2018-08-07 21:37:40 -04:00
parent b825f3a90e
commit 09ce46f230
5 changed files with 31 additions and 32 deletions

View file

@ -1,3 +1,13 @@
2018-08-07 Simon Marchi <simon.marchi@polymtl.ca>
* target.h (target_options_to_string): Return an std::string.
* target.c (str_comma_list_concat_elem): Return void, use
std::string.
(do_option): Likewise.
(target_options_to_string): Return an std::string.
* linux-nat.c (linux_nat_target::wait): Adjust.
* target-debug.h (target_debug_print_options): Adjust.
2018-08-07 Tom Tromey <tom@tromey.com> 2018-08-07 Tom Tromey <tom@tromey.com>
* Makefile.in (CPPFLAGS): New variable. * Makefile.in (CPPFLAGS): New variable.

View file

@ -3555,14 +3555,11 @@ linux_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
if (debug_linux_nat) if (debug_linux_nat)
{ {
char *options_string; std::string options_string = target_options_to_string (target_options);
options_string = target_options_to_string (target_options);
fprintf_unfiltered (gdb_stdlog, fprintf_unfiltered (gdb_stdlog,
"linux_nat_wait: [%s], [%s]\n", "linux_nat_wait: [%s], [%s]\n",
target_pid_to_str (ptid), target_pid_to_str (ptid),
options_string); options_string.c_str ());
xfree (options_string);
} }
/* Flush the async file first. */ /* Flush the async file first. */

View file

@ -203,10 +203,9 @@ target_debug_print_struct_target_waitstatus_p (struct target_waitstatus *status)
static void static void
target_debug_print_options (int options) target_debug_print_options (int options)
{ {
char *str = target_options_to_string (options); std::string str = target_options_to_string (options);
fputs_unfiltered (str, gdb_stdlog); fputs_unfiltered (str.c_str (), gdb_stdlog);
xfree (str);
} }
static void static void

View file

@ -3441,51 +3441,45 @@ target_continue (ptid_t ptid, enum gdb_signal signal)
target_resume (ptid, 0, signal); target_resume (ptid, 0, signal);
} }
/* Concatenate ELEM to LIST, a comma separate list, and return the /* Concatenate ELEM to LIST, a comma separate list. */
result. The LIST incoming argument is released. */
static char * static void
str_comma_list_concat_elem (char *list, const char *elem) str_comma_list_concat_elem (std::string *list, const char *elem)
{ {
if (list == NULL) if (!list->empty ())
return xstrdup (elem); list->append (", ");
else
return reconcat (list, list, ", ", elem, (char *) NULL); list->append (elem);
} }
/* Helper for target_options_to_string. If OPT is present in /* Helper for target_options_to_string. If OPT is present in
TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET. TARGET_OPTIONS, append the OPT_STR (string version of OPT) in RET.
Returns the new resulting string. OPT is removed from OPT is removed from TARGET_OPTIONS. */
TARGET_OPTIONS. */
static char * static void
do_option (int *target_options, char *ret, do_option (int *target_options, std::string *ret,
int opt, const char *opt_str) int opt, const char *opt_str)
{ {
if ((*target_options & opt) != 0) if ((*target_options & opt) != 0)
{ {
ret = str_comma_list_concat_elem (ret, opt_str); str_comma_list_concat_elem (ret, opt_str);
*target_options &= ~opt; *target_options &= ~opt;
} }
return ret;
} }
char * std::string
target_options_to_string (int target_options) target_options_to_string (int target_options)
{ {
char *ret = NULL; std::string ret;
#define DO_TARG_OPTION(OPT) \ #define DO_TARG_OPTION(OPT) \
ret = do_option (&target_options, ret, OPT, #OPT) do_option (&target_options, &ret, OPT, #OPT)
DO_TARG_OPTION (TARGET_WNOHANG); DO_TARG_OPTION (TARGET_WNOHANG);
if (target_options != 0) if (target_options != 0)
ret = str_comma_list_concat_elem (ret, "unknown???"); str_comma_list_concat_elem (&ret, "unknown???");
if (ret == NULL)
ret = xstrdup ("");
return ret; return ret;
} }

View file

@ -116,9 +116,8 @@ struct syscall
const char *name; const char *name;
}; };
/* Return a pretty printed form of TARGET_OPTIONS. /* Return a pretty printed form of TARGET_OPTIONS. */
Space for the result is malloc'd, caller must free. */ extern std::string target_options_to_string (int target_options);
extern char *target_options_to_string (int target_options);
/* Possible types of events that the inferior handler will have to /* Possible types of events that the inferior handler will have to
deal with. */ deal with. */