This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 07/18] Implement completion limiting for filename_completer.


This patch converts filename_completer to using maybe_add_completion
and adds some tests to exercise this new behavior.

gdb/ChangeLog

	* completer.c (filename_completer): Use maybe_add_completion.

gdb/testsuite/ChangeLog

	* gdb.base/completion.exp: Put completion truncation message
	into a variable for use by other tests.  Update existing tests.
	(test_completion_limit): New procedure.
	Add tests for completion limiting on file names.
---
 gdb/completer.c                       |   18 +++++++++
 gdb/testsuite/gdb.base/completion.exp |   65 +++++++++++++++++++++++++++++++--
 2 files changed, 78 insertions(+), 5 deletions(-)

diff --git a/gdb/completer.c b/gdb/completer.c
index 6708bb1..291ba73 100644
--- a/gdb/completer.c
+++ b/gdb/completer.c
@@ -136,6 +136,7 @@ filename_completer (struct completer_data *cdata,
   while (1)
     {
       char *p, *q;
+      enum maybe_add_completion_enum add_status;
 
       p = rl_filename_completion_function (text, subsequent_name);
       if (p == NULL)
@@ -172,7 +173,22 @@ filename_completer (struct completer_data *cdata,
 	  strcat (q, p);
 	  xfree (p);
 	}
-      VEC_safe_push (char_ptr, return_val, q);
+      add_status = maybe_add_completion (cdata, q);
+      switch (add_status)
+	{
+	case MAYBE_ADD_COMPLETION_OK:
+	  VEC_safe_push (char_ptr, return_val, q);
+	  break;
+	case MAYBE_ADD_COMPLETION_OK_MAX_REACHED:
+	  VEC_safe_push (char_ptr, return_val, q);
+	  return return_val;
+	case MAYBE_ADD_COMPLETION_MAX_REACHED:
+	  xfree (q);
+	  return return_val;
+	case MAYBE_ADD_COMPLETION_DUPLICATE:
+	  xfree (q);
+	  break;
+	}
     }
 #if 0
   /* There is no way to do this just long enough to affect quote
diff --git a/gdb/testsuite/gdb.base/completion.exp b/gdb/testsuite/gdb.base/completion.exp
index 5afd851..78ba216 100644
--- a/gdb/testsuite/gdb.base/completion.exp
+++ b/gdb/testsuite/gdb.base/completion.exp
@@ -858,6 +858,10 @@ gdb_test "complete break need" "need_malloc"
 # Completion limiting.
 #
 
+# The terminal at the end of the complete command
+set end "\\\*\\\*\\\* List may be truncated, "
+append end "max-completions reached\\\. \\\*\\\*\\\*"
+
 gdb_test_no_output "set max-completions 5"
 
 set test "command-name completion limiting using tab character"
@@ -866,7 +870,7 @@ gdb_test_multiple "" "$test" {
     -re "^p\\\x07$" {
 	send_gdb "\t"
 	gdb_test_multiple "" "$test" {
-	    -re "List may be truncated, max-completions reached.*\r\n$gdb_prompt p$" {
+	    -re "$end\r\n$gdb_prompt p$" {
 		# Complete the command and ignore the output to resync
 		# gdb for the next test.
 		send_gdb "\n"
@@ -898,7 +902,8 @@ gdb_test_multiple "" "$test" {
     }
 }
 
-gdb_test_no_output "set max-completions 3"
+set max_completions 3
+gdb_test_no_output "set max-completions $max_completions"
 
 set test "symbol-name completion limiting using tab character"
 send_gdb "p marker\t"
@@ -906,7 +911,7 @@ gdb_test_multiple "" "$test" {
     -re "^p marker\\\x07$" {
 	send_gdb "\t"
 	gdb_test_multiple "" "$test" {
-	    -re "List may be truncated, max-completions reached.*\r\n$gdb_prompt p marker$" {
+	    -re "$end\r\n$gdb_prompt p marker$" {
 		# Complete the command and ignore the output to resync
 		# gdb for the next test.
 		send_gdb "\n"
@@ -933,7 +938,59 @@ gdb_test_multiple "" "$test" {
 set test "symbol-name completion limiting using complete command"
 send_gdb "complete p mark\n"
 gdb_test_multiple "" "$test" {
-    -re "List may be truncated, max-completions reached.*\r\n$gdb_prompt $" {
+    -re "$end\r\n$gdb_prompt $" {
 	pass "$test"
     }
 }
+
+# A convenience function for testing completion limiting.
+# CMD is a GDB command to to run with "complete".
+# PATTERN is a regexp pattern matching the expected output
+#   of completion items "seen" in the output.
+# NUM is the number of maximum completions expected.
+#
+# The test will use the test name "limit complete CMD"
+# and will only count the number of completion items matching
+# PATTERN.  No assumptions are made on the order of the items
+# seen in GDB's output.
+#
+# If NUM items are seen before the truncation message, the test
+# passes, otherwise it fails.  The test can also fail if no
+# truncation message is seen at all, in which case the test
+# failure message will say "(unlimited)".
+
+proc test_completion_limit {cmd pattern num} {
+    global gdb_prompt
+
+    # The terminal at the end of the complete command
+    set end "\\\*\\\*\\\* List may be truncated, "
+    append end "max-completions reached\\\. \\\*\\\*\\\*"
+
+    set cmdr [string_to_regexp $cmd]
+    set seen 0
+    gdb_test_multiple "complete $cmd" "limit complete $cmd" {
+	"complete $cmdr" { exp_continue }
+	-re "$pattern\r\n" {
+	    incr seen
+	    exp_continue
+	}
+	-re ".*$end\r\n$gdb_prompt $" {
+	    if {$seen == $num} {
+		pass "limit complete $cmd"
+	    } else {
+		fail "limit complete $cmd ($seen/$num)"
+	    }
+	}
+	-re ".*$gdb_prompt $" {
+	    fail "limit complete $cmd (unlimited)"
+	}
+    }
+}
+
+test_completion_limit "file ./gdb.base/jit-s" \
+    "file \\\./gdb\\\.base/jit-s(imple|olib|o)(\\\.c|\\\.exp)?" \
+    $max_completions
+
+# same as above but completing on directory names.
+test_completion_limit "file ./gdb.a" "file \\\./gdb\\\.a(da|rch|sm)" \
+    $max_completions


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]