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}
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} {
72 set filepat {macscp[0-9]+\.[ch]}
76 send_gdb "info macro ${macro}\n"
80 if {$debug_me} {exp_internal 1}
82 -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
83 # `location' and `definition' should be empty when we see
85 if {[llength $location] == 0 && [llength $definition] == 0} {
86 set location $expect_out(1,string)
89 # Exit this expect loop, with a result indicating failure.
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
96 if {[llength $location] == 0 && [llength $definition] == 0} {
97 set definition undefined
100 # Exit this expect loop, with a result indicating failure.
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)
113 # Exit this expect loop, with a result indicating failure.
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)
125 # Exit this expect loop, with a result indicating failure.
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)
135 # Exit this expect loop, with a result indicating failure.
139 -re "has no preprocessor macro information.*$gdb_prompt $" {
140 set definition no-macro-info
142 -re "$gdb_prompt $" {
143 # Exit the expect loop; let the existing value of `definition'
144 # indicate failure or success.
147 set definition timeout
150 if {$debug_me} {exp_internal 0}
152 switch -exact -- $definition {
153 no-macro-info { return no-macro-info }
154 timeout { return timeout }
157 if {[llength $location] >= 1} {
158 return [concat $location [list $definition]]
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
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"
176 switch -exact -- $func_def {
178 xfail "executable includes no macro debugging information"
182 fail "info macro $macro $where (timeout)"
185 fail "info macro $macro $where"
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'"]
201 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
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}}
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'"
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)"
223 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
225 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
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'"
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)"
238 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
240 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
244 #### Test the selection of the macro scope by the current frame.
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.
260 {macscp1.c {before macscp1_1}}
264 {macscp2.h macscp1.c {before macscp2_1}}
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}}
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}}
278 {macscp2.h macscp1.c {before macscp2_2}}
282 {macscp1.c {before macscp1_2}}
286 {macscp3.h macscp1.c {before macscp3_1}}
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}}
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}}
300 {macscp3.h macscp1.c {before macscp3_2}}
304 {macscp1.c {before macscp1_3}}
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"
318 # Start the program running.
319 if {! [runto_main]} {
320 fail "macro tests suppressed: couldn't run to main"
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.*"
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]
338 # Run to the breakpoint for $func.
339 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
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)"
351 fail "info macro WHERE stopped in $func"
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} {
358 set func_j_entry [lindex $funcs $j]
359 set func_j [lindex $func_j_entry 0]
361 set before_macro "BEFORE_[string toupper $func_j]"
363 "$before_macro defined/undefined when stopped at $func"
364 set result [info_macro $before_macro]
367 if {[llength $result] >= 2 && \
368 [string compare [lindex $result end] {}] == 0} {
370 } elseif {[string compare $result timeout] == 0} {
371 fail "$test_name (timeout)"
373 maybe_kfail $func "$test_name"
376 switch -- [lindex $result end] {
377 undefined { pass $test_name }
378 timeout { fail "$test_name (timeout)" }
380 maybe_kfail $func "$test_name"
385 set until_macro "UNTIL_[string toupper $func_j]"
387 "$until_macro defined/undefined when stopped at $func"
388 set result [info_macro $until_macro]
391 switch -- [lindex $result end] {
392 undefined { pass $test_name }
393 timeout { fail "$test_name (timeout)" }
395 maybe_kfail $func "$test_name"
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"
412 gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
413 "Breakpoint.*at.* file .*, line.*" \
414 "breakpoint macscp_expr"
416 gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
418 gdb_test "print address.addr" \
422 gdb_test "print MACRO_TO_EXPAND" \
423 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
424 "print expression with macro before define."
426 gdb_test "next" "foo = 1;" "next to definition 1"
428 gdb_test "print MACRO_TO_EXPAND" \
430 "print expression with macro in scope."
432 gdb_test "macro define MACRO_TO_EXPAND 72" \
434 "user macro override"
436 gdb_test "print MACRO_TO_EXPAND" \
440 gdb_test "macro undef MACRO_TO_EXPAND" \
442 "remove user override"
444 gdb_test "print MACRO_TO_EXPAND" \
446 "print expression with macro after removing override"
448 gdb_test "next" "foo = 2;" "next to definition 2"
450 gdb_test "print MACRO_TO_EXPAND" \
451 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
452 "print expression with macro after undef."
454 gdb_test "macro define MACRO_TO_EXPAND 5" \
458 gdb_test "print MACRO_TO_EXPAND" \
460 "expansion of defined macro"
462 gdb_test "macro list" \
463 "macro define MACRO_TO_EXPAND 5" \
466 gdb_test "macro define MACRO_TO_EXPAND(x) x" \
468 "basic redefine, macro with args"
470 gdb_test "print MACRO_TO_EXPAND (7)" \
472 "expansion of macro with arguments"
474 gdb_test "macro undef MACRO_TO_EXPAND" \
478 gdb_test "print MACRO_TO_EXPAND" \
479 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
480 "print expression with macro after user undef."
482 # Regression test; this used to crash.
483 gdb_test "macro define" \
484 "usage: macro define.*" \
485 "macro define with no arguments"
487 # Regression test; this used to crash.
488 gdb_test "macro undef" \
489 "usage: macro undef.*" \
490 "macro undef with no arguments"
494 # The macro FIFTY_SEVEN is in scope at this point.
495 send_gdb "p FIFTY_\t"
497 -re "^p FIFTY_SEVEN $"\
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'"}
506 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'" }
507 timeout { fail "(timeout) complete 'p FIFTY_SEVEN' 2" }
510 # The macro TWENTY_THREE is not in scope.
511 send_gdb "p TWENTY_\t"
513 -re "^p TWENTY_\\\x07$"\
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_'"}
522 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'" }
523 timeout { fail "(timeout) complete 'p TWENTY_' 2" }
526 # The macro FORTY_EIGHT was undefined and thus is not in scope.
527 send_gdb "p FORTY_\t"
529 -re "^p FORTY_\\\x07$"\
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_'"}
538 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'" }
539 timeout { fail "(timeout) complete 'p FORTY_' 2" }
542 gdb_test "macro define TWENTY_THREE 25" \
544 "defining TWENTY_THREE"
546 # User-defined macros are always in scope.
547 send_gdb "p TWENTY_\t"
549 -re "^p TWENTY_THREE $"\
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'"}
558 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'" }
559 timeout { fail "(timeout) complete 'p TWENTY_THREE' 2" }
564 gdb_test "macro expand SPLICE(x, y)" \
566 "basic macro splicing"
568 gdb_test "macro define robotinvasion 2010" \
570 "define splice helper"
572 gdb_test "macro expand SPLICE(robot, invasion)" \
573 "expands to: *2010" \
574 "splicing plus expansion"
578 gdb_test "macro define va_c99(...) varfunc (fixedarg, __VA_ARGS__)" \
580 "define first varargs helper"
582 gdb_test "macro define va2_c99(x, y, ...) varfunc (fixedarg, x, y, __VA_ARGS__)" \
584 "define second varargs helper"
586 gdb_test "macro define va_gnu(args...) varfunc (fixedarg, args)" \
588 "define third varargs helper"
590 gdb_test "macro define va2_gnu(args...) varfunc (fixedarg, ## args)" \
592 "define fourth varargs helper"
594 gdb_test "macro expand va_c99(one, two, three)" \
595 "expands to: *varfunc \\(fixedarg, *one, two, three\\)" \
596 "c99 varargs expansion"
598 gdb_test "macro expand va_c99()" \
599 "expands to: *varfunc \\(fixedarg, *\\)" \
600 "c99 varargs expansion without an argument"
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"
606 gdb_test "macro expand va_gnu(one, two, three, four)" \
607 "expands to: *varfunc \\(fixedarg, *one, two, three, four\\)" \
608 "gnu varargs expansion"
610 gdb_test "macro expand va_gnu()" \
611 "expands to: *varfunc \\(fixedarg, *\\)" \
612 "gnu varargs expansion without an argument"
614 gdb_test "macro expand va2_gnu()" \
615 "expands to: *varfunc \\(fixedarg\\)" \
616 "gnu varargs expansion special splicing without an argument"
618 # Stringification tests.
620 gdb_test "macro define str(x) #x" \
622 "define stringification macro"
624 gdb_test "macro define maude 5" \
626 "define first stringification helper"
628 gdb_test "macro define xstr(x) str(x)" \
630 "define second stringification helper"
632 gdb_test "print str(5)" \
636 gdb_test "print str(hi bob)" \
638 "stringify with one space"
640 gdb_test "print str( hi bob )" \
642 "stringify with many spaces"
644 gdb_test "print str(hi \"bob\")" \
645 " = \"hi \\\\\"bob\\\\\"\"" \
646 "stringify with quotes"
648 gdb_test "print str(hi \\bob\\)" \
649 " = \"hi \\\\\\\\bob\\\\\\\\\"" \
650 "stringify with backslashes"
652 gdb_test "print str(maude)" \
654 "stringify without substitution"
656 gdb_test "print xstr(maude)" \
658 "stringify with substitution"
660 # Regression test for pp-number bug.
661 gdb_test "macro define si_addr fields.fault.si_addr" \
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"