aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.linespec/cpcompletion.exp
blob: 873dc785cdab937d7b5eef9f791a8444b090ce6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# Copyright 2017 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file is part of the gdb testsuite.

load_lib completion-support.exp

standard_testfile cpls.cc cpls2.cc cpls-hyphen.cc

if {[prepare_for_testing "failed to prepare" $testfile \
	 [list $srcfile $srcfile2 $srcfile3] {debug}]} {
    return -1
}

# Disable the completion limit for the whole testcase.
gdb_test_no_output "set max-completions unlimited"

# Start of tests.

# Test completion of all parameter prefixes, crossing "(" and ")",
# with and without whitespace.

proc_with_prefix all-param-prefixes {} {

    # Test both linespecs and explicit locations.
    foreach cmd_prefix {"b" "b -function"} {
	set line "$cmd_prefix param_prefixes_test_long(long)"
	set start [index_after "test_long" $line]
	test_complete_prefix_range $line $start

	# Same, but with extra spaces.  Note that the original spaces in
	# the input line are preserved after completion.
	test_gdb_complete_unique \
	    "$cmd_prefix param_prefixes_test_long(long "   \
	    "$cmd_prefix param_prefixes_test_long(long )"
	test_gdb_complete_unique \
	    "$cmd_prefix param_prefixes_test_long( long "  \
	    "$cmd_prefix param_prefixes_test_long( long )"
	test_gdb_complete_unique \
	    "$cmd_prefix param_prefixes_test_long ( long " \
	    "$cmd_prefix param_prefixes_test_long ( long )"

	# Complete all parameter prefixes between "(i" and "(int*, int&)".
	# Note that this exercises completing when the point is at the
	# space in "param_prefixes_test_intp_intr(int*, ".
	set line "$cmd_prefix param_prefixes_test_intp_intr(int*, int&)"
	set start [index_after "intp_intr" $line]
	test_complete_prefix_range $line $start

	# Similar, but with extra spaces.
	test_gdb_complete_unique \
	    "$cmd_prefix param_prefixes_test_intp_intr (  int* " \
	    "$cmd_prefix param_prefixes_test_intp_intr (  int* , int&)"

	test_gdb_complete_unique \
	    "$cmd_prefix param_prefixes_test_intp_intr (  int *" \
	    "$cmd_prefix param_prefixes_test_intp_intr (  int *, int&)"

	test_gdb_complete_unique \
	    "$cmd_prefix param_prefixes_test_intp_intr (  int *, int " \
	    "$cmd_prefix param_prefixes_test_intp_intr (  int *, int &)"

	test_gdb_complete_unique \
	    "$cmd_prefix param_prefixes_test_intp_intr (  int *,  int & " \
	    "$cmd_prefix param_prefixes_test_intp_intr (  int *,  int & )"
    }
}

# Test completion of an overloaded function.

proc_with_prefix overload {} {
    set completion_list {
	"overload_ambiguous_test(int, int)"
	"overload_ambiguous_test(int, long)"
	"overload_ambiguous_test(long)"
    }

    foreach cmd_prefix {"b" "b -function"} {
	test_gdb_complete_multiple \
	    "$cmd_prefix " "overload_ambiguous_" "test(" \
	    $completion_list
	check_bp_locations_match_list \
	    "$cmd_prefix overload_ambiguous_test" \
	    $completion_list

	# Test disambiguating by typing enough to pick the "int" as
	# first parameter type.  This then tests handling ambiguity in
	# the second parameter, which checks that tab completion when
	# the point is at the whitespace behaves naturally, by showing
	# the remaining matching overloads to the user.
	test_gdb_complete_multiple \
	    "$cmd_prefix " "overload_ambiguous_test(i" "nt, " {
	    "overload_ambiguous_test(int, int)"
	    "overload_ambiguous_test(int, long)"
	}

	# Add a few more characters to make the completion
	# unambiguous.
	test_gdb_complete_unique \
	    "$cmd_prefix overload_ambiguous_test(int, i" \
	    "$cmd_prefix overload_ambiguous_test(int, int)"
	check_bp_locations_match_list \
	    "$cmd_prefix overload_ambiguous_test(int, int)" {
		"overload_ambiguous_test(int, int)"
	    }
    }
}

# Test that when the function is unambiguous, linespec completion
# appends the end quote char automatically, both ' and ".

