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/>.
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
29 set binfile ${objdir}/${subdir}/${testfile}
33 get_compiler_info ${binfile}
34 if [test_compiler_info gcc*] {
35 lappend options additional_flags=-g3
38 if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable $options] != "" } {
45 gdb_reinitialize_dir $srcdir/$subdir
49 # Ask GDB to show the current definition of MACRO, and return a list
50 # describing the result.
52 # The return value has the form {FILE1 FILE2 ... DEF}, which means
53 # that MACRO has the definition `DEF', and was defined in `FILE1',
54 # which was included from `FILE2', included from ... .
56 # If GDB says that MACRO has no definition, return the string `undefined'.
58 # If GDB complains that it doesn't have any information about
59 # preprocessor macro definitions, return the string `no-macro-info'.
61 # If expect times out waiting for GDB, we return the string `timeout'.
63 # If GDB's output doesn't otherwise match what we're expecting, we
64 # return the empty string.
66 proc info_macro {macro} {
70 set filepat {macscp[0-9]+\.[ch]}
74 send_gdb "info macro ${macro}\n"
78 if {$debug_me} {exp_internal 1}
80 -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
81 # `location' and `definition' should be empty when we see
83 if {[llength $location] == 0 && [llength $definition] == 0} {
84 set location $expect_out(1,string)
87 # Exit this expect loop, with a result indicating failure.
91 -re "The symbol `${macro}' has no definition as a C/C\\+\\+ preprocessor macro\[^\r\n\]*\[\r\n\]" {
92 # `location' and `definition' should be empty when we see
94 if {[llength $location] == 0 && [llength $definition] == 0} {
95 set definition undefined
98 # Exit this expect loop, with a result indicating failure.
102 -re "^\[\r\n\]* included at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
103 # `location' should *not* be empty when we see this
104 # message. It should have recorded at least the initial
105 # `Defined at ' message (for definitions) or ` at' message
106 # (for undefined symbols).
107 if {[llength $location] != 0} {
108 lappend location $expect_out(1,string)
111 # Exit this expect loop, with a result indicating failure.
115 -re "^\[\r\n\]*at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
116 # This appears after a `has no definition' message.
117 # `location' should be empty when we see it.
118 if {[string compare $definition undefined] == 0 \
119 && [llength $location] == 0} {
120 set location $expect_out(1,string)
123 # Exit this expect loop, with a result indicating failure.
127 -re "#define ${macro} (\[^\r\n\]*)\[\r\n\]" {
128 # `definition' should be empty when we see this message.
129 if {[string compare $definition ""] == 0} {
130 set definition $expect_out(1,string)
133 # Exit this expect loop, with a result indicating failure.
137 -re "has no preprocessor macro information.*$gdb_prompt $" {
138 set definition no-macro-info
140 -re "$gdb_prompt $" {
141 # Exit the expect loop; let the existing value of `definition'
142 # indicate failure or success.
145 set definition timeout
148 if {$debug_me} {exp_internal 0}
150 switch -exact -- $definition {
151 no-macro-info { return no-macro-info }
152 timeout { return timeout }
155 if {[llength $location] >= 1} {
156 return [concat $location [list $definition]]
165 # Call info_macro to show the definition of MACRO. Expect a result of
166 # EXPECTED. Use WHERE in pass/fail messages to identify the context.
167 # Return non-zero if we should abort the entire test file, or zero if
169 proc check_macro {macro expected where} {
170 set func_def [info_macro $macro]
171 if {[string compare $func_def $expected] == 0} {
172 pass "info macro $macro $where"
174 switch -exact -- $func_def {
176 xfail "executable includes no macro debugging information"
180 fail "info macro $macro $where (timeout)"
183 fail "info macro $macro $where"
191 # List the function FUNC, and then show the definition of MACRO,
192 # expecting the result EXPECTED.
193 proc list_and_check_macro {func macro expected} {
194 gdb_test "list $func" ".*${func}.*"
195 return [check_macro $macro $expected "after `list $func'"]
199 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
202 list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
203 list_and_check_macro macscp3_2 WHERE {macscp3.h macscp1.c {before macscp3_2}}
206 # Although GDB's macro table structures distinguish between multiple
207 # #inclusions of the same file, GDB's other structures don't. So the
208 # `list' command here doesn't reliably select one #inclusion or the
209 # other, even though it could. It would be nice to eventually change
210 # GDB's structures to handle this correctly.
211 gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
212 switch -exact -- [info_macro WHERE] {
213 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
214 pass "info macro WHERE after `list macscp_4_2_from_macscp2'"
216 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
217 setup_kfail *-*-* "gdb/555"
218 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (gdb/555)"
221 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
223 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
226 gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
227 switch -exact -- [info_macro WHERE] {
228 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
229 pass "info macro WHERE after `list macscp_4_2_from_macscp3'"
231 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
232 setup_kfail *-*-* "gdb/555"
233 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (gdb/555)"
236 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
238 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
242 #### Test the selection of the macro scope by the current frame.
244 ### A table of functions, in the order they will be reached, which is
245 ### also the order they appear in the preprocessed output. Each entry
246 ### has the form {FUNCNAME WHERE KFAILWHERE}, where:
247 ### - FUNCNAME is the name of the function,
248 ### - WHERE is the definition we expect to see for the macro `WHERE', as
249 ### returned by `info_macro', and
250 ### - KFAILWHERE is an alternate definition which should be reported
251 ### as a `known failure', due to GDB's inability to distinguish multiple
252 ### #inclusions of the same file.
253 ### KFAILWHERE may be omitted.
258 {macscp1.c {before macscp1_1}}
262 {macscp2.h macscp1.c {before macscp2_1}}
265 macscp4_1_from_macscp2
266 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
267 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
270 macscp4_2_from_macscp2
271 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
272 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
276 {macscp2.h macscp1.c {before macscp2_2}}
280 {macscp1.c {before macscp1_2}}
284 {macscp3.h macscp1.c {before macscp3_1}}
287 macscp4_1_from_macscp3
288 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
289 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
292 macscp4_2_from_macscp3
293 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
294 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
298 {macscp3.h macscp1.c {before macscp3_2}}
302 {macscp1.c {before macscp1_3}}
306 proc maybe_kfail { func test_name } {
307 # We can't get the right scope info when we're stopped in
308 # the macro4_ functions.
309 if {[string match macscp4_* $func]} {
310 kfail gdb/555 "$test_name"
316 # Start the program running.
317 if {! [runto_main]} {
318 fail "macro tests suppressed: couldn't run to main"
322 # Set a breakpoint on each of the functions.
323 foreach func_entry $funcs {
324 set func [lindex $func_entry 0]
325 gdb_test "break $func" "Breakpoint.*"
328 # Run to each of the breakpoints and check the definition (or lack
329 # thereof) of each macro.
330 for {set i 0} {$i < [llength $funcs]} {incr i} {
331 set func_entry [lindex $funcs $i]
332 set func [lindex $func_entry 0]
333 set expected [lindex $func_entry 1]
334 set kfail_expected [lindex $func_entry 2]
336 # Run to the breakpoint for $func.
337 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
339 # Check the macro WHERE.
340 set result [info_macro WHERE]
341 if {[string compare $result $expected] == 0} {
342 pass "info macro WHERE stopped in $func"
343 } elseif {[string compare $result $kfail_expected] == 0} {
344 setup_kfail *-*-* "gdb/555"
345 fail "info macro WHERE stopped in $func (gdb/555)"
346 } elseif {[string compare $result timeout] == 0} {
347 fail "info macro WHERE stopped in $func (timeout)"
349 fail "info macro WHERE stopped in $func"
352 # Check that the BEFORE_<func> macros for all prior functions are
353 # #defined, and that those for all subsequent functions are not.
354 for {set j 0} {$j < [llength $funcs]} {incr j} {
356 set func_j_entry [lindex $funcs $j]
357 set func_j [lindex $func_j_entry 0]
359 set before_macro "BEFORE_[string toupper $func_j]"
361 "$before_macro defined/undefined when stopped at $func"
362 set result [info_macro $before_macro]
365 if {[llength $result] >= 2 && \
366 [string compare [lindex $result end] {}] == 0} {
368 } elseif {[string compare $result timeout] == 0} {
369 fail "$test_name (timeout)"
371 maybe_kfail $func "$test_name"
374 switch -- [lindex $result end] {
375 undefined { pass $test_name }
376 timeout { fail "$test_name (timeout)" }
378 maybe_kfail $func "$test_name"
383 set until_macro "UNTIL_[string toupper $func_j]"
385 "$until_macro defined/undefined when stopped at $func"
386 set result [info_macro $until_macro]
389 switch -- [lindex $result end] {
390 undefined { pass $test_name }
391 timeout { fail "$test_name (timeout)" }
393 maybe_kfail $func "$test_name"
397 if {[llength $result] >= 2 && \
398 [string compare [lindex $result end] {}] == 0} {
400 } elseif {[string compare $result timeout] == 0} {
401 fail "$test_name (timeout)"
403 maybe_kfail $func "$test_name"
410 gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
411 "Breakpoint.*at.* file .*, line.*" \
412 "breakpoint macscp_expr"
414 gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
417 "No symbol \"M\" in current context\." \
418 "print expression with macro before define."
420 gdb_test "next" "foo = 1;" "next to definition"
424 "print expression with macro in scope."
426 gdb_test "macro define M 72" \
428 "user macro override"
434 gdb_test "macro undef M" \
436 "remove user override"
440 "print expression with macro after removing override"
442 gdb_test "next" "foo = 2;" "next to definition"
445 "No symbol \"M\" in current context\." \
446 "print expression with macro after undef."
448 gdb_test "macro define M 5" \
454 "expansion of defined macro"
456 gdb_test "macro list" \
460 gdb_test "macro define M(x) x" \
462 "basic redefine, macro with args"
464 gdb_test "print M (7)" \
466 "expansion of macro with arguments"
468 gdb_test "macro undef M" \
473 "No symbol \"M\" in current context\." \
474 "print expression with macro after user undef."