summaryrefslogtreecommitdiff
path: root/gdb/cli
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2017-12-13 16:38:49 +0000
committerPedro Alves <palves@redhat.com>2017-12-13 16:38:49 +0000
commit60a20c190789fd75d1955576160cbbfe94c792fb (patch)
tree3ca6c6a997ea7ca80c2051857d7cf478eefca4ac /gdb/cli
parent0b982d685e840948eed4619843a0cc5ce8991d6c (diff)
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/cli')
-rw-r--r--gdb/cli/cli-decode.c41
1 files changed, 3 insertions, 38 deletions
diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 81df308f47..d657de2dd0 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -1816,8 +1816,6 @@ complete_on_cmdlist (struct cmd_list_element *list,
&& (!ignore_help_classes || ptr->func
|| ptr->prefixlist))
{
- char *match;
-
if (pass == 0)
{
if (ptr->cmd_deprecated)
@@ -1827,22 +1825,8 @@ complete_on_cmdlist (struct cmd_list_element *list,
}
}
- match = (char *) xmalloc (strlen (word) + strlen (ptr->name) + 1);
- if (word == text)
- strcpy (match, ptr->name);
- else if (word > text)
- {
- /* Return some portion of ptr->name. */
- strcpy (match, ptr->name + (word - text));
- }
- else
- {
- /* Return some of text plus ptr->name. */
- strncpy (match, word, text - word);
- match[text - word] = '\0';
- strcat (match, ptr->name);
- }
- tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
+ tracker.add_completion
+ (make_completion_match_str (ptr->name, text, word));
got_matches = true;
}
@@ -1876,26 +1860,7 @@ complete_on_enum (completion_tracker &tracker,
for (i = 0; (name = enumlist[i]) != NULL; i++)
if (strncmp (name, text, textlen) == 0)
- {
- char *match;
-
- match = (char *) xmalloc (strlen (word) + strlen (name) + 1);
- if (word == text)
- strcpy (match, name);
- else if (word > text)
- {
- /* Return some portion of name. */
- strcpy (match, name + (word - text));
- }
- else
- {
- /* Return some of text plus name. */
- strncpy (match, word, text - word);
- match[text - word] = '\0';
- strcat (match, name);
- }
- tracker.add_completion (gdb::unique_xmalloc_ptr<char> (match));
- }
+ tracker.add_completion (make_completion_match_str (name, text, word));
}