-Wwrite-strings: Some constification in gdb/breakpoint.c

The main motivation here is avoiding having to write a couple casts
like these:

     if (!arg)
  -    arg = "";
  +    arg = (char *) "";

in catch_exception_command_1 and catch_exec_command_1.

That requires making ep_parse_optional_if_clause and
check_for_argument take pointers to const strings.  I then tried
propagating the resulting constification all the way, but that was
spiraling out of control, so instead I settled for keeping const and
non-const overloads.

gdb/ChangeLog:
2017-04-05  Pedro Alves  <palves@redhat.com>

	* break-catch-throw.c (handle_gnu_v3_exceptions): Constify
	'cond_string' parameter.
	(extract_exception_regexp): Constify 'string' parameter.
	(catch_exception_command_1): Constify.
	* breakpoint.c (init_catchpoint)
	(create_fork_vfork_event_catchpoint): Constify 'cond_string'
	parameter.
	(ep_parse_optional_if_clause, catch_fork_command_1)
	(catch_exec_command_1): Constify.
	* breakpoint.h (init_catchpoint): Constify 'cond_string'
	parameter.
	(ep_parse_optional_if_clause): Constify.
	* cli/cli-utils.c (remove_trailing_whitespace)
	(check_for_argument): Constify.
	* cli/cli-utils.h (remove_trailing_whitespace): Constify and add
	non-const overload.
	(check_for_argument): Likewise.
This commit is contained in:
Pedro Alves 2017-04-05 19:21:36 +01:00
parent 9b2eba3dcc
commit 63160a4350
6 changed files with 72 additions and 29 deletions

View file

@ -1,3 +1,23 @@
2017-04-05 Pedro Alves <palves@redhat.com>
* break-catch-throw.c (handle_gnu_v3_exceptions): Constify
'cond_string' parameter.
(extract_exception_regexp): Constify 'string' parameter.
(catch_exception_command_1): Constify.
* breakpoint.c (init_catchpoint)
(create_fork_vfork_event_catchpoint): Constify 'cond_string'
parameter.
(ep_parse_optional_if_clause, catch_fork_command_1)
(catch_exec_command_1): Constify.
* breakpoint.h (init_catchpoint): Constify 'cond_string'
parameter.
(ep_parse_optional_if_clause): Constify.
* cli/cli-utils.c (remove_trailing_whitespace)
(check_for_argument): Constify.
* cli/cli-utils.h (remove_trailing_whitespace): Constify and add
non-const overload.
(check_for_argument): Likewise.
2017-04-05 Pedro Alves <palves@redhat.com> 2017-04-05 Pedro Alves <palves@redhat.com>
* event-top.c (command_line_handler): Add cast to execute_command * event-top.c (command_line_handler): Add cast to execute_command

View file

