2 # Copyright 2002, 2007, 2008 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 binfile ${objdir}/${subdir}/${testfile}
30 get_compiler_info ${binfile}
31 if [test_compiler_info gcc*] {
32 lappend options additional_flags=-g3
35 if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable $options] != "" } {
42 gdb_reinitialize_dir $srcdir/$subdir
46 # Ask GDB to show the current definition of MACRO, and return a list
47 # describing the result.
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 ... .
53 # If GDB says that MACRO has no definition, return the string `undefined'.
55 # If GDB complains that it doesn't have any information about
56 # preprocessor macro definitions, return the string `no-macro-info'.
58 # If expect times out waiting for GDB, we return the string `timeout'.
60 # If GDB's output doesn't otherwise match what we're expecting, we
61 # return the empty string.
63 proc info_macro {macro} {
67 set filepat {macscp[0-9]+\.[ch]}
71 send_gdb "info macro ${macro}\n"
75 if {$debug_me} {exp_internal 1}
77 -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
78 # `location' and `definition' should be empty when we see
80 if {[llength $location] == 0 && [llength $definition] == 0} {
81 set location $expect_out(1,string)
84 # Exit this expect loop, with a result indicating failure.
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
91 if {[llength $location] == 0 && [llength $definition] == 0} {
92 set definition undefined
95 # Exit this expect loop, with a result indicating failure.
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)
108 # Exit this expect loop, with a result indicating failure.
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)
120 # Exit this expect loop, with a result indicating failure.
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)
130 # Exit this expect loop, with a result indicating failure.
134 -re "has no preprocessor macro information.*$gdb_prompt $" {
135 set definition no-macro-info
137 -re "$gdb_prompt $" {
138 # Exit the expect loop; let the existing value of `definition'
139 # indicate failure or success.
142 set definition timeout
145 if {$debug_me} {exp_internal 0}
147 switch -exact -- $definition {
148 no-macro-info { return no-macro-info }
149 timeout { return timeout }
152 if {[llength $location] >= 1} {
153 return [concat $location [list $definition]]
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
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"
171 switch -exact -- $func_def {
173 xfail "executable includes no macro debugging information"
177 fail "info macro $macro $where (timeout)"
180 fail "info macro $macro $where"
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'"]
196 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
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}}
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'"
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)"
218 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
220 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
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'"
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)"
233 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
235 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
239 #### Test the selection of the macro scope by the current frame.
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.
255 {macscp1.c {before macscp1_1}}
259 {macscp2.h macscp1.c {before macscp2_1}}
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}}
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}}
273 {macscp2.h macscp1.c {before macscp2_2}}
277 {macscp1.c {before macscp1_2}}
281 {macscp3.h macscp1.c {before macscp3_1}}
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}}
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}}
295 {macscp3.h macscp1.c {before macscp3_2}}
299 {macscp1.c {before macscp1_3}}
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"
313 # Start the program running.
314 if {! [runto_main]} {
315 fail "macro tests suppressed: couldn't run to main"
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.*"
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]
333 # Run to the breakpoint for $func.
334 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
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)"
346 fail "info macro WHERE stopped in $func"
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} {
353 set func_j_entry [lindex $funcs $j]
354 set func_j [lindex $func_j_entry 0]
356 set before_macro "BEFORE_[string toupper $func_j]"
358 "$before_macro defined/undefined when stopped at $func"
359 set result [info_macro $before_macro]
362 if {[llength $result] >= 2 && \
363 [string compare [lindex $result end] {}] == 0} {
365 } elseif {[string compare $result timeout] == 0} {
366 fail "$test_name (timeout)"
368 maybe_kfail $func "$test_name"
371 switch -- [lindex $result end] {
372 undefined { pass $test_name }
373 timeout { fail "$test_name (timeout)" }
375 maybe_kfail $func "$test_name"
380 set until_macro "UNTIL_[string toupper $func_j]"
382 "$until_macro defined/undefined when stopped at $func"
383 set result [info_macro $until_macro]
386 switch -- [lindex $result end] {
387 undefined { pass $test_name }
388 timeout { fail "$test_name (timeout)" }
390 maybe_kfail $func "$test_name"
394 if {[llength $result] >= 2 && \
395 [string compare [lindex $result end] {}] == 0} {
397 } elseif {[string compare $result timeout] == 0} {
398 fail "$test_name (timeout)"
400 maybe_kfail $func "$test_name"
407 gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
408 "Breakpoint.*at.* file .*, line.*" \
409 "breakpoint macscp_expr"
411 gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
413 gdb_test "print MACRO_TO_EXPAND" \
414 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
415 "print expression with macro before define."
417 gdb_test "next" "foo = 1;" "next to definition"
419 gdb_test "print MACRO_TO_EXPAND" \
421 "print expression with macro in scope."
423 gdb_test "macro define MACRO_TO_EXPAND 72" \
425 "user macro override"
427 gdb_test "print MACRO_TO_EXPAND" \
431 gdb_test "macro undef MACRO_TO_EXPAND" \
433 "remove user override"
435 gdb_test "print MACRO_TO_EXPAND" \
437 "print expression with macro after removing override"
439 gdb_test "next" "foo = 2;" "next to definition"
441 gdb_test "print MACRO_TO_EXPAND" \
442 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
443 "print expression with macro after undef."
445 gdb_test "macro define MACRO_TO_EXPAND 5" \
449 gdb_test "print MACRO_TO_EXPAND" \
451 "expansion of defined macro"
453 gdb_test "macro list" \
454 "macro define MACRO_TO_EXPAND 5" \
457 gdb_test "macro define MACRO_TO_EXPAND(x) x" \
459 "basic redefine, macro with args"
461 gdb_test "print MACRO_TO_EXPAND (7)" \
463 "expansion of macro with arguments"
465 gdb_test "macro undef MACRO_TO_EXPAND" \
469 gdb_test "print MACRO_TO_EXPAND" \
470 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
471 "print expression with macro after user undef."
473 # Regression test; this used to crash.
474 gdb_test "macro define" \
475 "usage: macro define.*" \
476 "macro define with no arguments"
478 # Regression test; this used to crash.
479 gdb_test "macro undef" \
480 "usage: macro undef.*" \
481 "macro undef with no arguments"
485 # The macro FIFTY_SEVEN is in scope at this point.
486 send_gdb "p FIFTY_\t"
488 -re "^p FIFTY_SEVEN $"\
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'"}
497 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'" }
498 timeout { fail "(timeout) complete 'p FIFTY_SEVEN' 2" }
501 # The macro TWENTY_THREE is not in scope.
502 send_gdb "p TWENTY_\t"
504 -re "^p TWENTY_\\\x07$"\
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_'"}
513 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'" }
514 timeout { fail "(timeout) complete 'p TWENTY_' 2" }
517 # The macro FORTY_EIGHT was undefined and thus is not in scope.
518 send_gdb "p FORTY_\t"
520 -re "^p FORTY_\\\x07$"\
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_'"}
529 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'" }
530 timeout { fail "(timeout) complete 'p FORTY_' 2" }
533 gdb_test "macro define TWENTY_THREE 25" \
535 "defining TWENTY_THREE"
537 # User-defined macros are always in scope.
538 send_gdb "p TWENTY_\t"
540 -re "^p TWENTY_THREE $"\
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'"}
549 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'" }
550 timeout { fail "(timeout) complete 'p TWENTY_THREE' 2" }
555 gdb_test "macro expand SPLICE(x, y)" \
557 "basic macro splicing"
559 gdb_test "macro define robotinvasion 2010" \
561 "define splice helper"
563 gdb_test "macro expand SPLICE(robot, invasion)" \
564 "expands to: *2010" \
565 "splicing plus expansion"
569 gdb_test "macro define va_c99(...) fprintf (stderr, __VA_ARGS__)" \
571 "define first varargs helper"
573 gdb_test "macro define va2_c99(x, y, ...) fprintf (stderr, x, y, __VA_ARGS__)" \
575 "define second varargs helper"
577 gdb_test "macro define va_gnu(args...) fprintf (stderr, args)" \
579 "define third varargs helper"
581 gdb_test "macro define va2_gnu(args...) fprintf (stderr, ## args)" \
583 "define fourth varargs helper"
585 gdb_test "macro expand va_c99(one, two, three)" \
586 "expands to: *fprintf \\(stderr, *one, two, three\\)" \
587 "c99 varargs expansion"
589 gdb_test "macro expand va_c99()" \
590 "expands to: *fprintf \\(stderr, *\\)" \
591 "c99 varargs expansion without an argument"
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"
597 gdb_test "macro expand va_gnu(one, two, three, four)" \
598 "expands to: *fprintf \\(stderr, *one, two, three, four\\)" \
599 "gnu varargs expansion"
601 gdb_test "macro expand va_gnu()" \
602 "expands to: *fprintf \\(stderr, *\\)" \
603 "gnu varargs expansion without an argument"
605 gdb_test "macro expand va2_gnu()" \
606 "expands to: *fprintf \\(stderr\\)" \
607 "gnu varargs expansion special splicing without an argument"
609 # Stringification tests.
611 gdb_test "macro define str(x) #x" \
613 "define stringification macro"
615 gdb_test "macro define maude 5" \
617 "define first stringification helper"
619 gdb_test "macro define xstr(x) str(x)" \
621 "define second stringification helper"
623 gdb_test "print str(5)" \
627 gdb_test "print str(hi bob)" \
629 "stringify with one space"
631 gdb_test "print str( hi bob )" \
633 "stringify with many spaces"
635 gdb_test "print str(hi \"bob\")" \
636 " = \"hi \\\\\"bob\\\\\"\"" \
637 "stringify with quotes"
639 gdb_test "print str(hi \\bob\\)" \
640 " = \"hi \\\\\\\\bob\\\\\\\\\"" \
641 "stringify with backslashes"
643 gdb_test "print str(maude)" \
645 "stringify without substitution"
647 gdb_test "print xstr(maude)" \
649 "stringify with substitution"