2 # Copyright 2002-2023 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/>.
18 standard_testfile macscp1.c
19 set objfile [standard_output_file ${testfile}.o]
21 set options {debug macros additional_flags=-DFROM_COMMANDLINE=ARG}
23 # Generate the intermediate object file. This is required by Darwin to
24 # have access to the .debug_macinfo section.
25 if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${objfile}" \
26 object $options] != ""
27 || [gdb_compile "${objfile}" "${binfile}" executable $options] != "" } {
28 untested "failed to compile"
32 clean_restart ${binfile}
35 # Ask GDB to show the current definition of MACRO, and return a list
36 # describing the result.
38 # The return value has the form {FILE1 FILE2 ... DEF}, which means
39 # that MACRO has the definition `DEF', and was defined in `FILE1',
40 # which was included from `FILE2', included from ... .
42 # If GDB says that MACRO has no definition, return the string `undefined'.
44 # If GDB complains that it doesn't have any information about
45 # preprocessor macro definitions, return the string `no-macro-info'.
47 # If expect times out waiting for GDB, we return the string `timeout'.
49 # If GDB's output doesn't otherwise match what we're expecting, we
50 # return the empty string.
52 proc info_macro {macro} {
55 set filepat {macscp[0-9]+\.[ch]}
59 # Line number zero is set for macros defined from the compiler command-line.
60 # Such macros are not being tested by this function.
61 set nonzero {[1-9][0-9]*}
63 send_gdb "info macro ${macro}\n"
67 if {$debug_me} {exp_internal 1}
69 -re "Defined at \[^\r\n\]*(${filepat}):${nonzero}\[\r\n\]" {
70 # `location' and `definition' should be empty when we see
72 if {[llength $location] == 0 && [llength $definition] == 0} {
73 set location $expect_out(1,string)
76 # Exit this expect loop, with a result indicating failure.
80 -re "The symbol `${macro}' has no definition as a C/C\\+\\+ preprocessor macro\[^\r\n\]*\[\r\n\]" {
81 # `location' and `definition' should be empty when we see
83 if {[llength $location] == 0 && [llength $definition] == 0} {
84 set definition undefined
87 # Exit this expect loop, with a result indicating failure.
91 -re "^\[\r\n\]* included at \[^\r\n\]*(${filepat}):${nonzero}\[\r\n\]" {
92 # `location' should *not* be empty when we see this
93 # message. It should have recorded at least the initial
94 # `Defined at ' message (for definitions) or ` at' message
95 # (for undefined symbols).
96 if {[llength $location] != 0} {
97 lappend location $expect_out(1,string)
100 # Exit this expect loop, with a result indicating failure.
104 -re "^\[\r\n\]*at \[^\r\n\]*(${filepat}):${nonzero}\[\r\n\]" {
105 # This appears after a `has no definition' message.
106 # `location' should be empty when we see it.
107 if {[string compare $definition undefined] == 0 \
108 && [llength $location] == 0} {
109 set location $expect_out(1,string)
112 # Exit this expect loop, with a result indicating failure.
116 -re "#define ${macro} (\[^\r\n\]*)\[\r\n\]" {
117 # `definition' should be empty when we see this message.
118 if {[string compare $definition ""] == 0} {
119 set definition $expect_out(1,string)
122 # Exit this expect loop, with a result indicating failure.
126 -re "has no preprocessor macro information.*$gdb_prompt $" {
127 set definition no-macro-info
129 -re "$gdb_prompt $" {
130 # Exit the expect loop; let the existing value of `definition'
131 # indicate failure or success.
134 set definition timeout
137 if {$debug_me} {exp_internal 0}
139 switch -exact -- $definition {
140 no-macro-info { return no-macro-info }
141 timeout { return timeout }
142 undefined { return undefined }
144 if {[llength $location] >= 1} {
145 return [concat $location [list $definition]]
154 # Call info_macro to show the definition of MACRO. Expect a result of
155 # EXPECTED. Use WHERE in pass/fail messages to identify the context.
156 # Return non-zero if we should abort the entire test file, or zero if
158 proc check_macro {macro expected where} {
159 set func_def [info_macro $macro]
160 if {[string compare $func_def $expected] == 0} {
161 pass "info macro $macro $where"
163 switch -exact -- $func_def {
165 xfail "executable includes no macro debugging information"
169 fail "info macro $macro $where (undefined)"
173 fail "info macro $macro $where (timeout)"
176 fail "info macro $macro $where"
184 # List the function FUNC, and then show the definition of MACRO,
185 # expecting the result EXPECTED.
186 proc list_and_check_macro {func macro expected} {
187 gdb_test "list $func" ".*${func}.*" "list $func for $macro"
188 return [check_macro $macro $expected "after `list $func'"]
191 gdb_test "list -q main" ".*main.*" "list main for support check"
192 set macro_support "unknown"
193 gdb_test_multiple "info source" "test macro information" {
194 -re "Includes preprocessor macro info\..*$gdb_prompt $" {
196 verbose "Source has macro information"
198 -re "Does not include preprocessor macro info\..*$gdb_prompt $" {
200 verbose "Source has no macro information"
203 warning "couldn't check macro support (no valid response)."
206 if {$macro_support == 0} {
207 unsupported "skipping test because debug information does not include macro information."
211 list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}
212 list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
213 list_and_check_macro macscp3_2 WHERE {macscp3.h macscp1.c {before macscp3_2}}
216 # Assuming the current position inside program by `list' from above.
217 gdb_test "info macro FROM_COMMANDLINE" \
218 "Defined at \[^\r\n\]*:0\r\n-DFROM_COMMANDLINE=ARG"
220 gdb_test "info macro __FILE__" "#define __FILE__ \".*macscp3.h\"" \
221 "info macro __FILE__ before running"
222 gdb_test "info macro __LINE__" "#define __LINE__ 26" \
223 "info macro __LINE__ before running"
225 # Although GDB's macro table structures distinguish between multiple
226 # #inclusions of the same file, GDB's other structures don't. So the
227 # `list' command here doesn't reliably select one #inclusion or the
228 # other, even though it could. It would be nice to eventually change
229 # GDB's structures to handle this correctly.
230 gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
231 switch -exact -- [info_macro WHERE] {
232 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
233 pass "info macro WHERE after `list macscp_4_2_from_macscp2'"
235 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
236 setup_kfail "gdb/7660" *-*-*
237 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (gdb/7660)"
240 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
242 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
245 gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
246 switch -exact -- [info_macro WHERE] {
247 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
248 pass "info macro WHERE after `list macscp_4_2_from_macscp3'"
250 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
251 setup_kfail "gdb/7660" *-*-*
252 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (gdb/7660)"
255 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
257 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
261 #### Test the selection of the macro scope by the current frame.
263 ### A table of functions, in the order they will be reached, which is
264 ### also the order they appear in the preprocessed output. Each entry
265 ### has the form {FUNCNAME WHERE KFAILWHERE}, where:
266 ### - FUNCNAME is the name of the function,
267 ### - WHERE is the definition we expect to see for the macro `WHERE', as
268 ### returned by `info_macro', and
269 ### - KFAILWHERE is an alternate definition which should be reported
270 ### as a `known failure', due to GDB's inability to distinguish multiple
271 ### #inclusions of the same file.
272 ### KFAILWHERE may be omitted.
277 {macscp1.c {before macscp1_1}}
281 {macscp2.h macscp1.c {before macscp2_1}}
284 macscp4_1_from_macscp2
285 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
286 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
289 macscp4_2_from_macscp2
290 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
291 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
295 {macscp2.h macscp1.c {before macscp2_2}}
299 {macscp1.c {before macscp1_2}}
303 {macscp3.h macscp1.c {before macscp3_1}}
306 macscp4_1_from_macscp3
307 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
308 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
311 macscp4_2_from_macscp3
312 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
313 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
317 {macscp3.h macscp1.c {before macscp3_2}}
321 {macscp1.c {before macscp1_3}}
325 proc maybe_kfail { func test_name } {
326 # We can't get the right scope info when we're stopped in
327 # the macro4_ functions.
328 if {[string match macscp4_* $func]} {
329 kfail gdb/7660 "$test_name"
335 # Start the program running.
336 if {! [runto_main]} {
340 # Set a breakpoint on each of the functions.
341 foreach func_entry $funcs {
342 set func [lindex $func_entry 0]
343 gdb_test "break $func" "Breakpoint.*"
346 # Run to each of the breakpoints and check the definition (or lack
347 # thereof) of each macro.
348 for {set i 0} {$i < [llength $funcs]} {incr i} {
349 set func_entry [lindex $funcs $i]
350 set func [lindex $func_entry 0]
351 set expected [lindex $func_entry 1]
352 set kfail_expected [lindex $func_entry 2]
354 # Run to the breakpoint for $func.
355 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
357 # Check the macro WHERE.
358 set result [info_macro WHERE]
359 if {[string compare $result $expected] == 0} {
360 pass "info macro WHERE stopped in $func"
361 } elseif {[string compare $result $kfail_expected] == 0} {
362 setup_kfail "gdb/7660" *-*-*
363 fail "info macro WHERE stopped in $func (gdb/7660)"
364 } elseif {[string compare $result timeout] == 0} {
365 fail "info macro WHERE stopped in $func (timeout)"
367 fail "info macro WHERE stopped in $func"
370 # Check that the BEFORE_<func> macros for all prior functions are
371 # #defined, and that those for all subsequent functions are not.
372 for {set j 0} {$j < [llength $funcs]} {incr j} {
374 set func_j_entry [lindex $funcs $j]
375 set func_j [lindex $func_j_entry 0]
377 set before_macro "BEFORE_[string toupper $func_j]"
379 "$before_macro defined/undefined when stopped at $func"
380 set result [info_macro $before_macro]
383 if {[llength $result] >= 2 && \
384 [string compare [lindex $result end] {}] == 0} {
386 } elseif {[string compare $result timeout] == 0} {
387 fail "$test_name (timeout)"
389 maybe_kfail $func "$test_name"
392 switch -- [lindex $result end] {
393 undefined { pass $test_name }
394 timeout { fail "$test_name (timeout)" }
396 maybe_kfail $func "$test_name"
401 set until_macro "UNTIL_[string toupper $func_j]"
403 "$until_macro defined/undefined when stopped at $func"
404 set result [info_macro $until_macro]
407 switch -- [lindex $result end] {
408 undefined { pass $test_name }
409 timeout { fail "$test_name (timeout)" }
411 maybe_kfail $func "$test_name"
415 if {[llength $result] >= 2 && \
416 [string compare [lindex $result end] {}] == 0} {
418 } elseif {[string compare $result timeout] == 0} {
419 fail "$test_name (timeout)"
421 maybe_kfail $func "$test_name"
428 gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
429 "Breakpoint.*at.* file .*, line.*" \
430 "breakpoint macscp_expr"
432 gdb_test "cond \$bpnum foo == MACRO_TO_EXPAND" \
433 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
434 "macro MACRO_TO_EXPAND not in scope at breakpoint"
436 # Note that we choose the condition so that this breakpoint never
438 set l2 [gdb_get_line_number "set second breakpoint here"]
439 gdb_test "break $l2 if foo != MACRO_TO_EXPAND" \
441 "breakpoint macscp_expr using MACRO_TO_EXPAND"
443 gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
445 gdb_test "print address.addr" \
448 gdb_test "print MACRO_TO_EXPAND" \
449 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
450 "print expression with macro before define."
452 gdb_test "next" "foo = 1;.*here.*/" "next to definition 1"
454 gdb_test "print MACRO_TO_EXPAND" \
456 "print expression with macro in scope."
458 gdb_test_no_output "macro define MACRO_TO_EXPAND 72" \
459 "user macro override"
461 gdb_test "print MACRO_TO_EXPAND" \
465 gdb_test_no_output "macro undef MACRO_TO_EXPAND" \
466 "remove user override"
468 gdb_test "print MACRO_TO_EXPAND" \
470 "print expression with macro after removing override"
472 gdb_test "next" "foo = 2;.*" "next to definition 2"
474 gdb_test "print MACRO_TO_EXPAND" \
475 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
476 "print expression with macro after undef."
478 gdb_test_no_output "macro define MACRO_TO_EXPAND 5" \
481 gdb_test "print MACRO_TO_EXPAND" \
483 "expansion of defined macro"
485 gdb_test "macro list" \
486 "macro define MACRO_TO_EXPAND 5" \
489 gdb_test_no_output "macro define MACRO_TO_EXPAND(x) x" \
490 "basic redefine, macro with args"
492 gdb_test "print MACRO_TO_EXPAND (7)" \
494 "expansion of macro with arguments"
496 gdb_test_no_output "macro undef MACRO_TO_EXPAND" \
499 gdb_test "print MACRO_TO_EXPAND" \
500 "No symbol \"MACRO_TO_EXPAND\" in current context\." \
501 "print expression with macro after user undef."
503 # Regression test; this used to crash.
504 gdb_test "macro define" \
505 "usage: macro define.*" \
506 "macro define with no arguments"
508 # Regression test; this used to crash.
509 gdb_test "macro undef" \
510 "usage: macro undef.*" \
511 "macro undef with no arguments"
513 # Do completion tests if readline is used.
515 if { [readline_is_used] } {
517 # The macro FIFTY_SEVEN is in scope at this point.
518 send_gdb "p FIFTY_\t"
520 -re "^p FIFTY_SEVEN $" {
523 -re "^.* = 57.*$gdb_prompt $" {
524 pass "complete 'p FIFTY_SEVEN'"
526 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'" }
527 timeout { fail "(timeout) complete 'p FIFTY_SEVEN'" }
530 -re ".*$gdb_prompt $" { fail "complete 'p FIFTY_SEVEN'" }
531 timeout { fail "(timeout) complete 'p FIFTY_SEVEN' 2" }
534 # The macro TWENTY_THREE is not in scope.
535 send_gdb "p TWENTY_\t"
537 -re "^p TWENTY_\\\x07$" {
540 -re "No symbol \"TWENTY_\" in current context\\..*$gdb_prompt $" {
541 pass "complete 'p TWENTY_'"
543 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'" }
544 timeout { fail "(timeout) complete 'p TWENTY_'"}
547 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_'" }
548 timeout { fail "(timeout) complete 'p TWENTY_' 2" }
551 # The macro FORTY_EIGHT was undefined and thus is not in scope.
552 send_gdb "p FORTY_\t"
554 -re "^p FORTY_\\\x07$" {
557 -re "No symbol \"FORTY_\" in current context\\..*$gdb_prompt $" {
558 pass "complete 'p FORTY_'"
560 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'" }
561 timeout {fail "(timeout) complete 'p FORTY_'"}
564 -re ".*$gdb_prompt $" { fail "complete 'p FORTY_'" }
565 timeout { fail "(timeout) complete 'p FORTY_' 2" }
568 gdb_test_no_output "macro define TWENTY_THREE 25" \
569 "defining TWENTY_THREE"
571 # User-defined macros are always in scope.
572 send_gdb "p TWENTY_\t"
574 -re "^p TWENTY_THREE $" {
577 -re "^.* = 25.*$gdb_prompt $" {
578 pass "complete 'p TWENTY_THREE'"
580 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'"}
581 timeout { fail "(timeout) complete 'p TWENTY_THREE'" }
584 -re ".*$gdb_prompt $" { fail "complete 'p TWENTY_THREE'" }
585 timeout { fail "(timeout) complete 'p TWENTY_THREE' 2" }
591 gdb_test "macro expand SPLICE(x, y)" \
593 "basic macro splicing"
595 gdb_test_no_output "macro define robotinvasion 2010" \
596 "define splice helper"
598 gdb_test "macro expand SPLICE(robot, invasion)" \
599 "expands to: *2010" \
600 "splicing plus expansion"
604 gdb_test_no_output "macro define va_c99(...) varfunc (fixedarg, __VA_ARGS__)" \
605 "define first varargs helper"
607 gdb_test_no_output "macro define va2_c99(x, y, ...) varfunc (fixedarg, x, y, __VA_ARGS__)" \
608 "define second varargs helper"
610 gdb_test_no_output "macro define va_gnu(args...) varfunc (fixedarg, args)" \
611 "define third varargs helper"
613 gdb_test_no_output "macro define va2_gnu(args...) varfunc (fixedarg, ## args)" \
614 "define fourth varargs helper"
617 "macro define va3_cxx2a(x, ...) varfunc (x __VA_OPT__(,) __VA_ARGS__)" \
618 "define fifth varargs helper"
621 "macro define va4_cxx2a(x, ...) varfunc (x __VA_OPT__(, __VA_ARGS__))" \
622 "define sixth varargs helper"
625 "macro define va5_cxx2a(x, ...) varfunc (x __VA_OPT__(,) __VA_OPT__(__VA_ARGS__))" \
626 "define seventh varargs helper"
628 gdb_test "macro expand va_c99(one, two, three)" \
629 "expands to: *varfunc \\(fixedarg, *one, two, three\\)" \
630 "c99 varargs expansion"
632 gdb_test "macro expand va_c99()" \
633 "expands to: *varfunc \\(fixedarg, *\\)" \
634 "c99 varargs expansion without an argument"
636 gdb_test "macro expand va2_c99(one, two, three, four)" \
637 "expands to: *varfunc \\(fixedarg, *one, two, three, four\\)" \
638 "c99 varargs expansion, multiple formal arguments"
640 gdb_test "macro expand va_gnu(one, two, three, four)" \
641 "expands to: *varfunc \\(fixedarg, *one, two, three, four\\)" \
642 "gnu varargs expansion"
644 gdb_test "macro expand va_gnu()" \
645 "expands to: *varfunc \\(fixedarg, *\\)" \
646 "gnu varargs expansion without an argument"
648 gdb_test "macro expand va2_gnu()" \
649 "expands to: *varfunc \\(fixedarg\\)" \
650 "gnu varargs expansion special splicing without an argument"
652 gdb_test "macro expand va3_cxx2a(23)" \
653 "expands to: *varfunc \\(23 \\)" \
654 "C++2a __VA_OPT__ handling without variable argument"
656 gdb_test "macro expand va3_cxx2a(23, 24, 25)" \
657 "expands to: *varfunc \\(23, 24, 25\\)" \
658 "C++2a __VA_OPT__ handling with variable argument"
660 gdb_test "macro expand va4_cxx2a(23, 24, 25)" \
661 "expands to: *varfunc \\(23, 24, 25\\)" \
662 "C++2a __VA_OPT__ conditional __VA_ARGS__ handling with variable argument"
664 gdb_test "macro expand va4_cxx2a(23)" \
665 "expands to: *varfunc \\(23\\)" \
666 "C++2a __VA_OPT__ conditional __VA_ARGS__ handling without variable argument"
668 gdb_test "macro expand va5_cxx2a(23, 24, 25)" \
669 "expands to: *varfunc \\(23,24, 25\\)" \
670 "C++2a double __VA_OPT__ conditional __VA_ARGS__ handling with variable argument"
672 gdb_test "macro expand va5_cxx2a(23)" \
673 "expands to: *varfunc \\(23\\)" \
674 "C++2a double __VA_OPT__ conditional __VA_ARGS__ handling without variable argument"
677 "macro define badopt1(x, ...) __VA_OPT__) x" \
678 "define first invalid varargs helper"
679 gdb_test "macro expand badopt1(5)" \
680 "__VA_OPT__ must be followed by an open parenthesis" \
681 "__VA_OPT__ without open paren"
684 "macro define badopt2(x, ...) __VA_OPT__(__VA_OPT__(,)) x" \
685 "define second invalid varargs helper"
686 gdb_test "macro expand badopt2(5)" \
687 "__VA_OPT__ cannot appear inside __VA_OPT__" \
688 "__VA_OPT__ inside __VA_OPT__"
691 "macro define badopt3(x) __VA_OPT__" \
692 "define third invalid varargs helper"
693 gdb_test "macro expand badopt3(5)" \
694 "__VA_OPT__ is only valid in a variadic macro" \
695 "__VA_OPT__ not in variadic macro"
698 "macro define badopt4(x, ...) __VA_OPT__(x" \
699 "define fourth invalid varargs helper"
700 gdb_test "macro expand badopt4(5)" \
701 "Unterminated __VA_OPT__" \
702 "__VA_OPT__ without closing paren"
704 # Stringification tests.
706 gdb_test_no_output "macro define str(x) #x" \
707 "define stringification macro"
709 gdb_test_no_output "macro define maude 5" \
710 "define first stringification helper"
712 gdb_test_no_output "macro define xstr(x) str(x)" \
713 "define second stringification helper"
715 gdb_test "print str(5)" \
719 gdb_test "print str(hi bob)" \
721 "stringify with one space"
723 gdb_test "print str( hi bob )" \
725 "stringify with many spaces"
727 gdb_test "print str(hi \"bob\")" \
728 " = \"hi \\\\\"bob\\\\\"\"" \
729 "stringify with quotes"
731 gdb_test "print str(hi \\bob\\)" \
732 " = \"hi \\\\\\\\bob\\\\\\\\\"" \
733 "stringify with backslashes"
735 gdb_test "print str(maude)" \
737 "stringify without substitution"
739 gdb_test "print xstr(maude)" \
741 "stringify with substitution"
743 # Regression test for pp-number bug.
744 gdb_test_no_output "macro define si_addr fields.fault.si_addr" \
745 "define si_addr macro"
747 gdb_test "macro expand siginfo.si_addr" \
748 "expands to: siginfo.fields.fault.si_addr"
750 gdb_test "print __FILE__" " = \".*macscp1.c\""
751 gdb_test "print __LINE__" \
752 " = [gdb_get_line_number {stopping point for line test}]"