proc_with_prefix append-end-quote-char-when-unambiguous {} {
    foreach cmd_prefix {"b" "b -function"} {
	foreach qc $completion::all_quotes_list {
	    set linespec "${qc}not_overloaded_fn()${qc}"
	    foreach cmd [list "$cmd_prefix ${qc}not_overloaded_fn()" \
			      "$cmd_prefix ${qc}not_overloaded_fn" \
			      "$cmd_prefix ${qc}not_overloaded_"] {
		test_gdb_complete_unique $cmd "$cmd_prefix $linespec"
	    }
	    check_bp_locations_match_list \
		"$cmd_prefix $linespec" {"not_overloaded_fn()"}
	}
    }
}

# Test completing symbols of source files.

proc_with_prefix in-source-file-unconstrained {} {
    # First test that unconstrained matching picks up functions from
    # multiple files.
    test_gdb_complete_multiple "b " "file_constrained_test" "_cpls" {
	"file_constrained_test_cpls2_function(int)"
	"file_constrained_test_cpls_function(int)"
    }
    check_setting_bp_fails "b file_constrained_test_cpls"
}

# Test an unambiguous completion that would be ambiguous if it weren't
# for the source file component, due to
# "file_constrained_test_cpls_function" in cpls.cc.  Test with
# different components quoted, and with whitespace before the function
# name.

proc_with_prefix in-source-file-unambiguous {} {
    foreach sqc $completion::maybe_quoted_list {
	foreach fqc $completion::maybe_quoted_list {
	    # Linespec.
	    foreach sep {":" ": "} {
		set linespec \
		    "${sqc}cpls2.cc${sqc}${sep}${fqc}file_constrained_test_cpls2_function(int)${fqc}"
		set complete_line "b $linespec"
		set start [index_after "constrained_test" $complete_line]
		set input_line [string range $complete_line 0 $start]
		test_gdb_complete_unique $input_line ${complete_line}
		check_bp_locations_match_list "b $linespec" {
		    "file_constrained_test_cpls2_function(int)"
		}
	    }

	    # Explicit location.
	    set source_opt "-source ${sqc}cpls2.cc${sqc}"
	    set function_opt "-function ${fqc}file_constrained_test_cpls2_function(int)${fqc}"
	    set complete_line "b $source_opt $function_opt"
	    set start [index_after "cpls2_functio" $complete_line]
	    set input_line [string range $complete_line 0 $start]
	    test_gdb_complete_unique $input_line ${complete_line}
	    check_bp_locations_match_list "$complete_line" {
		    "file_constrained_test_cpls2_function(int)"
	    }
	}
    }
}

# Test an ambiguous completion constrained by a source file.  Test
# with different components quoted, and with whitespace before the
# function name.

proc_with_prefix in-source-file-ambiguous {} {
    foreach sqc $completion::maybe_quoted_list {
	foreach fqc $completion::maybe_quoted_list {
	    # Linespec.
	    foreach sep {":" ": "} {
		set cmd_prefix "b ${sqc}cpls2.cc${sqc}${sep}"
		test_gdb_complete_multiple "${cmd_prefix}" ${fqc} "" {
		    "another_file_constrained_test_cpls2_function(int)"
		    "file_constrained_test_cpls2_function(int)"
		} ${fqc} ${fqc}
	    }

	    # Explicit location.
	    test_gdb_complete_multiple \
		"b -source ${sqc}cpls2.cc${sqc} -function " ${fqc} "" {
		"another_file_constrained_test_cpls2_function(int)"
		"file_constrained_test_cpls2_function(int)"
	    } ${fqc} ${fqc}
	}
    }
}

# Check that completing a file name in a linespec auto-appends a colon
# instead of a whitespace character.

proc_with_prefix source-complete-appends-colon {} {
    # Test with quotes to make sure the end quote char is put at the
    # right place.
    foreach qc $completion::maybe_quoted_list {
	test_gdb_complete_unique \
	    "b ${qc}cpls2." \
	    "b ${qc}cpls2.cc${qc}" ":"
	test_gdb_complete_unique \
	    "b ${qc}cpls2.c" \
	    "b ${qc}cpls2.cc${qc}" ":"
	test_gdb_complete_unique \
	    "b ${qc}cpls2.cc" \
	    "b ${qc}cpls2.cc${qc}" ":"

	# Same, but with a filename with an hyphen (which is normally
	# a language word break char).
	test_gdb_complete_unique \
	    "b ${qc}cpls-" \
	    "b ${qc}cpls-hyphen.cc${qc}" ":"
	test_gdb_complete_unique \
	    "b ${qc}cpls-hyphen" \
	    "b ${qc}cpls-hyphen.cc${qc}" ":"
    }

    # Test the same, but with the name of a nonexisting file.

    # Cursor at the end of the string.
    test_gdb_complete_none "b nonexistingfilename.cc"
    # Cursor past the end of the string.
    test_gdb_complete_multiple "b nonexistingfilename.cc " "" "" \
	$completion::keyword_list
    foreach qc $completion::all_quotes_list {
	# Unterminated quote.
	test_gdb_complete_none "b ${qc}nonexistingfilename.cc"
	test_gdb_complete_none "b ${qc}nonexistingfilename.cc "
	# Terminated quote, cursor at the quote.
	test_gdb_complete_unique \
	    "b ${qc}nonexistingfilename.cc${qc}" \
	    "b ${qc}nonexistingfilename.cc${qc}"
	# Terminated quote, cursor past the quote.
	test_gdb_complete_multiple \
	    "b ${qc}nonexistingfilename.cc${qc} " "" "" \
	    $completion::keyword_list
    }
}

