xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.linespec/cpls-ops.exp (revision 901e7e84758515fbf39dfc064cb0b45ab146d8b0)
1# Copyright 2017-2020 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
19
20standard_testfile cpls-ops.cc
21
22if {[prepare_for_testing "failed to prepare" $testfile \
23	 [list $srcfile] {debug}]} {
24    return -1
25}
26
27# Tests below are about tab-completion, which doesn't work if readline
28# library isn't used.  Check it first.
29
30if { ![readline_is_used] } {
31    untested "no tab completion support without readline"
32    return -1
33}
34
35gdb_test_no_output "set max-completions unlimited"
36
37# Check that the explicit location completer manages to find the next
38# option name after a "-function function" option.  A useful test when
39# the -function options's argument is a C++ operator, which can
40# include characters like '-'.
41
42proc check_explicit_skips_function_argument {function} {
43    test_gdb_complete_unique \
44	"b -function $function -sour" \
45	"b -function $function -source"
46}
47
48# Helper function for the operator new/new[] tests.  CLASS_NAME is the
49# name of the class that contains the operator we're testing.
50# BRACKETS is set to [] if testing operator new[], and to empty if
51# testing operator new.
52
53proc test_operator_new {class_name brackets} {
54    global gdb_prompt
55
56    # Extract the type size_t is typedef-ed to.
57    set size_t ""
58    set test "get size_t underlying type"
59    gdb_test_multiple "ptype size_t" $test {
60	-re " = (\[ a-z\]*)\r\n$gdb_prompt $" {
61	    set size_t $expect_out(1,string)
62	    pass "$test"
63	}
64    }
65
66    # Complete all prefixes between "operato" and the full prototype.
67    foreach cmd_prefix {"b" "b -function"} {
68	set location "${class_name}::operator new${brackets}($size_t)"
69	set line "$cmd_prefix $location"
70	set start [index_after "operato" $line]
71	test_complete_prefix_range $line $start
72	check_bp_locations_match_list "$cmd_prefix $location" [list $location]
73
74	# Same, but with extra spaces.  Note that the original spaces in
75	# the input line are preserved after completion.
76
77	test_gdb_complete_unique \
78	    "$cmd_prefix ${class_name}::operator new " \
79	    "$cmd_prefix ${class_name}::operator new ${brackets}($size_t)"
80	test_gdb_complete_unique \
81	    "$cmd_prefix ${class_name}::operator new ${brackets} (" \
82	    "$cmd_prefix ${class_name}::operator new ${brackets} ($size_t)"
83	test_gdb_complete_unique \
84	    "$cmd_prefix ${class_name}::operator new ${brackets} ( $size_t " \
85	    "$cmd_prefix ${class_name}::operator new ${brackets} ( $size_t )"
86
87	check_setting_bp_fails "$cmd_prefix ${class_name}::operator"
88
89	set location_list \
90	    [list \
91		 "${class_name}::operator new${brackets}" \
92		 "${class_name}::operator new${brackets} ($size_t)" \
93		 "${class_name}::operator new ${brackets} ( $size_t )"]
94	foreach linespec $location_list {
95	    check_bp_locations_match_list \
96		"$cmd_prefix $linespec" [list $location]
97	}
98    }
99
100    # Check that the explicit location completer manages to find the
101    # option name after -function, when the -function's argument is a
102    # C++ operator new / new[].
103    check_explicit_skips_function_argument \
104	"${class_name}::operator new ${brackets} ( $size_t )"
105}
106
107proc_with_prefix operator-new {} {
108    test_operator_new test_op_new ""
109}
110
111proc_with_prefix operator-new\[\] {} {
112    test_operator_new test_op_new_array "\[\]"
113}
114
115# Helper function for the operator delete/delete[] tests.  CLASS_NAME
116# is the name of the class that contains the operator we're testing.
117# BRACKETS is set to "[]" if testing operator delete[], and to empty
118# if testing operator delete.
119
120proc test_operator_delete {class_name brackets} {
121    # Complete all prefixes between "operato" and the full prototype.
122    foreach cmd_prefix {"b" "b -function"} {
123	set location "${class_name}::operator delete${brackets}(void*)"
124	set line "$cmd_prefix $location"
125	set start [index_after "operato" $line]
126	test_complete_prefix_range $line $start
127	check_bp_locations_match_list "$cmd_prefix $location" [list $location]
128
129	# Same, but with extra spaces.  Note that the original spaces in
130	# the input line are preserved after completion.
131
132	test_gdb_complete_unique \
133	    "$cmd_prefix ${class_name}::operator delete " \
134	    "$cmd_prefix ${class_name}::operator delete ${brackets}(void*)"
135	test_gdb_complete_unique \
136	    "$cmd_prefix ${class_name}::operator delete ${brackets} (" \
137	    "$cmd_prefix ${class_name}::operator delete ${brackets} (void*)"
138	test_gdb_complete_unique \
139	    "$cmd_prefix ${class_name}::operator delete ${brackets} ( void* " \
140	    "$cmd_prefix ${class_name}::operator delete ${brackets} ( void* )"
141	test_gdb_complete_unique \
142	    "$cmd_prefix ${class_name}::operator delete ${brackets} ( void * " \
143	    "$cmd_prefix ${class_name}::operator delete ${brackets} ( void * )"
144
145	check_setting_bp_fails "$cmd_prefix ${class_name}::operator"
146
147	set location_list \
148	    [list \
149		 "${class_name}::operator delete${brackets}" \
150		 "${class_name}::operator delete${brackets}(void *)" \
151		 "${class_name}::operator delete ${brackets} ( void * )"]
152	foreach linespec $location_list {
153	    check_bp_locations_match_list \
154		"$cmd_prefix $linespec" [list $location]
155	}
156    }
157
158    # Check that the explicit location completer manages to find the
159    # option name after -function, when the -function's argument is a
160    # C++ operator delete / delete[].
161    check_explicit_skips_function_argument \
162	"${class_name}::operator delete ${brackets} ( void * )"
163}
164
165proc_with_prefix operator-delete {} {
166    test_operator_delete test_op_delete ""
167}
168
169proc_with_prefix operator-delete\[\] {} {
170    test_operator_delete test_op_delete_array "\[\]"
171}
172
173# Helper for testing both operator() and operator[].  Tests completion
174# when the operator match is unique.  CLASS_NAME is the class that
175# holds the operator to test.  OPN and CLS are the open and close
176# characters ("()" or "[]").
177
178proc test_operator_unique {class_name opn cls} {
179    # Complete all prefixes between "oper" and the full prototype.
180    foreach cmd_prefix {"b" "b -function"} {
181	set location "${class_name}::operator${opn}${cls}(int)"
182	set line "$cmd_prefix $location"
183	set start [index_after "${class_name}" $line]
184	test_complete_prefix_range $line $start
185	check_bp_locations_match_list "$cmd_prefix $location" [list $location]
186
187	# Same, but with extra spaces.  Note that the original spaces in
188	# the input line are preserved after completion.
189
190	test_gdb_complete_unique \
191	    "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( int " \
192	    "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( int )"
193	test_gdb_complete_unique \
194	    "$cmd_prefix ${class_name}::operator ${opn} ${cls}" \
195	    "$cmd_prefix ${class_name}::operator ${opn} ${cls}(int)"
196	test_gdb_complete_unique \
197	    "$cmd_prefix ${class_name}::operator ${opn}${cls}" \
198	    "$cmd_prefix ${class_name}::operator ${opn}${cls}(int)"
199	test_gdb_complete_unique \
200	    "$cmd_prefix ${class_name}::operator ${opn}" \
201	    "$cmd_prefix ${class_name}::operator ${opn}${cls}(int)"
202
203	check_setting_bp_fails "$cmd_prefix ${class_name}::operator"
204
205	set location_list \
206	    [list \
207		 "${class_name}::operator${opn}${cls}" \
208		 "${class_name}::operator ${opn}${cls}" \
209		 "${class_name}::operator ${opn}${cls}(int)" \
210		 "${class_name}::operator ${opn} ${cls} ( int )"]
211	foreach linespec $location_list {
212	    check_bp_locations_match_list \
213		"$cmd_prefix $linespec" [list $location]
214	}
215    }
216
217    # Check that the explicit location completer manages to find the
218    # option name after -function, when the -function's argument is a
219    # C++ operator().
220    check_explicit_skips_function_argument \
221	"${class_name}::operator ${opn} ${cls} ( int )"
222}
223
224# Helper for testing both operator() and operator[].  Tests completion
225# when the operator match is ambiguous.  CLASS_NAME is the class that
226# holds the operator to test.  OPN and CLS are the open and close
227# characters ("()" or "[]").
228
229proc test_operator_ambiguous {class_name opn cls} {
230    foreach cmd_prefix {"b" "b -function"} {
231	check_setting_bp_fails "$cmd_prefix ${class_name}::operator"
232
233	set linespec_noparams "${class_name}::operator${opn}${cls}"
234
235	set location_list \
236	    [list \
237		 "${class_name}::operator${opn}${cls}(int)" \
238		 "${class_name}::operator${opn}${cls}(long)" \
239		 "${class_name}::operator${opn}${cls}<int>(int*)"]
240	# The operator[] test can't have a "()" overload, since that
241	# wouldn't compile.
242	if {$opn == "("} {
243	    set location_list \
244		[concat \
245		     [list "${class_name}::operator${opn}${cls}()"] \
246		     $location_list]
247	}
248	test_gdb_complete_multiple \
249	    "$cmd_prefix " "$linespec_noparams" "" $location_list
250
251	# Setting the breakpoint doesn't create a breakpoint location
252	# for the template, because immediately after
253	# "operator()/operator[]" we have the template parameters, not
254	# the parameter list.
255	set location_list \
256	    [list \
257		 "${class_name}::operator${opn}${cls}(int)" \
258		 "${class_name}::operator${opn}${cls}(long)"]
259	if {$opn == "("} {
260	    set location_list \
261		[concat \
262		     [list "${class_name}::operator${opn}${cls}()"] \
263		     $location_list]
264	}
265	check_bp_locations_match_list "$cmd_prefix $linespec_noparams" \
266	    $location_list
267	check_bp_locations_match_list "$cmd_prefix $linespec_noparams<int>" \
268	    [list "${class_name}::operator${opn}${cls}<int>(int*)"]
269
270	# Test the template version.  Test both with and without
271	# return type.
272	test_gdb_complete_unique \
273	    "$cmd_prefix ${class_name}::operator${opn}${cls}<int>(in" \
274	    "$cmd_prefix ${class_name}::operator${opn}${cls}<int>(int*)"
275	check_bp_locations_match_list \
276	    "$cmd_prefix ${class_name}::operator${opn}${cls}<int>(int*)" \
277	    [list "${class_name}::operator${opn}${cls}<int>(int*)"]
278	test_gdb_complete_unique \
279	    "$cmd_prefix void ${class_name}::operator${opn}${cls}<int>(in" \
280	    "$cmd_prefix void ${class_name}::operator${opn}${cls}<int>(int*)"
281	check_bp_locations_match_list \
282	    "$cmd_prefix void ${class_name}::operator${opn}${cls}<int>(int*)" \
283	    [list "${class_name}::operator${opn}${cls}<int>(int*)"]
284
285	# Add extra spaces.
286	test_gdb_complete_unique \
287	    "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( in" \
288	    "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( int)"
289	check_bp_locations_match_list \
290	    "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( int )" \
291	    [list "${class_name}::operator${opn}${cls}(int)"]
292    }
293}
294
295proc_with_prefix operator()-unique {} {
296    test_operator_unique test_unique_op_call "(" ")"
297}
298
299proc_with_prefix operator\[\]-unique {} {
300    test_operator_unique test_unique_op_array "\[" "\]"
301}
302
303proc_with_prefix operator()-ambiguous {} {
304    test_operator_ambiguous test_op_call "(" ")"
305}
306
307proc_with_prefix operator\[\]-ambiguous {} {
308    test_operator_ambiguous test_op_array "\[" "\]"
309}
310
311# Test arithmetic/logical operators.  Test completing all C++
312# arithmetic/logical operators, when all the operators are in the same
313# class.
314
315proc_with_prefix ops-valid-ambiguous {} {
316    set locations {
317	"test_ops::operator!(E)"
318	"test_ops::operator!=(E, E)"
319	"test_ops::operator%(E, E)"
320	"test_ops::operator%=(E, E)"
321	"test_ops::operator&&(E, E)"
322	"test_ops::operator&(E, E)"
323	"test_ops::operator&=(E, E)"
324	"test_ops::operator*(E, E)"
325	"test_ops::operator*=(E, E)"
326	"test_ops::operator+(E, E)"
327	"test_ops::operator++(E)"
328	"test_ops::operator++(E, int)"
329	"test_ops::operator+=(E, E)"
330	"test_ops::operator,(E, E)"
331	"test_ops::operator-(E, E)"
332	"test_ops::operator--(E)"
333	"test_ops::operator--(E, int)"
334	"test_ops::operator-=(E, E)"
335	"test_ops::operator/(E, E)"
336	"test_ops::operator/=(E, E)"
337	"test_ops::operator<(E, E)"
338	"test_ops::operator<<(E, E)"
339	"test_ops::operator<<=(E, E)"
340	"test_ops::operator<=(E, E)"
341	"test_ops::operator==(E, E)"
342	"test_ops::operator>(E, E)"
343	"test_ops::operator>=(E, E)"
344	"test_ops::operator>>(E, E)"
345	"test_ops::operator>>=(E, E)"
346	"test_ops::operator^(E, E)"
347	"test_ops::operator^=(E, E)"
348	"test_ops::operator|(E, E)"
349	"test_ops::operator|=(E, E)"
350	"test_ops::operator||(E, E)"
351	"test_ops::operator~(E)"
352    }
353    foreach linespec $locations {
354	foreach cmd_prefix {"b" "b -function"} {
355	    test_gdb_complete_unique \
356		"$cmd_prefix $linespec" \
357		"$cmd_prefix $linespec"
358
359	}
360
361	check_explicit_skips_function_argument "$linespec"
362    }
363
364    foreach cmd_prefix {"b" "b -function"} {
365	test_gdb_complete_multiple \
366	    "$cmd_prefix " "test_ops::operator" "" $locations
367    }
368}
369
370# Test completing all C++ operators, with and without spaces.  The
371# test without spaces makes sure the completion matches exactly the
372# expected prototype.  The version with whitespace is a bit more lax
373# for simplicity.  In that case, we only make sure we get back the
374# terminating ')'.  Each operator is defined in a separate class so
375# that we can exercise unique completion matches.
376
377proc_with_prefix ops-valid-unique {} {
378    set locations {
379	"test_op_BIT_AND::operator&(E, E)"
380	"test_op_BIT_AND_A::operator&=(E, E)"
381	"test_op_BIT_O::operator|(E, E)"
382	"test_op_COMMA::operator,(E, E)"
383	"test_op_DIV::operator/(E, E)"
384	"test_op_DIV_A::operator/=(E, E)"
385	"test_op_EQ::operator==(E, E)"
386	"test_op_GT::operator>(E, E)"
387	"test_op_GTE::operator>=(E, E)"
388	"test_op_LAND::operator&&(E, E)"
389	"test_op_LOR::operator||(E, E)"
390	"test_op_LT::operator<(E, E)"
391	"test_op_LTE::operator<=(E, E)"
392	"test_op_MINUS::operator-(E, E)"
393	"test_op_MINUS_A::operator-=(E, E)"
394	"test_op_MOD::operator%(E, E)"
395	"test_op_MOD_A::operator%=(E, E)"
396	"test_op_MUL::operator*(E, E)"
397	"test_op_MUL_A::operator*=(E, E)"
398	"test_op_NEG::operator~(E)"
399	"test_op_NEQ::operator!=(E, E)"
400	"test_op_NOT::operator!(E)"
401	"test_op_OE::operator|=(E, E)"
402	"test_op_PLUS::operator+(E, E)"
403	"test_op_PLUS_A::operator+=(E, E)"
404	"test_op_POST_DEC::operator--(E, int)"
405	"test_op_POST_INC::operator++(E, int)"
406	"test_op_PRE_DEC::operator--(E)"
407	"test_op_PRE_INC::operator++(E)"
408	"test_op_SL::operator<<(E, E)"
409	"test_op_SL_A::operator<<=(E, E)"
410	"test_op_SR::operator>>(E, E)"
411	"test_op_SR_A::operator>>=(E, E)"
412	"test_op_XOR::operator^(E, E)"
413	"test_op_XOR_A::operator^=(E, E)"
414    }
415    set linespecs_ws {
416	"test_op_BIT_AND::operator & ( E , E )"
417	"test_op_BIT_AND_A::operator &= ( E , E )"
418	"test_op_BIT_O::operator | (E , E )"
419	"test_op_COMMA::operator , ( E , E )"
420	"test_op_DIV::operator / (E , E )"
421	"test_op_DIV_A::operator /= ( E , E )"
422	"test_op_EQ::operator == ( E , E )"
423	"test_op_GT::operator > ( E , E )"
424	"test_op_GTE::operator >= ( E , E )"
425	"test_op_LAND::operator && ( E , E )"
426	"test_op_LOR::operator || ( E , E )"
427	"test_op_LT::operator < ( E , E )"
428	"test_op_LTE::operator <= ( E , E )"
429	"test_op_MINUS::operator - ( E , E )"
430	"test_op_MINUS_A::operator -= ( E , E )"
431	"test_op_MOD::operator % ( E , E )"
432	"test_op_MOD_A::operator %= ( E , E )"
433	"test_op_MUL::operator * ( E , E )"
434	"test_op_MUL_A::operator *= ( E , E )"
435	"test_op_NEG::operator ~ ( E )"
436	"test_op_NEQ::operator != ( E , E )"
437	"test_op_NOT::operator ! ( E )"
438	"test_op_OE::operator |= ( E , E )"
439	"test_op_PLUS::operator + ( E , E )"
440	"test_op_PLUS_A::operator += ( E , E )"
441	"test_op_POST_DEC::operator -- ( E , int )"
442	"test_op_POST_INC::operator ++ ( E , int )"
443	"test_op_PRE_DEC::operator -- ( E )"
444	"test_op_PRE_INC::operator ++ ( E )"
445	"test_op_SL::operator << ( E , E )"
446	"test_op_SL_A::operator <<= ( E , E )"
447	"test_op_SR::operator >> ( E , E )"
448	"test_op_SR_A::operator >>= ( E , E )"
449	"test_op_XOR::operator ^ ( E , E )"
450	"test_op_XOR_A::operator ^= ( E , E )"
451    }
452    foreach linespec $locations linespec_ws $linespecs_ws {
453	foreach cmd_prefix {"b" "b -function"} {
454	    with_test_prefix "no-whitespace" {
455		set line "$cmd_prefix $linespec"
456		set start [index_after "::operato" $line]
457		test_complete_prefix_range $line $start
458	    }
459
460	    with_test_prefix "whitespace" {
461		set line_ws "$cmd_prefix $linespec_ws"
462		set start_ws [index_after "::operator " $line_ws]
463		test_complete_prefix_range_re \
464		    $line_ws "$cmd_prefix test_op_.*::operator .*\\\)" $start_ws
465	    }
466	}
467
468	check_explicit_skips_function_argument "$linespec"
469	check_explicit_skips_function_argument "$linespec_ws"
470    }
471}
472
473# Test completing an invalid (whitespace at the wrong place) operator
474# name.
475
476proc_with_prefix ops-invalid {} {
477    foreach linespec {
478	"test_op_BIT_AND_A::operator& =(E, E)"
479	"test_op_DIV_A::operator/ =(E, E)"
480	"test_op_EQ::operator= =(E, E)"
481	"test_op_GTE::operator> =(E, E)"
482	"test_op_LAND::operator& &(E, E)"
483	"test_op_LOR::operator| |(E, E)"
484	"test_op_LTE::operator< =(E, E)"
485	"test_op_MINUS_A::operator- =(E, E)"
486	"test_op_MOD_A::operator% =(E, E)"
487	"test_op_MUL_A::operator* =(E, E)"
488	"test_op_NEQ::operator! =(E, E)"
489	"test_op_OE::operator| =(E, E)"
490	"test_op_PLUS_A::operator+ =(E, E)"
491	"test_op_POST_DEC::operator- -(E, int)"
492	"test_op_POST_INC::operator+ +(E, int)"
493	"test_op_PRE_DEC::operator- -(E)"
494	"test_op_PRE_INC::operator+ +(E)"
495	"test_op_SL::operator< <(E, E)"
496	"test_op_SL_A::operator< < =(E, E)"
497	"test_op_SR::operator> >(E, E)"
498	"test_op_SR_A::operator> > =(E, E)"
499	"test_op_XOR_A::operator^ =(E, E)"
500    } {
501	foreach cmd_prefix {"b" "b -function"} {
502	    test_gdb_complete_tab_none "$cmd_prefix $linespec"
503	    check_setting_bp_fails "$cmd_prefix $linespec"
504	}
505    }
506}
507
508# Test completing function/method FUNCTION.  Completion is tested at
509# every point starting after START_AFTER.  FUNCTION_WS is a version of
510# FUNCTION with extra (but valid) whitespace.  FUNCTION_INVALID is a
511# version of FUNCTION with invalid whitespace.  Tests that completion
512# of FUNCTION_WS completes to self, and that a completion of
513# FUNCTION_INVALID fails.
514
515proc test_function {function start_after function_ws {function_invalid ""}} {
516    foreach cmd_prefix {"b" "b -function"} {
517	set line "$cmd_prefix $function"
518	set start [index_after $start_after $line]
519	test_complete_prefix_range $line $start
520    }
521
522    check_explicit_skips_function_argument $function
523    check_explicit_skips_function_argument $function_ws
524
525    foreach cmd_prefix {"b" "b -function"} {
526	test_gdb_complete_unique \
527	    "$cmd_prefix $function_ws" \
528	    "$cmd_prefix $function_ws"
529	if {$function_invalid != ""} {
530	    test_gdb_complete_tab_none "$cmd_prefix $function_invalid"
531	    check_setting_bp_fails "$cmd_prefix $function_invalid"
532	}
533    }
534}
535
536# Test completing a user-defined conversion operator.
537
538proc_with_prefix conversion-operator {} {
539    test_function \
540	"test_op_conversion::operator test_op_conversion_res const volatile**() const volatile" \
541	"test_op_conversio" \
542	"test_op_conversion::operator test_op_conversion_res const volatile * * ( ) const volatile"}
543
544# Test completing an assignment operator.
545
546proc_with_prefix assignment-operator {} {
547    test_function \
548	"test_op_assign::operator=(test_op_assign const&)" \
549	"test_op_assig" \
550	"test_op_assign::operator = ( test_op_assign const & )" \
551}
552
553# Test completing an arrow operator.
554
555proc_with_prefix arrow-operator {} {
556    test_function \
557	"test_op_arrow::operator->()" \
558	"test_op_arro" \
559	"test_op_arrow::operator -> ( )" \
560	"test_op_arrow::operator - > ( )"
561}
562
563# The testcase driver.  Calls all test procedures.
564
565proc test_driver {} {
566    operator-delete
567    operator-delete\[\]
568    operator-new
569    operator-new\[\]
570    operator()-unique
571    operator()-ambiguous
572    operator\[\]-unique
573    operator\[\]-ambiguous
574    ops-valid-ambiguous
575    ops-valid-unique
576    ops-invalid
577    conversion-operator
578    assignment-operator
579    arrow-operator
580}
581
582test_driver
583