[PATCH 16/18] -Wwrite-strings: Some constification in gdb/breakpoint.c

Pedro Alves
2017-04-04T17:26:19Z

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
spriraling out of control, so instead I settled for keeping const and
non-const overloads.

gdb/ChangeLog:
yyyy-mm-dd 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.
---
gdb/break-catch-throw.c | 23 +++++++++++++----------
gdb/breakpoint.c | 26 ++++++++++++++------------
gdb/breakpoint.h | 4 ++--
gdb/cli/cli-utils.c | 6 +++---
gdb/cli/cli-utils.h | 21 +++++++++++++++++++--
5 files changed, 51 insertions(+), 29 deletions(-)

diff --git a/gdb/break-catch-throw.c b/gdb/break-catch-throw.c
index f7c7cc8..2e18d2a 100644
--- a/gdb/break-catch-throw.c
+++ b/gdb/break-catch-throw.c
@@ -383,7 +383,8 @@ print_recreate_exception_catchpoint (struct breakpoint *b,
}

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)
{
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. */

static char *
-extract_exception_regexp (char **string)
+extract_exception_regexp (const char **string)
{
- char *start;
- char *last, *last_space;
+ const char *start;
+ const char *last, *last_space;

- start = skip_spaces (*string);
+ start = skip_spaces_const (*string);

last = start;
last_space = start;
while (*last != '\0')
{
- char *if_token = last;
+ const char *if_token = last;

/* Check for the "if". */
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. */
last_space = skip_to_space (last);
- last = skip_spaces (last_space);
+ last = skip_spaces_const (last_space);
}

*string = last;
@@ -457,16 +458,18 @@ extract_exception_regexp (char **string)
commands. */

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)
{
char *except_rx;
- char *cond_string = NULL;
+ const char *cond_string = NULL;
struct cleanup *cleanup;
+ const char *arg = arg_entry;

if (!arg)
arg = "";
- arg = skip_spaces (arg);
+ arg = skip_spaces_const (arg);

except_rx = extract_exception_regexp (&arg);
cleanup = make_cleanup (xfree, except_rx);
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index f0db3e4..4cd7a00 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -8582,7 +8582,7 @@ catch_unload_command_1 (char *arg, int from_tty,
void
init_catchpoint (struct breakpoint *b,
struct gdbarch *gdbarch, int tempflag,
- char *cond_string,
+ const char *cond_string,
const struct breakpoint_ops *ops)
{
struct symtab_and_line sal;
@@ -8613,7 +8613,7 @@ install_breakpoint (int internal, struct breakpoint *b, int update_gll)

static void
create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
- int tempflag, char *cond_string,
+ int tempflag, const char *cond_string,
const struct breakpoint_ops *ops)
{
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
if clause in the arg string. */

-char *
-ep_parse_optional_if_clause (char **arg)
+const char *
+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]))
return NULL;
@@ -11792,7 +11792,7 @@ ep_parse_optional_if_clause (char **arg)

/* Skip any extra leading whitespace, and record the start of the
condition string. */
- *arg = skip_spaces (*arg);
+ *arg = skip_spaces_const (*arg);
cond_string = *arg;

/* Assume that the condition occupies the remainder of the arg
@@ -11813,11 +11813,12 @@ typedef enum
catch_fork_kind;

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)
{
+ const char *arg = arg_entry;
struct gdbarch *gdbarch = get_current_arch ();
- char *cond_string = NULL;
+ const char *cond_string = NULL;
catch_fork_kind fork_kind;
int tempflag;

@@ -11827,7 +11828,7 @@ catch_fork_command_1 (char *arg, int from_tty,

if (!arg)
arg = "";
- arg = skip_spaces (arg);
+ arg = skip_spaces_const (arg);

/* The allowed syntax is:
catch [v]fork
@@ -11860,19 +11861,20 @@ catch_fork_command_1 (char *arg, int from_tty,
}

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)
{
+ const char *arg = arg_entry;
struct exec_catchpoint *c;
struct gdbarch *gdbarch = get_current_arch ();
int tempflag;
- char *cond_string = NULL;
+ const char *cond_string = NULL;

tempflag = get_cmd_context (command) == CATCH_TEMPORARY;

if (!arg)
arg = "";
- arg = skip_spaces (arg);
+ arg = skip_spaces_const (arg);

/* The allowed syntax is:
catch exec
diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h
index 7d08057..6940270 100644
--- a/gdb/breakpoint.h
+++ b/gdb/breakpoint.h
@@ -1295,7 +1295,7 @@ extern void

extern void init_catchpoint (struct breakpoint *b,
struct gdbarch *gdbarch, int tempflag,
- char *cond_string,
+ const char *cond_string,
const struct breakpoint_ops *ops);

/* 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 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
UIOUT iff debugging multiple threads. */
diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c
index b353c18..8eac7c4 100644
--- a/gdb/cli/cli-utils.c
+++ b/gdb/cli/cli-utils.c
@@ -238,8 +238,8 @@ number_is_in_list (const char *list, int number)

/* See documentation in cli-utils.h. */

-char *
-remove_trailing_whitespace (const char *start, char *s)
+const char *
+remove_trailing_whitespace (const char *start, const char *s)
{
while (s > start && isspace (*(s - 1)))
--s;
@@ -288,7 +288,7 @@ extract_arg (char **arg)
/* See documentation in cli-utils.h. */

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
&& ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h
index c80bae0..b1207b6 100644
--- a/gdb/cli/cli-utils.h
+++ b/gdb/cli/cli-utils.h
@@ -137,7 +137,15 @@ extern int number_is_in_list (const char *list, int number);
/* Reverse S to the last non-whitespace character without skipping past
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
delimited by whitespace. The return value is either NULL if no
@@ -156,6 +164,15 @@ extern char *extract_arg_const (const char **arg);
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,
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 */
--
2.5.5