@ -383,7 +383,8 @@ print_recreate_exception_catchpoint (struct breakpoint *b,
} }
static void static void
handle_gnu_v3_exceptions (int tempflag, char *except_rx, char *cond_string, handle_gnu_v3_exceptions (int tempflag, char *except_rx,
const char *cond_string,
enum exception_event_kind ex_event, int from_tty) enum exception_event_kind ex_event, int from_tty)
{ {
regex_t *pattern = NULL; regex_t *pattern = NULL;
@ -425,18 +426,18 @@ handle_gnu_v3_exceptions (int tempflag, char *except_rx, char *cond_string,
the end of the string. */ the end of the string. */
static char * static char *
extract_exception_regexp (char **string) extract_exception_regexp (const char **string)
{ {
char *start; const char *start;
char *last, *last_space; const char *last, *last_space;
start = skip_spaces (*string); start = skip_spaces_const (*string);
last = start; last = start;
last_space = start; last_space = start;
while (*last != '\0') while (*last != '\0')
{ {
char *if_token = last; const char *if_token = last;
/* Check for the "if". */ /* Check for the "if". */
if (check_for_argument (&if_token, "if", 2)) if (check_for_argument (&if_token, "if", 2))
@ -444,7 +445,7 @@ extract_exception_regexp (char **string)
/* No "if" token here. Skip to the next word start. */ /* No "if" token here. Skip to the next word start. */
last_space = skip_to_space (last); last_space = skip_to_space (last);
last = skip_spaces (last_space); last = skip_spaces_const (last_space);
} }
*string = last; *string = last;
@ -457,16 +458,18 @@ extract_exception_regexp (char **string)
commands. */ commands. */
static void static void
catch_exception_command_1 (enum exception_event_kind ex_event, char *arg, catch_exception_command_1 (enum exception_event_kind ex_event,
char *arg_entry,
int tempflag, int from_tty) int tempflag, int from_tty)
{ {
char *except_rx; char *except_rx;
char *cond_string = NULL; const char *cond_string = NULL;
struct cleanup *cleanup; struct cleanup *cleanup;
const char *arg = arg_entry;
if (!arg) if (!arg)
arg = ""; arg = "";
arg = skip_spaces (arg); arg = skip_spaces_const (arg);
except_rx = extract_exception_regexp (&arg); except_rx = extract_exception_regexp (&arg);
cleanup = make_cleanup (xfree, except_rx); cleanup = make_cleanup (xfree, except_rx);

View file

@ -8582,7 +8582,7 @@ catch_unload_command_1 (char *arg, int from_tty,
void void
init_catchpoint (struct breakpoint *b, init_catchpoint (struct breakpoint *b,
struct gdbarch *gdbarch, int tempflag, struct gdbarch *gdbarch, int tempflag,
char *cond_string, const char *cond_string,
const struct breakpoint_ops *ops) const struct breakpoint_ops *ops)
{ {
struct symtab_and_line sal; struct symtab_and_line sal;
@ -8613,7 +8613,7 @@ install_breakpoint (int internal, struct breakpoint *b, int update_gll)
static void static void
create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch, create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
int tempflag, char *cond_string, int tempflag, const char *cond_string,
const struct breakpoint_ops *ops) const struct breakpoint_ops *ops)
{ {
struct fork_catchpoint *c = new fork_catchpoint (); struct fork_catchpoint *c = new fork_catchpoint ();
@ -11779,10 +11779,10 @@ until_break_command (char *arg, int from_tty, int anywhere)
it updates arg to point to the first character following the parsed it updates arg to point to the first character following the parsed
if clause in the arg string. */ if clause in the arg string. */
char * const char *
ep_parse_optional_if_clause (char **arg) ep_parse_optional_if_clause (const char **arg)
{ {
char *cond_string; const char *cond_string;
if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2])) if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
return NULL; return NULL;
@ -11792,7 +11792,7 @@ ep_parse_optional_if_clause (char **arg)
/* Skip any extra leading whitespace, and record the start of the /* Skip any extra leading whitespace, and record the start of the
condition string. */ condition string. */
*arg = skip_spaces (*arg); *arg = skip_spaces_const (*arg);
cond_string = *arg; cond_string = *arg;
/* Assume that the condition occupies the remainder of the arg /* Assume that the condition occupies the remainder of the arg
@ -11813,11 +11813,12 @@ typedef enum
catch_fork_kind; catch_fork_kind;
static void static void
catch_fork_command_1 (char *arg, int from_tty, catch_fork_command_1 (char *arg_entry, int from_tty,
struct cmd_list_element *command) struct cmd_list_element *command)
{ {
const char *arg = arg_entry;
struct gdbarch *gdbarch = get_current_arch (); struct gdbarch *gdbarch = get_current_arch ();
char *cond_string = NULL; const char *cond_string = NULL;
catch_fork_kind fork_kind; catch_fork_kind fork_kind;
int tempflag; int tempflag;
@ -11827,7 +11828,7 @@ catch_fork_command_1 (char *arg, int from_tty,
if (!arg) if (!arg)
arg = ""; arg = "";
arg = skip_spaces (arg); arg = skip_spaces_const (arg);
/* The allowed syntax is: /* The allowed syntax is:
catch [v]fork catch [v]fork
@ -11860,19 +11861,20 @@ catch_fork_command_1 (char *arg, int from_tty,
} }
static void static void
catch_exec_command_1 (char *arg, int from_tty, catch_exec_command_1 (char *arg_entry, int from_tty,
struct cmd_list_element *command) struct cmd_list_element *command)
{ {
const char *arg = arg_entry;
struct exec_catchpoint *c; struct exec_catchpoint *c;
struct gdbarch *gdbarch = get_current_arch (); struct gdbarch *gdbarch = get_current_arch ();
int tempflag; int tempflag;
char *cond_string = NULL; const char *cond_string = NULL;
tempflag = get_cmd_context (command) == CATCH_TEMPORARY; tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
if (!arg) if (!arg)
arg = ""; arg = "";
arg = skip_spaces (arg); arg = skip_spaces_const (arg);
/* The allowed syntax is: /* The allowed syntax is:
catch exec catch exec

View file

@ -1295,7 +1295,7 @@ extern void
extern void init_catchpoint (struct breakpoint *b, extern void init_catchpoint (struct breakpoint *b,
struct gdbarch *gdbarch, int tempflag, struct gdbarch *gdbarch, int tempflag,
char *cond_string, const char *cond_string,
const struct breakpoint_ops *ops); const struct breakpoint_ops *ops);
/* Add breakpoint B on the breakpoint list, and notify the user, the /* Add breakpoint B on the breakpoint list, and notify the user, the
@ -1641,7 +1641,7 @@ extern struct gdbarch *get_sal_arch (struct symtab_and_line sal);
extern void breakpoint_free_objfile (struct objfile *objfile); extern void breakpoint_free_objfile (struct objfile *objfile);
extern char *ep_parse_optional_if_clause (char **arg); extern const char *ep_parse_optional_if_clause (const char **arg);
/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" to /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" to
UIOUT iff debugging multiple threads. */ UIOUT iff debugging multiple threads. */

View file

@ -238,8 +238,8 @@ number_is_in_list (const char *list, int number)
/* See documentation in cli-utils.h. */ /* See documentation in cli-utils.h. */
char * const char *
remove_trailing_whitespace (const char *start, char *s) remove_trailing_whitespace (const char *start, const char *s)
{ {
while (s > start && isspace (*(s - 1))) while (s > start && isspace (*(s - 1)))
--s; --s;
@ -288,7 +288,7 @@ extract_arg (char **arg)
/* See documentation in cli-utils.h. */ /* See documentation in cli-utils.h. */
int int
check_for_argument (char **str, char *arg, int arg_len) check_for_argument (const char **str, const char *arg, int arg_len)
{ {
if (strncmp (*str, arg, arg_len) == 0 if (strncmp (*str, arg, arg_len) == 0
&& ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len]))) && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))

View file

@ -137,7 +137,16 @@ extern int number_is_in_list (const char *list, int number);
/* Reverse S to the last non-whitespace character without skipping past /* Reverse S to the last non-whitespace character without skipping past
START. */ START. */
extern char *remove_trailing_whitespace (const char *start, char *s); extern const char *remove_trailing_whitespace (const char *start,
const char *s);
/* Same, for non-const S. */
static inline char *
remove_trailing_whitespace (const char *start, char *s)
{
return (char *) remove_trailing_whitespace (start, (const char *) s);
}
/* A helper function to extract an argument from *ARG. An argument is /* A helper function to extract an argument from *ARG. An argument is
delimited by whitespace. The return value is either NULL if no delimited by whitespace. The return value is either NULL if no
@ -156,6 +165,15 @@ extern char *extract_arg_const (const char **arg);
string. The argument must also either be at the end of the string, string. The argument must also either be at the end of the string,
or be followed by whitespace. Returns 1 if it finds the argument, or be followed by whitespace. Returns 1 if it finds the argument,
0 otherwise. If the argument is found, it updates *STR. */ 0 otherwise. If the argument is found, it updates *STR. */
extern int check_for_argument (char **str, char *arg, int arg_len); extern int check_for_argument (const char **str, const char *arg, int arg_len);
/* Same, for non-const STR. */
static inline int
check_for_argument (char **str, const char *arg, int arg_len)
{
return check_for_argument (const_cast<const char **> (str),
arg, arg_len);
}
#endif /* CLI_UTILS_H */ #endif /* CLI_UTILS_H */