]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/macscp.exp
7086e90fce2d4a3151c6eaa34372fc9655ed5b97
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / macscp.exp
1 # Test macro scoping.
2 # Copyright 2002, 2007, 2008, 2009 Free Software Foundation, Inc.
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
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
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.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 if $tracelevel then {
18 strace $tracelevel
19 }
20
21 set prms_id 0
22 set bug_id 0
23
24 set srcfile macscp1.c
25 set testfile "macscp"
26 set objfile ${objdir}/${subdir}/${testfile}.o
27 set binfile ${objdir}/${subdir}/${testfile}
28
29 set options { debug }
30
31 get_compiler_info ${binfile}
32 if [test_compiler_info gcc*] {
33 lappend options additional_flags=-g3
34 }
35
36 # Generate the intermediate object file. This is required by Darwin to
37 # have access to the .debug_macinfo section.
38 if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${objfile}" \
39 object $options] != ""
40 || [gdb_compile "${objfile}" "${binfile}" executable $options] != "" } {
41 untested macscp.exp
42 return -1
43 }
44
45 gdb_exit
46 gdb_start
47 gdb_reinitialize_dir $srcdir/$subdir
48 gdb_load ${binfile}
49
50
51 # Ask GDB to show the current definition of MACRO, and return a list
52 # describing the result.
53 #
54 # The return value has the form {FILE1 FILE2 ... DEF}, which means
55 # that MACRO has the definition `DEF', and was defined in `FILE1',
56 # which was included from `FILE2', included from ... .
57 #
58 # If GDB says that MACRO has no definition, return the string `undefined'.
59 #
60 # If GDB complains that it doesn't have any information about
61 # preprocessor macro definitions, return the string `no-macro-info'.
62 #
63 # If expect times out waiting for GDB, we return the string `timeout'.
64 #
65 # If GDB's output doesn't otherwise match what we're expecting, we
66 # return the empty string.
67
68 proc info_macro {macro} {
69 global gdb_prompt
70 global decimal
71
72 set filepat {macscp[0-9]+\.[ch]}
73 set definition {}
74 set location {}
75
76 send_gdb "info macro ${macro}\n"
77
78 set debug_me 0
79
80 if {$debug_me} {exp_internal 1}
81 gdb_expect {
82 -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
83 # `location' and `definition' should be empty when we see
84 # this message.
85 if {[llength $location] == 0 && [llength $definition] == 0} {
86 set location $expect_out(1,string)
87 exp_continue
88 } else {
89 # Exit this expect loop, with a result indicating failure.
90 set definition {}
91 }
92 }
93 -re "The symbol `${macro}' has no definition as a C/C\\+\\+ preprocessor macro\[^\r\n\]*\[\r\n\]" {
94 # `location' and `definition' should be empty when we see
95 # this message.
96 if {[llength $location] == 0 && [llength $definition] == 0} {
97 set definition undefined
98 exp_continue
99 } else {
100 # Exit this expect loop, with a result indicating failure.
101 set definition {}
102 }
103 }
104 -re "^\[\r\n\]* included at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
105 # `location' should *not* be empty when we see this
106 # message. It should have recorded at least the initial
107 # `Defined at ' message (for definitions) or ` at' message
108 # (for undefined symbols).
109 if {[llength $location] != 0} {
110 lappend location $expect_out(1,string)
111 exp_continue
112 } else {
113 # Exit this expect loop, with a result indicating failure.
114 set definition {}
115 }
116 }
117 -re "^\[\r\n\]*at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
118 # This appears after a `has no definition' message.
119 # `location' should be empty when we see it.
120 if {[string compare $definition undefined] == 0 \
121 && [llength $location] == 0} {
122 set location $expect_out(1,string)
123 exp_continue
124 } else {
125 # Exit this expect loop, with a result indicating failure.
126 set definition {}
127 }
128 }
129 -re "#define ${macro} (\[^\r\n\]*)\[\r\n\]" {
130 # `definition' should be empty when we see this message.
131 if {[string compare $definition ""] == 0} {
132 set definition $expect_out(1,string)
133 exp_continue
134 } else {
135 # Exit this expect loop, with a result indicating failure.
136 set definition {}
137 }
138 }
139 -re "has no preprocessor macro information.*$gdb_prompt $" {
140 set definition no-macro-info
141 }
142 -re "$gdb_prompt $" {
143 # Exit the expect loop; let the existing value of `definition'
144 # indicate failure or success.
145 }
146 timeout {
147 set definition timeout
148 }
149 }
150 if {$debug_me} {exp_internal 0}
151
152 switch -exact -- $definition {
153 no-macro-info { return no-macro-info }
154 timeout { return timeout }
155 undefined -
156 default {
157 if {[llength $location] >= 1} {
158 return [concat $location [list $definition]]
159 } else {
160 return {}
161 }
162 }
163 }
164 }
165
166
167 # Call info_macro to show the definition of MACRO. Expect a result of
168 # EXPECTED. Use WHERE in pass/fail messages to identify the context.
169 # Return non-zero if we should abort the entire test file, or zero if
170 # we can continue.
171 proc check_macro {macro expected where} {
172 set func_def [info_macro $macro]
173 if {[string compare $func_def $expected] == 0} {
174 pass "info macro $macro $where"
175 } else {
176 switch -exact -- $func_def {
177 no-macro-info {
178 xfail "executable includes no macro debugging information"
179 return 1
180 }
181 timeout {
182 fail "info macro $macro $where (timeout)"
183 }
184 default {
185 fail "info macro $macro $where"
186 }
187 }
188 }
189 return 0
190 }
191
192
193 # List the function FUNC, and then show the definition of MACRO,
194 # expecting the result EXPECTED.
195 proc list_and_check_macro {func macro expected} {
196 gdb_test "list $func" ".*${func}.*" "list $func for $macro"
197 return [check_macro $macro $expected "after `list $func'"]
198 }
199
200
201 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
202 return 0
203 }
204 list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
205 list_and_check_macro macscp3_2 WHERE {macscp3.h macscp1.c {before macscp3_2}}
206
207
208 # Although GDB's macro table structures distinguish between multiple
209 # #inclusions of the same file, GDB's other structures don't. So the
210 # `list' command here doesn't reliably select one #inclusion or the
211 # other, even though it could. It would be nice to eventually change
212 # GDB's structures to handle this correctly.
213 gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
214 switch -exact -- [info_macro WHERE] {
215 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
216 pass "info macro WHERE after `list macscp_4_2_from_macscp2'"
217 }
218 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
219 setup_kfail *-*-* "gdb/555"
220 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (gdb/555)"
221 }
222 timeout {
223 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
224 }
225 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
226 }
227
228 gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
229 switch -exact -- [info_macro WHERE] {
230 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
231 pass "info macro WHERE after `list macscp_4_2_from_macscp3'"
232 }
233 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
234 setup_kfail *-*-* "gdb/555"
235 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (gdb/555)"
236 }
237 timeout {
238 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
239 }
240 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
241 }
242
243
244 #### Test the selection of the macro scope by the current frame.
245
246 ### A table of functions, in the order they will be reached, which is
247 ### also the order they appear in the preprocessed output. Each entry
248 ### has the form {FUNCNAME WHERE KFAILWHERE}, where:
249 ### - FUNCNAME is the name of the function,
250 ### - WHERE is the definition we expect to see for the macro `WHERE', as
251 ### returned by `info_macro', and
252 ### - KFAILWHERE is an alternate definition which should be reported
253 ### as a `known failure', due to GDB's inability to distinguish multiple
254 ### #inclusions of the same file.
255 ### KFAILWHERE may be omitted.
256
257 set funcs {
258 {
259 macscp1_1
260 {macscp1.c {before macscp1_1}}
261 }
262 {
263 macscp2_1
264 {macscp2.h macscp1.c {before macscp2_1}}
265 }
266 {
267 macscp4_1_from_macscp2
268 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
269 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
270 }
271 {
272 macscp4_2_from_macscp2
273 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
274 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
275 }
276 {
277 macscp2_2
278 {macscp2.h macscp1.c {before macscp2_2}}
279 }
280 {
281 macscp1_2
282 {macscp1.c {before macscp1_2}}
283 }
284 {
285 macscp3_1
286 {macscp3.h macscp1.c {before macscp3_1}}
287 }
288 {
289 macscp4_1_from_macscp3
290 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
291 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
292 }
293 {
294 macscp4_2_from_macscp3
295 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
296 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
297 }
298 {
299 macscp3_2
300 {macscp3.h macscp1.c {before macscp3_2}}
301 }
302 {
303 macscp1_3
304 {macscp1.c {before macscp1_3}}
305 }
306 }
307
308 proc maybe_kfail { func test_name } {
309 # We can't get the right scope info when we're stopped in
310 # the macro4_ functions.
311 if {[string match macscp4_* $func]} {
312 kfail gdb/555 "$test_name"
313 } else {
314 fail "$test_name"
315 }
316 }
317
318 # Start the program running.
319 if {! [runto_main]} {
320 fail "macro tests suppressed: couldn't run to main"
321 return 0
322 }
323
324 # Set a breakpoint on each of the functions.
325 foreach func_entry $funcs {
326 set func [lindex $func_entry 0]
327 gdb_test "break $func" "Breakpoint.*"
328 }
329
330 # Run to each of the breakpoints and check the definition (or lack
331 # thereof) of each macro.
332 for {set i 0} {$i < [llength $funcs]} {incr i} {
333 set func_entry [lindex $funcs $i]
334 set func [lindex $func_entry 0]
335 set expected [lindex $func_entry 1]
336 set kfail_expected [lindex $func_entry 2]
337
338 # Run to the breakpoint for $func.
339 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
340
341 # Check the macro WHERE.
342 set result [info_macro WHERE]
343 if {[string compare $result $expected] == 0} {
344 pass "info macro WHERE stopped in $func"
345 } elseif {[string compare $result $kfail_expected] == 0} {
346 setup_kfail *-*-* "gdb/555"
347 fail "info macro WHERE stopped in $func (gdb/555)"
348 } elseif {[string compare $result timeout] == 0} {
349 fail "info macro WHERE stopped in $func (timeout)"
350 } else {
351 fail "info macro WHERE stopped in $func"
352 }
353
354 # Check that the BEFORE_<func> macros for all prior functions are
355 # #defined, and that those for all subsequent functions are not.
356 for {set j 0} {$j < [llength $funcs]} {incr j} {
357 if {$j != $i} {
358 set func_j_entry [lindex $funcs $j]
359 set func_j [lindex $func_j_entry 0]
360
361 set before_macro "BEFORE_[string toupper $func_j]"
362 set test_name \
363 "$before_macro defined/undefined when stopped at $func"
364 set result [info_macro $before_macro]
365
366 if {$j < $i} {
367 if {[llength $result] >= 2 && \
368 [string compare [lindex $result end] {}] == 0} {
369 pass $test_name
370 } elseif {[string compare $result timeout] == 0} {
371 fail "$test_name (timeout)"
372 } else {
373 maybe_kfail $func "$test_name"
374 }
375 } elseif {$j > $i} {
376 switch -- [lindex $result end] {
377 undefined { pass $test_name }
378 timeout { fail "$test_name (timeout)" }
379 default {
380 maybe_kfail $func "$test_name"
381 }
382 }
383 }
384
385 set until_macro "UNTIL_[string toupper $func_j]"
386 set test_name \
387 "$until_macro defined/undefined when stopped at $func"
388 set result [info_macro $until_macro]
389
390 if {$j <= $i} {
391 switch -- [lindex $result end] {
392 undefined { pass $test_name }
393 timeout { fail "$test_name (timeout)" }
394 default {
395 maybe_kfail $func "$test_name"
396 }
397 }
398 } elseif {$j > $i} {
399 if {[llength $result] >= 2 && \
400 [string compare [lindex $result end] {}] == 0} {
401 pass $test_name
402 } elseif {[string compare $result timeout] == 0} {
403 fail "$test_name (timeout)"
404 } else {
405 maybe_kfail $func "$test_name"
406 }
407 }
408 }
409 }
410 }
411
412 gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
413 "Breakpoint.*at.* file .*, line.*" \
414 "breakpoint macscp_expr"
415
416 gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
417
418 gdb_test "print address.addr" \
419 " = 0" \
420 "print address.addr"
421
422 gdb_test "print MACRO_TO_EXPAND" \
423 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
424 "print expression with macro before define."
425
426 gdb_test "next" "foo = 1;" "next to definition 1"
427
428 gdb_test "print MACRO_TO_EXPAND" \
429 " = 0" \
430 "print expression with macro in scope."
431
432 gdb_test "macro define MACRO_TO_EXPAND 72" \
433 "" \
434 "user macro override"
435
436 gdb_test "print MACRO_TO_EXPAND" \
437 " = 72" \
438 "choose user macro"
439
440 gdb_test "macro undef MACRO_TO_EXPAND" \
441 "" \
442 "remove user override"
443
444 gdb_test "print MACRO_TO_EXPAND" \
445 " = 0" \
446 "print expression with macro after removing override"
447
448 gdb_test "next" "foo = 2;" "next to definition 2"
449
450 gdb_test "print MACRO_TO_EXPAND" \
451 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
452 "print expression with macro after undef."
453
454 gdb_test "macro define MACRO_TO_EXPAND 5" \
455 "" \
456 "basic macro define"
457
458 gdb_test "print MACRO_TO_EXPAND" \
459 " = 5" \
460 "expansion of defined macro"
461
462 gdb_test "macro list" \
463 "macro define MACRO_TO_EXPAND 5" \
464 "basic macro list"
465
466 gdb_test "macro define MACRO_TO_EXPAND(x) x" \
467 "" \
468 "basic redefine, macro with args"
469
470 gdb_test "print MACRO_TO_EXPAND (7)" \
471 " = 7" \
472 "expansion of macro with arguments"
473
474 gdb_test "macro undef MACRO_TO_EXPAND" \
475 "" \
476 "basic macro undef"
477
478 gdb_test "print MACRO_TO_EXPAND" \
479 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
480 "print expression with macro after user undef."
481
482 # Regression test; this used to crash.
483 gdb_test "macro define" \
484 "usage: macro define.*" \
485 "macro define with no arguments"
486
487 # Regression test; this used to crash.
488 gdb_test "macro undef" \
489 "usage: macro undef.*" \
490 "macro undef with no arguments"
491
492 # Completion tests.
493
494 # The macro FIFTY_SEVEN is in scope at this point.
495 send_gdb "p FIFTY_\t"
496 gdb_expect {
497 -re "^p FIFTY_SEVEN $"\
498 { send_gdb "\n"
499 gdb_expect {
500 -re "^.* = 57.*$gdb_prompt $"\
501 { pass "complete 'p FIFTY_SEVEN'"}
502 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'"}
503 timeout {fail "(timeout) complete 'p FIFTY_SEVEN'"}
504 }
505 }
506 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'" }
507 timeout { fail "(timeout) complete 'p FIFTY_SEVEN' 2" }
508 }
509
510 # The macro TWENTY_THREE is not in scope.
511 send_gdb "p TWENTY_\t"
512 gdb_expect {
513 -re "^p TWENTY_\\\x07$"\
514 { send_gdb "\n"
515 gdb_expect {
516 -re "No symbol \"TWENTY_\" in current context\\..*$gdb_prompt $"\
517 { pass "complete 'p TWENTY_'"}
518 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'"}
519 timeout {fail "(timeout) complete 'p TWENTY_'"}
520 }
521 }
522 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'" }
523 timeout { fail "(timeout) complete 'p TWENTY_' 2" }
524 }
525
526 # The macro FORTY_EIGHT was undefined and thus is not in scope.
527 send_gdb "p FORTY_\t"
528 gdb_expect {
529 -re "^p FORTY_\\\x07$"\
530 { send_gdb "\n"
531 gdb_expect {
532 -re "No symbol \"FORTY_\" in current context\\..*$gdb_prompt $"\
533 { pass "complete 'p FORTY_'"}
534 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'"}
535 timeout {fail "(timeout) complete 'p FORTY_'"}
536 }
537 }
538 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'" }
539 timeout { fail "(timeout) complete 'p FORTY_' 2" }
540 }
541
542 gdb_test "macro define TWENTY_THREE 25" \
543 "" \
544 "defining TWENTY_THREE"
545
546 # User-defined macros are always in scope.
547 send_gdb "p TWENTY_\t"
548 gdb_expect {
549 -re "^p TWENTY_THREE $"\
550 { send_gdb "\n"
551 gdb_expect {
552 -re "^.* = 25.*$gdb_prompt $"\
553 { pass "complete 'p TWENTY_THREE'"}
554 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'"}
555 timeout {fail "(timeout) complete 'p TWENTY_THREE'"}
556 }
557 }
558 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'" }
559 timeout { fail "(timeout) complete 'p TWENTY_THREE' 2" }
560 }
561
562 # Splicing tests.
563
564 gdb_test "macro expand SPLICE(x, y)" \
565 "expands to: xy" \
566 "basic macro splicing"
567
568 gdb_test "macro define robotinvasion 2010" \
569 "" \
570 "define splice helper"
571
572 gdb_test "macro expand SPLICE(robot, invasion)" \
573 "expands to: *2010" \
574 "splicing plus expansion"
575
576 # Varargs tests.
577
578 gdb_test "macro define va_c99(...) varfunc (fixedarg, __VA_ARGS__)" \
579 "" \
580 "define first varargs helper"
581
582 gdb_test "macro define va2_c99(x, y, ...) varfunc (fixedarg, x, y, __VA_ARGS__)" \
583 "" \
584 "define second varargs helper"
585
586 gdb_test "macro define va_gnu(args...) varfunc (fixedarg, args)" \
587 "" \
588 "define third varargs helper"
589
590 gdb_test "macro define va2_gnu(args...) varfunc (fixedarg, ## args)" \
591 "" \
592 "define fourth varargs helper"
593
594 gdb_test "macro expand va_c99(one, two, three)" \
595 "expands to: *varfunc \\(fixedarg, *one, two, three\\)" \
596 "c99 varargs expansion"
597
598 gdb_test "macro expand va_c99()" \
599 "expands to: *varfunc \\(fixedarg, *\\)" \
600 "c99 varargs expansion without an argument"
601
602 gdb_test "macro expand va2_c99(one, two, three, four)" \
603 "expands to: *varfunc \\(fixedarg, *one, two, three, four\\)" \
604 "c99 varargs expansion, multiple formal arguments"
605
606 gdb_test "macro expand va_gnu(one, two, three, four)" \
607 "expands to: *varfunc \\(fixedarg, *one, two, three, four\\)" \
608 "gnu varargs expansion"
609
610 gdb_test "macro expand va_gnu()" \
611 "expands to: *varfunc \\(fixedarg, *\\)" \
612 "gnu varargs expansion without an argument"
613
614 gdb_test "macro expand va2_gnu()" \
615 "expands to: *varfunc \\(fixedarg\\)" \
616 "gnu varargs expansion special splicing without an argument"
617
618 # Stringification tests.
619
620 gdb_test "macro define str(x) #x" \
621 "" \
622 "define stringification macro"
623
624 gdb_test "macro define maude 5" \
625 "" \
626 "define first stringification helper"
627
628 gdb_test "macro define xstr(x) str(x)" \
629 "" \
630 "define second stringification helper"
631
632 gdb_test "print str(5)" \
633 " = \"5\"" \
634 "simple stringify"
635
636 gdb_test "print str(hi bob)" \
637 " = \"hi bob\"" \
638 "stringify with one space"
639
640 gdb_test "print str( hi bob )" \
641 " = \"hi bob\"" \
642 "stringify with many spaces"
643
644 gdb_test "print str(hi \"bob\")" \
645 " = \"hi \\\\\"bob\\\\\"\"" \
646 "stringify with quotes"
647
648 gdb_test "print str(hi \\bob\\)" \
649 " = \"hi \\\\\\\\bob\\\\\\\\\"" \
650 "stringify with backslashes"
651
652 gdb_test "print str(maude)" \
653 " = \"maude\"" \
654 "stringify without substitution"
655
656 gdb_test "print xstr(maude)" \
657 " = \"5\"" \
658 "stringify with substitution"
659
660 # Regression test for pp-number bug.
661 gdb_test "macro define si_addr fields.fault.si_addr" \
662 "" \
663 "define si_addr macro"
664 gdb_test "macro expand siginfo.si_addr" \
665 "expands to: siginfo.fields.fault.si_addr" \
666 "macro expand siginfo.si_addr"