• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GNU Binutils with patches for OS216


Commit MetaInfo

Revisionb1ecccc4cd06b922c2ea916f2902868de87f8a09 (tree)
Zeit2018-02-24 03:07:29
AutorKeith Seitz <keiths@redh...>
CommiterKeith Seitz

Log Message

Support template lookups in strncmp_iw_with_mode

This patch adds support for wild template parameter list matches, similar
to how ABI tags or function overloads are now handled.

With this patch, users will be able to "gloss over" the details of matching
template parameter lists. This is accomplished by adding (yet more) logic
to strncmp_iw_with_mode to skip parameter lists if none is explicitly given
by the user.

Here's a simple example using gdb.linespec/cpls-ops.exp:

Before


(gdb) ptype test_op_call
type = struct test_op_call {

public:
void operator()(void);
void operator()(int);
void operator()(long);
void operator()<int>(int *);

}
(gdb) b test_op_call::operator()
Breakpoint 1 at 0x400583: test_op_call::operator(). (3 locations)
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y <MULTIPLE>
1.1 y 0x400583 in test_op_call::operator()(int)

at cpls-ops.cc:43

1.2 y 0x40058e in test_op_call::operator()()

at cpls-ops.cc:47

1.3 y 0x40059e in test_op_call::operator()(long)

at cpls-ops.cc:51

The breakpoint at test_op_call::operator()<int> was never set.

After


(gdb) b test_op_call::operator()
Breakpoint 1 at 0x400583: test_op_call::operator(). (4 locations)
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y <MULTIPLE>
1.1 y 0x400583 in test_op_call::operator()(int)

at cpls-ops.cc:43

1.2 y 0x40058e in test_op_call::operator()()

at cpls-ops.cc:47

1.3 y 0x40059e in test_op_call::operator()(long)

at cpls-ops.cc:51

1.4 y 0x4008d0 in test_op_call::operator()<int>(int*)

at cpls-ops.cc:57

Similar to how scope lookups work, passing "-qualified" to the break command
will cause a literal lookup of the symbol. In the example immediately above,
this will cause GDB to only find the three non-template functions.

gdb/ChangeLog:

* NEWS: Mention new template parameter support for
locations/completion.
* cp-support.c (cp_search_name_hash): Break on template parameter
lists.
(cp_symbol_name_matches_1): Tell strncmp_iw_with_mode to ignore
template parameter lists.
* utils.c (skip_template_parameter_list): New function.
(strncmp_iw_with_mode): Support "ignoring" of template parameter lists.
* utils.h (strncmp_iw_with_mode): Add new parameter
ignore_template_params'.

gdb/doc/ChangeLog:

* gdb.texinfo (Debugging C Plus Plus): Document setting breakpoints
in templates.

gdb/testsuite/ChangeLog:

* gdb.cp/templates.cc (Foozle, operator<, operator<<): New.
(main): Add uses of new functions/types.
* gdb.cp/templates.exp: Add template parameter list tests.
* gdb.linespec/cpcompletion.exp: Load data-structures.exp.
(consume, maket, makearg, makefoo, test_makefoo_1, test_makefoo)
(maketype, makem, test_makem_1, test_makem, template-class-with-method)
(template-function-foo): New procedures.
(template-ret-type): Split range completion to account for template
names completing without template parameter list.
Change "setting breakpoint without template parameter test" to pass.
(test_driver): Call template-function-foo test procedure.
* gdb.linespec/cpls-ops.exp (test_operator_ambiguous): Remove
template restriction.
Add new whitespace tests for template functions.
* gdb.linespec/cpls.cc (foo, a, b, c, d, A, B, NA, NB): New
template definitions.
(template_function_foo): New function.
(main): Call template_function_foo.

Ändern Zusammenfassung

Diff

