]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/macscp.exp
gdb
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / macscp.exp
1 # Test macro scoping.
2 # Copyright 2002, 2007, 2008 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 binfile ${objdir}/${subdir}/${testfile}
27
28 set options { debug }
29
30 get_compiler_info ${binfile}
31 if [test_compiler_info gcc*] {
32 lappend options additional_flags=-g3
33 }
34
35 if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable $options] != "" } {
36 untested macscp.exp
37 return -1
38 }
39
40 gdb_exit
41 gdb_start
42 gdb_reinitialize_dir $srcdir/$subdir
43 gdb_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
63 proc 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.
166 proc 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.
190 proc 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
196 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
197 return 0
198 }
199 list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
200 list_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.
208 gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
209 switch -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}} {
214 setup_kfail *-*-* "gdb/555"
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
223 gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
224 switch -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}} {
229 setup_kfail *-*-* "gdb/555"
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
252 set 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
303 proc 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 }
312
313 # Start the program running.
314 if {! [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.
320 foreach 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.
327 for {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} {
341 setup_kfail *-*-* "gdb/555"
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
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 {
368 maybe_kfail $func "$test_name"
369 }
370 } elseif {$j > $i} {
371 switch -- [lindex $result end] {
372 undefined { pass $test_name }
373 timeout { fail "$test_name (timeout)" }
374 default {
375 maybe_kfail $func "$test_name"
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
385 if {$j <= $i} {
386 switch -- [lindex $result end] {
387 undefined { pass $test_name }
388 timeout { fail "$test_name (timeout)" }
389 default {
390 maybe_kfail $func "$test_name"
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 {
400 maybe_kfail $func "$test_name"
401 }
402 }
403 }
404 }
405 }
406
407 gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
408 "Breakpoint.*at.* file .*, line.*" \
409 "breakpoint macscp_expr"
410
411 gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
412
413 gdb_test "print MACRO_TO_EXPAND" \
414 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
415 "print expression with macro before define."
416
417 gdb_test "next" "foo = 1;" "next to definition"
418
419 gdb_test "print MACRO_TO_EXPAND" \
420 " = 0" \
421 "print expression with macro in scope."
422
423 gdb_test "macro define MACRO_TO_EXPAND 72" \
424 "" \
425 "user macro override"
426
427 gdb_test "print MACRO_TO_EXPAND" \
428 " = 72" \
429 "choose user macro"
430
431 gdb_test "macro undef MACRO_TO_EXPAND" \
432 "" \
433 "remove user override"
434
435 gdb_test "print MACRO_TO_EXPAND" \
436 " = 0" \
437 "print expression with macro after removing override"
438
439 gdb_test "next" "foo = 2;" "next to definition"
440
441 gdb_test "print MACRO_TO_EXPAND" \
442 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
443 "print expression with macro after undef."
444
445 gdb_test "macro define MACRO_TO_EXPAND 5" \
446 "" \
447 "basic macro define"
448
449 gdb_test "print MACRO_TO_EXPAND" \
450 " = 5" \
451 "expansion of defined macro"
452
453 gdb_test "macro list" \
454 "macro define MACRO_TO_EXPAND 5" \
455 "basic macro list"
456
457 gdb_test "macro define MACRO_TO_EXPAND(x) x" \
458 "" \
459 "basic redefine, macro with args"
460
461 gdb_test "print MACRO_TO_EXPAND (7)" \
462 " = 7" \
463 "expansion of macro with arguments"
464
465 gdb_test "macro undef MACRO_TO_EXPAND" \
466 "" \
467 "basic macro undef"
468
469 gdb_test "print MACRO_TO_EXPAND" \
470 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
471 "print expression with macro after user undef."
472
473 # Regression test; this used to crash.
474 gdb_test "macro define" \
475 "usage: macro define.*" \
476 "macro define with no arguments"
477
478 # Regression test; this used to crash.
479 gdb_test "macro undef" \
480 "usage: macro undef.*" \
481 "macro undef with no arguments"
482
483 # Completion tests.
484
485 # The macro FIFTY_SEVEN is in scope at this point.
486 send_gdb "p FIFTY_\t"
487 gdb_expect {
488 -re "^p FIFTY_SEVEN $"\
489 { send_gdb "\n"
490 gdb_expect {
491 -re "^.* = 57.*$gdb_prompt $"\
492 { pass "complete 'p FIFTY_SEVEN'"}
493 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'"}
494 timeout {fail "(timeout) complete 'p FIFTY_SEVEN'"}
495 }
496 }
497 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'" }
498 timeout { fail "(timeout) complete 'p FIFTY_SEVEN' 2" }
499 }
500
501 # The macro TWENTY_THREE is not in scope.
502 send_gdb "p TWENTY_\t"
503 gdb_expect {
504 -re "^p TWENTY_\\\x07$"\
505 { send_gdb "\n"
506 gdb_expect {
507 -re "No symbol \"TWENTY_\" in current context\\..*$gdb_prompt $"\
508 { pass "complete 'p TWENTY_'"}
509 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'"}
510 timeout {fail "(timeout) complete 'p TWENTY_'"}
511 }
512 }
513 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'" }
514 timeout { fail "(timeout) complete 'p TWENTY_' 2" }
515 }
516
517 # The macro FORTY_EIGHT was undefined and thus is not in scope.
518 send_gdb "p FORTY_\t"
519 gdb_expect {
520 -re "^p FORTY_\\\x07$"\
521 { send_gdb "\n"
522 gdb_expect {
523 -re "No symbol \"FORTY_\" in current context\\..*$gdb_prompt $"\
524 { pass "complete 'p FORTY_'"}
525 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'"}
526 timeout {fail "(timeout) complete 'p FORTY_'"}
527 }
528 }
529 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'" }
530 timeout { fail "(timeout) complete 'p FORTY_' 2" }
531 }
532
533 gdb_test "macro define TWENTY_THREE 25" \
534 "" \
535 "defining TWENTY_THREE"
536
537 # User-defined macros are always in scope.
538 send_gdb "p TWENTY_\t"
539 gdb_expect {
540 -re "^p TWENTY_THREE $"\
541 { send_gdb "\n"
542 gdb_expect {
543 -re "^.* = 25.*$gdb_prompt $"\
544 { pass "complete 'p TWENTY_THREE'"}
545 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'"}
546 timeout {fail "(timeout) complete 'p TWENTY_THREE'"}
547 }
548 }
549 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'" }
550 timeout { fail "(timeout) complete 'p TWENTY_THREE' 2" }
551 }
552
553 # Splicing tests.
554
555 gdb_test "macro expand SPLICE(x, y)" \
556 "expands to: xy" \
557 "basic macro splicing"
558
559 gdb_test "macro define robotinvasion 2010" \
560 "" \
561 "define splice helper"
562
563 gdb_test "macro expand SPLICE(robot, invasion)" \
564 "expands to: *2010" \
565 "splicing plus expansion"
566
567 # Varargs tests.
568
569 gdb_test "macro define va_c99(...) fprintf (stderr, __VA_ARGS__)" \
570 "" \
571 "define first varargs helper"
572
573 gdb_test "macro define va2_c99(x, y, ...) fprintf (stderr, x, y, __VA_ARGS__)" \
574 "" \
575 "define second varargs helper"
576
577 gdb_test "macro define va_gnu(args...) fprintf (stderr, args)" \
578 "" \
579 "define third varargs helper"
580
581 gdb_test "macro define va2_gnu(args...) fprintf (stderr, ## args)" \
582 "" \
583 "define fourth varargs helper"
584
585 gdb_test "macro expand va_c99(one, two, three)" \
586 "expands to: *fprintf \\(stderr, *one, two, three\\)" \
587 "c99 varargs expansion"
588
589 gdb_test "macro expand va_c99()" \
590 "expands to: *fprintf \\(stderr, *\\)" \
591 "c99 varargs expansion without an argument"
592
593 gdb_test "macro expand va2_c99(one, two, three, four)" \
594 "expands to: *fprintf \\(stderr, *one, two, three, four\\)" \
595 "c99 varargs expansion, multiple formal arguments"
596
597 gdb_test "macro expand va_gnu(one, two, three, four)" \
598 "expands to: *fprintf \\(stderr, *one, two, three, four\\)" \
599 "gnu varargs expansion"
600
601 gdb_test "macro expand va_gnu()" \
602 "expands to: *fprintf \\(stderr, *\\)" \
603 "gnu varargs expansion without an argument"
604
605 gdb_test "macro expand va2_gnu()" \
606 "expands to: *fprintf \\(stderr\\)" \
607 "gnu varargs expansion special splicing without an argument"
608
609 # Stringification tests.
610
611 gdb_test "macro define str(x) #x" \
612 "" \
613 "define stringification macro"
614
615 gdb_test "macro define maude 5" \
616 "" \
617 "define first stringification helper"
618
619 gdb_test "macro define xstr(x) str(x)" \
620 "" \
621 "define second stringification helper"
622
623 gdb_test "print str(5)" \
624 " = \"5\"" \
625 "simple stringify"
626
627 gdb_test "print str(hi bob)" \
628 " = \"hi bob\"" \
629 "stringify with one space"
630
631 gdb_test "print str( hi bob )" \
632 " = \"hi bob\"" \
633 "stringify with many spaces"
634
635 gdb_test "print str(hi \"bob\")" \
636 " = \"hi \\\\\"bob\\\\\"\"" \
637 "stringify with quotes"
638
639 gdb_test "print str(hi \\bob\\)" \
640 " = \"hi \\\\\\\\bob\\\\\\\\\"" \
641 "stringify with backslashes"
642
643 gdb_test "print str(maude)" \
644 " = \"maude\"" \
645 "stringify without substitution"
646
647 gdb_test "print xstr(maude)" \
648 " = \"5\"" \
649 "stringify with substitution"