diff options
author | Pedro Alves <palves@redhat.com> | 2017-12-13 16:38:49 +0000 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2017-12-13 16:38:49 +0000 |
commit | 60a20c190789fd75d1955576160cbbfe94c792fb (patch) | |
tree | 3ca6c6a997ea7ca80c2051857d7cf478eefca4ac /gdb/symtab.c | |
parent | 0b982d685e840948eed4619843a0cc5ce8991d6c (diff) | |
download | gdb-60a20c190789fd75d1955576160cbbfe94c792fb.zip gdb-60a20c190789fd75d1955576160cbbfe94c792fb.tar.gz gdb-60a20c190789fd75d1955576160cbbfe94c792fb.tar.bz2 |
Factor out final completion match string building
We have several places doing essentially the same thing; factor them
out to a central place. Some of the places overallocate for no good
reason, or use strcat unnecessarily. The centralized version is more
precise and to the point.
(I considered making the gdb::unique_xmalloc_ptr overload version of
make_completer_match_str try to realloc (not xrealloc) probably
avoiding an allocation in most cases, but that'd be probably overdoing
it, and also, now that I'm writing this I thought I'd try to see how
could we ever get to filename_completer with "text != word", but I
couldn't figure it out. Running the testsuite with 'gdb_assert (text
== word);' never tripped on the assertion either. So post gdb 8.1,
I'll probably propose a patch to simplify filename_completer a bit,
and the gdb::unique_xmalloc_str overload can be removed then.)
gdb/ChangeLog:
2017-12-13 Pedro Alves <palves@redhat.com>
* cli/cli-decode.c (complete_on_cmdlist, complete_on_enum): Use
make_completion_match_str.
* completer.c: Use gdb::unique_xmalloc_ptr and
make_completion_match_str.
(make_completion_match_str_1): New.
(make_completion_match_str(const char *, const char *,
const char *)): New.
(make_completion_match_str(gdb::unique_xmalloc_ptr<char> &&,
const char *, const char *)): New.
* completer.h (make_completion_match_str(const char *,
const char *, const char *)): New.
(make_completion_match_str(gdb::unique_xmalloc_ptr<char> &&,
const char *, const char *)): New.
* interps.c (interpreter_completer): Use make_completion_match_str.
* symtab.c (completion_list_add_name, add_filename_to_list): Use
make_completion_match_str.
Diffstat (limited to 'gdb/symtab.c')
-rw-r--r-- | gdb/symtab.c | 50 |
1 files changed, 3 insertions, 47 deletions
diff --git a/gdb/symtab.c b/gdb/symtab.c index 996d521..bb98619 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -4725,29 +4725,8 @@ completion_list_add_name (completion_tracker &tracker, of matches. Note that the name is moved to freshly malloc'd space. */ { - char *newobj; - - if (word == text) - { - newobj = (char *) xmalloc (strlen (symname) + 5); - strcpy (newobj, symname); - } - else if (word > text) - { - /* Return some portion of symname. */ - newobj = (char *) xmalloc (strlen (symname) + 5); - strcpy (newobj, symname + (word - text)); - } - else - { - /* Return some of SYM_TEXT plus symname. */ - newobj = (char *) xmalloc (strlen (symname) + (text - word) + 5); - strncpy (newobj, word, text - word); - newobj[text - word] = '\0'; - strcat (newobj, symname); - } - - gdb::unique_xmalloc_ptr<char> completion (newobj); + gdb::unique_xmalloc_ptr<char> completion + = make_completion_match_str (symname, text, word); /* Here we pass the match-for-lcd object to add_completion. Some languages match the user text against substrings of symbol @@ -5322,30 +5301,7 @@ static void add_filename_to_list (const char *fname, const char *text, const char *word, completion_list *list) { - char *newobj; - size_t fnlen = strlen (fname); - - if (word == text) - { - /* Return exactly fname. */ - newobj = (char *) xmalloc (fnlen + 5); - strcpy (newobj, fname); - } - else if (word > text) - { - /* Return some portion of fname. */ - newobj = (char *) xmalloc (fnlen + 5); - strcpy (newobj, fname + (word - text)); - } - else - { - /* Return some of TEXT plus fname. */ - newobj = (char *) xmalloc (fnlen + (text - word) + 5); - strncpy (newobj, word, text - word); - newobj[text - word] = '\0'; - strcat (newobj, fname); - } - list->emplace_back (newobj); + list->emplace_back (make_completion_match_str (fname, text, word)); } static int |