--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -6,6 +6,10 @@
66 * 'info proc' now works on running processes on FreeBSD systems and core
77 files created on FreeBSD systems.
88
9+* C++ developers may now ignore template parameter lists when specifying
10+ locations, e.g., while setting breakpoints. This is analogous to how
11+ completion and symbol lookup handles overloaded functions or ABI tags.
12+
913 *** Changes in GDB 8.1
1014
1115 * GDB now supports dynamically creating arbitrary register groups specified
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1643,6 +1643,12 @@ cp_search_name_hash (const char *search_name)
16431643 && string[5] != ':')
16441644 break;
16451645
1646+ /* Ignore template parameter lists. */
1647+ if (string[0] == '<'
1648+ && string[1] != '(' && string[1] != '<' && string[1] != '='
1649+ && string[1] != ' ' && string[1] != '\0')
1650+ break;
1651+
16461652 hash = SYMBOL_HASH_NEXT (hash, *string);
16471653 }
16481654 return hash;
@@ -1696,7 +1702,7 @@ cp_symbol_name_matches_1 (const char *symbol_search_name,
16961702 while (true)
16971703 {
16981704 if (strncmp_iw_with_mode (sname, lookup_name, lookup_name_len,
1699- mode, language_cplus, match_for_lcd) == 0)
1705+ mode, language_cplus, match_for_lcd, true) == 0)
17001706 {
17011707 if (comp_match_res != NULL)
17021708 {
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -15188,6 +15188,52 @@ also use the @value{GDBN} command-line word completion facilities to list the
1518815188 available choices, or to finish the type list for you.
1518915189 @xref{Completion,, Command Completion}, for details on how to do this.
1519015190
15191+@item @r{Breakpoints in template functions}
15192+
15193+Similar to overloaded symbols, @value{GDBN} will set breakpoints in all
15194+template instantiations with the user-specified name. To set a breakpoint in
15195+a specific template instantiation, include the template parameter list.
15196+
15197+@smallexample
15198+(gdb) b mytemplate
15199+Breakpoint 1 at 0x40085b: mytemplate. (2 locations)
15200+(gdb) info breakpoints
15201+Num Type Disp Enb Address What
15202+1 breakpoint keep y <MULTIPLE>
15203+1.1 y 0x40085b in mytemplate<int>(int)
15204+ at mytemplate.cc:8
15205+1.2 y 0x40087a in mytemplate<double>(double)
15206+ at mytemplate.cc:8
15207+@end smallexample
15208+
15209+@noindent
15210+In the above example, @value{GDBN} searches all namespaces and types for
15211+functions named @code{mytemplate}, ignoring template parameter lists and
15212+function arguments.
15213+
15214+In the below example, a template parameter list is specified, and @value{GDBN}
15215+searches all namespaces and types for the specific instantiation:
15216+
15217+@smallexample
15218+(gdb) b mytemplate<int>
15219+Breakpoint 2 at 0x40085b: file mytemplate.cc, line 8.
15220+(gdb) info breakpoints
15221+Num Type Disp Enb Address What
15222+2 breakpoint keep y 0x40085b in mytemplate<int>(int)
15223+ at mytemplate.cc:8
15224+@end smallexample
15225+
15226+Just as with function overloads, the @kbd{-qualified} flag may be used to
15227+override this behavior, causing @value{GDBN} to search for a specific
15228+function without ignoring template parameter lists.
15229+
15230+@smallexample
15231+(gdb) b mytemplate
15232+Breakpoint 3 at 0x40085b: mytemplate. (2 locations)
15233+(gdb) b -qualified mytemplate
15234+Function "mytemplate" not defined.
15235+@end smallexample
15236+
1519115237 @item @r{Breakpoints in functions with ABI tags}
1519215238
1519315239 The GNU C@t{++} compiler introduced the notion of ABI ``tags'', which
--- a/gdb/testsuite/gdb.cp/templates.cc
+++ b/gdb/testsuite/gdb.cp/templates.cc
@@ -747,6 +747,34 @@ template<class C> int FunctionArg<C>::method(Empty<void (FunctionArg<C>)> &arg)
747747 Empty<void(FunctionArg<int>)> empty;
748748 FunctionArg<int> arg;
749749
750+template <typename T1>
751+struct Foozle
752+{
753+ int x;
754+ T1 t;
755+ template <typename T2>
756+ T2 fogey (T2 plop);
757+};
758+
759+template <typename T1>
760+template <typename T2>
761+T2 Foozle<T1>::fogey (T2 plop)
762+{
763+ return plop;
764+}
765+
766+template <typename T>
767+int operator< (T &lhs, T &rhs)
768+{
769+ return 0;
770+}
771+
772+template <typename T>
773+int operator<< (T &obj, T &val)
774+{
775+ return 1;
776+};
777+
750778 int main()
751779 {
752780 int i;
@@ -818,5 +846,24 @@ int main()
818846
819847 arg.method(empty);
820848
849+ Empty<int> e;
850+ Foozle<int> fzi;
851+ x = fzi.fogey (0);
852+ c = fzi.fogey<char> ('a');
853+ e = fzi.fogey<Empty<int>> (e);
854+ Foozle<char> fzc;
855+ c = fzc.fogey ('b');
856+ x = fzc.fogey<int> (0);
857+ e = fzc.fogey<Empty<int>> (e);
858+ Foozle<Empty<int>> fze;
859+ e = fze.fogey (e);
860+ c = fze.fogey<char> ('c');
861+ x = fze.fogey<int> (0);
862+
863+ z = e < e;
864+ z += e << e;
865+ z += fzi < fzi;
866+ z += fzi << fzi;
867+
821868 return 0; // break here
822869 }
--- a/gdb/testsuite/gdb.cp/templates.exp
+++ b/gdb/testsuite/gdb.cp/templates.exp
@@ -580,3 +580,70 @@ gdb_test "print Garply<Garply<char> >::garply" \
580580 # Now should work fine
581581 gdb_test "break Garply<Garply<char> >::garply" \
582582 "Breakpoint \[0-9\]* at $hex: file .*templates.cc, line.*"
583+
584+#
585+# Template wild-matching tests
586+#
587+
588+# Turn off "ask" when multiple symbols are seen.
589+gdb_test_no_output "set multiple-symbols all"
590+
591+# Test setting breakpoints in a method of all class template instantiations,
592+# including overloads.
593+gdb_test "break Foo::foo" "Breakpoint.*at.* \\(3 locations\\)"
594+foreach t [list "int" "char" "volatile char *"] {
595+ gdb_breakpoint "Foo<$t>::foo (int, $t)"
596+ gdb_breakpoint "Foo::foo (int, $t)"
597+}
598+
599+gdb_test "break Bar::bar" "Breakpoint.*at.* \\(2 locations\\)"
600+gdb_test "break Bar::bar (int, int)" "Breakpoint.*at.* \\(2 locations\\)"
601+foreach val [list 1 33] {
602+ gdb_breakpoint "Bar<int, $val>::bar (int, int)"
603+}
604+
605+# Test setting breakpoints in a member function template of a class template,
606+# including overloads.
607+gdb_test "break Foozle::fogey" "Breakpoint.*at.* \\(9 locations\\)" \
608+ "break at template method fogey"
609+foreach t [list "int" "char" "Empty<int>"] {
610+ gdb_test "break Foozle::fogey ($t)" "Breakpoint.*at.* \\(3 locations\\)"
611+ gdb_test "break Foozle::fogey<$t>" "Breakpoint.*at.* \\(3 locations\\)"
612+ foreach u [list "int" "char" "Empty<int>"] {
613+ gdb_breakpoint "Foozle<$t>::fogey<$u>" message
614+ gdb_breakpoint "Foozle<$t>::fogey<$u> ($u)" message
615+ }
616+}
617+
618+# Test templated operators < and <<. Restrict results to only the test
619+# source file.
620+# operator<:
621+# 1. operator< (const T2&, const T2&)
622+# 2. operator< (const T2&, char)
623+# 3. operator< <Empty<int>>
624+# 4. operator< <Foozle<in>>
625+#
626+# operator<<:
627+# 1. operator<< <Empty<int>>
628+# 2. operator<< <Foozle<int>>
629+gdb_test "break -source $srcfile -func operator<" \
630+ "Breakpoint.*at.* \\(4 locations\\)"
631+gdb_test "break -source $srcfile -func operator<<" \
632+ "Breakpoint.*at.* \\(2 locations\\)"
633+foreach t [list "Empty" "Foozle"] {
634+ set tt "$t<int>"
635+ gdb_breakpoint "operator< <$tt>" message
636+ gdb_breakpoint "operator<< <$tt>" message
637+
638+ # Try a specific instance, both with and without whitespace
639+ # after the template-template parameter.
640+ gdb_breakpoint "operator< <$tt> ($tt&, $tt&)" message
641+ gdb_breakpoint "operator< <$tt > ($tt&, $tt&)" message
642+ gdb_breakpoint "operator<< <$tt> ($tt&, $tt&)" message
643+ gdb_breakpoint "operator<< <$tt > ($tt&, $tt&)" message
644+}
645+
646+# Test that "-qualified" finds no matching locations.
647+gdb_test_no_output "set breakpoint pending off"
648+gdb_test "break -qualified Foozle::fogey" \
649+ "Function \"Foozle::fogey\" not defined."
--- a/gdb/testsuite/gdb.linespec/cpcompletion.exp
+++ b/gdb/testsuite/gdb.linespec/cpcompletion.exp
@@ -16,6 +16,7 @@
1616 # This file is part of the gdb testsuite.
1717
1818 load_lib completion-support.exp
19+load_lib data-structures.exp
1920
2021 standard_testfile cpls.cc cpls2.cc cpls-hyphen.cc
2122
@@ -24,6 +25,217 @@ if {[prepare_for_testing "failed to prepare" $testfile \
2425 return -1
2526 }
2627
28+#
29+# Some convenience procedures for testing template parameter list
30+# completion.
31+#
32+
33+# For the variable named ARGLISTVAR, which should be the name of an
34+# argument list in the calling frame, "consume" the top-most token.
35+# [See comments for makefoo for description of arglist format.]
36+
37+proc consume {arglistvar} {
38+ upvar $arglistvar arglist
39+
40+ # ARGLIST is a string -- simply strip off the first character.
41+ set arglist [string range $arglist 1 end]
42+}
43+
44+# Create a function template named NAME, using the given stack ID to grab
45+# NUM template parameters. The result is pushed back onto the
46+# stack. NUM may be "all," in which case we use the entire stack
47+# to create the function template, including function arguments.
48+# The resulting template function's arguments are taken from the test
49+# source code for the function "foo" and is not generalized.
50+
51+proc maket {sid name {num 1}} {
52+
53+ # Set up a temporary stack of parameters. This will reverse
54+ # the order in SID so that when they are popped again below,
55+ # we get them in the correct order. This also takes into account
56+ # how many levels of the result stack we want to consider.
57+
58+ set paramstack [::Stack::new]
59+ if {[string equal $num "all"]} {
60+ while {![stack empty $sid]} {
61+ stack push $paramstack [stack pop $sid]
62+ }
63+ } else {
64+ for {set i 0} {$i < $num} {incr i} {
65+ stack push $paramstack [stack pop $sid]
66+ }
67+ }
68+
69+ # Construct the function template and push it back to the
70+ # top of the stack given by SID.
71+ set result ""
72+ set first true
73+ while {![stack empty $paramstack]} {
74+ set top [stack pop $paramstack]
75+ if {$first} {
76+ set first false
77+ } else {
78+ append result ", "
79+ }
80+ append result $top
81+ }
82+
83+ # Save argument list.
84+ set args $result
85+
86+ # GDB outputs "> >" instead of ">>".
87+ if {[string index $top end] == ">"} {
88+ append result " "
89+ }
90+ set result "$name<$result>"
91+ if {[string equal $num "all"]} {
92+ append result "($args)"
93+ }
94+ stack push $sid $result
95+ stack delete $paramstack
96+}
97+
98+# Given the stack SID and the name of a variable of the desired template
99+# parameters, construct the actual template parameter and push it to the
100+# top of the stack.
101+
102+proc makearg {sid arglistvar} {
103+ upvar $arglistvar arglist
104+
105+ set c [string index $arglist 0]
106+ consume arglist
107+ switch $c {
108+ A -
109+ B {
110+ makearg $sid arglist
111+ makearg $sid arglist
112+ maket $sid $c 2
113+ }
114+
115+ a -
116+ b -
117+ c -
118+ d {
119+ makearg $sid arglist
120+ maket $sid $c
121+ }
122+
123+ i {
124+ stack push $sid "int"
125+ }
126+
127+ n {
128+ # These are not templates.
129+ set c [string index $arglist 0]
130+ stack push $sid "n::n$c"
131+ consume arglist
132+ }
133+
134+ N {
135+ set c [string index $arglist 0]
136+ makearg $sid arglist
137+ set top [stack pop $sid]
138+ stack push $sid "n::N$top"
139+ }
140+
141+ default { error "unhandled arglist identifier: '$c'" }
142+ }
143+}
144+
145+# Given ARGLIST, construct a class template for the type and return
146+# it as a string.
147+
148+proc maketype {arglist} {
149+ set s [Stack::new]
150+ makearg $s arglist
151+ set result [stack pop $s]
152+ stack delete $s
153+ return $result
154+}
155+
156+# Returns a function template declaration for the function "foo" in the
157+# corresponding test source code. ARGLIST specifies the exact instantiation
158+# that is desired.
159+#
160+# Generically, this procedure returns a string of the form,
161+# "foo<parameter-list> (arg-list)", where ARGLIST describes the parameter(s).
162+#
163+# Valid specifiers for ARGLIST (must be kept in sync with source code):
164+#
165+# i: Creates an "int" type.
166+# a, b, c, d: Creates the struct template of the same name, taking a single
167+# template parameter.
168+# A, B: Creates the struct template of the same name, taking two template
169+# parameters.
170+# na, nb: Creates the non-template structs n::na and n::nb, respectively.
171+# NA, NB: Creates the struct templates n::NA and n::NB, respectively, which
172+# take two template parameters.
173+#
174+# Examples:
175+# makefoo i
176+# --> foo<int> (int)
177+# makefoo ii
178+# --> foo<int, int> (int, int)
179+# makefoo Aiabi
180+# --> foo<A<int, a<b<int> > > > (A<int, a<b<int> > >)
181+# makefoo NANAiaiNBbiabi
182+# --> foo<n::NA<n::NA<int, a<int> >, n::NB<b<int>, a<b<int> > > > >
183+# (n::NA<n::NA<int, a<int> >, n::NB<b<int>, a<b<int> > > >)
184+
185+proc makefoo {arglist} {
186+ set s [::Stack::new]
187+ while {[string length $arglist] > 0} {
188+ makearg $s arglist
189+ }
190+
191+ maket $s "foo" all
192+ set result [stack pop $s]
193+ stack delete $s
194+ return $result
195+}
196+
197+# Test wrapper for a single "makefoo" unit test.
198+
199+proc test_makefoo_1 {arglist expected} {
200+ set exp "foo<$expected"
201+ if {[string index $exp end] == ">"} {
202+ append exp " "
203+ }
204+ append exp ">"
205+ append exp "($expected)"
206+
207+ set calc [makefoo $arglist]
208+ send_log "makefoo $arglist = $calc\n"
209+ send_log "expecting: $exp\n"
210+ if {[string equal $exp $calc]} {
211+ pass "makefoo unit test: $arglist"
212+ } else {
213+ fail "makefoo unit test: $arglist"
214+ }
215+}
216+
217+# Test whether the procedure "makefoo" is functioning as expected.
218+
219+proc test_makefoo {} {
220+ test_makefoo_1 "i" "int"
221+ test_makefoo_1 "ai" "a<int>"
222+ test_makefoo_1 "aai" "a<a<int> >"
223+ test_makefoo_1 "ii" "int, int"
224+ test_makefoo_1 "aaibi" "a<a<int> >, b<int>"
225+ test_makefoo_1 \
226+ "ababiibababai" "a<b<a<b<int> > > >, int, b<a<b<a<b<a<int> > > > > >"
227+ test_makefoo_1 "Aii" "A<int, int>"
228+ test_makefoo_1 "ABaibibi" "A<B<a<int>, b<int> >, b<int> >"
229+ test_makefoo_1 "na" "n::na"
230+ test_makefoo_1 "nana" "n::na, n::na"
231+ test_makefoo_1 "NAii" "n::NA<int, int>"
232+ test_makefoo_1 "NANAiiNAii" "n::NA<n::NA<int, int>, n::NA<int, int> >"
233+}
234+
235+#
236+# Tests start here.
237+#
238+
27239 # Disable the completion limit for the whole testcase.
28240 gdb_test_no_output "set max-completions unlimited"
29241
@@ -377,12 +589,11 @@ proc_with_prefix template-ret-type {} {
377589 test_complete_prefix_range $complete_line $start
378590 }
379591
380- # Setting a breakpoint without the template params doesn't work.
381- check_setting_bp_fails "$cmd_prefix template2_fn"
382- # However, setting a breakpoint with template params and without
383- # the method params does work, just like with non-template
384- # functions. It also works with or without return type.
592+ # Setting a breakpoint with or without template params and without
593+ # the method params works, just like with non-template functions.
594+ # It also works with or without return type.
385595 foreach linespec [list \
596+ "template2_fn" \
386597 "${method_name}" \
387598 "${method_name}${param_list}" \
388599 "${struct_type}::${method_name}" \
@@ -396,6 +607,218 @@ proc_with_prefix template-ret-type {} {
396607 }
397608 }
398609
610+# Test completion of function template foo.
611+
612+proc_with_prefix template-function-foo {} {
613+
614+ foreach cmd_prefix {"b" "b -function"} {
615+ # "foo" is ambiguous, this will set many different breakpoints.
616+ set completion_list \
617+ [list \
618+ [makefoo Aabiaai] \
619+ [makefoo Aabiabi] \
620+ [makefoo Aabicdi] \
621+ [makefoo AabicdiAabiaai] \
622+ [makefoo AabicdiAabiabi] \
623+ [makefoo AabicdiBabicdi] \
624+ [makefoo Babicdi] \
625+ [makefoo aai] \
626+ [makefoo aaiabi] \
627+ [makefoo aaicci] \
628+ [makefoo aaicdi] \
629+ [makefoo abi] \
630+ [makefoo anabnb] \
631+ [makefoo cci] \
632+ [makefoo cdi] \
633+ [makefoo NAnanbNBnanb] \
634+ [makefoo nanb]]
635+ test_gdb_complete_multiple "$cmd_prefix " "foo" "<" $completion_list
636+ check_bp_locations_match_list "$cmd_prefix foo" $completion_list
637+
638+ # "foo<" should give the same result, but it should not set any
639+ # breakpoints.
640+ test_gdb_complete_multiple "$cmd_prefix " "foo<" "" $completion_list
641+ check_setting_bp_fails "$cmd_prefix foo<"
642+
643+ # "foo<A" should only give completions in COMPLETION_LIST that
644+ # start with "A" but should set no breakpoints.
645+ set completion_list \
646+ [list \
647+ [makefoo Aabiaai] \
648+ [makefoo Aabiabi] \
649+ [makefoo Aabicdi] \
650+ [makefoo AabicdiAabiaai] \
651+ [makefoo AabicdiAabiabi] \
652+ [makefoo AabicdiBabicdi]]
653+ test_gdb_complete_multiple "$cmd_prefix " "foo<A" "<a<b<int> >, " \
654+ $completion_list
655+ check_setting_bp_fails "$cmd_prefix foo<A"
656+
657+ # "foo<A>" should give any function with one parameter of any type
658+ # of A. While the parameter list in the template should be ignored,
659+ # the function's argument list should not be ignored.
660+ set completion_list \
661+ [list \
662+ [makefoo Aabiaai] \
663+ [makefoo Aabiabi] \
664+ [makefoo Aabicdi]]
665+ test_gdb_complete_multiple "$cmd_prefix " "foo<A>" \
666+ "(A<a<b<int> >, " $completion_list
667+ check_bp_locations_match_list "$cmd_prefix foo<A>" $completion_list
668+
669+ # "foo<A," should complete to any function with more than one
670+ # parameter where the first parameter is any type of A. Insufficient
671+ # location to set breakpoints.
672+ set completion_list \
673+ [list \
674+ [makefoo AabicdiAabiaai] \
675+ [makefoo AabicdiAabiabi] \
676+ [makefoo AabicdiBabicdi]]
677+ test_gdb_complete_multiple "$cmd_prefix " "foo<A," " " \
678+ $completion_list
679+ check_setting_bp_fails "$cmd_prefix foo<A,"
680+
681+ # "foo<A<a<b<int>, a" should give all completions starting with
682+ # "Aabia" but it is insufficient to set breakpoints.
683+ set completion_list \
684+ [list \
685+ [makefoo Aabiaai] \
686+ [makefoo Aabiabi]]
687+ test_gdb_complete_multiple "$cmd_prefix " "foo<A<a<b<int> >, a" \
688+ "<" $completion_list
689+ check_setting_bp_fails "$cmd_prefix foo<A<a<b<int> >, a"
690+
691+ # "foo<A<a<b<int>, a<" should yield the same results as above.
692+ test_gdb_complete_multiple "$cmd_prefix " "foo<A<a<b<int> >, a<" \
693+ "" $completion_list
694+ check_setting_bp_fails "$cmd_prefix foo<A<a<b<int> >, a<"
695+
696+ # "foo<A<a<b<int>, a<a" is unique but insufficient to set a
697+ # breakpoint. This has an ignored template parameter list, so
698+ # the completion will contain an ignored list ("a<a>")
699+ test_gdb_complete_unique "$cmd_prefix foo<A<a<b<int> >, a<a" \
700+ "$cmd_prefix [makefoo Aabiaai]"
701+ check_setting_bp_fails "$cmd_prefix foo<A<b<int> >, a<a"
702+
703+ # "foo<A<a<b<int>, a<b" is also unique. Same parameter ignoring
704+ # happens here, too (except "a<b>").
705+ test_gdb_complete_unique "$cmd_prefix foo<A<a<b<int> >, a<b" \
706+ "$cmd_prefix [makefoo Aabiabi]"
707+ check_setting_bp_fails "$cmd_prefix foo<A<a<b<int> >, a<b"
708+
709+ # "foo<B" is unique but insufficient to set a breakpoint.
710+ test_gdb_complete_unique "$cmd_prefix foo<B" \
711+ "$cmd_prefix [makefoo Babicdi]"
712+ check_setting_bp_fails "$cmd_prefix foo<B"
713+
714+ # "foo<B>" yields the same unique result and sets a breakpoint.
715+ # Since the input skips the parameter list, so does the completion.
716+ test_gdb_complete_unique "$cmd_prefix foo<B>" \
717+ "$cmd_prefix foo<B>(B<a<b<int> >, c<d<int> > >)"
718+ check_bp_locations_match_list "$cmd_prefix foo<B>" \
719+ [list [makefoo Babicdi]]
720+
721+ # "foo<B," should return no completions and no breakpoints.
722+ test_gdb_complete_none "$cmd_prefix foo<B,"
723+ check_setting_bp_fails "$cmd_prefix foo<B,"
724+
725+ # "foo<n::" should yield only the functions starting with
726+ # "n" and "N" and no breakpoints.
727+ set completion_list \
728+ [list \
729+ [makefoo NAnanbNBnanb] \
730+ [makefoo nanb]]
731+ test_gdb_complete_multiple "$cmd_prefix " "foo<n::" "" \
732+ $completion_list
733+ check_setting_bp_fails "$cmd_prefix foo<n::"
734+
735+ # "foo<A<a, c> >" is unique and sets a breakpoint.
736+ # Multiple template parameter lists are skipped, so GDB will ignore
737+ # them in the completion.
738+ test_gdb_complete_unique "$cmd_prefix foo<A<a, c> >" \
739+ "$cmd_prefix foo<A<a, c> >(A<a<b<int> >, c<d<int> > >)"
740+ check_bp_locations_match_list "$cmd_prefix foo<A<a, c> >" \
741+ [list [makefoo Aabicdi]]
742+ }
743+}
744+
745+# Helper for template-class-with-method to build completion lists.
746+
747+proc makem {arglist_list} {
748+ set completion_list {}
749+ foreach arglist $arglist_list {
750+ lappend completion_list "[maketype $arglist]::method()"
751+ }
752+ return $completion_list
753+}
754+
755+# Returns a list of elements that look like
756+# void TYPE::method()
757+# where TYPE is derived from each arglist in ARGLIST_LIST.
758+
759+proc test_makem_1 {arglist_list expected_list} {
760+ set result [makem $arglist_list]
761+ send_log "makem $arglist = $result\n"
762+ send_log "expecting $expected_list\n"
763+
764+ # Do list equality via canonical strings.
765+ if {[expr {[list {*}$expected_list] eq [list {*}$result]}]} {
766+ pass "makem unit test: $arglist"
767+ } else {
768+ fail "makem unit test: $arglist"
769+ }
770+}
771+
772+# Unit tests for makem.
773+
774+proc test_makem {} {
775+ test_makem_1 ai {"a<int>::method()"}
776+ test_makem_1 bi {"b<int>::method()"}
777+ test_makem_1 {ai bi} {"a<int>::method()" "b<int>::method()"}
778+ test_makem_1 {Aaiaai Bbibbi abi cdi} {
779+ "A<a<int>, a<a<int> > >::method()"
780+ "B<b<int>, b<b<int> > >::method()"
781+ "a<b<int> >::method()"
782+ "c<d<int> >::method()"
783+ }
784+}
785+
786+# Test class template containing a (non-templated) method called "method."
787+
788+proc_with_prefix template-class-with-method {} {
789+
790+ foreach {type type_list} \
791+ [list \
792+ "" {aai abi cci cdi Aabicdi Aabiaai Aabiabi Babicdi} \
793+ "a" {aai abi} \
794+ "b" {} \
795+ "c" {cci cdi} \
796+ "A" {Aabicdi Aabiaai Aabiabi} \
797+ "B" {Babicdi} \
798+ "A<a, a>" {Aabiaai Aabiabi} \
799+ "A<a<b>, c>" {Aabicdi}\
800+ "A<a, d>" {} \
801+ "B<a, a>" {} \
802+ "B<a, b>" {} \
803+ "B<a, c>" {Babicdi}] \
804+ {
805+ foreach cmd_prefix {"b" "b -function"} {
806+ set c "$cmd_prefix "
807+ if {$type != ""} {
808+ append c "${type}::"
809+ }
810+ append c "method"
811+
812+ if {[llength $type_list] > 0} {
813+ test_gdb_complete_unique $c "${c}()"
814+ check_bp_locations_match_list $c [makem $type_list]
815+ } else {
816+ test_gdb_complete_none $c
817+ }
818+ }
819+ }
820+}
821+
399822 # Test completion of a const-overloaded funtion (const-overload).
400823 # Note that "const" appears after the function/method parameters.
401824
@@ -935,6 +1358,10 @@ proc test_driver {} {
9351358 overload-3
9361359 template-overload
9371360 template-ret-type
1361+ #test_makefoo
1362+ template-function-foo
1363+ #test_makem
1364+ template-class-with-method
9381365 const-overload
9391366 const-overload-quoted
9401367 append-end-quote-char-when-unambiguous
--- a/gdb/testsuite/gdb.linespec/cpls-ops.exp
+++ b/gdb/testsuite/gdb.linespec/cpls-ops.exp
@@ -240,20 +240,6 @@ proc test_operator_ambiguous {class_name opn cls} {
240240 test_gdb_complete_multiple \
241241 "$cmd_prefix " "$linespec_noparams" "" $location_list
242242
243- # Setting the breakpoint doesn't create a breakpoint location
244- # for the template, because immediately after
245- # "operator()/operator[]" we have the template parameters, not
246- # the parameter list.
247- set location_list \
248- [list \
249- "${class_name}::operator${opn}${cls}(int)" \
250- "${class_name}::operator${opn}${cls}(long)"]
251- if {$opn == "("} {
252- set location_list \
253- [concat \
254- [list "${class_name}::operator${opn}${cls}()"] \
255- $location_list]
256- }
257243 check_bp_locations_match_list "$cmd_prefix $linespec_noparams" \
258244 $location_list
259245 check_bp_locations_match_list "$cmd_prefix $linespec_noparams<int>" \
@@ -261,26 +247,33 @@ proc test_operator_ambiguous {class_name opn cls} {
261247
262248 # Test the template version. Test both with and without
263249 # return type.
264- test_gdb_complete_unique \
265- "$cmd_prefix ${class_name}::operator${opn}${cls}<int>(in" \
266- "$cmd_prefix ${class_name}::operator${opn}${cls}<int>(int*)"
267- check_bp_locations_match_list \
268- "$cmd_prefix ${class_name}::operator${opn}${cls}<int>(int*)" \
269- [list "${class_name}::operator${opn}${cls}<int>(int*)"]
270- test_gdb_complete_unique \
271- "$cmd_prefix void ${class_name}::operator${opn}${cls}<int>(in" \
272- "$cmd_prefix void ${class_name}::operator${opn}${cls}<int>(int*)"
273- check_bp_locations_match_list \
274- "$cmd_prefix void ${class_name}::operator${opn}${cls}<int>(int*)" \
275- [list "${class_name}::operator${opn}${cls}<int>(int*)"]
250+ set f "${class_name}::operator"
251+ foreach ws1 {"" " "} {
252+ foreach ws2 {"" " "} {
253+ foreach ws3 {"" " "} {
254+ test_gdb_complete_unique \
255+ "$cmd_prefix ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(in" \
256+ "$cmd_prefix ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(int*)"
257+ check_bp_locations_match_list \
258+ "$cmd_prefix ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(int*)" \
259+ [list "${f}${opn}${cls}<int>(int*)"]
260+ test_gdb_complete_unique \
261+ "$cmd_prefix void ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(in" \
262+ "$cmd_prefix void ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(int*)"
263+ check_bp_locations_match_list \
264+ "$cmd_prefix void ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(int*)" \
265+ [list "${f}${opn}${cls}<int>(int*)"]
266+ }
267+ }
268+ }
276269
277270 # Add extra spaces.
278271 test_gdb_complete_unique \
279- "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( in" \
280- "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( int)"
272+ "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( lo" \
273+ "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( long)"
281274 check_bp_locations_match_list \
282- "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( int )" \
283- [list "${class_name}::operator${opn}${cls}(int)"]
275+ "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( long )" \
276+ [list "${class_name}::operator${opn}${cls}(long)"]
284277 }
285278 }
286279
--- a/gdb/testsuite/gdb.linespec/cpls.cc
+++ b/gdb/testsuite/gdb.linespec/cpls.cc
@@ -146,7 +146,7 @@ namespace ns_overload3_test
146146 }
147147 }
148148
149-/* Code for the template-overload tests. */
149+/* Code for the template-function_foo (template parameter completion) tests. */
150150
151151 template <typename T>
152152 struct template_struct
@@ -163,6 +163,113 @@ T template_struct<T>::template_overload_fn (T t)
163163 template_struct<int> template_struct_int;
164164 template_struct<long> template_struct_long;
165165
166+/* Code for the template-parameter-overload test. */
167+
168+template <typename T>
169+void foo (T c) {}
170+
171+template <typename T1, typename T2>
172+void foo (T1 a, T2 b) {}
173+
174+template <typename T>
175+struct a
176+{
177+ void method () {}
178+};
179+
180+template <typename T>
181+struct b
182+{
183+ void method () {}
184+};
185+
186+template <typename T>
187+struct c
188+{
189+ void method () {}
190+};
191+
192+template <typename T>
193+struct d
194+{
195+ void method () {};
196+};
197+
198+template <typename T1, typename T2>
199+struct A
200+{
201+ void method () {}
202+};
203+
204+template <typename T1, typename T2>
205+struct B
206+{
207+ void method () {}
208+};
209+
210+namespace n
211+{
212+ struct na {};
213+ struct nb {};
214+
215+ template <typename T1, typename T2>
216+ struct NA {};
217+
218+ template <typename T1, typename T2>
219+ struct NB {};
220+};
221+
222+static void
223+template_function_foo ()
224+{
225+ a<a<int>> aa;
226+ aa.method ();
227+ a<b<int>> ab;
228+ ab.method ();
229+ c<c<int>> cc;
230+ cc.method ();
231+ c<d<int>> cd;
232+ cd.method ();
233+ foo (aa);
234+ foo (ab);
235+ foo (cc);
236+ foo (cd);
237+ foo (aa, ab);
238+ foo (aa, cc);
239+ foo (aa, cd);
240+
241+ A<a<b<int>>, c<d<int>>> Aabcd;
242+ Aabcd.method ();
243+ foo (Aabcd);
244+
245+ A<a<b<int>>, a<a<int>>> Aabaa;
246+ Aabaa.method ();
247+ foo (Aabaa);
248+
249+ A<a<b<int>>, a<b<int>>> Aabab;
250+ Aabab.method ();
251+ foo (Aabab);
252+
253+ B<a<b<int>>, c<d<int>>> Babcd;
254+ Babcd.method ();
255+ foo (Babcd);
256+
257+ foo (Aabcd, Babcd);
258+ foo (Aabcd, Aabaa);
259+ foo (Aabcd, Aabab);
260+
261+ n::na na;
262+ n::nb nb;
263+ foo (na, nb);
264+ a<n::na> ana;
265+ b<n::nb> bnb;
266+ foo (ana, bnb);
267+
268+ n::NA<n::na, n::nb> NAnanb;
269+ n::NB<n::na, n::nb> Nbnanb;
270+ foo (NAnanb, Nbnanb);
271+}
272+
166273 /* Code for the template2-ret-type tests. */
167274
168275 template <typename T>
@@ -381,6 +488,7 @@ main ()
381488 template2_struct_inst.template2_fn<int, int> ();
382489 template_struct_int.template_overload_fn(0);
383490 template_struct_long.template_overload_fn(0);
491+ template_function_foo ();
384492
385493 return 0;
386494 }
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -2271,13 +2271,45 @@ skip_abi_tag (const char **name)
22712271 return false;
22722272 }
22732273
2274+/* If *NAME points at a template parameter list, skip it and return true.
2275+ Otherwise do nothing and return false. */
2276+
2277+static bool
2278+skip_template_parameter_list (const char **name)
2279+{
2280+ const char *p = *name;
2281+
2282+ if (*p == '<')
2283+ {
2284+ const char *template_param_list_end = find_toplevel_char (p + 1, '>');
2285+
2286+ if (template_param_list_end == NULL)
2287+ return false;
2288+
2289+ p = template_param_list_end + 1;
2290+
2291+ /* Skip any whitespace that might occur after the closing of the
2292+ parameter list, but only if it is the end of parameter list. */
2293+ const char *q = p;
2294+ while (isspace (*q))
2295+ ++q;
2296+ if (*q == '>')
2297+ p = q;
2298+ *name = p;
2299+ return true;
2300+ }
2301+
2302+ return false;
2303+}
2304+
22742305 /* See utils.h. */
22752306
22762307 int
22772308 strncmp_iw_with_mode (const char *string1, const char *string2,
22782309 size_t string2_len, strncmp_iw_mode mode,
22792310 enum language language,
2280- completion_match_for_lcd *match_for_lcd)
2311+ completion_match_for_lcd *match_for_lcd,
2312+ bool ignore_template_params)
22812313 {
22822314 const char *string1_start = string1;
22832315 const char *end_str2 = string2 + string2_len;
@@ -2327,6 +2359,48 @@ strncmp_iw_with_mode (const char *string1, const char *string2,
23272359 string1++;
23282360 }
23292361
2362+ /* Skip template parameters in STRING1 if STRING2 does not contain
2363+ any. E.g.:
2364+
2365+ Case 1: User is looking for all functions named "foo".
2366+ string1: foo <...> (...)
2367+ string2: foo
2368+
2369+ Case 2: User is looking for all methods named "foo" in all template
2370+ class instantiations.
2371+ string1: Foo<...>::foo <...> (...)
2372+ string2: Foo::foo (...)
2373+
2374+ Case 3: User is looking for a specific overload of a template
2375+ function or method.
2376+ string1: foo<...>
2377+ string2: foo(...)
2378+
2379+ Case 4: User is looking for a specific overload of a specific
2380+ template instantiation.
2381+ string1: foo<A> (...)
2382+ string2: foo<B> (...)
2383+
2384+ Case 5: User is looking wild parameter match.
2385+ string1: foo<A<a<b<...> > > > (...)
2386+ string2: foo<A
2387+ */
2388+ if (language == language_cplus && ignore_template_params
2389+ && *string1 == '<' && *string2 != '<')
2390+ {
2391+ /* Skip any parameter list in STRING1. */
2392+ const char *template_start = string1;
2393+
2394+ if (skip_template_parameter_list (&string1))
2395+ {
2396+ /* Don't mark the parameter list ignored if the user didn't
2397+ try to ignore it. [Case #5 above] */
2398+ if (*string2 != '\0'
2399+ && match_for_lcd != NULL && template_start != string1)
2400+ match_for_lcd->mark_ignored_range (template_start, string1);
2401+ }
2402+ }
2403+
23302404 if (*string1 == '\0' || string2 == end_str2)
23312405 break;
23322406
@@ -2435,6 +2509,12 @@ strncmp_iw_with_mode (const char *string1, const char *string2,
24352509 break;
24362510 if (*string1 == '(' || *string2 == '(')
24372511 break;
2512+
2513+ /* If STRING1 or STRING2 starts with a template
2514+ parameter list, break out of operator processing. */
2515+ skip_ws (string1, string2, end_str2);
2516+ if (*string1 == '<' || *string2 == '<')
2517+ break;
24382518 }
24392519
24402520 continue;
--- a/gdb/utils.h
+++ b/gdb/utils.h
@@ -57,11 +57,14 @@ enum class strncmp_iw_mode
5757
5858 MATCH_FOR_LCD is passed down so that the function can mark parts of
5959 the symbol name as ignored for completion matching purposes (e.g.,
60- to handle abi tags). */
60+ to handle abi tags). If IGNORE_TEMPLATE_PARAMS is true, all template
61+ parameter lists will be ignored when language is C++. */
62+
6163 extern int strncmp_iw_with_mode
6264 (const char *string1, const char *string2, size_t string2_len,
6365 strncmp_iw_mode mode, enum language language,
64- completion_match_for_lcd *match_for_lcd = NULL);
66+ completion_match_for_lcd *match_for_lcd = NULL,
67+ bool ignore_template_params = false);
6568
6669 /* Do a strncmp() type operation on STRING1 and STRING2, ignoring any
6770 differences in whitespace. STRING2_LEN is STRING2's length.