Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | # Copyright 2017-2020 Free Software Foundation, Inc. |
8955eb2d PA |
2 | |
3 | # This program is free software; you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation; either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # This program is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | # This file is part of the gdb testsuite. | |
17 | ||
18 | # Any variable or procedure in the namespace whose name starts with | |
19 | # "_" is private to the module. Do not use these. | |
20 | ||
21 | namespace eval completion { | |
22 | variable bell_re "\\\x07" | |
23 | ||
24 | # List of all quote chars. | |
25 | variable all_quotes_list {"'" "\""} | |
26 | ||
27 | # List of all quote chars, including no-quote at all. | |
28 | variable maybe_quoted_list {"" "'" "\""} | |
29 | ||
30 | variable keyword_list {"if" "task" "thread"} | |
31 | ||
32 | variable explicit_opts_list \ | |
a20714ff | 33 | {"-function" "-label" "-line" "-qualified" "-source"} |
8955eb2d PA |
34 | } |
35 | ||
36 | # Make a regular expression that matches a TAB completion list. | |
37 | ||
38 | proc make_tab_completion_list_re { completion_list } { | |
39 | # readline separates the completion columns that fit on the same | |
40 | # line with whitespace. Since we're testing under "set width | |
41 | # unlimited", all completions will be printed on the same line. | |
42 | # The amount of whitespace depends on the length of the widest | |
43 | # completion. We could compute that here and expect the exact | |
44 | # number of ws characters between each completion match, but to | |
45 | # keep it simple, we accept any number of characters. | |
46 | set ws " +" | |
47 | ||
48 | set completion_list_re "" | |
49 | foreach c $completion_list { | |
50 | append completion_list_re [string_to_regexp $c] | |
51 | append completion_list_re $ws | |
52 | } | |
53 | append completion_list_re $ws | |
54 | ||
55 | return $completion_list_re | |
56 | } | |
57 | ||
58 | # Make a regular expression that matches a "complete" command | |
59 | # completion list. CMD_PREFIX is the command prefix added to each | |
60 | # completion match. | |
61 | ||
62 | proc make_cmd_completion_list_re { cmd_prefix completion_list start_quote_char end_quote_char } { | |
63 | ||
64 | set completion_list_re "" | |
65 | foreach c $completion_list { | |
66 | # The command prefix is included in all completion matches. | |
67 | append completion_list_re [string_to_regexp $cmd_prefix$start_quote_char$c$end_quote_char] | |
68 | append completion_list_re "\r\n" | |
69 | } | |
70 | ||
71 | return $completion_list_re | |
72 | } | |
73 | ||
74 | # Clear the input line. | |
75 | ||
76 | proc clear_input_line { test } { | |
77 | global gdb_prompt | |
78 | ||
79 | send_gdb "\003" | |
80 | gdb_test_multiple "" "$test (clearing input line)" { | |
81 | -re "Quit\r\n$gdb_prompt $" { | |
82 | } | |
83 | } | |
84 | } | |
85 | ||
86 | # Test that completing LINE with TAB completes to nothing. | |
87 | ||
88 | proc test_gdb_complete_tab_none { line } { | |
89 | set line_re [string_to_regexp $line] | |
90 | ||
91 | set test "tab complete \"$line\"" | |
92 | send_gdb "$line\t" | |
93 | gdb_test_multiple "" "$test" { | |
94 | -re "^$line_re$completion::bell_re$" { | |
95 | pass "$test" | |
96 | } | |
97 | } | |
98 | ||
99 | clear_input_line $test | |
100 | } | |
101 | ||
102 | # Test that completing INPUT_LINE with TAB completes to | |
103 | # COMPLETE_LINE_RE. APPEND_CHAR_RE is the character expected to be | |
104 | # appended after EXPECTED_OUTPUT. Normally that's a whitespace, but | |
105 | # in some cases it's some other character, like a colon. | |
106 | ||
107 | proc test_gdb_complete_tab_unique { input_line complete_line_re append_char_re } { | |
108 | ||
109 | set test "tab complete \"$input_line\"" | |
110 | send_gdb "$input_line\t" | |
111 | gdb_test_multiple "" "$test" { | |
112 | -re "^$complete_line_re$append_char_re$" { | |
113 | pass "$test" | |
114 | } | |
115 | } | |
116 | ||
117 | clear_input_line $test | |
118 | } | |
119 | ||
120 | # Test that completing INPUT_LINE with TAB completes to "INPUT_LINE + | |
121 | # ADD_COMPLETED_LINE" and that it displays the completion matches in | |
e2a689da PA |
122 | # COMPLETION_LIST. If MAX_COMPLETIONS then we expect the completion |
123 | # to hit the max-completions limit. | |
8955eb2d PA |
124 | |
125 | proc test_gdb_complete_tab_multiple { input_line add_completed_line \ | |
e2a689da | 126 | completion_list {max_completions 0}} { |
8955eb2d PA |
127 | global gdb_prompt |
128 | ||
129 | set input_line_re [string_to_regexp $input_line] | |
130 | set add_completed_line_re [string_to_regexp $add_completed_line] | |
131 | ||
132 | set expected_re [make_tab_completion_list_re $completion_list] | |
133 | ||
e2a689da PA |
134 | if {$max_completions} { |
135 | append expected_re "\r\n" | |
136 | append expected_re \ | |
137 | "\\*\\*\\* List may be truncated, max-completions reached\\. \\*\\*\\*" | |
138 | } | |
139 | ||
8955eb2d PA |
140 | set test "tab complete \"$input_line\"" |
141 | send_gdb "$input_line\t" | |
142 | gdb_test_multiple "" "$test (first tab)" { | |
143 | -re "^${input_line_re}${completion::bell_re}$add_completed_line_re$" { | |
144 | send_gdb "\t" | |
145 | # If we auto-completed to an ambiguous prefix, we need an | |
146 | # extra tab to show the matches list. | |
147 | if {$add_completed_line != ""} { | |
148 | send_gdb "\t" | |
6892d2e4 PA |
149 | set maybe_bell ${completion::bell_re} |
150 | } else { | |
151 | set maybe_bell "" | |
8955eb2d PA |
152 | } |
153 | gdb_test_multiple "" "$test (second tab)" { | |
b2b2a215 PA |
154 | -re "^${maybe_bell}\r\n$expected_re\r\n$gdb_prompt " { |
155 | gdb_test_multiple "" "$test (second tab)" { | |
156 | -re "^$input_line_re$add_completed_line_re$" { | |
157 | pass "$test" | |
158 | } | |
159 | } | |
8955eb2d PA |
160 | } |
161 | } | |
162 | } | |
163 | } | |
164 | ||
165 | clear_input_line $test | |
166 | } | |
167 | ||
168 | # Test that completing LINE with the complete command completes to | |
169 | # nothing. | |
170 | ||
171 | proc test_gdb_complete_cmd_none { line } { | |
172 | gdb_test_no_output "complete $line" "cmd complete \"$line\"" | |
173 | } | |
174 | ||
175 | # Test that completing LINE with the complete command completes to | |
176 | # COMPLETE_LINE_RE. | |
177 | ||
178 | proc test_gdb_complete_cmd_unique { input_line complete_line_re } { | |
179 | global gdb_prompt | |
180 | ||
181 | set cmd "complete $input_line" | |
182 | set cmd_re [string_to_regexp $cmd] | |
183 | set test "cmd complete \"$input_line\"" | |
184 | gdb_test_multiple $cmd $test { | |
185 | -re "^$cmd_re\r\n$complete_line_re\r\n$gdb_prompt $" { | |
186 | pass $test | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
191 | # Test that completing "CMD_PREFIX + COMPLETION_WORD" with the | |
192 | # complete command displays the COMPLETION_LIST completion list. Each | |
e2a689da PA |
193 | # entry in the list should be prefixed by CMD_PREFIX. If |
194 | # MAX_COMPLETIONS then we expect the completion to hit the | |
195 | # max-completions limit. | |
8955eb2d | 196 | |
e2a689da | 197 | proc test_gdb_complete_cmd_multiple { cmd_prefix completion_word completion_list {start_quote_char ""} {end_quote_char ""} {max_completions 0}} { |
8955eb2d PA |
198 | global gdb_prompt |
199 | ||
200 | set expected_re [make_cmd_completion_list_re $cmd_prefix $completion_list $start_quote_char $end_quote_char] | |
e2a689da PA |
201 | |
202 | if {$max_completions} { | |
a994424f | 203 | set cmd_prefix_re [string_to_regexp $cmd_prefix] |
e2a689da | 204 | append expected_re \ |
a994424f | 205 | "$cmd_prefix_re \\*\\*\\* List may be truncated, max-completions reached\\. \\*\\*\\*.*\r\n" |
e2a689da PA |
206 | } |
207 | ||
8955eb2d PA |
208 | set cmd_re [string_to_regexp "complete $cmd_prefix$completion_word"] |
209 | set test "cmd complete \"$cmd_prefix$completion_word\"" | |
210 | gdb_test_multiple "complete $cmd_prefix$completion_word" $test { | |
211 | -re "^$cmd_re\r\n$expected_re$gdb_prompt $" { | |
212 | pass $test | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | # Test that completing LINE completes to nothing. | |
218 | ||
219 | proc test_gdb_complete_none { input_line } { | |
26655f53 SL |
220 | if { [readline_is_used] } { |
221 | test_gdb_complete_tab_none $input_line | |
222 | } | |
8955eb2d PA |
223 | test_gdb_complete_cmd_none $input_line |
224 | } | |
225 | ||
226 | # Test that completing INPUT_LINE completes to COMPLETE_LINE_RE. | |
227 | # | |
228 | # APPEND_CHAR is the character expected to be appended after | |
229 | # EXPECTED_OUTPUT when TAB completing. Normally that's a whitespace, | |
230 | # but in some cases it's some other character, like a colon. | |
231 | # | |
232 | # If MAX_COMPLETIONS is true, then we expect the completion to hit the | |
233 | # max-completions limit. Since we're expecting a unique completion | |
234 | # match, this will only be visible in the "complete" command output. | |
235 | # Tab completion will just auto-complete the only match and won't | |
236 | # display a match list. | |
237 | # | |
238 | # Note: usually it's more convenient to pass a literal string instead | |
239 | # of a regular expression (as COMPLETE_LINE_RE). See | |
240 | # test_gdb_complete_unique below. | |
241 | ||
242 | proc test_gdb_complete_unique_re { input_line complete_line_re {append_char " "} {max_completions 0}} { | |
243 | set append_char_re [string_to_regexp $append_char] | |
26655f53 SL |
244 | if { [readline_is_used] } { |
245 | test_gdb_complete_tab_unique $input_line $complete_line_re $append_char_re | |
246 | } | |
8955eb2d PA |
247 | |
248 | # Trim INPUT_LINE and COMPLETE LINE, for the case we're completing | |
249 | # a command with leading whitespace. Leading command whitespace | |
250 | # is discarded by GDB. | |
251 | set input_line [string trimleft $input_line] | |
252 | set expected_output_re [string trimleft $complete_line_re] | |
253 | if {$append_char_re != " "} { | |
254 | append expected_output_re $append_char_re | |
255 | } | |
256 | if {$max_completions} { | |
257 | set max_completion_reached_msg \ | |
258 | "*** List may be truncated, max-completions reached. ***" | |
259 | set input_line_re \ | |
260 | [string_to_regexp $input_line] | |
261 | set max_completion_reached_msg_re \ | |
262 | [string_to_regexp $max_completion_reached_msg] | |
263 | ||
264 | append expected_output_re \ | |
265 | "\r\n$input_line_re $max_completion_reached_msg_re" | |
266 | } | |
267 | ||
268 | test_gdb_complete_cmd_unique $input_line $expected_output_re | |
269 | } | |
270 | ||
271 | # Like TEST_GDB_COMPLETE_UNIQUE_RE, but COMPLETE_LINE is a string, not | |
272 | # a regular expression. | |
273 | ||
274 | proc test_gdb_complete_unique { input_line complete_line {append_char " "} {max_completions 0}} { | |
275 | set complete_line_re [string_to_regexp $complete_line] | |
276 | test_gdb_complete_unique_re $input_line $complete_line_re $append_char $max_completions | |
277 | } | |
278 | ||
279 | # Test that completing "CMD_PREFIX + COMPLETION_WORD" adds | |
280 | # ADD_COMPLETED_LINE to the input line, and that it displays | |
281 | # COMPLETION_LIST as completion match list. COMPLETION_WORD is the | |
e2a689da PA |
282 | # completion word. If MAX_COMPLETIONS then we expect the completion |
283 | # to hit the max-completions limit. | |
284 | ||
285 | proc test_gdb_complete_multiple { | |
286 | cmd_prefix completion_word add_completed_line completion_list | |
287 | {start_quote_char ""} {end_quote_char ""} {max_completions 0} | |
288 | } { | |
26655f53 SL |
289 | if { [readline_is_used] } { |
290 | test_gdb_complete_tab_multiple "$cmd_prefix$completion_word" $add_completed_line $completion_list $max_completions | |
291 | } | |
e2a689da | 292 | test_gdb_complete_cmd_multiple $cmd_prefix $completion_word $completion_list $start_quote_char $end_quote_char $max_completions |
8955eb2d PA |
293 | } |
294 | ||
6a3c6ee4 PA |
295 | # Test that all the substring prefixes of INPUT from [0..START) to |
296 | # [0..END) complete to COMPLETION_RE (a regular expression). If END | |
297 | # is ommitted, default to the length of INPUT. | |
8955eb2d | 298 | |
6a3c6ee4 | 299 | proc test_complete_prefix_range_re {input completion_re start {end -1}} { |
8955eb2d | 300 | if {$end == -1} { |
6a3c6ee4 | 301 | set end [string length $input] |
8955eb2d PA |
302 | } |
303 | ||
304 | for {set i $start} {$i < $end} {incr i} { | |
6a3c6ee4 PA |
305 | set line [string range $input 0 $i] |
306 | test_gdb_complete_unique_re "$line" $completion_re | |
8955eb2d PA |
307 | } |
308 | } | |
309 | ||
6a3c6ee4 PA |
310 | # Test that all the substring prefixes of COMPLETION from [0..START) |
311 | # to [0..END) complete to COMPLETION. If END is ommitted, default to | |
312 | # the length of COMPLETION. | |
313 | ||
314 | proc test_complete_prefix_range {completion start {end -1}} { | |
315 | set completion_re [string_to_regexp $completion] | |
316 | test_complete_prefix_range_re $completion $completion_re $start $end | |
317 | } | |
318 | ||
8955eb2d PA |
319 | # Find NEEDLE in HAYSTACK and return the index _after_ NEEDLE. E.g., |
320 | # searching for "(" in "foo(int)" returns 4, which would be useful if | |
321 | # you want to find the "(" to try completing "foo(". | |
322 | ||
323 | proc index_after {needle haystack} { | |
324 | set start [string first $needle $haystack] | |
325 | if {$start == -1} { | |
326 | error "could not find \"$needle\" in \"$haystack\"" | |
327 | } | |
328 | return [expr $start + [string length $needle]] | |
329 | } | |
330 | ||
331 | # Create a breakpoint using BREAK_COMMAND, and return the number | |
332 | # of locations found. | |
333 | ||
334 | proc completion::_create_bp {break_command} { | |
335 | global gdb_prompt | |
336 | global decimal hex | |
337 | ||
338 | set found_locations -1 | |
339 | ||
340 | set test "set breakpoint" | |
341 | gdb_test_multiple "$break_command" $test { | |
342 | -re "\\\(\($decimal\) locations\\\)\r\n$gdb_prompt $" { | |
343 | set found_locations "$expect_out(1,string)" | |
344 | } | |
345 | -re "Breakpoint $decimal at $hex: file .*, line .*$gdb_prompt $" { | |
346 | set found_locations 1 | |
347 | } | |
348 | -re "Make breakpoint pending on future shared library load.*y or .n.. $" { | |
349 | send_gdb "n\n" | |
350 | gdb_test_multiple "" "$test (prompt)" { | |
351 | -re "$gdb_prompt $" { | |
352 | } | |
353 | } | |
354 | set found_locations 0 | |
355 | } | |
356 | -re "invalid explicit location argument, \[^\r\n\]*\r\n$gdb_prompt $" { | |
357 | set found_locations 0 | |
358 | } | |
359 | -re "Function \[^\r\n\]* not defined in \[^\r\n\]*\r\n$gdb_prompt $" { | |
360 | set found_locations 0 | |
361 | } | |
362 | } | |
363 | return $found_locations | |
364 | } | |
365 | ||
366 | # Return true if lists A and B have the same elements. Order of | |
367 | # elements does not matter. | |
368 | ||
369 | proc completion::_leq {a b} { | |
370 | return [expr {[lsort $a] eq [lsort $b]}] | |
371 | } | |
372 | ||
373 | # Check that trying to create a breakpoint using BREAK_COMMAND fails. | |
374 | ||
375 | proc check_setting_bp_fails {break_command} { | |
376 | with_test_prefix "\"$break_command\" creates no bp locations" { | |
377 | set found_locations [completion::_create_bp $break_command] | |
378 | gdb_assert {$found_locations == 0} "matches" | |
379 | if {$found_locations != 0} { | |
380 | delete_breakpoints | |
381 | } | |
382 | } | |
383 | } | |
384 | ||
385 | # Check that creating the breakpoint using BREAK_COMMAND finds the | |
386 | # same breakpoint locations as completing BREAK_COMMAND. | |
387 | # COMPLETION_LIST is the expected completion match list. | |
388 | ||
389 | proc check_bp_locations_match_list {break_command completion_list} { | |
390 | global gdb_prompt | |
391 | global hex | |
392 | ||
393 | with_test_prefix "compare \"$break_command\" completion list with bp location list" { | |
394 | set num_locations [completion::_create_bp $break_command] | |
395 | ||
396 | set found_list "" | |
397 | ||
398 | set any "\[^\r\n\]*" | |
399 | ||
400 | gdb_test_multiple "info breakpoint \$bpnum" "info breakpoint" { | |
401 | -re "in \(\[^\r\n\]*\) at " { | |
402 | # A function location. | |
403 | set found_location "$expect_out(1,string)" | |
404 | lappend found_list $found_location | |
405 | exp_continue | |
406 | } | |
407 | -re "breakpoint${any}keep${any}y${any}$hex\[ \t]*\(${any}\)\r\n" { | |
408 | # A label location. | |
409 | set found_location "$expect_out(1,string)" | |
410 | lappend found_list $found_location | |
411 | exp_continue | |
412 | } | |
413 | -re "$gdb_prompt $" { | |
414 | } | |
415 | } | |
416 | ||
417 | gdb_assert {[completion::_leq $found_list $completion_list]} "matches" | |
418 | ||
419 | delete_breakpoints | |
420 | } | |
421 | } | |
422 | ||
423 | # Build linespec and explicit locations out of all the combinations of | |
424 | # SOURCES, FUNCTIONS and LABELS, with all combinations of possible | |
425 | # quoting and whitespace around separators, and run BODY_LINESPEC and | |
426 | # BODY_EXPLICIT in the context of the caller for each combination. A | |
427 | # variable named "location" is set in the callers context with the | |
428 | # currently iterated location. | |
429 | ||
430 | proc foreach_location_functions { sources functions body_linespec body_explicit } { | |
431 | upvar source source | |
432 | upvar function function | |
433 | upvar source_sep source_sep | |
434 | upvar location location | |
435 | ||
436 | foreach source $sources { | |
437 | # Test with and without source quoting. | |
438 | foreach sqc $completion::maybe_quoted_list { | |
439 | if {$source == "" && $sqc != ""} { | |
440 | # Invalid combination. | |
441 | continue | |
442 | } | |
443 | ||
444 | # Test with and without function quoting. | |
445 | foreach fqc $completion::maybe_quoted_list { | |
446 | # Test known and unknown functions. | |
447 | foreach function $functions { | |
448 | # Linespec version. Test with and without spacing | |
449 | # after the source/colon colon separator. | |
450 | foreach source_sep {"" ":" ": "} { | |
451 | # Skip invalid combinations. | |
452 | if {$source == "" && $source_sep != ""} { | |
453 | continue | |
454 | } | |
455 | if {$source != "" && $source_sep == ""} { | |
456 | continue | |
457 | } | |
458 | ||
459 | set location "${sqc}${source}${sqc}${source_sep}${fqc}$function${fqc}" | |
460 | uplevel 1 $body_linespec | |
461 | } | |
462 | ||
463 | # Explicit locations version. | |
464 | if {$source != ""} { | |
465 | set loc_src "-source ${sqc}${source}${sqc} " | |
466 | } else { | |
467 | set loc_src "" | |
468 | } | |
469 | ||
470 | set location "${loc_src}-function ${fqc}$function${fqc}" | |
471 | uplevel 1 $body_explicit | |
472 | } | |
473 | } | |
474 | } | |
475 | } | |
476 | } | |
477 | ||
478 | # Same as foreach_locations_functions, but also iterate over | |
479 | # combinations of labels. | |
480 | proc foreach_location_labels { sources functions labels body_linespec body_explicit } { | |
481 | upvar source source | |
482 | upvar function function | |
483 | upvar label label | |
484 | upvar source_sep source_sep | |
485 | upvar label_sep label_sep | |
486 | upvar location location | |
487 | ||
488 | # Test both with a known source file and without a source file | |
489 | # component. | |
490 | foreach_location_functions \ | |
491 | $sources \ | |
492 | $functions \ | |
493 | { | |
494 | # Linespec version. Test various spacing around the label | |
495 | # colon separator. | |
496 | set saved_location ${location} | |
497 | foreach label_sep {":" " :" ": " " : "} { | |
498 | # Test both known and unknown label. | |
499 | foreach label $labels { | |
500 | set location "${saved_location}${label_sep}$label" | |
501 | uplevel 1 $body_linespec | |
502 | } | |
503 | } | |
504 | } \ | |
505 | { | |
506 | # Explicit locations version. | |
507 | set saved_location ${location} | |
508 | foreach label $labels { | |
509 | set location "${saved_location} -label $label" | |
510 | uplevel 1 $body_explicit | |
511 | } | |
512 | } | |
513 | } | |
e2a689da PA |
514 | |
515 | # Check that completion of INPUT_LINE results in GDB completing on all | |
516 | # command names. | |
517 | proc test_gdb_completion_offers_commands {input_line} { | |
518 | global gdb_prompt | |
519 | ||
520 | # There are two many commands to usefully check here. So we force | |
521 | # max-completions to 2, and check if those 2 come out. | |
522 | ||
523 | # Save current max-completions. | |
524 | set max_completions 0 | |
525 | set test "show max-completions" | |
526 | gdb_test_multiple $test $test { | |
527 | -re "Maximum number of completion candidates is (.*)\\.\r\n$gdb_prompt $" { | |
528 | set max_completions $expect_out(1,string) | |
529 | } | |
530 | } | |
531 | ||
532 | # Force showing two commands. | |
533 | gdb_test_no_output "set max-completions 2" "" | |
534 | ||
535 | test_gdb_complete_multiple $input_line "" "" { | |
536 | "!" | |
537 | "+" | |
538 | } "" "" 1 | |
539 | ||
540 | # Restore. | |
541 | gdb_test_no_output "set max-completions $max_completions" "" | |
542 | } |