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
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	check_bp_locations_match_list "$cmd_prefix $linespec_noparams" \
252	    $location_list
253	check_bp_locations_match_list "$cmd_prefix $linespec_noparams<int>" \
254	    [list "${class_name}::operator${opn}${cls}<int>(int*)"]
255
256	# Test the template version.  Test both with and without
257	# return type.
258	set f "${class_name}::operator"
259	foreach ws1 {"" " "} {
260	    foreach ws2 {"" " "} {
261		foreach ws3 {"" " "} {
262		    test_gdb_complete_unique \
263			"$cmd_prefix ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(in" \
264			"$cmd_prefix ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(int*)"
265		    check_bp_locations_match_list \
266			"$cmd_prefix ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(int*)" \
267			[list "${f}${opn}${cls}<int>(int*)"]
268		    test_gdb_complete_unique \
269			"$cmd_prefix void ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(in" \
270			"$cmd_prefix void ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(int*)"
271		    check_bp_locations_match_list \
272			"$cmd_prefix void ${f}${opn}${ws1}${cls}<${ws2}int${ws3}>(int*)" \
273			[list "${f}${opn}${cls}<int>(int*)"]
274		}
275	    }
276	}
277
278	# Add extra spaces.
279	test_gdb_complete_unique \
280	    "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( lo" \
281	    "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( long)"
282	check_bp_locations_match_list \
283	    "$cmd_prefix ${class_name}::operator ${opn} ${cls} ( long )" \
284	    [list "${class_name}::operator${opn}${cls}(long)"]
285    }
286}
287
288proc_with_prefix operator()-unique {} {
289    test_operator_unique test_unique_op_call "(" ")"
290}
291
292proc_with_prefix operator\[\]-unique {} {
293    test_operator_unique test_unique_op_array "\[" "\]"
294}
295
296proc_with_prefix operator()-ambiguous {} {
297    test_operator_ambiguous test_op_call "(" ")"
298}
299
300proc_with_prefix operator\[\]-ambiguous {} {
301    test_operator_ambiguous test_op_array "\[" "\]"
302}
303
304# Test arithmetic/logical operators.  Test completing all C++
305# arithmetic/logical operators, when all the operators are in the same
306# class.
307
308proc_with_prefix ops-valid-ambiguous {} {
309    set locations {
310	"test_ops::operator!(E)"
311	"test_ops::operator!=(E, E)"
312	"test_ops::operator%(E, E)"
313	"test_ops::operator%=(E, E)"
314	"test_ops::operator&&(E, E)"
315	"test_ops::operator&(E, E)"
316	"test_ops::operator&=(E, E)"
317	"test_ops::operator*(E, E)"
318	"test_ops::operator*=(E, E)"
319	"test_ops::operator+(E, E)"
320	"test_ops::operator++(E)"
321	"test_ops::operator++(E, int)"
322	"test_ops::operator+=(E, E)"
323	"test_ops::operator,(E, E)"
324	"test_ops::operator-(E, E)"
325	"test_ops::operator--(E)"
326	"test_ops::operator--(E, int)"
327	"test_ops::operator-=(E, E)"
328	"test_ops::operator/(E, E)"
329	"test_ops::operator/=(E, E)"
330	"test_ops::operator<(E, E)"
331	"test_ops::operator<<(E, E)"
332	"test_ops::operator<<=(E, E)"
333	"test_ops::operator<=(E, E)"
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)"
345    }
346    foreach linespec $locations {
347	foreach cmd_prefix {"b" "b -function"} {
348	    test_gdb_complete_unique \
349		"$cmd_prefix $linespec" \
350		"$cmd_prefix $linespec"
351
352	}
353
354	check_explicit_skips_function_argument "$linespec"
355    }
356
357    foreach cmd_prefix {"b" "b -function"} {
358	test_gdb_complete_multiple \
359	    "$cmd_prefix " "test_ops::operator" "" $locations
360    }
361}
362
363# Test completing all C++ operators, with and without spaces.  The
364# test without spaces makes sure the completion matches exactly the
365# expected prototype.  The version with whitespace is a bit more lax
366# for simplicity.  In that case, we only make sure we get back the
367# terminating ')'.  Each operator is defined in a separate class so
368# that we can exercise unique completion matches.
369
370proc_with_prefix ops-valid-unique {} {
371    set locations {
372	"test_op_BIT_AND::operator&(E, E)"
373	"test_op_BIT_AND_A::operator&=(E, E)"
374	"test_op_BIT_O::operator|(E, E)"
375	"test_op_COMMA::operator,(E, E)"
376	"test_op_DIV::operator/(E, E)"
377	"test_op_DIV_A::operator/=(E, E)"
378	"test_op_EQ::operator==(E, E)"
379	"test_op_GT::operator>(E, E)"
380	"test_op_GTE::operator>=(E, E)"
381	"test_op_LAND::operator&&(E, E)"
382	"test_op_LOR::operator||(E, E)"
383	"test_op_LT::operator<(E, E)"
384	"test_op_LTE::operator<=(E, E)"
385	"test_op_MINUS::operator-(E, E)"
386	"test_op_MINUS_A::operator-=(E, E)"
387	"test_op_MOD::operator%(E, E)"
388	"test_op_MOD_A::operator%=(E, E)"
389	"test_op_MUL::operator*(E, E)"
390	"test_op_MUL_A::operator*=(E, E)"
391	"test_op_NEG::operator~(E)"
392	"test_op_NEQ::operator!=(E, E)"
393	"test_op_NOT::operator!(E)"
394	"test_op_OE::operator|=(E, E)"
395	"test_op_PLUS::operator+(E, E)"
396	"test_op_PLUS_A::operator+=(E, E)"
397	"test_op_POST_DEC::operator--(E, int)"
398	"test_op_POST_INC::operator++(E, int)"
399	"test_op_PRE_DEC::operator--(E)"
400	"test_op_PRE_INC::operator++(E)"
401	"test_op_SL::operator<<(E, E)"
402	"test_op_SL_A::operator<<=(E, E)"
403	"test_op_SR::operator>>(E, E)"
404	"test_op_SR_A::operator>>=(E, E)"
405	"test_op_XOR::operator^(E, E)"
406	"test_op_XOR_A::operator^=(E, E)"
407    }
408    set linespecs_ws {
409	"test_op_BIT_AND::operator & ( E , E )"
410	"test_op_BIT_AND_A::operator &= ( E , E )"
411	"test_op_BIT_O::operator | (E , E )"
412	"test_op_COMMA::operator , ( E , E )"
413	"test_op_DIV::operator / (E , E )"
414	"test_op_DIV_A::operator /= ( E , E )"
415	"test_op_EQ::operator == ( E , E )"
416	"test_op_GT::operator > ( E , E )"
417	"test_op_GTE::operator >= ( E , E )"
418	"test_op_LAND::operator && ( E , E )"
419	"test_op_LOR::operator || ( E , E )"
420	"test_op_LT::operator < ( E , E )"
421	"test_op_LTE::operator <= ( E , E )"
422	"test_op_MINUS::operator - ( E , E )"
423	"test_op_MINUS_A::operator -= ( E , E )"
424	"test_op_MOD::operator % ( E , E )"
425	"test_op_MOD_A::operator %= ( E , E )"
426	"test_op_MUL::operator * ( E , E )"
427	"test_op_MUL_A::operator *= ( E , E )"
428	"test_op_NEG::operator ~ ( E )"
429	"test_op_NEQ::operator != ( E , E )"
430	"test_op_NOT::operator ! ( E )"
431	"test_op_OE::operator |= ( E , E )"
432	"test_op_PLUS::operator + ( E , E )"
433	"test_op_PLUS_A::operator += ( E , E )"
434	"test_op_POST_DEC::operator -- ( E , int )"
435	"test_op_POST_INC::operator ++ ( E , int )"
436	"test_op_PRE_DEC::operator -- ( E )"
437	"test_op_PRE_INC::operator ++ ( E )"
438	"test_op_SL::operator << ( E , E )"
439	"test_op_SL_A::operator <<= ( E , E )"
440	"test_op_SR::operator >> ( E , E )"
441	"test_op_SR_A::operator >>= ( E , E )"
442	"test_op_XOR::operator ^ ( E , E )"
443	"test_op_XOR_A::operator ^= ( E , E )"
444    }
445    foreach linespec $locations linespec_ws $linespecs_ws {
446	foreach cmd_prefix {"b" "b -function"} {
447	    with_test_prefix "no-whitespace" {
448		set line "$cmd_prefix $linespec"
449		set start [index_after "::operato" $line]
450		test_complete_prefix_range $line $start
451	    }
452
453	    with_test_prefix "whitespace" {
454		set line_ws "$cmd_prefix $linespec_ws"
455		set start_ws [index_after "::operator " $line_ws]
456		test_complete_prefix_range_re \
457		    $line_ws "$cmd_prefix test_op_.*::operator .*\\\)" $start_ws
458	    }
459	}
460
461	check_explicit_skips_function_argument "$linespec"
462	check_explicit_skips_function_argument "$linespec_ws"
463    }
464}
465
466# Test completing an invalid (whitespace at the wrong place) operator
467# name.
468
469proc_with_prefix ops-invalid {} {
470    foreach linespec {
471	"test_op_BIT_AND_A::operator& =(E, E)"
472	"test_op_DIV_A::operator/ =(E, E)"
473	"test_op_EQ::operator= =(E, E)"
474	"test_op_GTE::operator> =(E, E)"
475	"test_op_LAND::operator& &(E, E)"
476	"test_op_LOR::operator| |(E, E)"
477	"test_op_LTE::operator< =(E, E)"
478	"test_op_MINUS_A::operator- =(E, E)"
479	"test_op_MOD_A::operator% =(E, E)"
480	"test_op_MUL_A::operator* =(E, E)"
481	"test_op_NEQ::operator! =(E, E)"
482	"test_op_OE::operator| =(E, E)"
483	"test_op_PLUS_A::operator+ =(E, E)"
484	"test_op_POST_DEC::operator- -(E, int)"
485	"test_op_POST_INC::operator+ +(E, int)"
486	"test_op_PRE_DEC::operator- -(E)"
487	"test_op_PRE_INC::operator+ +(E)"
488	"test_op_SL::operator< <(E, E)"
489	"test_op_SL_A::operator< < =(E, E)"
490	"test_op_SR::operator> >(E, E)"
491	"test_op_SR_A::operator> > =(E, E)"
492	"test_op_XOR_A::operator^ =(E, E)"
493    } {
494	foreach cmd_prefix {"b" "b -function"} {
495	    test_gdb_complete_tab_none "$cmd_prefix $linespec"
496	    check_setting_bp_fails "$cmd_prefix $linespec"
497	}
498    }
499}
500
501# Test completing function/method FUNCTION.  Completion is tested at
502# every point starting after START_AFTER.  FUNCTION_WS is a version of
503# FUNCTION with extra (but valid) whitespace.  FUNCTION_INVALID is a
504# version of FUNCTION with invalid whitespace.  Tests that completion
505# of FUNCTION_WS completes to self, and that a completion of
506# FUNCTION_INVALID fails.
507
508proc test_function {function start_after function_ws {function_invalid ""}} {
509    foreach cmd_prefix {"b" "b -function"} {
510	set line "$cmd_prefix $function"
511	set start [index_after $start_after $line]
512	test_complete_prefix_range $line $start
513    }
514
515    check_explicit_skips_function_argument $function
516    check_explicit_skips_function_argument $function_ws
517
518    foreach cmd_prefix {"b" "b -function"} {
519	test_gdb_complete_unique \
520	    "$cmd_prefix $function_ws" \
521	    "$cmd_prefix $function_ws"
522	if {$function_invalid != ""} {
523	    test_gdb_complete_tab_none "$cmd_prefix $function_invalid"
524	    check_setting_bp_fails "$cmd_prefix $function_invalid"
525	}
526    }
527}
528
529# Test completing a user-defined conversion operator.
530
531proc_with_prefix conversion-operator {} {
532    test_function \
533	"test_op_conversion::operator test_op_conversion_res const volatile**() const volatile" \
534	"test_op_conversio" \
535	"test_op_conversion::operator test_op_conversion_res const volatile * * ( ) const volatile"}
536
537# Test completing an assignment operator.
538
539proc_with_prefix assignment-operator {} {
540    test_function \
541	"test_op_assign::operator=(test_op_assign const&)" \
542	"test_op_assig" \
543	"test_op_assign::operator = ( test_op_assign const & )" \
544}
545
546# Test completing an arrow operator.
547
548proc_with_prefix arrow-operator {} {
549    test_function \
550	"test_op_arrow::operator->()" \
551	"test_op_arro" \
552	"test_op_arrow::operator -> ( )" \
553	"test_op_arrow::operator - > ( )"
554}
555
556# The testcase driver.  Calls all test procedures.
557
558proc test_driver {} {
559    operator-delete
560    operator-delete\[\]
561    operator-new
562    operator-new\[\]
563    operator()-unique
564    operator()-ambiguous
565    operator\[\]-unique
566    operator\[\]-ambiguous
567    ops-valid-ambiguous
568    ops-valid-unique
569    ops-invalid
570    conversion-operator
571    assignment-operator
572    arrow-operator
573}
574
575test_driver
576