1# Copyright 2017-2023 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
18load_lib completion-support.exp
19load_lib data-structures.exp
20
21standard_testfile cpls.cc cpls2.cc cpls-hyphen.cc
22
23if {[prepare_for_testing "failed to prepare" $testfile \
24	 [list $srcfile $srcfile2 $srcfile3] {debug}]} {
25    return -1
26}
27
28# Tests below are about tab-completion, which doesn't work if readline
29# library isn't used.  Check it first.
30
31if { ![readline_is_used] } {
32    untested "no tab completion support without readline"
33    return -1
34}
35
36#
37# Some convenience procedures for testing template parameter list
38# completion.
39#
40
41# For the variable named ARGLISTVAR, which should be the name of an
42# argument list in the calling frame, "consume" the top-most token.
43# [See comments for makefoo for description of arglist format.]
44
45proc consume {arglistvar} {
46  upvar $arglistvar arglist
47
48  # ARGLIST is a string -- simply strip off the first character.
49  set arglist [string range $arglist 1 end]
50}
51
52# Create a function template named NAME, using the given stack ID to grab
53# NUM template parameters.  The result is pushed back onto the
54# stack.  NUM may be "all," in which case we use the entire stack
55# to create the function template, including function arguments.
56# The resulting template function's arguments are taken from the test
57# source code for the function "foo" and is not generalized.
58
59proc maket {sid name {num 1}} {
60
61  # Set up a temporary stack of parameters.  This will reverse
62  # the order in SID so that when they are popped again below,
63  # we get them in the correct order.  This also takes into account
64  # how many levels of the result stack we want to consider.
65
66  set paramstack [::Stack::new]
67  if {[string equal $num "all"]} {
68    while {![stack empty $sid]} {
69      stack push $paramstack [stack pop $sid]
70    }
71  } else {
72    for {set i 0} {$i < $num} {incr i} {
73      stack push $paramstack [stack pop $sid]
74    }
75  }
76
77  # Construct the function template and push it back to the
78  # top of the stack given by SID.
79  set result ""
80  set first true
81  while {![stack empty $paramstack]} {
82    set top [stack pop $paramstack]
83    if {$first} {
84      set first false
85    } else {
86      append result ", "
87    }
88    append result $top
89  }
90
91  # Save argument list.
92  set args $result
93
94  # GDB outputs "> >" instead of ">>".
95  if {[string index $top end] == ">"} {
96      append result " "
97  }
98  set result "$name<$result>"
99  if {[string equal $num "all"]} {
100    append result "($args)"
101  }
102  stack push $sid $result
103  stack delete $paramstack
104}
105
106# Given the stack SID and the name of a variable of the desired template
107# parameters, construct the actual template parameter and push it to the
108# top of the stack.
109
110proc makearg {sid arglistvar} {
111    upvar $arglistvar arglist
112
113  set c [string index $arglist 0]
114  consume arglist
115  switch $c {
116    A -
117    B {
118      makearg $sid arglist
119      makearg $sid arglist
120      maket $sid $c 2
121    }
122
123    a -
124    b -
125    c -
126    d {
127      makearg $sid arglist
128      maket $sid $c
129    }
130
131    i {
132      stack push $sid "int"
133    }
134
135    n {
136      # These are not templates.
137      set c [string index $arglist 0]
138      stack push $sid "n::n$c"
139      consume arglist
140    }
141
142    N {
143      set c [string index $arglist 0]
144      makearg $sid arglist
145      set top [stack pop $sid]
146      stack push $sid "n::N$top"
147    }
148
149    default { error "unhandled arglist identifier: '$c'" }
150  }
151}
152
153# Given ARGLIST, construct a class template for the type and return
154# it as a string.
155
156proc maketype {arglist} {
157    set s [Stack::new]
158    makearg $s arglist
159    set result [stack pop $s]
160    stack delete $s
161    return $result
162}
163
164# Returns a function template declaration for the function "foo" in the
165# corresponding test source code.  ARGLIST specifies the exact instantiation
166# that is desired.
167#
168# Generically, this procedure returns a string of the form,
169# "foo<parameter-list> (arg-list)", where ARGLIST describes the parameter(s).
170#
171# Valid specifiers for ARGLIST (must be kept in sync with source code):
172#
173# i: Creates an "int" type.
174# a, b, c, d: Creates the struct template of the same name, taking a single
175#    template parameter.
176# A, B: Creates the struct template of the same name, taking two template
177#    parameters.
178# na, nb: Creates the non-template structs n::na and n::nb, respectively.
179# NA, NB: Creates the struct templates n::NA and n::NB, respectively, which
180#    take two template parameters.
181#
182# Examples:
183# makefoo i
184# --> foo<int> (int)
185# makefoo ii
186# --> foo<int, int> (int, int)
187# makefoo Aiabi
188# --> foo<A<int, a<b<int> > > > (A<int, a<b<int> > >)
189# makefoo NANAiaiNBbiabi
190# --> foo<n::NA<n::NA<int, a<int> >, n::NB<b<int>, a<b<int> > > > >
191#          (n::NA<n::NA<int, a<int> >, n::NB<b<int>, a<b<int> > > >)
192
193proc makefoo {arglist} {
194    set s [::Stack::new]
195    while {[string length $arglist] > 0} {
196	makearg $s arglist
197    }
198
199  maket $s "foo" all
200  set result [stack pop $s]
201  stack delete $s
202  return $result
203}
204
205# Test wrapper for a single "makefoo" unit test.
206
207proc test_makefoo_1 {arglist expected} {
208  set exp "foo<$expected"
209  if {[string index $exp end] == ">"} {
210    append exp " "
211  }
212  append exp ">"
213  append exp "($expected)"
214
215  set calc [makefoo $arglist]
216  send_log "makefoo $arglist = $calc\n"
217  send_log "expecting: $exp\n"
218  if {[string equal $exp $calc]} {
219    pass "makefoo unit test: $arglist"
220  } else {
221      fail "makefoo unit test: $arglist"
222  }
223}
224
225# Test whether the procedure "makefoo" is functioning as expected.
226
227proc test_makefoo {} {
228  test_makefoo_1 "i" "int"
229  test_makefoo_1 "ai" "a<int>"
230  test_makefoo_1 "aai" "a<a<int> >"
231  test_makefoo_1 "ii" "int, int"
232  test_makefoo_1 "aaibi" "a<a<int> >, b<int>"
233    test_makefoo_1 \
234      "ababiibababai" "a<b<a<b<int> > > >, int, b<a<b<a<b<a<int> > > > > >"
235  test_makefoo_1 "Aii" "A<int, int>"
236  test_makefoo_1 "ABaibibi" "A<B<a<int>, b<int> >, b<int> >"
237  test_makefoo_1 "na" "n::na"
238  test_makefoo_1 "nana" "n::na, n::na"
239  test_makefoo_1 "NAii" "n::NA<int, int>"
240  test_makefoo_1 "NANAiiNAii" "n::NA<n::NA<int, int>, n::NA<int, int> >"
241}
242
243#
244# Tests start here.
245#
246
247# Disable the completion limit for the whole testcase.
248gdb_test_no_output "set max-completions unlimited"
249
250# Start of tests.
251
252# Test completion of all parameter prefixes, crossing "(" and ")",
253# with and without whitespace.
254
255proc_with_prefix all-param-prefixes {} {
256
257    # Test both linespecs and explicit locations.
258    foreach cmd_prefix {"b" "b -function"} {
259	set line "$cmd_prefix param_prefixes_test_long(long)"
260	set start [index_after "test_long" $line]
261	test_complete_prefix_range $line $start
262
263	# Same, but with extra spaces.  Note that the original spaces in
264	# the input line are preserved after completion.
265	test_gdb_complete_unique \
266	    "$cmd_prefix param_prefixes_test_long(long "   \
267	    "$cmd_prefix param_prefixes_test_long(long )"
268	test_gdb_complete_unique \
269	    "$cmd_prefix param_prefixes_test_long( long "  \
270	    "$cmd_prefix param_prefixes_test_long( long )"
271	test_gdb_complete_unique \
272	    "$cmd_prefix param_prefixes_test_long ( long " \
273	    "$cmd_prefix param_prefixes_test_long ( long )"
274
275	# Complete all parameter prefixes between "(i" and "(int*, int&)".
276	# Note that this exercises completing when the point is at the
277	# space in "param_prefixes_test_intp_intr(int*, ".
278	set line "$cmd_prefix param_prefixes_test_intp_intr(int*, int&)"
279	set start [index_after "intp_intr" $line]
280	test_complete_prefix_range $line $start
281
282	# Similar, but with extra spaces.
283	test_gdb_complete_unique \
284	    "$cmd_prefix param_prefixes_test_intp_intr (  int* " \
285	    "$cmd_prefix param_prefixes_test_intp_intr (  int* , int&)"
286
287	test_gdb_complete_unique \
288	    "$cmd_prefix param_prefixes_test_intp_intr (  int *" \
289	    "$cmd_prefix param_prefixes_test_intp_intr (  int *, int&)"
290
291	test_gdb_complete_unique \
292	    "$cmd_prefix param_prefixes_test_intp_intr (  int *, int " \
293	    "$cmd_prefix param_prefixes_test_intp_intr (  int *, int &)"
294
295	test_gdb_complete_unique \
296	    "$cmd_prefix param_prefixes_test_intp_intr (  int *,  int & " \
297	    "$cmd_prefix param_prefixes_test_intp_intr (  int *,  int & )"
298    }
299}
300
301# Test completion of an overloaded function.
302
303proc_with_prefix overload {} {
304    set completion_list {
305	"overload_ambiguous_test(int, int)"
306	"overload_ambiguous_test(int, long)"
307	"overload_ambiguous_test(long)"
308    }
309
310    foreach cmd_prefix {"b" "b -function"} {
311	test_gdb_complete_multiple \
312	    "$cmd_prefix " "overload_ambiguous_" "test(" \
313	    $completion_list
314	check_bp_locations_match_list \
315	    "$cmd_prefix overload_ambiguous_test" \
316	    $completion_list
317
318	# Test disambiguating by typing enough to pick the "int" as
319	# first parameter type.  This then tests handling ambiguity in
320	# the second parameter, which checks that tab completion when
321	# the point is at the whitespace behaves naturally, by showing
322	# the remaining matching overloads to the user.
323	test_gdb_complete_multiple \
324	    "$cmd_prefix " "overload_ambiguous_test(i" "nt, " {
325	    "overload_ambiguous_test(int, int)"
326	    "overload_ambiguous_test(int, long)"
327	}
328
329	# Add a few more characters to make the completion
330	# unambiguous.
331	test_gdb_complete_unique \
332	    "$cmd_prefix overload_ambiguous_test(int, i" \
333	    "$cmd_prefix overload_ambiguous_test(int, int)"
334	check_bp_locations_match_list \
335	    "$cmd_prefix overload_ambiguous_test(int, int)" {
336		"overload_ambiguous_test(int, int)"
337	    }
338    }
339}
340
341# Test completion of a function that is defined in different scopes
342# with different parameters.
343
344proc_with_prefix overload-2 {} {
345    with_test_prefix "all" {
346	set completion_list {
347	    "(anonymous namespace)::overload2_function(overload2_arg3)"
348	    "(anonymous namespace)::struct_overload2_test::overload2_function(overload2_arg4)"
349	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::overload2_function(overload2_arg9)"
350	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::struct_overload2_test::overload2_function(overload2_arga)"
351	    "ns_overload2_test::(anonymous namespace)::overload2_function(overload2_arg7)"
352	    "ns_overload2_test::(anonymous namespace)::struct_overload2_test::overload2_function(overload2_arg8)"
353	    "ns_overload2_test::overload2_function(overload2_arg5)"
354	    "ns_overload2_test::struct_overload2_test::overload2_function(overload2_arg6)"
355	    "overload2_function(overload2_arg1)"
356	    "struct_overload2_test::overload2_function(overload2_arg2)"
357	}
358	foreach cmd_prefix {"b" "b -function"} {
359	    test_gdb_complete_multiple \
360		"$cmd_prefix " "overload2_func" "tion(overload2_arg" $completion_list
361	    check_bp_locations_match_list \
362		"$cmd_prefix overload2_function" $completion_list
363	}
364    }
365
366    # Same, but restrict to functions/methods in some scope.
367    with_test_prefix "restrict scope" {
368	set completion_list {
369	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::overload2_function(overload2_arg9)"
370	    "ns_overload2_test::overload2_function(overload2_arg5)"
371	}
372	foreach cmd_prefix {"b" "b -function"} {
373	    test_gdb_complete_multiple \
374		"$cmd_prefix " "ns_overload2_test::overload2_func" "tion(overload2_arg" $completion_list
375	    check_bp_locations_match_list \
376		"$cmd_prefix ns_overload2_test::overload2_function" $completion_list
377	}
378    }
379
380    # Restrict to anonymous namespace scopes.
381    with_test_prefix "restrict scope 2" {
382	set completion_list {
383	    "(anonymous namespace)::overload2_function(overload2_arg3)"
384	    "ns_overload2_test::(anonymous namespace)::overload2_function(overload2_arg7)"
385	}
386	foreach cmd_prefix {"b" "b -function"} {
387	    test_gdb_complete_multiple \
388		"$cmd_prefix " "(anonymous namespace)::overload2_func" "tion(overload2_arg" $completion_list
389	    check_bp_locations_match_list \
390		"$cmd_prefix (anonymous namespace)::overload2_function" $completion_list
391	}
392    }
393
394    # Add enough scopes, and we get a unique completion.
395    with_test_prefix "unique completion" {
396	foreach cmd_prefix {"b" "b -function"} {
397	    test_gdb_complete_unique \
398		"$cmd_prefix ns_overload2_test::(anonymous namespace)::overload2_func" \
399		"$cmd_prefix ns_overload2_test::(anonymous namespace)::overload2_function(overload2_arg7)"
400	    check_setting_bp_fails "$cmd_prefix ns_overload2_test::(anonymous namespace)::overload2_func"
401	    check_bp_locations_match_list \
402		"$cmd_prefix ns_overload2_test::(anonymous namespace)::overload2_function" \
403		{"ns_overload2_test::(anonymous namespace)::overload2_function(overload2_arg7)"}
404	}
405    }
406}
407
408# Test linespecs / locations using fully-qualified names.
409
410proc_with_prefix fqn {} {
411
412    # "-qualified" works with both explicit locations and linespecs.
413    # Also test that combining a source file with a function name
414    # still results in a full match, with both linespecs and explicit
415    # locations.
416    foreach cmd_prefix {
417	"b -qualified "
418	"b -qualified -function "
419	"b -qualified cpls.cc:"
420	"b -qualified -source cpls.cc -function "
421	"b -source cpls.cc -qualified -function "
422    } {
423	test_gdb_complete_unique \
424	    "${cmd_prefix}overload2_func" \
425	    "${cmd_prefix}overload2_function(overload2_arg1)"
426
427	# Drill down until we find a unique completion.
428	test_gdb_complete_multiple "${cmd_prefix}" "ns_overload2_test::" "" {
429	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::overload2_function(overload2_arg9)"
430	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::struct_overload2_test::overload2_function(overload2_arga)"
431	    "ns_overload2_test::(anonymous namespace)::overload2_function(overload2_arg7)"
432	    "ns_overload2_test::(anonymous namespace)::struct_overload2_test::overload2_function(overload2_arg8)"
433	    "ns_overload2_test::overload2_function(overload2_arg5)"
434	    "ns_overload2_test::struct_overload2_test::overload2_function(overload2_arg6)"
435	}
436
437	test_gdb_complete_multiple "${cmd_prefix}" "ns_overload2_test::(anonymous namespace)::" "" {
438	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::overload2_function(overload2_arg9)"
439	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::struct_overload2_test::overload2_function(overload2_arga)"
440	    "ns_overload2_test::(anonymous namespace)::overload2_function(overload2_arg7)"
441	    "ns_overload2_test::(anonymous namespace)::struct_overload2_test::overload2_function(overload2_arg8)"
442	}
443
444	test_gdb_complete_multiple "${cmd_prefix}" "ns_overload2_test::(anonymous namespace)::ns_overload2_test::" "" {
445	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::overload2_function(overload2_arg9)"
446	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::struct_overload2_test::overload2_function(overload2_arga)"
447	}
448
449	test_gdb_complete_unique \
450	    "${cmd_prefix}ns_overload2_test::(anonymous namespace)::ns_overload2_test::overload2_func" \
451	    "${cmd_prefix}ns_overload2_test::(anonymous namespace)::ns_overload2_test::overload2_function(overload2_arg9)"
452
453    }
454}
455
456# Check that a fully-qualified lookup name doesn't match symbols in
457# nested scopes.
458
459proc_with_prefix fqn-2 {} {
460    set linespec "struct_overload2_test::overload2_function(overload2_arg6)"
461    set cmd_prefix "b -qualified"
462    check_setting_bp_fails "$cmd_prefix $linespec"
463    test_gdb_complete_none "$cmd_prefix $linespec"
464
465    # Check that using the same name, but not fully-qualifying it,
466    # would find something, just to make sure the test above is
467    # testing what we intend to test.
468    set cmd_prefix "b -function"
469    test_gdb_complete_unique "$cmd_prefix $linespec" "$cmd_prefix $linespec"
470    check_bp_locations_match_list \
471	"$cmd_prefix $linespec" \
472	{"ns_overload2_test::struct_overload2_test::overload2_function(overload2_arg6)"}
473}
474
475# Test completion of functions in different scopes that have the same
476# name and parameters.  Restricting the scopes should find fewer and
477# fewer matches.
478
479proc_with_prefix overload-3 {} {
480    with_test_prefix "all overloads" {
481	set completion_list {
482	    "(anonymous namespace)::overload3_function(int)"
483	    "(anonymous namespace)::overload3_function(long)"
484	    "(anonymous namespace)::struct_overload3_test::overload3_function(int)"
485	    "(anonymous namespace)::struct_overload3_test::overload3_function(long)"
486	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::overload3_function(int)"
487	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::overload3_function(long)"
488	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::struct_overload3_test::overload3_function(int)"
489	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::struct_overload3_test::overload3_function(long)"
490	    "ns_overload3_test::(anonymous namespace)::overload3_function(int)"
491	    "ns_overload3_test::(anonymous namespace)::overload3_function(long)"
492	    "ns_overload3_test::(anonymous namespace)::struct_overload3_test::overload3_function(int)"
493	    "ns_overload3_test::(anonymous namespace)::struct_overload3_test::overload3_function(long)"
494	    "ns_overload3_test::overload3_function(int)"
495	    "ns_overload3_test::overload3_function(long)"
496	    "ns_overload3_test::struct_overload3_test::overload3_function(int)"
497	    "ns_overload3_test::struct_overload3_test::overload3_function(long)"
498	    "overload3_function(int)"
499	    "overload3_function(long)"
500	    "struct_overload3_test::overload3_function(int)"
501	    "struct_overload3_test::overload3_function(long)"
502	}
503	foreach cmd_prefix {"b" "b -function"} {
504	    test_gdb_complete_multiple "$cmd_prefix " "overload3_func" "tion(" $completion_list
505	    check_bp_locations_match_list "$cmd_prefix overload3_function" $completion_list
506	}
507    }
508
509    with_test_prefix "restrict overload" {
510	foreach cmd_prefix {"b" "b -function"} {
511	    test_gdb_complete_unique \
512		"$cmd_prefix overload3_function(int)" \
513		"$cmd_prefix overload3_function(int)"
514	    check_bp_locations_match_list "$cmd_prefix overload3_function(int)" {
515		"(anonymous namespace)::overload3_function(int)"
516		"(anonymous namespace)::struct_overload3_test::overload3_function(int)"
517		"ns_overload3_test::(anonymous namespace)::ns_overload3_test::overload3_function(int)"
518		"ns_overload3_test::(anonymous namespace)::ns_overload3_test::struct_overload3_test::overload3_function(int)"
519		"ns_overload3_test::(anonymous namespace)::overload3_function(int)"
520		"ns_overload3_test::(anonymous namespace)::struct_overload3_test::overload3_function(int)"
521		"ns_overload3_test::overload3_function(int)"
522		"ns_overload3_test::struct_overload3_test::overload3_function(int)"
523		"overload3_function(int)"
524		"struct_overload3_test::overload3_function(int)"
525	    }
526	}
527    }
528
529    with_test_prefix "restrict scope" {
530	set completion_list {
531	    "(anonymous namespace)::struct_overload3_test::overload3_function(int)"
532	    "(anonymous namespace)::struct_overload3_test::overload3_function(long)"
533	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::struct_overload3_test::overload3_function(int)"
534	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::struct_overload3_test::overload3_function(long)"
535	    "ns_overload3_test::(anonymous namespace)::struct_overload3_test::overload3_function(int)"
536	    "ns_overload3_test::(anonymous namespace)::struct_overload3_test::overload3_function(long)"
537	    "ns_overload3_test::struct_overload3_test::overload3_function(int)"
538	    "ns_overload3_test::struct_overload3_test::overload3_function(long)"
539	    "struct_overload3_test::overload3_function(int)"
540	    "struct_overload3_test::overload3_function(long)"
541	}
542	foreach cmd_prefix {"b" "b -function"} {
543	    test_gdb_complete_multiple \
544		"$cmd_prefix " "struct_overload3_test::overload3_func" "tion(" \
545		$completion_list
546	    check_bp_locations_match_list \
547		"$cmd_prefix struct_overload3_test::overload3_function" \
548		$completion_list
549	}
550    }
551}
552
553# Test completing an overloaded template method.
554
555proc_with_prefix template-overload {} {
556    set completion_list {
557	"template_struct<int>::template_overload_fn(int)"
558	"template_struct<long>::template_overload_fn(long)"
559    }
560    foreach cmd_prefix {"b" "b -function"} {
561	test_gdb_complete_multiple "$cmd_prefix " "template_overload_fn" "(" $completion_list
562	check_bp_locations_match_list "$cmd_prefix template_overload_fn" $completion_list
563	check_bp_locations_match_list \
564	    "$cmd_prefix template_struct<int>::template_overload_fn" \
565	    "template_struct<int>::template_overload_fn(int)"
566    }
567}
568
569# Test completing template methods with non-void return type.
570
571proc_with_prefix template-ret-type {} {
572    set method_name "template2_fn<int, int>"
573    set param_list "(template2_ret_type<int>, int, int)"
574    set struct_type "template2_struct<template2_ret_type<int> >"
575    set ret_type "template2_ret_type<int>"
576
577    # Templates are listed both with and without return type, making
578    # "template2_<tab>" ambiguous.
579    foreach cmd_prefix {"b" "b -function"} {
580	set completion_list \
581	    [list \
582		 "${ret_type} ${struct_type}::${method_name}${param_list}" \
583		 "${struct_type}::${method_name}${param_list}"]
584	test_gdb_complete_multiple "$cmd_prefix " "template2_" "" $completion_list
585
586	# Add one character more after "2_", and the linespec becomes
587	# unambiguous.  Test completing the whole prefix range after that,
588	# thus testing completing either with or without return type.
589	foreach {s t} [list \
590			   "template2_r" \
591			   "${ret_type} ${struct_type}::${method_name}${param_list}" \
592			   "template2_s" \
593			   "${struct_type}::${method_name}${param_list}"] {
594	    set linespec $t
595	    set complete_line "$cmd_prefix $linespec"
596	    set start [index_after $s $complete_line]
597	    test_complete_prefix_range $complete_line $start
598	}
599
600	# Setting a breakpoint with or without template params and without
601	# the method params works, just like with non-template functions.
602	#  It also works with or without return type.
603	foreach linespec [list \
604			      "template2_fn" \
605			      "${method_name}" \
606			      "${method_name}${param_list}" \
607			      "${struct_type}::${method_name}" \
608			      "${struct_type}::${method_name}${param_list}" \
609			      "${ret_type} ${struct_type}::${method_name}" \
610			      "${ret_type} ${struct_type}::${method_name}${param_list}"] {
611	    check_bp_locations_match_list \
612		"$cmd_prefix $linespec" \
613		[list "${struct_type}::${method_name}${param_list}"]
614	}
615    }
616}
617
618# Test completion of function template foo.
619
620proc_with_prefix template-function-foo {} {
621
622    foreach cmd_prefix {"b" "b -function"} {
623	# "foo" is ambiguous, this will set many different breakpoints.
624	set completion_list \
625	    [list \
626		 [makefoo Aabiaai] \
627		 [makefoo Aabiabi] \
628		 [makefoo Aabicdi] \
629		 [makefoo AabicdiAabiaai] \
630		 [makefoo AabicdiAabiabi] \
631		 [makefoo AabicdiBabicdi] \
632		 [makefoo Babicdi] \
633		 [makefoo aai] \
634		 [makefoo aaiabi] \
635		 [makefoo aaicci] \
636		 [makefoo aaicdi] \
637		 [makefoo abi] \
638		 [makefoo anabnb] \
639		 [makefoo cci] \
640		 [makefoo cdi] \
641		 [makefoo NAnanbNBnanb] \
642		 [makefoo nanb]]
643	test_gdb_complete_multiple "$cmd_prefix " "foo" "<" $completion_list
644	check_bp_locations_match_list "$cmd_prefix foo" $completion_list
645
646	# "foo<" should give the same result, but it should not set any
647	# breakpoints.
648	test_gdb_complete_multiple "$cmd_prefix " "foo<" "" $completion_list
649	check_setting_bp_fails "$cmd_prefix foo<"
650
651	# "foo<A" should only give completions in COMPLETION_LIST that
652	# start with "A" but should set no breakpoints.
653	set completion_list \
654	    [list \
655		 [makefoo Aabiaai] \
656		 [makefoo Aabiabi] \
657		 [makefoo Aabicdi] \
658		 [makefoo AabicdiAabiaai] \
659		 [makefoo AabicdiAabiabi] \
660		 [makefoo AabicdiBabicdi]]
661	test_gdb_complete_multiple "$cmd_prefix " "foo<A" "<a<b<int> >, " \
662	    $completion_list
663	check_setting_bp_fails "$cmd_prefix foo<A"
664
665	# "foo<A>" should give any function with one parameter of any type
666	# of A.  While the parameter list in the template should be ignored,
667	# the function's argument list should not be ignored.
668	set completion_list \
669	    [list \
670		 [makefoo Aabiaai] \
671		 [makefoo Aabiabi] \
672		 [makefoo Aabicdi]]
673	test_gdb_complete_multiple "$cmd_prefix " "foo<A>" \
674	    "(A<a<b<int> >, " $completion_list
675	check_bp_locations_match_list "$cmd_prefix foo<A>" $completion_list
676
677	# "foo<A," should complete to any function with more than one
678	# parameter where the first parameter is any type of A.  Insufficient
679	# location to set breakpoints.
680	set completion_list \
681	    [list \
682		 [makefoo AabicdiAabiaai] \
683		 [makefoo AabicdiAabiabi] \
684		 [makefoo AabicdiBabicdi]]
685	test_gdb_complete_multiple "$cmd_prefix " "foo<A," " " \
686	    $completion_list
687	check_setting_bp_fails "$cmd_prefix foo<A,"
688
689	# "foo<A<a<b<int>, a" should give all completions starting with
690	# "Aabia" but it is insufficient to set breakpoints.
691	set completion_list \
692	    [list \
693		 [makefoo Aabiaai] \
694		 [makefoo Aabiabi]]
695	test_gdb_complete_multiple "$cmd_prefix " "foo<A<a<b<int> >, a" \
696	    "<" $completion_list
697	check_setting_bp_fails "$cmd_prefix foo<A<a<b<int> >, a"
698
699	# "foo<A<a<b<int>, a<" should yield the same results as above.
700	test_gdb_complete_multiple "$cmd_prefix " "foo<A<a<b<int> >, a<" \
701	    "" $completion_list
702	check_setting_bp_fails "$cmd_prefix foo<A<a<b<int> >, a<"
703
704	# "foo<A<a<b<int>, a<a" is unique but insufficient to set a
705	# breakpoint.  This has an ignored template parameter list, so
706	# the completion will contain an ignored list ("a<a>")
707	test_gdb_complete_unique "$cmd_prefix foo<A<a<b<int> >, a<a" \
708	    "$cmd_prefix [makefoo Aabiaai]"
709	check_setting_bp_fails "$cmd_prefix foo<A<b<int> >, a<a"
710
711	# "foo<A<a<b<int>, a<b" is also unique.  Same parameter ignoring
712	# happens here, too (except "a<b>").
713	test_gdb_complete_unique "$cmd_prefix foo<A<a<b<int> >, a<b" \
714	    "$cmd_prefix [makefoo Aabiabi]"
715	check_setting_bp_fails "$cmd_prefix foo<A<a<b<int> >, a<b"
716
717	# "foo<B" is unique but insufficient to set a breakpoint.
718	test_gdb_complete_unique "$cmd_prefix foo<B" \
719	    "$cmd_prefix [makefoo Babicdi]"
720	check_setting_bp_fails "$cmd_prefix foo<B"
721
722	# "foo<B>" yields the same unique result and sets a breakpoint.
723	# Since the input skips the parameter list, so does the completion.
724	test_gdb_complete_unique "$cmd_prefix foo<B>" \
725	    "$cmd_prefix foo<B>(B<a<b<int> >, c<d<int> > >)"
726	check_bp_locations_match_list "$cmd_prefix foo<B>" \
727	    [list [makefoo Babicdi]]
728
729	# "foo<B," should return no completions and no breakpoints.
730	test_gdb_complete_none "$cmd_prefix foo<B,"
731	check_setting_bp_fails "$cmd_prefix foo<B,"
732
733	# "foo<n::" should yield only the functions starting with
734	# "n" and "N" and no breakpoints.
735	set completion_list \
736	    [list \
737		 [makefoo NAnanbNBnanb] \
738		 [makefoo nanb]]
739	test_gdb_complete_multiple "$cmd_prefix " "foo<n::" "" \
740	    $completion_list
741	check_setting_bp_fails "$cmd_prefix foo<n::"
742
743	# "foo<A<a, c> >" is unique and sets a breakpoint.
744	# Multiple template parameter lists are skipped, so GDB will ignore
745	# them in the completion.
746	test_gdb_complete_unique "$cmd_prefix foo<A<a, c> >" \
747	    "$cmd_prefix foo<A<a, c> >(A<a<b<int> >, c<d<int> > >)"
748	check_bp_locations_match_list "$cmd_prefix foo<A<a, c> >" \
749	    [list [makefoo Aabicdi]]
750    }
751}
752
753# Helper for template-class-with-method to build completion lists.
754
755proc makem {arglist_list} {
756    set completion_list {}
757    foreach arglist $arglist_list {
758	lappend completion_list "[maketype $arglist]::method()"
759    }
760    return $completion_list
761}
762
763# Returns a list of elements that look like
764#   void TYPE::method()
765# where TYPE is derived from each arglist in ARGLIST_LIST.
766
767proc test_makem_1 {arglist_list expected_list} {
768    set result [makem $arglist_list]
769    send_log "makem $arglist = $result\n"
770    send_log "expecting $expected_list\n"
771
772    # Do list equality via canonical strings.
773    if {[expr {[list {*}$expected_list] eq [list {*}$result]}]} {
774	pass "makem unit test: $arglist"
775    } else {
776	fail "makem unit test: $arglist"
777    }
778}
779
780# Unit tests for makem.
781
782proc test_makem {} {
783    test_makem_1 ai {"a<int>::method()"}
784    test_makem_1 bi {"b<int>::method()"}
785    test_makem_1 {ai bi} {"a<int>::method()" "b<int>::method()"}
786    test_makem_1 {Aaiaai Bbibbi abi cdi} {
787	"A<a<int>, a<a<int> > >::method()"
788	"B<b<int>, b<b<int> > >::method()"
789	"a<b<int> >::method()"
790	"c<d<int> >::method()"
791    }
792}
793
794# Test class template containing a (non-templated) method called "method."
795
796proc_with_prefix template-class-with-method {} {
797
798    foreach {type type_list} \
799	[list \
800	     "" {aai abi cci cdi Aabicdi Aabiaai Aabiabi Babicdi} \
801	     "a" {aai abi} \
802	     "b" {} \
803	     "c" {cci cdi} \
804	     "A" {Aabicdi Aabiaai Aabiabi} \
805	     "B" {Babicdi} \
806	     "A<a, a>" {Aabiaai Aabiabi} \
807	     "A<a<b>, c>" {Aabicdi}\
808	     "A<a, d>" {} \
809	     "B<a, a>" {} \
810	     "B<a, b>" {} \
811	     "B<a, c>" {Babicdi}] \
812	{
813	    foreach cmd_prefix {"b" "b -function"} {
814		set c "$cmd_prefix "
815		if {$type != ""} {
816		    append c "${type}::"
817		}
818		append c "method"
819
820		if {[llength $type_list] > 0} {
821		    test_gdb_complete_unique $c "${c}()"
822		    check_bp_locations_match_list $c [makem $type_list]
823		} else {
824		    test_gdb_complete_none $c
825		}
826	    }
827	}
828}
829
830# Test completion of a const-overloaded funtion (const-overload).
831# Note that "const" appears after the function/method parameters.
832
833proc_with_prefix const-overload {} {
834    set completion_list {
835	"struct_with_const_overload::const_overload_fn()"
836	"struct_with_const_overload::const_overload_fn() const"
837    }
838    foreach cmd_prefix {"b" "b -function"} {
839	test_gdb_complete_multiple \
840	    "$cmd_prefix " "const_overload_fn" "()" \
841	    $completion_list
842	test_gdb_complete_multiple \
843	    "$cmd_prefix " "const_overload_fn ( " ")" \
844	    $completion_list
845	test_gdb_complete_multiple \
846	    "$cmd_prefix " "const_overload_fn()" "" \
847	    $completion_list
848
849	check_bp_locations_match_list \
850	    "$cmd_prefix const_overload_fn" \
851	    {"struct_with_const_overload::const_overload_fn()"
852		"struct_with_const_overload::const_overload_fn() const"}
853
854	check_setting_bp_fails "$cmd_prefix const_overload_fn("
855	check_bp_locations_match_list \
856	    "$cmd_prefix const_overload_fn()" \
857	    {"struct_with_const_overload::const_overload_fn()"}
858	check_bp_locations_match_list \
859	    "$cmd_prefix const_overload_fn() const" \
860	    {"struct_with_const_overload::const_overload_fn() const"}
861    }
862}
863
864# Same but quote-enclose the function name.  This makes the overload
865# no longer be ambiguous.
866
867proc_with_prefix const-overload-quoted {} {
868    foreach cmd_prefix {"b" "b -function"} {
869	set linespec "'const_overload_fn()'"
870	test_gdb_complete_unique "$cmd_prefix $linespec" "$cmd_prefix $linespec"
871	check_bp_locations_match_list \
872	    "$cmd_prefix $linespec" {
873		"struct_with_const_overload::const_overload_fn()"
874	    }
875
876	set linespec "'const_overload_fn() const'"
877	test_gdb_complete_unique "$cmd_prefix $linespec" "$cmd_prefix $linespec"
878	check_bp_locations_match_list \
879	    "$cmd_prefix $linespec" {
880		"struct_with_const_overload::const_overload_fn() const"
881	    }
882    }
883}
884
885# Test that when the function is unambiguous, linespec completion
886# appends the end quote char automatically, both ' and ".
887
888proc_with_prefix append-end-quote-char-when-unambiguous {} {
889    foreach cmd_prefix {"b" "b -function"} {
890	foreach qc $completion::all_quotes_list {
891	    set linespec "${qc}not_overloaded_fn()${qc}"
892	    foreach cmd [list "$cmd_prefix ${qc}not_overloaded_fn()" \
893			      "$cmd_prefix ${qc}not_overloaded_fn" \
894			      "$cmd_prefix ${qc}not_overloaded_"] {
895		test_gdb_complete_unique $cmd "$cmd_prefix $linespec"
896	    }
897	    check_bp_locations_match_list \
898		"$cmd_prefix $linespec" {"not_overloaded_fn()"}
899	}
900    }
901}
902
903# Test completing symbols of source files.
904
905proc_with_prefix in-source-file-unconstrained {} {
906    # First test that unconstrained matching picks up functions from
907    # multiple files.
908    test_gdb_complete_multiple "b " "file_constrained_test" "_cpls" {
909	"file_constrained_test_cpls2_function(int)"
910	"file_constrained_test_cpls_function(int)"
911    }
912    check_setting_bp_fails "b file_constrained_test_cpls"
913}
914
915# Test an unambiguous completion that would be ambiguous if it weren't
916# for the source file component, due to
917# "file_constrained_test_cpls_function" in cpls.cc.  Test with
918# different components quoted, and with whitespace before the function
919# name.
920
921proc_with_prefix in-source-file-unambiguous {} {
922    foreach sqc $completion::maybe_quoted_list {
923	foreach fqc $completion::maybe_quoted_list {
924	    # Linespec.
925	    foreach sep {":" ": "} {
926		set linespec \
927		    "${sqc}cpls2.cc${sqc}${sep}${fqc}file_constrained_test_cpls2_function(int)${fqc}"
928		set complete_line "b $linespec"
929		set start [index_after "constrained_test" $complete_line]
930		set input_line [string range $complete_line 0 $start]
931		test_gdb_complete_unique $input_line ${complete_line}
932		check_bp_locations_match_list "b $linespec" {
933		    "file_constrained_test_cpls2_function(int)"
934		}
935	    }
936
937	    # Explicit location.
938	    set source_opt "-source ${sqc}cpls2.cc${sqc}"
939	    set function_opt "-function ${fqc}file_constrained_test_cpls2_function(int)${fqc}"
940	    set complete_line "b $source_opt $function_opt"
941	    set start [index_after "cpls2_functio" $complete_line]
942	    set input_line [string range $complete_line 0 $start]
943	    test_gdb_complete_unique $input_line ${complete_line}
944	    check_bp_locations_match_list "$complete_line" {
945		    "file_constrained_test_cpls2_function(int)"
946	    }
947	}
948    }
949}
950
951# Test an ambiguous completion constrained by a source file.  Test
952# with different components quoted, and with whitespace before the
953# function name.
954
955proc_with_prefix in-source-file-ambiguous {} {
956    foreach sqc $completion::maybe_quoted_list {
957	foreach fqc $completion::maybe_quoted_list {
958	    # Linespec.
959	    foreach sep {":" ": "} {
960		set cmd_prefix "b ${sqc}cpls2.cc${sqc}${sep}"
961		test_gdb_complete_multiple "${cmd_prefix}" ${fqc} "" {
962		    "another_file_constrained_test_cpls2_function(int)"
963		    "file_constrained_test_cpls2_function(int)"
964		} ${fqc} ${fqc}
965	    }
966
967	    # Explicit location.
968	    test_gdb_complete_multiple \
969		"b -source ${sqc}cpls2.cc${sqc} -function " ${fqc} "" {
970		"another_file_constrained_test_cpls2_function(int)"
971		"file_constrained_test_cpls2_function(int)"
972	    } ${fqc} ${fqc}
973	}
974    }
975}
976
977# Check that completing a file name in a linespec auto-appends a colon
978# instead of a whitespace character.
979
980proc_with_prefix source-complete-appends-colon {} {
981    # Test with quotes to make sure the end quote char is put at the
982    # right place.
983    foreach qc $completion::maybe_quoted_list {
984	test_gdb_complete_unique \
985	    "b ${qc}cpls2." \
986	    "b ${qc}cpls2.cc${qc}" ":"
987	test_gdb_complete_unique \
988	    "b ${qc}cpls2.c" \
989	    "b ${qc}cpls2.cc${qc}" ":"
990	test_gdb_complete_unique \
991	    "b ${qc}cpls2.cc" \
992	    "b ${qc}cpls2.cc${qc}" ":"
993
994	# Same, but with a filename with an hyphen (which is normally
995	# a language word break char).
996	test_gdb_complete_unique \
997	    "b ${qc}cpls-" \
998	    "b ${qc}cpls-hyphen.cc${qc}" ":"
999	test_gdb_complete_unique \
1000	    "b ${qc}cpls-hyphen" \
1001	    "b ${qc}cpls-hyphen.cc${qc}" ":"
1002    }
1003
1004    # Test the same, but with the name of a nonexisting file.
1005
1006    # Cursor at the end of the string.
1007    test_gdb_complete_none "b nonexistingfilename.cc"
1008    # Cursor past the end of the string.
1009    test_gdb_complete_multiple "b nonexistingfilename.cc " "" "" \
1010	$completion::keyword_list
1011    foreach qc $completion::all_quotes_list {
1012	# Unterminated quote.
1013	test_gdb_complete_none "b ${qc}nonexistingfilename.cc"
1014	test_gdb_complete_none "b ${qc}nonexistingfilename.cc "
1015	# Terminated quote, cursor at the quote.
1016	test_gdb_complete_unique \
1017	    "b ${qc}nonexistingfilename.cc${qc}" \
1018	    "b ${qc}nonexistingfilename.cc${qc}"
1019	# Terminated quote, cursor past the quote.
1020	test_gdb_complete_multiple \
1021	    "b ${qc}nonexistingfilename.cc${qc} " "" "" \
1022	    $completion::keyword_list
1023    }
1024}
1025
1026####################################################################
1027
1028# Test that a colon at the end of the linespec is understood as an
1029# incomplete scope operator (incomplete-scope-colon), instead of a
1030# source/function separator.
1031
1032proc_with_prefix incomplete-scope-colon {} {
1033
1034    # Helper for the loop below to simplify it.  Tests completion of
1035    # the range defined by the RANGE_SS found in the constructed line.
1036    #
1037    # E.g., with:
1038    #
1039    #   source="source.cc"
1040    #   fqc="'"
1041    #   prototype="ns::function()"
1042    #   range_ss="s::f"
1043    #
1044    # we'd try completing with the cursor set in each of the
1045    # underlined range's positions of:
1046    #
1047    #   b source.cc:'ns::function()'"
1048    #                 ^^^^
1049    #
1050    # Also test that setting a breakpoint at the constructed line
1051    # finds the same breakpoint location as completion does.
1052    #
1053    proc incomplete_scope_colon_helper {prototype range_ss {skip_check_bp 0}} {
1054	foreach source {"" "cpls.cc"} {
1055	    # Test with and without source quoting.
1056	    foreach sqc $completion::maybe_quoted_list {
1057		if {$source == "" && $sqc != ""} {
1058		    # Invalid combination.
1059		    continue
1060		}
1061
1062		# Test with and without function quoting.
1063		foreach fqc $completion::maybe_quoted_list {
1064		    if {$source == ""} {
1065			set linespec_source ""
1066			set explicit_source ""
1067		    } else {
1068			set linespec_source "${sqc}${source}${sqc}:"
1069			set explicit_source "-source ${sqc}${source}${sqc}"
1070		    }
1071
1072		    # Even though this use case is trickier with
1073		    # linespecs due to the ":" as separator, test both
1074		    # linespecs and explicit locations for
1075		    # completeness.
1076		    foreach location [list \
1077					  "${linespec_source}${fqc}$prototype${fqc}" \
1078					  "${explicit_source} -function ${fqc}$prototype${fqc}"] {
1079			set complete_line "b $location"
1080			set start [string first $range_ss $complete_line]
1081			set end [expr ($start + [string length $range_ss])]
1082			test_complete_prefix_range $complete_line $start $end
1083			if {!$skip_check_bp} {
1084			    check_bp_locations_match_list "b $location" [list "$prototype"]
1085			}
1086		    }
1087		}
1088	    }
1089	}
1090    }
1091
1092    incomplete_scope_colon_helper \
1093	"struct_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
1094	"t::i"
1095
1096    incomplete_scope_colon_helper \
1097	"ns_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
1098	"t::i"
1099
1100    # Test completing around both "::"s.
1101    foreach range_ss {"t::s" "t::i"} skip_check_bp {0 1} {
1102	incomplete_scope_colon_helper \
1103	    "ns2_incomplete_scope_colon_test::struct_in_ns2_incomplete_scope_colon_test::incomplete_scope_colon_test()" \
1104	    $range_ss $skip_check_bp
1105    }
1106}
1107
1108# Test completing functions/methods in anonymous namespaces.
1109
1110proc_with_prefix anon-ns {} {
1111    foreach cmd_prefix {"b" "b -function"} {
1112	foreach qc $completion::maybe_quoted_list {
1113	    test_gdb_complete_unique \
1114		"$cmd_prefix ${qc}anon_ns_function" \
1115		"$cmd_prefix ${qc}anon_ns_function()${qc}"
1116	    check_bp_locations_match_list "$cmd_prefix ${qc}anon_ns_function()${qc}" {
1117		"(anonymous namespace)::anon_ns_function()"
1118		"(anonymous namespace)::anon_ns_struct::anon_ns_function()"
1119		"the_anon_ns_wrapper_ns::(anonymous namespace)::anon_ns_function()"
1120		"the_anon_ns_wrapper_ns::(anonymous namespace)::anon_ns_struct::anon_ns_function()"
1121	    }
1122	}
1123
1124	# A "(" finds all anonymous namespace functions/methods in all
1125	# scopes.
1126	test_gdb_complete_multiple "$cmd_prefix " "(" "anonymous namespace)::" {
1127	    "(anonymous namespace)::anon_ns_function()"
1128	    "(anonymous namespace)::anon_ns_struct::anon_ns_function()"
1129	    "(anonymous namespace)::overload2_function(overload2_arg3)"
1130	    "(anonymous namespace)::overload3_function(int)"
1131	    "(anonymous namespace)::overload3_function(long)"
1132	    "(anonymous namespace)::struct_overload2_test::overload2_function(overload2_arg4)"
1133	    "(anonymous namespace)::struct_overload3_test::overload3_function(int)"
1134	    "(anonymous namespace)::struct_overload3_test::overload3_function(long)"
1135	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::overload2_function(overload2_arg9)"
1136	    "ns_overload2_test::(anonymous namespace)::ns_overload2_test::struct_overload2_test::overload2_function(overload2_arga)"
1137	    "ns_overload2_test::(anonymous namespace)::overload2_function(overload2_arg7)"
1138	    "ns_overload2_test::(anonymous namespace)::struct_overload2_test::overload2_function(overload2_arg8)"
1139	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::overload3_function(int)"
1140	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::overload3_function(long)"
1141	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::struct_overload3_test::overload3_function(int)"
1142	    "ns_overload3_test::(anonymous namespace)::ns_overload3_test::struct_overload3_test::overload3_function(long)"
1143	    "ns_overload3_test::(anonymous namespace)::overload3_function(int)"
1144	    "ns_overload3_test::(anonymous namespace)::overload3_function(long)"
1145	    "ns_overload3_test::(anonymous namespace)::struct_overload3_test::overload3_function(int)"
1146	    "ns_overload3_test::(anonymous namespace)::struct_overload3_test::overload3_function(long)"
1147	    "the_anon_ns_wrapper_ns::(anonymous namespace)::anon_ns_function()"
1148	    "the_anon_ns_wrapper_ns::(anonymous namespace)::anon_ns_struct::anon_ns_function()"
1149	}
1150
1151	set function "the_anon_ns_wrapper_ns::(anonymous namespace)::anon_ns_function()"
1152	test_gdb_complete_unique "$cmd_prefix $function" "$cmd_prefix $function"
1153	check_bp_locations_match_list "$cmd_prefix $function" [list $function]
1154
1155	# Test completing after the "(anonymous namespace)" part.
1156	test_gdb_complete_unique \
1157	    "$cmd_prefix the_anon_ns_wrapper_ns::(anonymous namespace)::anon_ns_fu" \
1158	    "$cmd_prefix $function"
1159
1160	# Test whitespace in the "(anonymous namespace)" component.
1161
1162	test_gdb_complete_unique \
1163	    "$cmd_prefix the_anon_ns_wrapper_ns::( anonymous   namespace )::anon_ns_fu" \
1164	    "$cmd_prefix the_anon_ns_wrapper_ns::( anonymous   namespace )::anon_ns_function()"
1165	check_setting_bp_fails \
1166	    "$cmd_prefix the_anon_ns_wrapper_ns::( anonymous   namespace )::anon_ns_fu"
1167
1168	set function_ws \
1169	    "the_anon_ns_wrapper_ns::( anonymous   namespace )::anon_ns_function ( )"
1170	test_gdb_complete_unique "$cmd_prefix $function_ws" "$cmd_prefix $function_ws"
1171	check_bp_locations_match_list "$cmd_prefix $function_ws" [list $function]
1172    }
1173}
1174
1175# Basic test for completing "operator<".  More extensive C++ operator
1176# tests in cpls-op.exp.
1177
1178proc_with_prefix operator< {} {
1179    # Complete all prefixes between "oper" and the whole prototype.
1180    set function "operator<(foo_enum, foo_enum)"
1181    foreach cmd_prefix {"b" "b -function"} {
1182	set line "$cmd_prefix $function"
1183	set start [index_after "oper" $line]
1184	test_complete_prefix_range $line $start
1185    }
1186
1187    # There's a label in the function; try completing it.  (Exhaustive
1188    # label completion tests further below.)
1189    foreach location [list \
1190		     "$function:label1" \
1191		     "-function $function -label label1"] {
1192
1193	set cmd "b $location"
1194	set input_line [string range $cmd 0 [expr [string length $cmd] - 3]]
1195
1196	test_gdb_complete_unique $input_line $cmd
1197	test_gdb_complete_unique $cmd $cmd
1198	check_bp_locations_match_list $cmd [list "$location"]
1199    }
1200}
1201
1202# Test completion of scopes with an ambiguous prefix.
1203
1204proc_with_prefix ambiguous-prefix {} {
1205    foreach cmd_prefix {"b" "b -function"} {
1206	test_gdb_complete_multiple "$cmd_prefix " "ambiguous_pre" "fix_" {
1207	    "ambiguous_prefix_global_func()"
1208	    "the_ambiguous_prefix_ns::ambiguous_prefix_ns_func()"
1209	    "the_ambiguous_prefix_struct::ambiguous_prefix_method()"
1210	}
1211	check_setting_bp_fails "$cmd_prefix ambiguous_prefix_"
1212    }
1213}
1214
1215# Test completion of function labels.
1216
1217proc_with_prefix function-labels {} {
1218    # Test with and without a source file component.
1219    foreach_location_functions \
1220	{ "" "cpls.cc" } \
1221	{ "function_with_labels(int)" } \
1222	{
1223	    # Linespec version.  Test various spacing around the label
1224	    # colon separator.
1225	    foreach label_sep {":" " :" ": " " : "} {
1226		set linespec "${location}${label_sep}"
1227		test_gdb_complete_multiple "b $linespec" "l" "abel" {
1228		    "label1"
1229		    "label2"
1230		}
1231		check_setting_bp_fails "b ${linespec}label"
1232
1233		set tsep [string trim ${source_sep}]
1234		check_bp_locations_match_list \
1235		    "b ${linespec}label1" [list "${source}${tsep}${function}:label1"]
1236		check_bp_locations_match_list \
1237		    "b ${linespec}label2" [list "${source}${tsep}${function}:label2"]
1238	    }
1239	} \
1240	{
1241	    # Explicit locations version.
1242	    append location " -label"
1243	    test_gdb_complete_multiple "b $location " "l" "abel" {
1244		"label1"
1245		"label2"
1246	    }
1247	    check_setting_bp_fails "b $location label"
1248
1249	    if {$source != ""} {
1250		set bp_loc_src "-source ${source} "
1251	    } else {
1252		set bp_loc_src ""
1253	    }
1254	    check_bp_locations_match_list \
1255		"b ${location} label1" [list "${bp_loc_src}-function $function -label label1"]
1256	    check_bp_locations_match_list \
1257		"b ${location} label2" [list "${bp_loc_src}-function $function -label label2"]
1258	}
1259}
1260
1261# Test that completion after a function name offers keyword
1262# (if/task/thread/-force-condition) matches in linespec mode, and also
1263# the explicit location options in explicit locations mode.
1264
1265proc_with_prefix keywords-after-function {} {
1266    set explicit_list \
1267	[lsort [concat \
1268		    $completion::explicit_opts_list \
1269		    $completion::keyword_list]]
1270
1271    # Test without a source file, with a known source file, and with
1272    # and unknown source file.
1273    # Test a known and an unknown function.
1274    foreach_location_functions \
1275	{ "" "cpls.cc" "unknown_file.cc" } \
1276	{ "function_with_labels(int)" "unknown_function(int)" } \
1277	{
1278	    # Linespec version.
1279	    test_gdb_complete_multiple "b ${location} " "" "" \
1280		$completion::keyword_list
1281	} \
1282	{
1283	    # Explicit locations version.
1284	    test_gdb_complete_multiple "b ${location} " "" "" \
1285		$explicit_list
1286	}
1287}
1288
1289# Same, but after a label.
1290
1291proc_with_prefix keywords-after-label {} {
1292    set explicit_list \
1293	[lsort [concat \
1294		    $completion::explicit_opts_list \
1295		    $completion::keyword_list]]
1296
1297    foreach_location_labels \
1298	{ "" "cpls.cc" } \
1299	{ "function_with_labels(int)" "unknown_function(int)" } \
1300	{ "label1" "non_existing_label" } \
1301	{
1302	    # Linespec version.
1303	    test_gdb_complete_multiple "b ${location} " "" "" \
1304		$completion::keyword_list
1305	} \
1306	{
1307	    # Explicit locations version.
1308	    test_gdb_complete_multiple "b ${location} " "" "" \
1309		$explicit_list
1310	}
1311}
1312
1313# Similar, but after an unknown file, and in linespec mode only.
1314
1315proc_with_prefix keywords-after-unknown-file {} {
1316    # Test with and without quoting.
1317    foreach qc $completion::maybe_quoted_list {
1318	set line "b ${qc}unknown_file.cc${qc}: "
1319	test_gdb_complete_multiple $line "" "" $completion::keyword_list
1320    }
1321}
1322
1323# Test that linespec / function completion does not match data
1324# symbols, only functions/methods.
1325
1326proc_with_prefix no-data-symbols {} {
1327    foreach cmd_prefix {"b" "b -function"} {
1328	test_gdb_complete_unique "$cmd_prefix code_" "$cmd_prefix code_function()"
1329    }
1330}
1331
1332
1333# After "if", we expect an expression, which has a different completer
1334# that matches data symbols as well.  Check that that works.
1335
1336proc_with_prefix if-expression {} {
1337    foreach cmd_prefix {"b" "b -function"} {
1338	test_gdb_complete_multiple "$cmd_prefix function() if " "code_" "" {
1339	    "code_data"
1340	    "code_function()"
1341	}
1342
1343	test_gdb_complete_unique \
1344	    "$cmd_prefix function() if code_data + another_da" \
1345	    "$cmd_prefix function() if code_data + another_data"
1346
1347	test_gdb_complete_unique \
1348	    "$cmd_prefix non_existing_function() if code_data + another_da" \
1349	    "$cmd_prefix non_existing_function() if code_data + another_data"
1350
1351	# FIXME: For now, thread and task also use the expression
1352	# completer.
1353	test_gdb_complete_unique \
1354	    "$cmd_prefix function() thread code_data + another_da" \
1355	    "$cmd_prefix function() thread code_data + another_data"
1356	test_gdb_complete_unique \
1357	    "$cmd_prefix function() task code_data + another_da" \
1358	    "$cmd_prefix function() task code_data + another_data"
1359    }
1360}
1361
1362# The testcase driver.  Calls all test procedures.
1363
1364proc test_driver {} {
1365    all-param-prefixes
1366    overload
1367    overload-2
1368    fqn
1369    fqn-2
1370    overload-3
1371    template-overload
1372    template-ret-type
1373    #test_makefoo
1374    template-function-foo
1375    #test_makem
1376    template-class-with-method
1377    const-overload
1378    const-overload-quoted
1379    append-end-quote-char-when-unambiguous
1380    in-source-file-unconstrained
1381    in-source-file-unambiguous
1382    in-source-file-ambiguous
1383    source-complete-appends-colon
1384    incomplete-scope-colon
1385    anon-ns
1386    operator<
1387    ambiguous-prefix
1388    function-labels
1389    keywords-after-function
1390    keywords-after-label
1391    keywords-after-unknown-file
1392    no-data-symbols
1393    if-expression
1394}
1395
1396test_driver
1397