####################################################################

# Test that a colon at the end of the linespec is understood as an
# incomplete scope operator (incomplete-scope-colon), instead of a
# source/function separator.

proc_with_prefix incomplete-scope-colon {} {

    # Helper for the loop below to simplify it.  Tests completion of
    # the range defined by the RANGE_SS found in the constructed line.
    #
    # E.g., with:
    #
    #   source="source.cc"
    #   fqc="'"
    #   prototype="ns::function()"
    #   range_ss="s::f"
    #
    # we'd try completing with the cursor set in each of the
    # underlined range's positions of:
    #
    #   b source.cc:'ns::function()'"
    #                 ^^^^
    #
    # Also test that setting a breakpoint at the constructed line
    # finds the same breakpoint location as completion does.
    #
    proc incomplete_scope_colon_helper {prototype range_ss {skip_check_bp 0}} {
	foreach source {"" "cpls.cc"} {
	    # Test with and without source quoting.
	    foreach sqc $completion::maybe_quoted_list {
		if {$source == "" && $sqc != ""} {
		    # Invalid combination.
		    continue
		}

		# Test with and without function quoting.
		foreach fqc $completion::maybe_quoted_list {
		    if {$source == ""} {
			set linespec_source ""
			set explicit_source ""
		    } else {
			set linespec_source "${sqc}${source}${sqc}:"
			set explicit_source "-source ${sqc}${source}${sqc}"
		    }

		    # Even though this use case is trickier with
		    # linespecs due to the ":" as separator, test both
		    # linespecs and explicit locations for
		    # completeness.
		    foreach location [list \
					  "${linespec_source}${fqc}$prototype${fqc}" \
					  "${explicit_source} -function ${fqc}$prototype${fqc}"] {
			set complete_line "b $location"
			set start [string first $range_ss $complete_line]
			set end [expr ($start + [string length $range_ss])]
			test_complete_prefix_range $complete_line $start $end
			if {!$skip_check_bp} {
			    check_bp_locations_match_list "b $location" [list "$prototype"]
			}
		    }
		}
	    }
	}
    }

    incomplete_scope_colon_helper \
	"struct_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
	"t::i"

    incomplete_scope_colon_helper \
	"ns_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
	"t::i"

    # Test completing around both "::"s.
    foreach range_ss {"t::s" "t::i"} skip_check_bp {0 1} {
	incomplete_scope_colon_helper \
	    "ns2_incomplete_scope_colon_test::struct_in_ns2_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
	    $range_ss $skip_check_bp
    }
}

# Basic test for completing "operator<".  More extensive C++ operator
# tests in cpls-op.exp.

proc_with_prefix operator< {} {
    # Complete all prefixes between "oper" and the whole prototype.
    set function "operator<(foo_enum, foo_enum)"
    foreach cmd_prefix {"b" "b -function"} {
	set line "$cmd_prefix $function"
	set start [index_after "oper" $line]
	test_complete_prefix_range $line $start
    }

    # There's a label in the function; try completing it.  (Exhaustive
    # label completion tests further below.)
    foreach location [list \
		     "$function:label1" \
		     "-function $function -label label1"] {

	set cmd "b $location"
	set input_line [string range $cmd 0 [expr [string length $cmd] - 3]]

	test_gdb_complete_unique $input_line $cmd
	test_gdb_complete_unique $cmd $cmd
	check_bp_locations_match_list $cmd [list "$location"]
    }
}

# Test completion of function labels.

