-Wwrite-strings: Constify word break character arrays

-Wwrite-strings flags several cases of missing casts around
initializations like:

   static char *gdb_completer_command_word_break_characters =
    " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";

Obviously these could/should be const.  However, while at it, there's
no need for these variables to be pointers instead of arrays.  They
are never changed to point to anything else.

Unfortunately, readline's rl_completer_word_break_characters is
"char *", not "const char *".  So we always need a cast somewhere.  The
approach taken here is to add a new
set_rl_completer_word_break_characters function that becomes the only
place that writes to rl_completer_word_break_characters, and thus the
single place that needs the cast.

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

	* ada-lang.c (ada_completer_word_break_characters): Now a const
	array.
	(ada_get_gdb_completer_word_break_characters): Constify.
	* completer.c (gdb_completer_command_word_break_characters)
	(gdb_completer_file_name_break_characters)
	(gdb_completer_quote_characters): Now const arrays.
	(get_gdb_completer_quote_characters): Constify.
	(set_rl_completer_word_break_characters): New function.
	(set_gdb_completion_word_break_characters)
	(complete_line_internal): Use it.
	* completer.h (get_gdb_completer_quote_characters): Constify.
	(set_rl_completer_word_break_characters): Declare.
	* f-lang.c (f_word_break_characters): Constify.
	* language.c (default_word_break_characters): Constify.
	* language.h (language_defn::la_word_break_characters): Constify.
	(default_word_break_characters): Constify.
	* top.c (init_main): Use set_rl_completer_word_break_characters.
This commit is contained in:
Pedro Alves 2017-04-05 19:21:34 +01:00
parent 7a1149643d
commit 67cb5b2da2
8 changed files with 69 additions and 34 deletions

View file

@ -1,3 +1,23 @@
2017-04-05 Pedro Alves <palves@redhat.com>
* ada-lang.c (ada_completer_word_break_characters): Now a const
array.
(ada_get_gdb_completer_word_break_characters): Constify.
* completer.c (gdb_completer_command_word_break_characters)
(gdb_completer_file_name_break_characters)
(gdb_completer_quote_characters): Now const arrays.
(get_gdb_completer_quote_characters): Constify.
(set_rl_completer_word_break_characters): New function.
(set_gdb_completion_word_break_characters)
(complete_line_internal): Use it.
* completer.h (get_gdb_completer_quote_characters): Constify.
(set_rl_completer_word_break_characters): Declare.
* f-lang.c (f_word_break_characters): Constify.
* language.c (default_word_break_characters): Constify.
* language.h (language_defn::la_word_break_characters): Constify.
(default_word_break_characters): Constify.
* top.c (init_main): Use set_rl_completer_word_break_characters.
2017-04-05 Pedro Alves <palves@redhat.com> 2017-04-05 Pedro Alves <palves@redhat.com>
* aix-thread.c (aix_thread_pid_to_str) * aix-thread.c (aix_thread_pid_to_str)

View file

