[PATCH 11/18] Implement completion limiting for reg_or_group_completer.

Keith Seitz
2015-04-13T19:23:31Z

This patch converts reg_or_group_completer to use maybe_add_completion
and adds tests for this new behavior.

gdb/ChangeLog

* completer.c (reg_or_group_completer): Use maybe_add_completion.

gdb/testsuite/ChangeLog

* gdb.base/completion.exp: Add tests for limit completing
"info reigsters".
---
gdb/completer.c | 46 ++++++++++++++++++++++++++++++---
gdb/testsuite/gdb.base/completion.exp | 4 +++
2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/gdb/completer.c b/gdb/completer.c
index 3022d7e..b112c2c 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -1048,6 +1048,7 @@ reg_or_group_completer (struct completer_data *cdata,
struct reggroup *group;
const char *name;
int i;
+ enum maybe_add_completion_enum add_status;

if (!target_has_registers)
return result;
@@ -1059,16 +1060,53 @@ reg_or_group_completer (struct completer_data *cdata,
i++)
{
if (*name != '\0' && strncmp (word, name, len) == 0)
- VEC_safe_push (char_ptr, result, xstrdup (name));
+ {
+ char *match = xstrdup (name);
+
+ add_status = maybe_add_completion (cdata, match);
+ switch (add_status)
+ {
+ case MAYBE_ADD_COMPLETION_OK:
+ VEC_safe_push (char_ptr, result, match);
+ break;
+ case MAYBE_ADD_COMPLETION_OK_MAX_REACHED:
+ VEC_safe_push (char_ptr, result, match);
+ return result;
+ case MAYBE_ADD_COMPLETION_MAX_REACHED:
+ xfree (match);
+ return result;
+ case MAYBE_ADD_COMPLETION_DUPLICATE:
+ xfree (match);
+ break;
+ }
+ }
}

for (group = reggroup_next (gdbarch, NULL);
group != NULL;
group = reggroup_next (gdbarch, group))
{
- name = reggroup_name (group);
- if (strncmp (word, name, len) == 0)
- VEC_safe_push (char_ptr, result, xstrdup (name));
+ char *match = xstrdup (reggroup_name (group));
+
+ if (strncmp (word, match, len) == 0)
+ {
+ add_status = maybe_add_completion (cdata, match);
+ switch (add_status)
+ {
+ case MAYBE_ADD_COMPLETION_OK:
+ VEC_safe_push (char_ptr, result, match);
+ break;
+ case MAYBE_ADD_COMPLETION_OK_MAX_REACHED:
+ VEC_safe_push (char_ptr, result, match);
+ return result;
+ case MAYBE_ADD_COMPLETION_MAX_REACHED:
+ xfree (match);
+ return result;
+ case MAYBE_ADD_COMPLETION_DUPLICATE:
+ xfree (match);
+ break;
+ }
+ }
}

return result;
diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp
index d07ca86..4a35cd1 100644
--- a/gdb/testsuite/gdb.base/completion.exp
+++ b/gdb/testsuite/gdb.base/completion.exp
@@ -1037,3 +1037,7 @@ with_test_prefix "interpreter_completer reset" {
set max_completions $old_max
gdb_test_no_output "set max-completions $max_completions"
}
+
+# Test reg_or_group_completer.
+test_completion_limit "info registers " \
+ "info registers \[a-zA-Z0-9\]+" $max_completions