proc_with_prefix function-labels {} {
    # Test with and without a source file component.
    foreach_location_functions \
	{ "" "cpls.cc" } \
	{ "function_with_labels(int)" } \
	{
	    # Linespec version.  Test various spacing around the label
	    # colon separator.
	    foreach label_sep {":" " :" ": " " : "} {
		set linespec "${location}${label_sep}"
		test_gdb_complete_multiple "b $linespec" "l" "abel" {
		    "label1"
		    "label2"
		}
		check_setting_bp_fails "b ${linespec}label"

		set tsep [string trim ${source_sep}]
		check_bp_locations_match_list \
		    "b ${linespec}label1" [list "${source}${tsep}${function}:label1"]
		check_bp_locations_match_list \
		    "b ${linespec}label2" [list "${source}${tsep}${function}:label2"]
	    }
	} \
	{
	    # Explicit locations version.
	    append location " -label"
	    test_gdb_complete_multiple "b $location " "l" "abel" {
		"label1"
		"label2"
	    }
	    check_setting_bp_fails "b $location label"

	    if {$source != ""} {
		set bp_loc_src "-source ${source} "
	    } else {
		set bp_loc_src ""
	    }
	    check_bp_locations_match_list \
		"b ${location} label1" [list "${bp_loc_src}-function $function -label label1"]
	    check_bp_locations_match_list \
		"b ${location} label2" [list "${bp_loc_src}-function $function -label label2"]
	}
}

# Test that completion after a function name offers keyword
# (if/task/thread) matches in linespec mode, and also the explicit
# location options in explicit locations mode.

proc_with_prefix keywords-after-function {} {
    set explicit_list \
	[concat $completion::explicit_opts_list $completion::keyword_list]

    # Test without a source file, with a known source file, and with
    # and unknown source file.
    # Test a known and an unknown function.
    foreach_location_functions \
	{ "" "cpls.cc" "unknown_file.cc" } \
	{ "function_with_labels(int)" "unknown_function(int)" } \
	{
	    # Linespec version.
	    test_gdb_complete_multiple "b ${location} " "" "" \
		$completion::keyword_list
	} \
	{
	    # Explicit locations version.
	    test_gdb_complete_multiple "b ${location} " "" "" \
		$explicit_list
	}
}

# Same, but after a label.

proc_with_prefix keywords-after-label {} {
    set explicit_list \
	[concat $completion::explicit_opts_list $completion::keyword_list]

    foreach_location_labels \
	{ "" "cpls.cc" } \
	{ "function_with_labels(int)" "unknown_function(int)" } \
	{ "label1" "non_existing_label" } \
	{
	    # Linespec version.
	    test_gdb_complete_multiple "b ${location} " "" "" \
		$completion::keyword_list
	} \
	{
	    # Explicit locations version.
	    test_gdb_complete_multiple "b ${location} " "" "" \
		$explicit_list
	}
}

# Similar, but after an unknown file, and in linespec mode only.

proc_with_prefix keywords-after-unknown-file {} {
    # Test with and without quoting.
    foreach qc $completion::maybe_quoted_list {
	set line "b ${qc}unknown_file.cc${qc}: "
	test_gdb_complete_multiple $line "" "" $completion::keyword_list
    }
}

# Test that linespec / function completion does not match data
# symbols, only functions/methods.

proc_with_prefix no-data-symbols {} {
    foreach cmd_prefix {"b" "b -function"} {
	test_gdb_complete_unique "$cmd_prefix code_" "$cmd_prefix code_function()"
    }
}


# After "if", we expect an expression, which has a different completer
# that matches data symbols as well.  Check that that works.

proc_with_prefix if-expression {} {
    foreach cmd_prefix {"b" "b -function"} {
	test_gdb_complete_multiple "$cmd_prefix function() if " "code_" "" {
	    "code_data"
	    "code_function()"
	}

	test_gdb_complete_unique \
	    "$cmd_prefix function() if code_data + another_da" \
	    "$cmd_prefix function() if code_data + another_data"

	test_gdb_complete_unique \
	    "$cmd_prefix non_existing_function() if code_data + another_da" \
	    "$cmd_prefix non_existing_function() if code_data + another_data"

	# FIXME: For now, thread and task also use the expression
	# completer.
	test_gdb_complete_unique \
	    "$cmd_prefix function() thread code_data + another_da" \
	    "$cmd_prefix function() thread code_data + another_data"
	test_gdb_complete_unique \
	    "$cmd_prefix function() task code_data + another_da" \
	    "$cmd_prefix function() task code_data + another_data"
    }
}

# The testcase driver.  Calls all test procedures.

proc test_driver {} {
    all-param-prefixes
    overload
    append-end-quote-char-when-unambiguous
    in-source-file-unconstrained
    in-source-file-unambiguous
    in-source-file-ambiguous
    source-complete-appends-colon
    incomplete-scope-colon
    operator<
    function-labels
    keywords-after-function
    keywords-after-label
    keywords-after-unknown-file
    no-data-symbols
    if-expression
}

test_driver