@ -315,9 +315,7 @@ static void ada_free_symbol_cache (struct ada_symbol_cache *sym_cache);
/* Maximum-sized dynamic type. */ /* Maximum-sized dynamic type. */
static unsigned int varsize_limit; static unsigned int varsize_limit;
/* FIXME: brobecker/2003-09-17: No longer a const because it is static const char ada_completer_word_break_characters[] =
returned by a function that does not return a const char *. */
static char *ada_completer_word_break_characters =
#ifdef VMS #ifdef VMS
" \t\n!@#%^&*()+=|~`}{[]\";:?/,-"; " \t\n!@#%^&*()+=|~`}{[]\";:?/,-";
#else #else
@ -558,7 +556,7 @@ add_angle_brackets (const char *str)
return result; return result;
} }
static char * static const char *
ada_get_gdb_completer_word_break_characters (void) ada_get_gdb_completer_word_break_characters (void)
{ {
return ada_completer_word_break_characters; return ada_completer_word_break_characters;

View file

@ -84,29 +84,30 @@ char *line_completion_function (const char *text, int matches,
readline library sees one in any of the current completion strings, readline library sees one in any of the current completion strings,
it thinks that the string needs to be quoted and automatically it thinks that the string needs to be quoted and automatically
supplies a leading quote. */ supplies a leading quote. */
static char *gdb_completer_command_word_break_characters = static const char gdb_completer_command_word_break_characters[] =
" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
/* When completing on file names, we remove from the list of word /* When completing on file names, we remove from the list of word
break characters any characters that are commonly used in file break characters any characters that are commonly used in file
names, such as '-', '+', '~', etc. Otherwise, readline displays names, such as '-', '+', '~', etc. Otherwise, readline displays
incorrect completion candidates. */ incorrect completion candidates. */
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
/* MS-DOS and MS-Windows use colon as part of the drive spec, and most /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
programs support @foo style response files. */ programs support @foo style response files. */
static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@"; static const char gdb_completer_file_name_break_characters[] =
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
" \t\n*|\"';?><@";
#else #else
static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><"; " \t\n*|\"';:?><";
#endif #endif
/* Characters that can be used to quote completion strings. Note that /* Characters that can be used to quote completion strings. Note that
we can't include '"' because the gdb C parser treats such quoted we can't include '"' because the gdb C parser treats such quoted
sequences as strings. */ sequences as strings. */
static char *gdb_completer_quote_characters = "'"; static const char gdb_completer_quote_characters[] = "'";
/* Accessor for some completer data that may interest other files. */ /* Accessor for some completer data that may interest other files. */
char * const char *
get_gdb_completer_quote_characters (void) get_gdb_completer_quote_characters (void)
{ {
return gdb_completer_quote_characters; return gdb_completer_quote_characters;
@ -651,17 +652,27 @@ expression_completer (struct cmd_list_element *ignore,
/* See definition in completer.h. */ /* See definition in completer.h. */
void
set_rl_completer_word_break_characters (const char *break_chars)
{
rl_completer_word_break_characters = (char *) break_chars;
}
/* See definition in completer.h. */
void void
set_gdb_completion_word_break_characters (completer_ftype *fn) set_gdb_completion_word_break_characters (completer_ftype *fn)
{ {
const char *break_chars;
/* So far we are only interested in differentiating filename /* So far we are only interested in differentiating filename
completers from everything else. */ completers from everything else. */
if (fn == filename_completer) if (fn == filename_completer)
rl_completer_word_break_characters break_chars = gdb_completer_file_name_break_characters;
= gdb_completer_file_name_break_characters;
else else
rl_completer_word_break_characters break_chars = gdb_completer_command_word_break_characters;
= gdb_completer_command_word_break_characters;
set_rl_completer_word_break_characters (break_chars);
} }
/* Here are some useful test cases for completion. FIXME: These /* Here are some useful test cases for completion. FIXME: These
@ -743,8 +754,8 @@ complete_line_internal (const char *text,
then we will switch to the special word break set for command then we will switch to the special word break set for command
strings, which leaves out the '-' character used in some strings, which leaves out the '-' character used in some
commands. */ commands. */
rl_completer_word_break_characters = set_rl_completer_word_break_characters
current_language->la_word_break_characters(); (current_language->la_word_break_characters());
/* Decide whether to complete on a list of gdb commands or on /* Decide whether to complete on a list of gdb commands or on
symbols. */ symbols. */
@ -821,8 +832,8 @@ complete_line_internal (const char *text,
} }
/* Ensure that readline does the right thing with respect to /* Ensure that readline does the right thing with respect to
inserting quotes. */ inserting quotes. */
rl_completer_word_break_characters = set_rl_completer_word_break_characters
gdb_completer_command_word_break_characters; (gdb_completer_command_word_break_characters);
} }
} }
else else
@ -848,8 +859,8 @@ complete_line_internal (const char *text,
/* Ensure that readline does the right thing /* Ensure that readline does the right thing
with respect to inserting quotes. */ with respect to inserting quotes. */
rl_completer_word_break_characters = set_rl_completer_word_break_characters
gdb_completer_command_word_break_characters; (gdb_completer_command_word_break_characters);
} }
else if (reason == handle_help) else if (reason == handle_help)
list = NULL; list = NULL;
@ -857,8 +868,8 @@ complete_line_internal (const char *text,
{ {
if (reason != handle_brkchars) if (reason != handle_brkchars)
list = complete_on_enum (c->enums, p, word); list = complete_on_enum (c->enums, p, word);
rl_completer_word_break_characters = set_rl_completer_word_break_characters
gdb_completer_command_word_break_characters; (gdb_completer_command_word_break_characters);
} }
else else
{ {
@ -879,8 +890,8 @@ complete_line_internal (const char *text,
&& strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL; && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
p--) p--)
; ;
rl_completer_word_break_characters = set_rl_completer_word_break_characters
gdb_completer_file_name_break_characters; (gdb_completer_file_name_break_characters);
} }
if (reason == handle_brkchars if (reason == handle_brkchars
&& c->completer_handle_brkchars != NULL) && c->completer_handle_brkchars != NULL)
@ -913,8 +924,8 @@ complete_line_internal (const char *text,
/* Ensure that readline does the right thing /* Ensure that readline does the right thing
with respect to inserting quotes. */ with respect to inserting quotes. */
rl_completer_word_break_characters = set_rl_completer_word_break_characters
gdb_completer_command_word_break_characters; (gdb_completer_command_word_break_characters);
} }
} }
else if (reason == handle_help) else if (reason == handle_help)
@ -947,8 +958,8 @@ complete_line_internal (const char *text,
p[-1]) == NULL; p[-1]) == NULL;
p--) p--)
; ;
rl_completer_word_break_characters = set_rl_completer_word_break_characters
gdb_completer_file_name_break_characters; (gdb_completer_file_name_break_characters);
} }
if (reason == handle_brkchars if (reason == handle_brkchars
&& c->completer_handle_brkchars != NULL) && c->completer_handle_brkchars != NULL)

