]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/macscp.exp
* config/tc-tic4x.c (tic4x_operands_parse): Make static.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / macscp.exp
CommitLineData
6827a8f8 1# Test macro scoping.
9b254dd1 2# Copyright 2002, 2007, 2008 Free Software Foundation, Inc.
6827a8f8
JB
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
e22f8b7c 6# the Free Software Foundation; either version 3 of the License, or
6827a8f8 7# (at your option) any later version.
e22f8b7c 8#
6827a8f8
JB
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
e22f8b7c 13#
6827a8f8 14# You should have received a copy of the GNU General Public License
e22f8b7c 15# along with this program. If not, see <http://www.gnu.org/licenses/>.
6827a8f8 16
6827a8f8
JB
17if $tracelevel then {
18 strace $tracelevel
19}
20
21set prms_id 0
22set bug_id 0
23
d705c43c 24set srcfile macscp1.c
6827a8f8
JB
25set testfile "macscp"
26set binfile ${objdir}/${subdir}/${testfile}
27
d848e687
DJ
28set options { debug }
29
30get_compiler_info ${binfile}
31if [test_compiler_info gcc*] {
32 lappend options additional_flags=-g3
33}
34
35if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable $options] != "" } {
b60f0898
JB
36 untested macscp.exp
37 return -1
6827a8f8
JB
38}
39
40gdb_exit
41gdb_start
42gdb_reinitialize_dir $srcdir/$subdir
43gdb_load ${binfile}
44
45
46# Ask GDB to show the current definition of MACRO, and return a list
47# describing the result.
48#
49# The return value has the form {FILE1 FILE2 ... DEF}, which means
50# that MACRO has the definition `DEF', and was defined in `FILE1',
51# which was included from `FILE2', included from ... .
52#
53# If GDB says that MACRO has no definition, return the string `undefined'.
54#
55# If GDB complains that it doesn't have any information about
56# preprocessor macro definitions, return the string `no-macro-info'.
57#
58# If expect times out waiting for GDB, we return the string `timeout'.
59#
60# If GDB's output doesn't otherwise match what we're expecting, we
61# return the empty string.
62
63proc info_macro {macro} {
64 global gdb_prompt
65 global decimal
66
67 set filepat {macscp[0-9]+\.[ch]}
68 set definition {}
69 set location {}
70
71 send_gdb "info macro ${macro}\n"
72
73 set debug_me 0
74
75 if {$debug_me} {exp_internal 1}
76 gdb_expect {
77 -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
78 # `location' and `definition' should be empty when we see
79 # this message.
80 if {[llength $location] == 0 && [llength $definition] == 0} {
81 set location $expect_out(1,string)
82 exp_continue
83 } else {
84 # Exit this expect loop, with a result indicating failure.
85 set definition {}
86 }
87 }
88 -re "The symbol `${macro}' has no definition as a C/C\\+\\+ preprocessor macro\[^\r\n\]*\[\r\n\]" {
89 # `location' and `definition' should be empty when we see
90 # this message.
91 if {[llength $location] == 0 && [llength $definition] == 0} {
92 set definition undefined
93 exp_continue
94 } else {
95 # Exit this expect loop, with a result indicating failure.
96 set definition {}
97 }
98 }
99 -re "^\[\r\n\]* included at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
100 # `location' should *not* be empty when we see this
101 # message. It should have recorded at least the initial
102 # `Defined at ' message (for definitions) or ` at' message
103 # (for undefined symbols).
104 if {[llength $location] != 0} {
105 lappend location $expect_out(1,string)
106 exp_continue
107 } else {
108 # Exit this expect loop, with a result indicating failure.
109 set definition {}
110 }
111 }
112 -re "^\[\r\n\]*at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
113 # This appears after a `has no definition' message.
114 # `location' should be empty when we see it.
115 if {[string compare $definition undefined] == 0 \
116 && [llength $location] == 0} {
117 set location $expect_out(1,string)
118 exp_continue
119 } else {
120 # Exit this expect loop, with a result indicating failure.
121 set definition {}
122 }
123 }
124 -re "#define ${macro} (\[^\r\n\]*)\[\r\n\]" {
125 # `definition' should be empty when we see this message.
126 if {[string compare $definition ""] == 0} {
127 set definition $expect_out(1,string)
128 exp_continue
129 } else {
130 # Exit this expect loop, with a result indicating failure.
131 set definition {}
132 }
133 }
134 -re "has no preprocessor macro information.*$gdb_prompt $" {
135 set definition no-macro-info
136 }
137 -re "$gdb_prompt $" {
138 # Exit the expect loop; let the existing value of `definition'
139 # indicate failure or success.
140 }
141 timeout {
142 set definition timeout
143 }
144 }
145 if {$debug_me} {exp_internal 0}
146
147 switch -exact -- $definition {
148 no-macro-info { return no-macro-info }
149 timeout { return timeout }
150 undefined -
151 default {
152 if {[llength $location] >= 1} {
153 return [concat $location [list $definition]]
154 } else {
155 return {}
156 }
157 }
158 }
159}
160
161
162# Call info_macro to show the definition of MACRO. Expect a result of
163# EXPECTED. Use WHERE in pass/fail messages to identify the context.
164# Return non-zero if we should abort the entire test file, or zero if
165# we can continue.
166proc check_macro {macro expected where} {
167 set func_def [info_macro $macro]
168 if {[string compare $func_def $expected] == 0} {
169 pass "info macro $macro $where"
170 } else {
171 switch -exact -- $func_def {
172 no-macro-info {
173 xfail "executable includes no macro debugging information"
174 return 1
175 }
176 timeout {
177 fail "info macro $macro $where (timeout)"
178 }
179 default {
180 fail "info macro $macro $where"
181 }
182 }
183 }
184 return 0
185}
186
187
188# List the function FUNC, and then show the definition of MACRO,
189# expecting the result EXPECTED.
190proc list_and_check_macro {func macro expected} {
191 gdb_test "list $func" ".*${func}.*"
192 return [check_macro $macro $expected "after `list $func'"]
193}
194
195
196if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
197 return 0
198}
199list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
200list_and_check_macro macscp3_2 WHERE {macscp3.h macscp1.c {before macscp3_2}}
201
202
203# Although GDB's macro table structures distinguish between multiple
204# #inclusions of the same file, GDB's other structures don't. So the
205# `list' command here doesn't reliably select one #inclusion or the
206# other, even though it could. It would be nice to eventually change
207# GDB's structures to handle this correctly.
208gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
209switch -exact -- [info_macro WHERE] {
210 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
211 pass "info macro WHERE after `list macscp_4_2_from_macscp2'"
212 }
213 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
d848e687 214 setup_kfail *-*-* "gdb/555"
6827a8f8
JB
215 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (gdb/555)"
216 }
217 timeout {
218 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
219 }
220 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
221}
222
223gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
224switch -exact -- [info_macro WHERE] {
225 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
226 pass "info macro WHERE after `list macscp_4_2_from_macscp3'"
227 }
228 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
d848e687 229 setup_kfail *-*-* "gdb/555"
6827a8f8
JB
230 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (gdb/555)"
231 }
232 timeout {
233 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
234 }
235 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
236}
237
238
239#### Test the selection of the macro scope by the current frame.
240
241### A table of functions, in the order they will be reached, which is
242### also the order they appear in the preprocessed output. Each entry
243### has the form {FUNCNAME WHERE KFAILWHERE}, where:
244### - FUNCNAME is the name of the function,
245### - WHERE is the definition we expect to see for the macro `WHERE', as
246### returned by `info_macro', and
247### - KFAILWHERE is an alternate definition which should be reported
248### as a `known failure', due to GDB's inability to distinguish multiple
249### #inclusions of the same file.
250### KFAILWHERE may be omitted.
251
252set funcs {
253 {
254 macscp1_1
255 {macscp1.c {before macscp1_1}}
256 }
257 {
258 macscp2_1
259 {macscp2.h macscp1.c {before macscp2_1}}
260 }
261 {
262 macscp4_1_from_macscp2
263 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
264 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
265 }
266 {
267 macscp4_2_from_macscp2
268 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
269 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
270 }
271 {
272 macscp2_2
273 {macscp2.h macscp1.c {before macscp2_2}}
274 }
275 {
276 macscp1_2
277 {macscp1.c {before macscp1_2}}
278 }
279 {
280 macscp3_1
281 {macscp3.h macscp1.c {before macscp3_1}}
282 }
283 {
284 macscp4_1_from_macscp3
285 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
286 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
287 }
288 {
289 macscp4_2_from_macscp3
290 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
291 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
292 }
293 {
294 macscp3_2
295 {macscp3.h macscp1.c {before macscp3_2}}
296 }
297 {
298 macscp1_3
299 {macscp1.c {before macscp1_3}}
300 }
301}
302
d848e687
DJ
303proc maybe_kfail { func test_name } {
304 # We can't get the right scope info when we're stopped in
305 # the macro4_ functions.
306 if {[string match macscp4_* $func]} {
307 kfail gdb/555 "$test_name"
308 } else {
309 fail "$test_name"
310 }
311}
6827a8f8
JB
312
313# Start the program running.
314if {! [runto_main]} {
315 fail "macro tests suppressed: couldn't run to main"
316 return 0
317}
318
319# Set a breakpoint on each of the functions.
320foreach func_entry $funcs {
321 set func [lindex $func_entry 0]
322 gdb_test "break $func" "Breakpoint.*"
323}
324
325# Run to each of the breakpoints and check the definition (or lack
326# thereof) of each macro.
327for {set i 0} {$i < [llength $funcs]} {incr i} {
328 set func_entry [lindex $funcs $i]
329 set func [lindex $func_entry 0]
330 set expected [lindex $func_entry 1]
331 set kfail_expected [lindex $func_entry 2]
332
333 # Run to the breakpoint for $func.
334 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
335
336 # Check the macro WHERE.
337 set result [info_macro WHERE]
338 if {[string compare $result $expected] == 0} {
339 pass "info macro WHERE stopped in $func"
340 } elseif {[string compare $result $kfail_expected] == 0} {
d848e687 341 setup_kfail *-*-* "gdb/555"
6827a8f8
JB
342 fail "info macro WHERE stopped in $func (gdb/555)"
343 } elseif {[string compare $result timeout] == 0} {
344 fail "info macro WHERE stopped in $func (timeout)"
345 } else {
346 fail "info macro WHERE stopped in $func"
347 }
348
349 # Check that the BEFORE_<func> macros for all prior functions are
350 # #defined, and that those for all subsequent functions are not.
351 for {set j 0} {$j < [llength $funcs]} {incr j} {
352 if {$j != $i} {
353 set func_j_entry [lindex $funcs $j]
354 set func_j [lindex $func_j_entry 0]
355
356 set before_macro "BEFORE_[string toupper $func_j]"
357 set test_name \
358 "$before_macro defined/undefined when stopped at $func"
359 set result [info_macro $before_macro]
360
6827a8f8
JB
361 if {$j < $i} {
362 if {[llength $result] >= 2 && \
363 [string compare [lindex $result end] {}] == 0} {
364 pass $test_name
365 } elseif {[string compare $result timeout] == 0} {
366 fail "$test_name (timeout)"
367 } else {
d848e687 368 maybe_kfail $func "$test_name"
6827a8f8
JB
369 }
370 } elseif {$j > $i} {
371 switch -- [lindex $result end] {
372 undefined { pass $test_name }
373 timeout { fail "$test_name (timeout)" }
374 default {
d848e687 375 maybe_kfail $func "$test_name"
6827a8f8
JB
376 }
377 }
378 }
379
380 set until_macro "UNTIL_[string toupper $func_j]"
381 set test_name \
382 "$until_macro defined/undefined when stopped at $func"
383 set result [info_macro $until_macro]
384
6827a8f8
JB
385 if {$j <= $i} {
386 switch -- [lindex $result end] {
387 undefined { pass $test_name }
388 timeout { fail "$test_name (timeout)" }
389 default {
d848e687 390 maybe_kfail $func "$test_name"
6827a8f8
JB
391 }
392 }
393 } elseif {$j > $i} {
394 if {[llength $result] >= 2 && \
395 [string compare [lindex $result end] {}] == 0} {
396 pass $test_name
397 } elseif {[string compare $result timeout] == 0} {
398 fail "$test_name (timeout)"
399 } else {
d848e687 400 maybe_kfail $func "$test_name"
6827a8f8
JB
401 }
402 }
403 }
404 }
405}
d705c43c
PA
406
407gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
408 "Breakpoint.*at.* file .*, line.*" \
409 "breakpoint macscp_expr"
410
411gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
412
413gdb_test "print M" \
414 "No symbol \"M\" in current context\." \
415 "print expression with macro before define."
416
417gdb_test "next" "foo = 1;" "next to definition"
418
419gdb_test "print M" \
420 " = 0" \
421 "print expression with macro in scope."
422
d7d9f01e
TT
423gdb_test "macro define M 72" \
424 "" \
425 "user macro override"
426
427gdb_test "print M" \
428 " = 72" \
429 "choose user macro"
430
431gdb_test "macro undef M" \
432 "" \
433 "remove user override"
434
435gdb_test "print M" \
436 " = 0" \
437 "print expression with macro after removing override"
438
d705c43c
PA
439gdb_test "next" "foo = 2;" "next to definition"
440
441gdb_test "print M" \
442 "No symbol \"M\" in current context\." \
443 "print expression with macro after undef."
d7d9f01e
TT
444
445gdb_test "macro define M 5" \
446 "" \
447 "basic macro define"
448
449gdb_test "print M" \
450 " = 5" \
451 "expansion of defined macro"
452
453gdb_test "macro list" \
454 "macro define M 5" \
455 "basic macro list"
456
457gdb_test "macro define M(x) x" \
458 "" \
459 "basic redefine, macro with args"
460
461gdb_test "print M (7)" \
462 " = 7" \
463 "expansion of macro with arguments"
464
465gdb_test "macro undef M" \
466 "" \
467 "basic macro undef"
468
469gdb_test "print M" \
470 "No symbol \"M\" in current context\." \
471 "print expression with macro after user undef."
ccb3ac8a
TT
472
473# Regression test; this used to emit the wrong error.
474gdb_test "macro expand SPLICE(x, y)" \
475 "Token splicing is not implemented yet." \
476 "macro splicing lexes correctly"