2 # Copyright 2002, 2007, 2008, 2009 Free Software Foundation, Inc.
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.
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.
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/>.
26 set objfile ${objdir}/${subdir}/${testfile}.o
27 set binfile ${objdir}/${subdir}/${testfile}
29 set options { debug additional_flags=-DFROM_COMMANDLINE=ARG}
31 get_compiler_info ${binfile}
32 if [test_compiler_info gcc*] {
33 lappend options additional_flags=-g3
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] != "" } {
47 gdb_reinitialize_dir $srcdir/$subdir
51 # Ask GDB to show the current definition of MACRO, and return a list
52 # describing the result.
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 ... .
58 # If GDB says that MACRO has no definition, return the string `undefined'.
60 # If GDB complains that it doesn't have any information about
61 # preprocessor macro definitions, return the string `no-macro-info'.
63 # If expect times out waiting for GDB, we return the string `timeout'.
65 # If GDB's output doesn't otherwise match what we're expecting, we
66 # return the empty string.
68 proc info_macro {macro} {
71 set filepat {macscp[0-9]+\.[ch]}
75 # Line number zero is set for macros defined from the compiler command-line.
76 # Such macros are not being tested by this function.
77 set nonzero {[1-9][0-9]*}
79 send_gdb "info macro ${macro}\n"
83 if {$debug_me} {exp_internal 1}
85 -re "Defined at \[^\r\n\]*(${filepat}):${nonzero}\[\r\n\]" {
86 # `location' and `definition' should be empty when we see
88 if {[llength $location] == 0 && [llength $definition] == 0} {
89 set location $expect_out(1,string)
92 # Exit this expect loop, with a result indicating failure.
96 -re "The symbol `${macro}' has no definition as a C/C\\+\\+ preprocessor macro\[^\r\n\]*\[\r\n\]" {
97 # `location' and `definition' should be empty when we see
99 if {[llength $location] == 0 && [llength $definition] == 0} {
100 set definition undefined
103 # Exit this expect loop, with a result indicating failure.
107 -re "^\[\r\n\]* included at \[^\r\n\]*(${filepat}):${nonzero}\[\r\n\]" {
108 # `location' should *not* be empty when we see this
109 # message. It should have recorded at least the initial
110 # `Defined at ' message (for definitions) or ` at' message
111 # (for undefined symbols).
112 if {[llength $location] != 0} {
113 lappend location $expect_out(1,string)
116 # Exit this expect loop, with a result indicating failure.
120 -re "^\[\r\n\]*at \[^\r\n\]*(${filepat}):${nonzero}\[\r\n\]" {
121 # This appears after a `has no definition' message.
122 # `location' should be empty when we see it.
123 if {[string compare $definition undefined] == 0 \
124 && [llength $location] == 0} {
125 set location $expect_out(1,string)
128 # Exit this expect loop, with a result indicating failure.
132 -re "#define ${macro} (\[^\r\n\]*)\[\r\n\]" {
133 # `definition' should be empty when we see this message.
134 if {[string compare $definition ""] == 0} {
135 set definition $expect_out(1,string)
138 # Exit this expect loop, with a result indicating failure.
142 -re "has no preprocessor macro information.*$gdb_prompt $" {
143 set definition no-macro-info
145 -re "$gdb_prompt $" {
146 # Exit the expect loop; let the existing value of `definition'
147 # indicate failure or success.
150 set definition timeout
153 if {$debug_me} {exp_internal 0}
155 switch -exact -- $definition {
156 no-macro-info { return no-macro-info }
157 timeout { return timeout }
158 undefined { return undefined }
160 if {[llength $location] >= 1} {
161 return [concat $location [list $definition]]
170 # Call info_macro to show the definition of MACRO. Expect a result of
171 # EXPECTED. Use WHERE in pass/fail messages to identify the context.
172 # Return non-zero if we should abort the entire test file, or zero if
174 proc check_macro {macro expected where} {
175 set func_def [info_macro $macro]
176 if {[string compare $func_def $expected] == 0} {
177 pass "info macro $macro $where"
179 switch -exact -- $func_def {
181 xfail "executable includes no macro debugging information"
185 fail "info macro $macro $where (undefined)"
189 fail "info macro $macro $where (timeout)"
192 fail "info macro $macro $where"
200 # List the function FUNC, and then show the definition of MACRO,
201 # expecting the result EXPECTED.
202 proc list_and_check_macro {func macro expected} {
203 gdb_test "list $func" ".*${func}.*" "list $func for $macro"
204 return [check_macro $macro $expected "after `list $func'"]
208 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
210 set macro_support "unknown"
211 send_gdb "info source\n"
212 gdb_test_multiple "info source" "Test macro information" {
213 -re "Includes preprocessor macro info\..*$gdb_prompt $" {
215 verbose "Source has macro information"
217 -re "Does not include preprocessor macro info\..*$gdb_prompt $" {
219 verbose "Source has no macro information"
222 warning "couldn't check macro support (no valid response)."
225 if {$macro_support == 0} {
226 unsupported "Skipping test because debug information does not include macro information."
231 list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
232 list_and_check_macro macscp3_2 WHERE {macscp3.h macscp1.c {before macscp3_2}}
235 # Assuming the current position inside program by `list' from above.
236 gdb_test "info macro FROM_COMMANDLINE" \
237 "Defined at \[^\r\n\]*:0\r\n-DFROM_COMMANDLINE=ARG"
240 # Although GDB's macro table structures distinguish between multiple
241 # #inclusions of the same file, GDB's other structures don't. So the
242 # `list' command here doesn't reliably select one #inclusion or the
243 # other, even though it could. It would be nice to eventually change
244 # GDB's structures to handle this correctly.
245 gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
246 switch -exact -- [info_macro WHERE] {
247 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
248 pass "info macro WHERE after `list macscp_4_2_from_macscp2'"
250 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
251 setup_kfail *-*-* "gdb/555"
252 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (gdb/555)"
255 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
257 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
260 gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
261 switch -exact -- [info_macro WHERE] {
262 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
263 pass "info macro WHERE after `list macscp_4_2_from_macscp3'"
265 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
266 setup_kfail *-*-* "gdb/555"
267 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (gdb/555)"
270 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
272 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
276 #### Test the selection of the macro scope by the current frame.
278 ### A table of functions, in the order they will be reached, which is
279 ### also the order they appear in the preprocessed output. Each entry
280 ### has the form {FUNCNAME WHERE KFAILWHERE}, where:
281 ### - FUNCNAME is the name of the function,
282 ### - WHERE is the definition we expect to see for the macro `WHERE', as
283 ### returned by `info_macro', and
284 ### - KFAILWHERE is an alternate definition which should be reported
285 ### as a `known failure', due to GDB's inability to distinguish multiple
286 ### #inclusions of the same file.
287 ### KFAILWHERE may be omitted.
292 {macscp1.c {before macscp1_1}}
296 {macscp2.h macscp1.c {before macscp2_1}}
299 macscp4_1_from_macscp2
300 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
301 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
304 macscp4_2_from_macscp2
305 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
306 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
310 {macscp2.h macscp1.c {before macscp2_2}}
314 {macscp1.c {before macscp1_2}}
318 {macscp3.h macscp1.c {before macscp3_1}}
321 macscp4_1_from_macscp3
322 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
323 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
326 macscp4_2_from_macscp3
327 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
328 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
332 {macscp3.h macscp1.c {before macscp3_2}}
336 {macscp1.c {before macscp1_3}}
340 proc maybe_kfail { func test_name } {
341 # We can't get the right scope info when we're stopped in
342 # the macro4_ functions.
343 if {[string match macscp4_* $func]} {
344 kfail gdb/555 "$test_name"
350 # Start the program running.
351 if {! [runto_main]} {
352 fail "macro tests suppressed: couldn't run to main"
356 # Set a breakpoint on each of the functions.
357 foreach func_entry $funcs {
358 set func [lindex $func_entry 0]
359 gdb_test "break $func" "Breakpoint.*"
362 # Run to each of the breakpoints and check the definition (or lack
363 # thereof) of each macro.
364 for {set i 0} {$i < [llength $funcs]} {incr i} {
365 set func_entry [lindex $funcs $i]
366 set func [lindex $func_entry 0]
367 set expected [lindex $func_entry 1]
368 set kfail_expected [lindex $func_entry 2]
370 # Run to the breakpoint for $func.
371 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
373 # Check the macro WHERE.
374 set result [info_macro WHERE]
375 if {[string compare $result $expected] == 0} {
376 pass "info macro WHERE stopped in $func"
377 } elseif {[string compare $result $kfail_expected] == 0} {
378 setup_kfail *-*-* "gdb/555"
379 fail "info macro WHERE stopped in $func (gdb/555)"
380 } elseif {[string compare $result timeout] == 0} {
381 fail "info macro WHERE stopped in $func (timeout)"
383 fail "info macro WHERE stopped in $func"
386 # Check that the BEFORE_<func> macros for all prior functions are
387 # #defined, and that those for all subsequent functions are not.
388 for {set j 0} {$j < [llength $funcs]} {incr j} {
390 set func_j_entry [lindex $funcs $j]
391 set func_j [lindex $func_j_entry 0]
393 set before_macro "BEFORE_[string toupper $func_j]"
395 "$before_macro defined/undefined when stopped at $func"
396 set result [info_macro $before_macro]
399 if {[llength $result] >= 2 && \
400 [string compare [lindex $result end] {}] == 0} {
402 } elseif {[string compare $result timeout] == 0} {
403 fail "$test_name (timeout)"
405 maybe_kfail $func "$test_name"
408 switch -- [lindex $result end] {
409 undefined { pass $test_name }
410 timeout { fail "$test_name (timeout)" }
412 maybe_kfail $func "$test_name"
417 set until_macro "UNTIL_[string toupper $func_j]"
419 "$until_macro defined/undefined when stopped at $func"
420 set result [info_macro $until_macro]
423 switch -- [lindex $result end] {
424 undefined { pass $test_name }
425 timeout { fail "$test_name (timeout)" }
427 maybe_kfail $func "$test_name"
431 if {[llength $result] >= 2 && \
432 [string compare [lindex $result end] {}] == 0} {
434 } elseif {[string compare $result timeout] == 0} {
435 fail "$test_name (timeout)"
437 maybe_kfail $func "$test_name"
444 gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
445 "Breakpoint.*at.* file .*, line.*" \
446 "breakpoint macscp_expr"
448 gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
450 gdb_test "print address.addr" \
454 gdb_test "print MACRO_TO_EXPAND" \
455 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
456 "print expression with macro before define."
458 gdb_test "next" "foo = 1;" "next to definition 1"
460 gdb_test "print MACRO_TO_EXPAND" \
462 "print expression with macro in scope."
464 gdb_test "macro define MACRO_TO_EXPAND 72" \
466 "user macro override"
468 gdb_test "print MACRO_TO_EXPAND" \
472 gdb_test "macro undef MACRO_TO_EXPAND" \
474 "remove user override"
476 gdb_test "print MACRO_TO_EXPAND" \
478 "print expression with macro after removing override"
480 gdb_test "next" "foo = 2;" "next to definition 2"
482 gdb_test "print MACRO_TO_EXPAND" \
483 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
484 "print expression with macro after undef."
486 gdb_test "macro define MACRO_TO_EXPAND 5" \
490 gdb_test "print MACRO_TO_EXPAND" \
492 "expansion of defined macro"
494 gdb_test "macro list" \
495 "macro define MACRO_TO_EXPAND 5" \
498 gdb_test "macro define MACRO_TO_EXPAND(x) x" \
500 "basic redefine, macro with args"
502 gdb_test "print MACRO_TO_EXPAND (7)" \
504 "expansion of macro with arguments"
506 gdb_test "macro undef MACRO_TO_EXPAND" \
510 gdb_test "print MACRO_TO_EXPAND" \
511 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
512 "print expression with macro after user undef."
514 # Regression test; this used to crash.
515 gdb_test "macro define" \
516 "usage: macro define.*" \
517 "macro define with no arguments"
519 # Regression test; this used to crash.
520 gdb_test "macro undef" \
521 "usage: macro undef.*" \
522 "macro undef with no arguments"
526 # The macro FIFTY_SEVEN is in scope at this point.
527 send_gdb "p FIFTY_\t"
529 -re "^p FIFTY_SEVEN $"\
532 -re "^.* = 57.*$gdb_prompt $"\
533 { pass "complete 'p FIFTY_SEVEN'"}
534 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'"}
535 timeout {fail "(timeout) complete 'p FIFTY_SEVEN'"}
538 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'" }
539 timeout { fail "(timeout) complete 'p FIFTY_SEVEN' 2" }
542 # The macro TWENTY_THREE is not in scope.
543 send_gdb "p TWENTY_\t"
545 -re "^p TWENTY_\\\x07$"\
548 -re "No symbol \"TWENTY_\" in current context\\..*$gdb_prompt $"\
549 { pass "complete 'p TWENTY_'"}
550 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'"}
551 timeout {fail "(timeout) complete 'p TWENTY_'"}
554 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'" }
555 timeout { fail "(timeout) complete 'p TWENTY_' 2" }
558 # The macro FORTY_EIGHT was undefined and thus is not in scope.
559 send_gdb "p FORTY_\t"
561 -re "^p FORTY_\\\x07$"\
564 -re "No symbol \"FORTY_\" in current context\\..*$gdb_prompt $"\
565 { pass "complete 'p FORTY_'"}
566 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'"}
567 timeout {fail "(timeout) complete 'p FORTY_'"}
570 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'" }
571 timeout { fail "(timeout) complete 'p FORTY_' 2" }
574 gdb_test "macro define TWENTY_THREE 25" \
576 "defining TWENTY_THREE"
578 # User-defined macros are always in scope.
579 send_gdb "p TWENTY_\t"
581 -re "^p TWENTY_THREE $"\
584 -re "^.* = 25.*$gdb_prompt $"\
585 { pass "complete 'p TWENTY_THREE'"}
586 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'"}
587 timeout {fail "(timeout) complete 'p TWENTY_THREE'"}
590 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'" }
591 timeout { fail "(timeout) complete 'p TWENTY_THREE' 2" }
596 gdb_test "macro expand SPLICE(x, y)" \
598 "basic macro splicing"
600 gdb_test "macro define robotinvasion 2010" \
602 "define splice helper"
604 gdb_test "macro expand SPLICE(robot, invasion)" \
605 "expands to: *2010" \
606 "splicing plus expansion"
610 gdb_test "macro define va_c99(...) varfunc (fixedarg, __VA_ARGS__)" \
612 "define first varargs helper"
614 gdb_test "macro define va2_c99(x, y, ...) varfunc (fixedarg, x, y, __VA_ARGS__)" \
616 "define second varargs helper"
618 gdb_test "macro define va_gnu(args...) varfunc (fixedarg, args)" \
620 "define third varargs helper"
622 gdb_test "macro define va2_gnu(args...) varfunc (fixedarg, ## args)" \
624 "define fourth varargs helper"
626 gdb_test "macro expand va_c99(one, two, three)" \
627 "expands to: *varfunc \\(fixedarg, *one, two, three\\)" \
628 "c99 varargs expansion"
630 gdb_test "macro expand va_c99()" \
631 "expands to: *varfunc \\(fixedarg, *\\)" \
632 "c99 varargs expansion without an argument"
634 gdb_test "macro expand va2_c99(one, two, three, four)" \
635 "expands to: *varfunc \\(fixedarg, *one, two, three, four\\)" \
636 "c99 varargs expansion, multiple formal arguments"
638 gdb_test "macro expand va_gnu(one, two, three, four)" \
639 "expands to: *varfunc \\(fixedarg, *one, two, three, four\\)" \
640 "gnu varargs expansion"
642 gdb_test "macro expand va_gnu()" \
643 "expands to: *varfunc \\(fixedarg, *\\)" \
644 "gnu varargs expansion without an argument"
646 gdb_test "macro expand va2_gnu()" \
647 "expands to: *varfunc \\(fixedarg\\)" \
648 "gnu varargs expansion special splicing without an argument"
650 # Stringification tests.
652 gdb_test "macro define str(x) #x" \
654 "define stringification macro"
656 gdb_test "macro define maude 5" \
658 "define first stringification helper"
660 gdb_test "macro define xstr(x) str(x)" \
662 "define second stringification helper"
664 gdb_test "print str(5)" \
668 gdb_test "print str(hi bob)" \
670 "stringify with one space"
672 gdb_test "print str( hi bob )" \
674 "stringify with many spaces"
676 gdb_test "print str(hi \"bob\")" \
677 " = \"hi \\\\\"bob\\\\\"\"" \
678 "stringify with quotes"
680 gdb_test "print str(hi \\bob\\)" \
681 " = \"hi \\\\\\\\bob\\\\\\\\\"" \
682 "stringify with backslashes"
684 gdb_test "print str(maude)" \
686 "stringify without substitution"
688 gdb_test "print xstr(maude)" \
690 "stringify with substitution"
692 # Regression test for pp-number bug.
693 gdb_test "macro define si_addr fields.fault.si_addr" \
695 "define si_addr macro"
696 gdb_test "macro expand siginfo.si_addr" \
697 "expands to: siginfo.fields.fault.si_addr" \
698 "macro expand siginfo.si_addr"