View file

@ -99,10 +99,16 @@ extern VEC (char_ptr) *reg_or_group_completer (struct cmd_list_element *,
extern VEC (char_ptr) *reggroup_completer (struct cmd_list_element *, extern VEC (char_ptr) *reggroup_completer (struct cmd_list_element *,
const char *, const char *); const char *, const char *);
extern char *get_gdb_completer_quote_characters (void); extern const char *get_gdb_completer_quote_characters (void);
extern char *gdb_completion_word_break_characters (void); extern char *gdb_completion_word_break_characters (void);
/* Set the word break characters array to BREAK_CHARS. This function
is useful as const-correct alternative to direct assignment to
rl_completer_word_break_characters, which is "char *",
not "const char *". */
extern void set_rl_completer_word_break_characters (const char *break_chars);
/* Set the word break characters array to the corresponding set of /* Set the word break characters array to the corresponding set of
chars, based on FN. This function is useful for cases when the chars, based on FN. This function is useful for cases when the
completer doesn't know the type of the completion until some completer doesn't know the type of the completion until some

View file

@ -203,7 +203,7 @@ f_language_arch_info (struct gdbarch *gdbarch,
/* Remove the modules separator :: from the default break list. */ /* Remove the modules separator :: from the default break list. */
static char * static const char *
f_word_break_characters (void) f_word_break_characters (void)
{ {
static char *retval; static char *retval;

View file

@ -698,7 +698,7 @@ default_pass_by_reference (struct type *type)
delimiting words. This is a reasonable default value that delimiting words. This is a reasonable default value that
most languages should be able to use. */ most languages should be able to use. */
char * const char *
default_word_break_characters (void) default_word_break_characters (void)
{ {
return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; return " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";

View file

@ -321,7 +321,7 @@ struct language_defn
char string_lower_bound; char string_lower_bound;
/* The list of characters forming word boundaries. */ /* The list of characters forming word boundaries. */
char *(*la_word_break_characters) (void); const char *(*la_word_break_characters) (void);
/* Should return a vector of all symbols which are possible /* Should return a vector of all symbols which are possible
completions for TEXT. WORD is the entire command on which the completions for TEXT. WORD is the entire command on which the
@ -583,7 +583,7 @@ extern char *language_class_name_from_physname (const struct language_defn *,
const char *physname); const char *physname);
/* Splitting strings into words. */ /* Splitting strings into words. */
extern char *default_word_break_characters (void); extern const char *default_word_break_characters (void);
/* Print the index of an array element using the C99 syntax. */ /* Print the index of an array element using the C99 syntax. */
extern void default_print_array_index (struct value *index_value, extern void default_print_array_index (struct value *index_value,

View file

@ -2022,7 +2022,7 @@ init_main (void)
/* Setup important stuff for command line editing. */ /* Setup important stuff for command line editing. */
rl_completion_word_break_hook = gdb_completion_word_break_characters; rl_completion_word_break_hook = gdb_completion_word_break_characters;
rl_completion_entry_function = readline_line_completion_function; rl_completion_entry_function = readline_line_completion_function;
rl_completer_word_break_characters = default_word_break_characters (); set_rl_completer_word_break_characters (default_word_break_characters ());
rl_completer_quote_characters = get_gdb_completer_quote_characters (); rl_completer_quote_characters = get_gdb_completer_quote_characters ();
rl_completion_display_matches_hook = cli_display_match_list; rl_completion_display_matches_hook = cli_display_match_list;
rl_readline_name = "gdb"; rl_readline_name = "gdb";