873dc785cdab937d7b5eef9f791a8444b090ce6b
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.linespec / cpcompletion.exp
1 # Copyright 2017 Free Software Foundation, Inc.
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 load_lib completion-support.exp
19
20 standard_testfile cpls.cc cpls2.cc cpls-hyphen.cc
21
22 if {[prepare_for_testing "failed to prepare" $testfile \
23 [list $srcfile $srcfile2 $srcfile3] {debug}]} {
24 return -1
25 }
26
27 # Disable the completion limit for the whole testcase.
28 gdb_test_no_output "set max-completions unlimited"
29
30 # Start of tests.
31
32 # Test completion of all parameter prefixes, crossing "(" and ")",
33 # with and without whitespace.
34
35 proc_with_prefix all-param-prefixes {} {
36
37 # Test both linespecs and explicit locations.
38 foreach cmd_prefix {"b" "b -function"} {
39 set line "$cmd_prefix param_prefixes_test_long(long)"
40 set start [index_after "test_long" $line]
41 test_complete_prefix_range $line $start
42
43 # Same, but with extra spaces. Note that the original spaces in
44 # the input line are preserved after completion.
45 test_gdb_complete_unique \
46 "$cmd_prefix param_prefixes_test_long(long " \
47 "$cmd_prefix param_prefixes_test_long(long )"
48 test_gdb_complete_unique \
49 "$cmd_prefix param_prefixes_test_long( long " \
50 "$cmd_prefix param_prefixes_test_long( long )"
51 test_gdb_complete_unique \
52 "$cmd_prefix param_prefixes_test_long ( long " \
53 "$cmd_prefix param_prefixes_test_long ( long )"
54
55 # Complete all parameter prefixes between "(i" and "(int*, int&)".
56 # Note that this exercises completing when the point is at the
57 # space in "param_prefixes_test_intp_intr(int*, ".
58 set line "$cmd_prefix param_prefixes_test_intp_intr(int*, int&)"
59 set start [index_after "intp_intr" $line]
60 test_complete_prefix_range $line $start
61
62 # Similar, but with extra spaces.
63 test_gdb_complete_unique \
64 "$cmd_prefix param_prefixes_test_intp_intr ( int* " \
65 "$cmd_prefix param_prefixes_test_intp_intr ( int* , int&)"
66
67 test_gdb_complete_unique \
68 "$cmd_prefix param_prefixes_test_intp_intr ( int *" \
69 "$cmd_prefix param_prefixes_test_intp_intr ( int *, int&)"
70
71 test_gdb_complete_unique \
72 "$cmd_prefix param_prefixes_test_intp_intr ( int *, int " \
73 "$cmd_prefix param_prefixes_test_intp_intr ( int *, int &)"
74
75 test_gdb_complete_unique \
76 "$cmd_prefix param_prefixes_test_intp_intr ( int *, int & " \
77 "$cmd_prefix param_prefixes_test_intp_intr ( int *, int & )"
78 }
79 }
80
81 # Test completion of an overloaded function.
82
83 proc_with_prefix overload {} {
84 set completion_list {
85 "overload_ambiguous_test(int, int)"
86 "overload_ambiguous_test(int, long)"
87 "overload_ambiguous_test(long)"
88 }
89
90 foreach cmd_prefix {"b" "b -function"} {
91 test_gdb_complete_multiple \
92 "$cmd_prefix " "overload_ambiguous_" "test(" \
93 $completion_list
94 check_bp_locations_match_list \
95 "$cmd_prefix overload_ambiguous_test" \
96 $completion_list
97
98 # Test disambiguating by typing enough to pick the "int" as
99 # first parameter type. This then tests handling ambiguity in
100 # the second parameter, which checks that tab completion when
101 # the point is at the whitespace behaves naturally, by showing
102 # the remaining matching overloads to the user.
103 test_gdb_complete_multiple \
104 "$cmd_prefix " "overload_ambiguous_test(i" "nt, " {
105 "overload_ambiguous_test(int, int)"
106 "overload_ambiguous_test(int, long)"
107 }
108
109 # Add a few more characters to make the completion
110 # unambiguous.
111 test_gdb_complete_unique \
112 "$cmd_prefix overload_ambiguous_test(int, i" \
113 "$cmd_prefix overload_ambiguous_test(int, int)"
114 check_bp_locations_match_list \
115 "$cmd_prefix overload_ambiguous_test(int, int)" {
116 "overload_ambiguous_test(int, int)"
117 }
118 }
119 }
120
121 # Test that when the function is unambiguous, linespec completion
122 # appends the end quote char automatically, both ' and ".
123
124 proc_with_prefix append-end-quote-char-when-unambiguous {} {
125 foreach cmd_prefix {"b" "b -function"} {
126 foreach qc $completion::all_quotes_list {
127 set linespec "${qc}not_overloaded_fn()${qc}"
128 foreach cmd [list "$cmd_prefix ${qc}not_overloaded_fn()" \
129 "$cmd_prefix ${qc}not_overloaded_fn" \
130 "$cmd_prefix ${qc}not_overloaded_"] {
131 test_gdb_complete_unique $cmd "$cmd_prefix $linespec"
132 }
133 check_bp_locations_match_list \
134 "$cmd_prefix $linespec" {"not_overloaded_fn()"}
135 }
136 }
137 }
138
139 # Test completing symbols of source files.
140
141 proc_with_prefix in-source-file-unconstrained {} {
142 # First test that unconstrained matching picks up functions from
143 # multiple files.
144 test_gdb_complete_multiple "b " "file_constrained_test" "_cpls" {
145 "file_constrained_test_cpls2_function(int)"
146 "file_constrained_test_cpls_function(int)"
147 }
148 check_setting_bp_fails "b file_constrained_test_cpls"
149 }
150
151 # Test an unambiguous completion that would be ambiguous if it weren't
152 # for the source file component, due to
153 # "file_constrained_test_cpls_function" in cpls.cc. Test with
154 # different components quoted, and with whitespace before the function
155 # name.
156
157 proc_with_prefix in-source-file-unambiguous {} {
158 foreach sqc $completion::maybe_quoted_list {
159 foreach fqc $completion::maybe_quoted_list {
160 # Linespec.
161 foreach sep {":" ": "} {
162 set linespec \
163 "${sqc}cpls2.cc${sqc}${sep}${fqc}file_constrained_test_cpls2_function(int)${fqc}"
164 set complete_line "b $linespec"
165 set start [index_after "constrained_test" $complete_line]
166 set input_line [string range $complete_line 0 $start]
167 test_gdb_complete_unique $input_line ${complete_line}
168 check_bp_locations_match_list "b $linespec" {
169 "file_constrained_test_cpls2_function(int)"
170 }
171 }
172
173 # Explicit location.
174 set source_opt "-source ${sqc}cpls2.cc${sqc}"
175 set function_opt "-function ${fqc}file_constrained_test_cpls2_function(int)${fqc}"
176 set complete_line "b $source_opt $function_opt"
177 set start [index_after "cpls2_functio" $complete_line]
178 set input_line [string range $complete_line 0 $start]
179 test_gdb_complete_unique $input_line ${complete_line}
180 check_bp_locations_match_list "$complete_line" {
181 "file_constrained_test_cpls2_function(int)"
182 }
183 }
184 }
185 }
186
187 # Test an ambiguous completion constrained by a source file. Test
188 # with different components quoted, and with whitespace before the
189 # function name.
190
191 proc_with_prefix in-source-file-ambiguous {} {
192 foreach sqc $completion::maybe_quoted_list {
193 foreach fqc $completion::maybe_quoted_list {
194 # Linespec.
195 foreach sep {":" ": "} {
196 set cmd_prefix "b ${sqc}cpls2.cc${sqc}${sep}"
197 test_gdb_complete_multiple "${cmd_prefix}" ${fqc} "" {
198 "another_file_constrained_test_cpls2_function(int)"
199 "file_constrained_test_cpls2_function(int)"
200 } ${fqc} ${fqc}
201 }
202
203 # Explicit location.
204 test_gdb_complete_multiple \
205 "b -source ${sqc}cpls2.cc${sqc} -function " ${fqc} "" {
206 "another_file_constrained_test_cpls2_function(int)"
207 "file_constrained_test_cpls2_function(int)"
208 } ${fqc} ${fqc}
209 }
210 }
211 }
212
213 # Check that completing a file name in a linespec auto-appends a colon
214 # instead of a whitespace character.
215
216 proc_with_prefix source-complete-appends-colon {} {
217 # Test with quotes to make sure the end quote char is put at the
218 # right place.
219 foreach qc $completion::maybe_quoted_list {
220 test_gdb_complete_unique \
221 "b ${qc}cpls2." \
222 "b ${qc}cpls2.cc${qc}" ":"
223 test_gdb_complete_unique \
224 "b ${qc}cpls2.c" \
225 "b ${qc}cpls2.cc${qc}" ":"
226 test_gdb_complete_unique \
227 "b ${qc}cpls2.cc" \
228 "b ${qc}cpls2.cc${qc}" ":"
229
230 # Same, but with a filename with an hyphen (which is normally
231 # a language word break char).
232 test_gdb_complete_unique \
233 "b ${qc}cpls-" \
234 "b ${qc}cpls-hyphen.cc${qc}" ":"
235 test_gdb_complete_unique \
236 "b ${qc}cpls-hyphen" \
237 "b ${qc}cpls-hyphen.cc${qc}" ":"
238 }
239
240 # Test the same, but with the name of a nonexisting file.
241
242 # Cursor at the end of the string.
243 test_gdb_complete_none "b nonexistingfilename.cc"
244 # Cursor past the end of the string.
245 test_gdb_complete_multiple "b nonexistingfilename.cc " "" "" \
246 $completion::keyword_list
247 foreach qc $completion::all_quotes_list {
248 # Unterminated quote.
249 test_gdb_complete_none "b ${qc}nonexistingfilename.cc"
250 test_gdb_complete_none "b ${qc}nonexistingfilename.cc "
251 # Terminated quote, cursor at the quote.
252 test_gdb_complete_unique \
253 "b ${qc}nonexistingfilename.cc${qc}" \
254 "b ${qc}nonexistingfilename.cc${qc}"
255 # Terminated quote, cursor past the quote.
256 test_gdb_complete_multiple \
257 "b ${qc}nonexistingfilename.cc${qc} " "" "" \
258 $completion::keyword_list
259 }
260 }
261
262 ####################################################################
263
264 # Test that a colon at the end of the linespec is understood as an
265 # incomplete scope operator (incomplete-scope-colon), instead of a
266 # source/function separator.
267
268 proc_with_prefix incomplete-scope-colon {} {
269
270 # Helper for the loop below to simplify it. Tests completion of
271 # the range defined by the RANGE_SS found in the constructed line.
272 #
273 # E.g., with:
274 #
275 # source="source.cc"
276 # fqc="'"
277 # prototype="ns::function()"
278 # range_ss="s::f"
279 #
280 # we'd try completing with the cursor set in each of the
281 # underlined range's positions of:
282 #
283 # b source.cc:'ns::function()'"
284 # ^^^^
285 #
286 # Also test that setting a breakpoint at the constructed line
287 # finds the same breakpoint location as completion does.
288 #
289 proc incomplete_scope_colon_helper {prototype range_ss {skip_check_bp 0}} {
290 foreach source {"" "cpls.cc"} {
291 # Test with and without source quoting.
292 foreach sqc $completion::maybe_quoted_list {
293 if {$source == "" && $sqc != ""} {
294 # Invalid combination.
295 continue
296 }
297
298 # Test with and without function quoting.
299 foreach fqc $completion::maybe_quoted_list {
300 if {$source == ""} {
301 set linespec_source ""
302 set explicit_source ""
303 } else {
304 set linespec_source "${sqc}${source}${sqc}:"
305 set explicit_source "-source ${sqc}${source}${sqc}"
306 }
307
308 # Even though this use case is trickier with
309 # linespecs due to the ":" as separator, test both
310 # linespecs and explicit locations for
311 # completeness.
312 foreach location [list \
313 "${linespec_source}${fqc}$prototype${fqc}" \
314 "${explicit_source} -function ${fqc}$prototype${fqc}"] {
315 set complete_line "b $location"
316 set start [string first $range_ss $complete_line]
317 set end [expr ($start + [string length $range_ss])]
318 test_complete_prefix_range $complete_line $start $end
319 if {!$skip_check_bp} {
320 check_bp_locations_match_list "b $location" [list "$prototype"]
321 }
322 }
323 }
324 }
325 }
326 }
327
328 incomplete_scope_colon_helper \
329 "struct_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
330 "t::i"
331
332 incomplete_scope_colon_helper \
333 "ns_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
334 "t::i"
335
336 # Test completing around both "::"s.
337 foreach range_ss {"t::s" "t::i"} skip_check_bp {0 1} {
338 incomplete_scope_colon_helper \
339 "ns2_incomplete_scope_colon_test::struct_in_ns2_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
340 $range_ss $skip_check_bp
341 }
342 }
343
344 # Basic test for completing "operator<". More extensive C++ operator
345 # tests in cpls-op.exp.
346
347 proc_with_prefix operator< {} {
348 # Complete all prefixes between "oper" and the whole prototype.
349 set function "operator<(foo_enum, foo_enum)"
350 foreach cmd_prefix {"b" "b -function"} {
351 set line "$cmd_prefix $function"
352 set start [index_after "oper" $line]
353 test_complete_prefix_range $line $start
354 }
355
356 # There's a label in the function; try completing it. (Exhaustive
357 # label completion tests further below.)
358 foreach location [list \
359 "$function:label1" \
360 "-function $function -label label1"] {
361
362 set cmd "b $location"
363 set input_line [string range $cmd 0 [expr [string length $cmd] - 3]]
364
365 test_gdb_complete_unique $input_line $cmd
366 test_gdb_complete_unique $cmd $cmd
367 check_bp_locations_match_list $cmd [list "$location"]
368 }
369 }
370
371 # Test completion of function labels.
372
373 proc_with_prefix function-labels {} {
374 # Test with and without a source file component.
375 foreach_location_functions \
376 { "" "cpls.cc" } \
377 { "function_with_labels(int)" } \
378 {
379 # Linespec version. Test various spacing around the label
380 # colon separator.
381 foreach label_sep {":" " :" ": " " : "} {
382 set linespec "${location}${label_sep}"
383 test_gdb_complete_multiple "b $linespec" "l" "abel" {
384 "label1"
385 "label2"
386 }
387 check_setting_bp_fails "b ${linespec}label"
388
389 set tsep [string trim ${source_sep}]
390 check_bp_locations_match_list \
391 "b ${linespec}label1" [list "${source}${tsep}${function}:label1"]
392 check_bp_locations_match_list \
393 "b ${linespec}label2" [list "${source}${tsep}${function}:label2"]
394 }
395 } \
396 {
397 # Explicit locations version.
398 append location " -label"
399 test_gdb_complete_multiple "b $location " "l" "abel" {
400 "label1"
401 "label2"
402 }
403 check_setting_bp_fails "b $location label"
404
405 if {$source != ""} {
406 set bp_loc_src "-source ${source} "
407 } else {
408 set bp_loc_src ""
409 }
410 check_bp_locations_match_list \
411 "b ${location} label1" [list "${bp_loc_src}-function $function -label label1"]
412 check_bp_locations_match_list \
413 "b ${location} label2" [list "${bp_loc_src}-function $function -label label2"]
414 }
415 }
416
417 # Test that completion after a function name offers keyword
418 # (if/task/thread) matches in linespec mode, and also the explicit
419 # location options in explicit locations mode.
420
421 proc_with_prefix keywords-after-function {} {
422 set explicit_list \
423 [concat $completion::explicit_opts_list $completion::keyword_list]
424
425 # Test without a source file, with a known source file, and with
426 # and unknown source file.
427 # Test a known and an unknown function.
428 foreach_location_functions \
429 { "" "cpls.cc" "unknown_file.cc" } \
430 { "function_with_labels(int)" "unknown_function(int)" } \
431 {
432 # Linespec version.
433 test_gdb_complete_multiple "b ${location} " "" "" \
434 $completion::keyword_list
435 } \
436 {
437 # Explicit locations version.
438 test_gdb_complete_multiple "b ${location} " "" "" \
439 $explicit_list
440 }
441 }
442
443 # Same, but after a label.
444
445 proc_with_prefix keywords-after-label {} {
446 set explicit_list \
447 [concat $completion::explicit_opts_list $completion::keyword_list]
448
449 foreach_location_labels \
450 { "" "cpls.cc" } \
451 { "function_with_labels(int)" "unknown_function(int)" } \
452 { "label1" "non_existing_label" } \
453 {
454 # Linespec version.
455 test_gdb_complete_multiple "b ${location} " "" "" \
456 $completion::keyword_list
457 } \
458 {
459 # Explicit locations version.
460 test_gdb_complete_multiple "b ${location} " "" "" \
461 $explicit_list
462 }
463 }
464
465 # Similar, but after an unknown file, and in linespec mode only.
466
467 proc_with_prefix keywords-after-unknown-file {} {
468 # Test with and without quoting.
469 foreach qc $completion::maybe_quoted_list {
470 set line "b ${qc}unknown_file.cc${qc}: "
471 test_gdb_complete_multiple $line "" "" $completion::keyword_list
472 }
473 }
474
475 # Test that linespec / function completion does not match data
476 # symbols, only functions/methods.
477
478 proc_with_prefix no-data-symbols {} {
479 foreach cmd_prefix {"b" "b -function"} {
480 test_gdb_complete_unique "$cmd_prefix code_" "$cmd_prefix code_function()"
481 }
482 }
483
484
485 # After "if", we expect an expression, which has a different completer
486 # that matches data symbols as well. Check that that works.
487
488 proc_with_prefix if-expression {} {
489 foreach cmd_prefix {"b" "b -function"} {
490 test_gdb_complete_multiple "$cmd_prefix function() if " "code_" "" {
491 "code_data"
492 "code_function()"
493 }
494
495 test_gdb_complete_unique \
496 "$cmd_prefix function() if code_data + another_da" \
497 "$cmd_prefix function() if code_data + another_data"
498
499 test_gdb_complete_unique \
500 "$cmd_prefix non_existing_function() if code_data + another_da" \
501 "$cmd_prefix non_existing_function() if code_data + another_data"
502
503 # FIXME: For now, thread and task also use the expression
504 # completer.
505 test_gdb_complete_unique \
506 "$cmd_prefix function() thread code_data + another_da" \
507 "$cmd_prefix function() thread code_data + another_data"
508 test_gdb_complete_unique \
509 "$cmd_prefix function() task code_data + another_da" \
510 "$cmd_prefix function() task code_data + another_data"
511 }
512 }
513
514 # The testcase driver. Calls all test procedures.
515
516 proc test_driver {} {
517 all-param-prefixes
518 overload
519 append-end-quote-char-when-unambiguous
520 in-source-file-unconstrained
521 in-source-file-unambiguous
522 in-source-file-ambiguous
523 source-complete-appends-colon
524 incomplete-scope-colon
525 operator<
526 function-labels
527 keywords-after-function
528 keywords-after-label
529 keywords-after-unknown-file
530 no-data-symbols
531 if-expression
532 }
533
534 test_driver
This page took 0.041217 seconds and 3 git commands to generate.