2 # Copyright 2002 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 2 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, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # Please email any bugs, comments, and/or additions to this file to:
19 # bug-gdb@prep.ai.mit.edu
29 set binfile ${objdir}/${subdir}/${testfile}
31 if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable {debug}] != "" } {
38 gdb_reinitialize_dir $srcdir/$subdir
42 # Ask GDB to show the current definition of MACRO, and return a list
43 # describing the result.
45 # The return value has the form {FILE1 FILE2 ... DEF}, which means
46 # that MACRO has the definition `DEF', and was defined in `FILE1',
47 # which was included from `FILE2', included from ... .
49 # If GDB says that MACRO has no definition, return the string `undefined'.
51 # If GDB complains that it doesn't have any information about
52 # preprocessor macro definitions, return the string `no-macro-info'.
54 # If expect times out waiting for GDB, we return the string `timeout'.
56 # If GDB's output doesn't otherwise match what we're expecting, we
57 # return the empty string.
59 proc info_macro {macro} {
63 set filepat {macscp[0-9]+\.[ch]}
67 send_gdb "info macro ${macro}\n"
71 if {$debug_me} {exp_internal 1}
73 -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
74 # `location' and `definition' should be empty when we see
76 if {[llength $location] == 0 && [llength $definition] == 0} {
77 set location $expect_out(1,string)
80 # Exit this expect loop, with a result indicating failure.
84 -re "The symbol `${macro}' has no definition as a C/C\\+\\+ preprocessor macro\[^\r\n\]*\[\r\n\]" {
85 # `location' and `definition' should be empty when we see
87 if {[llength $location] == 0 && [llength $definition] == 0} {
88 set definition undefined
91 # Exit this expect loop, with a result indicating failure.
95 -re "^\[\r\n\]* included at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
96 # `location' should *not* be empty when we see this
97 # message. It should have recorded at least the initial
98 # `Defined at ' message (for definitions) or ` at' message
99 # (for undefined symbols).
100 if {[llength $location] != 0} {
101 lappend location $expect_out(1,string)
104 # Exit this expect loop, with a result indicating failure.
108 -re "^\[\r\n\]*at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
109 # This appears after a `has no definition' message.
110 # `location' should be empty when we see it.
111 if {[string compare $definition undefined] == 0 \
112 && [llength $location] == 0} {
113 set location $expect_out(1,string)
116 # Exit this expect loop, with a result indicating failure.
120 -re "#define ${macro} (\[^\r\n\]*)\[\r\n\]" {
121 # `definition' should be empty when we see this message.
122 if {[string compare $definition ""] == 0} {
123 set definition $expect_out(1,string)
126 # Exit this expect loop, with a result indicating failure.
130 -re "has no preprocessor macro information.*$gdb_prompt $" {
131 set definition no-macro-info
133 -re "$gdb_prompt $" {
134 # Exit the expect loop; let the existing value of `definition'
135 # indicate failure or success.
138 set definition timeout
141 if {$debug_me} {exp_internal 0}
143 switch -exact -- $definition {
144 no-macro-info { return no-macro-info }
145 timeout { return timeout }
148 if {[llength $location] >= 1} {
149 return [concat $location [list $definition]]
158 # Call info_macro to show the definition of MACRO. Expect a result of
159 # EXPECTED. Use WHERE in pass/fail messages to identify the context.
160 # Return non-zero if we should abort the entire test file, or zero if
162 proc check_macro {macro expected where} {
163 set func_def [info_macro $macro]
164 if {[string compare $func_def $expected] == 0} {
165 pass "info macro $macro $where"
167 switch -exact -- $func_def {
169 xfail "executable includes no macro debugging information"
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}.*"
188 return [check_macro $macro $expected "after `list $func'"]
192 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
195 list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
196 list_and_check_macro macscp3_2 WHERE {macscp3.h macscp1.c {before macscp3_2}}
199 # Although GDB's macro table structures distinguish between multiple
200 # #inclusions of the same file, GDB's other structures don't. So the
201 # `list' command here doesn't reliably select one #inclusion or the
202 # other, even though it could. It would be nice to eventually change
203 # GDB's structures to handle this correctly.
204 gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
205 switch -exact -- [info_macro WHERE] {
206 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
207 pass "info macro WHERE after `list macscp_4_2_from_macscp2'"
209 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
210 # setup_kfail "gdb/555"
211 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (gdb/555)"
214 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
216 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
219 gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
220 switch -exact -- [info_macro WHERE] {
221 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
222 pass "info macro WHERE after `list macscp_4_2_from_macscp3'"
224 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
225 # setup_kfail "gdb/555"
226 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (gdb/555)"
229 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
231 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
235 #### Test the selection of the macro scope by the current frame.
237 ### A table of functions, in the order they will be reached, which is
238 ### also the order they appear in the preprocessed output. Each entry
239 ### has the form {FUNCNAME WHERE KFAILWHERE}, where:
240 ### - FUNCNAME is the name of the function,
241 ### - WHERE is the definition we expect to see for the macro `WHERE', as
242 ### returned by `info_macro', and
243 ### - KFAILWHERE is an alternate definition which should be reported
244 ### as a `known failure', due to GDB's inability to distinguish multiple
245 ### #inclusions of the same file.
246 ### KFAILWHERE may be omitted.
251 {macscp1.c {before macscp1_1}}
255 {macscp2.h macscp1.c {before macscp2_1}}
258 macscp4_1_from_macscp2
259 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
260 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
263 macscp4_2_from_macscp2
264 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
265 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
269 {macscp2.h macscp1.c {before macscp2_2}}
273 {macscp1.c {before macscp1_2}}
277 {macscp3.h macscp1.c {before macscp3_1}}
280 macscp4_1_from_macscp3
281 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
282 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
285 macscp4_2_from_macscp3
286 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
287 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
291 {macscp3.h macscp1.c {before macscp3_2}}
295 {macscp1.c {before macscp1_3}}
300 # Start the program running.
301 if {! [runto_main]} {
302 fail "macro tests suppressed: couldn't run to main"
306 # Set a breakpoint on each of the functions.
307 foreach func_entry $funcs {
308 set func [lindex $func_entry 0]
309 gdb_test "break $func" "Breakpoint.*"
312 # Run to each of the breakpoints and check the definition (or lack
313 # thereof) of each macro.
314 for {set i 0} {$i < [llength $funcs]} {incr i} {
315 set func_entry [lindex $funcs $i]
316 set func [lindex $func_entry 0]
317 set expected [lindex $func_entry 1]
318 set kfail_expected [lindex $func_entry 2]
320 # Run to the breakpoint for $func.
321 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
323 # Check the macro WHERE.
324 set result [info_macro WHERE]
325 if {[string compare $result $expected] == 0} {
326 pass "info macro WHERE stopped in $func"
327 } elseif {[string compare $result $kfail_expected] == 0} {
328 # setup_kfail "gdb/555"
329 fail "info macro WHERE stopped in $func (gdb/555)"
330 } elseif {[string compare $result timeout] == 0} {
331 fail "info macro WHERE stopped in $func (timeout)"
333 fail "info macro WHERE stopped in $func"
336 # Check that the BEFORE_<func> macros for all prior functions are
337 # #defined, and that those for all subsequent functions are not.
338 for {set j 0} {$j < [llength $funcs]} {incr j} {
340 set func_j_entry [lindex $funcs $j]
341 set func_j [lindex $func_j_entry 0]
343 set before_macro "BEFORE_[string toupper $func_j]"
345 "$before_macro defined/undefined when stopped at $func"
346 set result [info_macro $before_macro]
348 # We can't get the right scope info when we're stopped in
349 # the macro4_ functions.
350 if {[string match macscp4_* $func]} {
351 # setup_kfail "gdb/555"
352 set test_name "$test_name (gdb/555)"
355 if {[llength $result] >= 2 && \
356 [string compare [lindex $result end] {}] == 0} {
358 } elseif {[string compare $result timeout] == 0} {
359 fail "$test_name (timeout)"
364 switch -- [lindex $result end] {
365 undefined { pass $test_name }
366 timeout { fail "$test_name (timeout)" }
373 set until_macro "UNTIL_[string toupper $func_j]"
375 "$until_macro defined/undefined when stopped at $func"
376 set result [info_macro $until_macro]
378 # We can't get the right scope info when we're stopped in
379 # the macro4_ functions.
380 if {[string match macscp4_* $func]} {
381 # setup_kfail "gdb/555"
382 set test_name "$test_name (gdb/555)"
385 switch -- [lindex $result end] {
386 undefined { pass $test_name }
387 timeout { fail "$test_name (timeout)" }
393 if {[llength $result] >= 2 && \
394 [string compare [lindex $result end] {}] == 0} {
396 } elseif {[string compare $result timeout] == 0} {
397 fail "$test_name (timeout)"