xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.linespec/ls-errs.exp (revision 528ce0b18ee40383f14928382d06afd754b01561)
1# Copyright 2012-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# Tests for linespec errors with C and C++.
17
18# The test proper.  LANG is either C or C++.
19
20proc do_test {lang} {
21    global testfile srcfile error_messages compiler_info
22
23    standard_testfile
24    set exefile $testfile
25    if [info exists compiler_info] {
26	# Unsetting compiler_info allows us to switch compilers
27	# used by prepare_for_testing.
28        unset compiler_info
29    }
30    set options {debug}
31
32    if {$lang == "C++"} {
33	if {[skip_cplus_tests]} {
34	    return 0
35	}
36	# Build ".c" source file with g++.
37	lappend options "c++"
38    }
39
40    if {[prepare_for_testing "failed to prepare" $exefile $srcfile $options]} {
41	return -1
42    }
43
44    # Turn off the pending breakpoint queries.
45    gdb_test_no_output "set breakpoint pending off"
46
47    # Turn off completion limiting
48    gdb_test_no_output "set max-completions unlimited"
49
50    if {![runto_main]} {
51	fail "can't run to main"
52	return 0
53    }
54
55    # Run to a location in the file.
56    set bp_location [gdb_get_line_number "set breakpoint here"]
57
58    gdb_test "break $srcfile:$bp_location" \
59    "Breakpoint.*at.* file .*$srcfile, line $bp_location\\." \
60    "breakpoint line number in file"
61
62    gdb_continue_to_breakpoint "$bp_location"
63
64    # Common error message format strings.
65    array set error_messages {
66	invalid_file "No source file named %s."
67	invalid_function "Function \"%s\" not defined."
68	invalid_var_or_func
69	    "Undefined convenience variable or function \"%s\" not defined."
70	invalid_function_f "Function \"%s\" not defined in \"%s\"."
71	invalid_var_or_func_f \
72	    "Undefined convenience variable or function \"%s\" not defined in \"%s\"."
73	invalid_label "No label \"%s\" defined in function \"%s\"."
74	invalid_parm "invalid linespec argument, \"%s\""
75	invalid_offset "No line %d in the current file."
76	invalid_offset_f "No line %d in file \"%s\"."
77	malformed_line_offset "malformed line offset: \"%s\""
78	source_incomplete \
79	    "Source filename requires function, label, or line offset."
80	unexpected "malformed linespec error: unexpected %s"
81	unexpected_opt "malformed linespec error: unexpected %s, \"%s\""
82	unmatched_quote "unmatched quote"
83	garbage "Garbage '%s' at end of command"
84    }
85
86    # We intentionally do not use gdb_breakpoint for these tests.
87
88    # Break at 'linespec' and expect the message in ::error_messages
89    # indexed by msg_id with the associated args.
90    proc test_break {linespec msg_id args} {
91	global error_messages
92
93	gdb_test "break $linespec" [string_to_regexp \
94				    [eval format \$error_messages($msg_id) \
95				     $args]] \
96	    "'break $linespec'"
97    }
98
99    # Some commonly used whitespace tests around ':'.
100    set spaces [list \
101		    ":" \
102		    ": " \
103		    " :" \
104		    " : " \
105		    "  :  " \
106		   ]
107
108    # A list of invalid offsets.
109    set invalid_offsets [list -100 +500 1000]
110
111    # Try some simple, invalid linespecs involving spaces.
112    foreach x $spaces {
113	test_break $x unexpected "colon"
114    }
115
116    # Test invalid filespecs starting with offset.  This is done
117    # first so that default offsets are tested.
118    foreach x $invalid_offsets {
119	set offset $x
120
121	# Relative offsets are relative to the current line.  Adjust
122	# expected offset from error message accordingly.
123	if {[string index $x 0] == "+" || [string index $x 0] == "-"} {
124	    incr offset $bp_location
125	}
126	test_break $x invalid_offset $offset
127	test_break "-line $x" invalid_offset $offset
128    }
129
130    # Test offsets with trailing tokens w/ and w/o spaces.
131    foreach x $spaces {
132	test_break "3$x" unexpected "colon"
133	test_break "+10$x" unexpected "colon"
134	test_break "-10$x" unexpected "colon"
135    }
136
137    foreach x {1 +1 +100 -10} {
138	test_break "3 $x" unexpected_opt "number" $x
139	test_break "-line 3 $x" garbage $x
140	test_break "+10 $x" unexpected_opt "number" $x
141	test_break "-line +10 $x" garbage $x
142	test_break "-10 $x" unexpected_opt "number" $x
143	test_break "-line -10 $x" garbage $x
144    }
145
146    foreach x {3 +10 -10} {
147	test_break "$x foo" unexpected_opt "string" "foo"
148	test_break "-line $x foo" garbage "foo"
149    }
150
151    # Test invalid linespecs starting with filename.
152    # It's OK to use the ".c" extension for the C++ test
153    # since the extension doesn't affect GDB's lookup.
154    set invalid_files [list "this_file_doesn't_exist.c" \
155			    "this file has spaces.c" \
156			    "\"file::colons.c\"" \
157			    "'file::colons.c'" \
158			    "\"this \"file\" has quotes.c\"" \
159			    "'this \"file\" has quotes.c'" \
160			    "'this 'file' has quotes.c'" \
161			    "\"this 'file' has quotes.c\"" \
162			    "\"spaces: and :colons.c\"" \
163			    "'more: :spaces: :and  colons::.c'" \
164			    "C:/nonexist-with-windrive.c"]
165
166    foreach x $invalid_files {
167	# Remove any quoting from FILENAME for the error message.
168	test_break "$x:3" invalid_file [string trim $x \"']
169    }
170    foreach x [list "this_file_doesn't_exist.c" \
171		    "file::colons.c" \
172		    "'file::colons.c'"] {
173	test_break "-source $x -line 3" invalid_file [string trim $x \"']
174    }
175
176    # Test that option lexing stops at whitespace boundaries, except
177    # when lexing function names, where we want to handle setting
178    # breakpoints on e.g., "int template_function<int>()".
179    test_break "-source this file has spaces.c -line 3" source_incomplete
180    test_break "-function ret_type tmpl_function" \
181	invalid_function "ret_type tmpl_function"
182    test_break "-source $srcfile -function ret_type tmpl_function" \
183	       invalid_function_f "ret_type tmpl_function" $srcfile
184
185    test_break "-function main -label label whitespace" \
186	       invalid_label "label" "main"
187
188    # Test unmatched quotes.
189    foreach x {"\"src-file.c'" "'src-file.c"} {
190	test_break "$x:3" unmatched_quote
191    }
192
193    test_break $srcfile invalid_function $srcfile
194    foreach x {"foo" " foo" " foo "} {
195	# Trim any leading/trailing whitespace for error messages.
196	test_break "$srcfile:$x" invalid_function_f [string trim $x] $srcfile
197	test_break "-source $srcfile -function $x" \
198		   invalid_function_f [string trim $x] $srcfile
199	test_break "$srcfile:main:$x" invalid_label [string trim $x] "main"
200	test_break "-source $srcfile -function main -label $x" \
201		   invalid_label [string trim $x] "main"
202    }
203
204    foreach x $spaces {
205	test_break "$srcfile$x" unexpected "end of input"
206	test_break "$srcfile:main$x" unexpected "end of input"
207    }
208
209    test_break "${srcfile}::" invalid_function "${srcfile}::"
210    test_break "$srcfile:3 1" unexpected_opt "number" "1"
211    test_break "-source $srcfile -line 3 1" garbage "1"
212    test_break "$srcfile:3 +100" unexpected_opt "number" "+100"
213    test_break "-source $srcfile -line 3 +100" garbage "+100"
214    test_break "$srcfile:3 -100" unexpected_opt "number" "-100"
215    test_break "$srcfile:3 foo" unexpected_opt "string" "foo"
216    test_break "-source $srcfile -line 3 foo" garbage "foo"
217
218    foreach x $invalid_offsets {
219	test_break "$srcfile:$x" invalid_offset_f $x $srcfile
220	test_break "\"$srcfile:$x\"" invalid_offset_f $x $srcfile
221	test_break "'$srcfile:$x'" invalid_offset_f $x $srcfile
222	test_break "-source $srcfile -line $x" invalid_offset_f $x $srcfile
223    }
224    test_break "-source $srcfile -line -x" malformed_line_offset "-x"
225
226    # Test invalid filespecs starting with function.
227    foreach x {"foobar" "foo::bar" "foo.bar" "foo ." "foo bar" "foo 1" \
228	       "foo 0" "foo +10" "foo -10" "foo +100" "foo -100"} {
229	test_break $x invalid_function $x
230	test_break "-function \"$x\"" invalid_function $x
231    }
232
233    foreach x $spaces {
234	test_break "main${x}there" invalid_label "there" "main"
235	test_break "main:here${x}" unexpected "end of input"
236    }
237
238    foreach_with_prefix x {"3" "+100" "-100" "foo"} {
239	test_break "main 3" invalid_function "main 3"
240	test_break "-function \"main $x\"" invalid_function "main $x"
241	if {$x == "foo"} {
242	    test_break "main:here $x" unexpected_opt "string" $x
243	} else {
244	    test_break "main:here $x" unexpected_opt "number" $x
245	}
246
247	test_break "-function main -label \"here $x\"" \
248		   invalid_label "here $x" "main"
249    }
250
251    foreach x {"if" "task" "thread"} {
252	test_break $x invalid_function $x
253    }
254
255    test_break "'main.c'flubber" unexpected_opt "string" "flubber"
256    test_break "'main.c',21" invalid_function "main.c"
257    test_break "'main.c' " invalid_function "main.c"
258    test_break "'main.c'3" unexpected_opt "number" "3"
259    test_break "'main.c'+3" unexpected_opt "number" "+3"
260
261    # Test undefined convenience variables.
262    set x {$zippo}
263    test_break $x invalid_var_or_func $x
264    test_break "$srcfile:$x" invalid_var_or_func_f $x $srcfile
265
266    # Explicit linespec-specific tests
267    test_break "-source $srcfile" source_incomplete
268    test_break "-source $srcfile main" source_incomplete
269}
270
271foreach_with_prefix lang {"C" "C++"} {
272    do_test ${lang}
273}
274