constify search_symbols

Message ID 1403285060-5556-1-git-send-email-tromey@redhat.com
State Committed
Headers

Commit Message

Tom Tromey June 20, 2014, 5:24 p.m. UTC
  This constifies the parameters to search_symbols and fixes up the
fallout.

Tested by rebuilding.

2014-06-20  Tom Tromey  <tromey@redhat.com>

	* symtab.c (operator_chars): Make parameters and return type
	const.
	(file_matches): Make "files" const.
	(struct search_symbols_data) <files>: Now const.
	(search_symbols): Make "regexp" and "files" parameters const.
	Update.
	(symtab_symbol_info): Remove cast.
	(rbreak_command): Update.
	* symtab.h (search_symbols): Update.
---
 gdb/ChangeLog | 12 ++++++++++++
 gdb/symtab.c  | 35 +++++++++++++++++++----------------
 gdb/symtab.h  |  4 ++--
 3 files changed, 33 insertions(+), 18 deletions(-)
  

Comments

Yao Qi June 24, 2014, 3:09 a.m. UTC | #1
On 06/21/2014 01:24 AM, Tom Tromey wrote:
> -  char **files = NULL, *file_name;
> +  const char **files = NULL;
> +  const char *file_name;
>    int nfiles = 0;
>  
>    if (regexp)
> @@ -3960,13 +3961,15 @@ rbreak_command (char *regexp, int from_tty)
>        if (colon && *(colon + 1) != ':')
>  	{
>  	  int colon_index;
> +	  char *local_name;
>  
>  	  colon_index = colon - regexp;
> -	  file_name = alloca (colon_index + 1);
> -	  memcpy (file_name, regexp, colon_index);
> -	  file_name[colon_index--] = 0;
> -	  while (isspace (file_name[colon_index]))
> -	    file_name[colon_index--] = 0; 
> +	  local_name = alloca (colon_index + 1);
> +	  memcpy (local_name, regexp, colon_index);
> +	  local_name[colon_index--] = 0;
> +	  while (isspace (local_name[colon_index]))
> +	    local_name[colon_index--] = 0;
> +	  file_name = local_name;

file_name isn't used out side of this block, so we can define file_name
within this block of type "char *", and patch can be shorter.
  
Tom Tromey June 24, 2014, 1:18 p.m. UTC | #2
>>>>> "Yao" == Yao Qi <yao@codesourcery.com> writes:

Yao> file_name isn't used out side of this block, so we can define file_name
Yao> within this block of type "char *", and patch can be shorter.

The problem is that we need to set "file_names = &file_name", and you
can't take the address of a "char *" and cast that to "const char **".

Tom
  
Yao Qi June 24, 2014, 1:42 p.m. UTC | #3
On 06/24/2014 09:18 PM, Tom Tromey wrote:
>>>>>> "Yao" == Yao Qi <yao@codesourcery.com> writes:
> 
> Yao> file_name isn't used out side of this block, so we can define file_name
> Yao> within this block of type "char *", and patch can be shorter.
> 
> The problem is that we need to set "file_names = &file_name", and you
> can't take the address of a "char *" and cast that to "const char **".
> 

Ah.  You are right.  Compiler complains on this statement "files =
&file_name".  I have no comments on this patch then.
  
Tom Tromey June 30, 2014, 2:31 p.m. UTC | #4
Tom> This constifies the parameters to search_symbols and fixes up the
Tom> fallout.

I'm pushing this now.

Tom
  

Patch

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 655c6e8..09f3114 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -3041,8 +3041,8 @@  skip_prologue_sal (struct symtab_and_line *sal)
    beginning of the substring of the operator text.
    Otherwise, return "".  */
 
-static char *
-operator_chars (char *p, char **end)
+static const char *
+operator_chars (const char *p, const char **end)
 {
   *end = "";
   if (strncmp (p, "operator", 8))
@@ -3062,7 +3062,7 @@  operator_chars (char *p, char **end)
 
   if (isalpha (*p) || *p == '_' || *p == '$')
     {
-      char *q = p + 1;
+      const char *q = p + 1;
 
       while (isalnum (*q) || *q == '_' || *q == '$')
 	q++;
@@ -3341,7 +3341,7 @@  sources_info (char *ignore, int from_tty)
    non-zero compare only lbasename of FILES.  */
 
 static int
-file_matches (const char *file, char *files[], int nfiles, int basenames)
+file_matches (const char *file, const char *files[], int nfiles, int basenames)
 {
   int i;
 
@@ -3466,7 +3466,7 @@  sort_search_symbols_remove_dups (struct symbol_search *found, int nfound,
 struct search_symbols_data
 {
   int nfiles;
-  char **files;
+  const char **files;
 
   /* It is true if PREG contains valid data, false otherwise.  */
   unsigned preg_p : 1;
@@ -3511,8 +3511,8 @@  search_symbols_name_matches (const char *symname, void *user_data)
    Duplicate entries are removed.  */
 
 void
-search_symbols (char *regexp, enum search_domain kind,
-		int nfiles, char *files[],
+search_symbols (const char *regexp, enum search_domain kind,
+		int nfiles, const char *files[],
 		struct symbol_search **matches)
 {
   struct symtab *s;
@@ -3562,8 +3562,8 @@  search_symbols (char *regexp, enum search_domain kind,
          This is just a courtesy to make the matching less sensitive
          to how many spaces the user leaves between 'operator'
          and <TYPENAME> or <OPERATOR>.  */
-      char *opend;
-      char *opname = operator_chars (regexp, &opend);
+      const char *opend;
+      const char *opname = operator_chars (regexp, &opend);
       int errcode;
 
       if (*opname)
@@ -3871,7 +3871,7 @@  symtab_symbol_info (char *regexp, enum search_domain kind, int from_tty)
   gdb_assert (kind <= TYPES_DOMAIN);
 
   /* Must make sure that if we're interrupted, symbols gets freed.  */
-  search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
+  search_symbols (regexp, kind, 0, NULL, &symbols);
   old_chain = make_cleanup_free_search_symbols (&symbols);
 
   if (regexp != NULL)
@@ -3950,7 +3950,8 @@  rbreak_command (char *regexp, int from_tty)
   struct cleanup *old_chain;
   char *string = NULL;
   int len = 0;
-  char **files = NULL, *file_name;
+  const char **files = NULL;
+  const char *file_name;
   int nfiles = 0;
 
   if (regexp)
@@ -3960,13 +3961,15 @@  rbreak_command (char *regexp, int from_tty)
       if (colon && *(colon + 1) != ':')
 	{
 	  int colon_index;
+	  char *local_name;
 
 	  colon_index = colon - regexp;
-	  file_name = alloca (colon_index + 1);
-	  memcpy (file_name, regexp, colon_index);
-	  file_name[colon_index--] = 0;
-	  while (isspace (file_name[colon_index]))
-	    file_name[colon_index--] = 0; 
+	  local_name = alloca (colon_index + 1);
+	  memcpy (local_name, regexp, colon_index);
+	  local_name[colon_index--] = 0;
+	  while (isspace (local_name[colon_index]))
+	    local_name[colon_index--] = 0;
+	  file_name = local_name;
 	  files = &file_name;
 	  nfiles = 1;
 	  regexp = skip_spaces (colon + 1);
diff --git a/gdb/symtab.h b/gdb/symtab.h
index 5a6f831..399ae54 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -1356,8 +1356,8 @@  struct symbol_search
   struct symbol_search *next;
 };
 
-extern void search_symbols (char *, enum search_domain, int, char **,
-			    struct symbol_search **);
+extern void search_symbols (const char *, enum search_domain, int,
+			    const char **, struct symbol_search **);
 extern void free_search_symbols (struct symbol_search *);
 extern struct cleanup *make_cleanup_free_search_symbols (struct symbol_search
 							 **);