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
28 set binfile ${objdir}/${subdir}/${testfile}
32 get_compiler_info ${binfile}
33 if [test_compiler_info gcc*] {
34 lappend options additional_flags=-g3
37 if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable $options] != "" } {
44 gdb_reinitialize_dir $srcdir/$subdir
48 # Ask GDB to show the current definition of MACRO, and return a list
49 # describing the result.
51 # The return value has the form {FILE1 FILE2 ... DEF}, which means
52 # that MACRO has the definition `DEF', and was defined in `FILE1',
53 # which was included from `FILE2', included from ... .
55 # If GDB says that MACRO has no definition, return the string `undefined'.
57 # If GDB complains that it doesn't have any information about
58 # preprocessor macro definitions, return the string `no-macro-info'.
60 # If expect times out waiting for GDB, we return the string `timeout'.
62 # If GDB's output doesn't otherwise match what we're expecting, we
63 # return the empty string.
65 proc info_macro {macro} {
69 set filepat {macscp[0-9]+\.[ch]}
73 send_gdb "info macro ${macro}\n"
77 if {$debug_me} {exp_internal 1}
79 -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
80 # `location' and `definition' should be empty when we see
82 if {[llength $location] == 0 && [llength $definition] == 0} {
83 set location $expect_out(1,string)
86 # Exit this expect loop, with a result indicating failure.
90 -re "The symbol `${macro}' has no definition as a C/C\\+\\+ preprocessor macro\[^\r\n\]*\[\r\n\]" {
91 # `location' and `definition' should be empty when we see
93 if {[llength $location] == 0 && [llength $definition] == 0} {
94 set definition undefined
97 # Exit this expect loop, with a result indicating failure.
101 -re "^\[\r\n\]* included at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
102 # `location' should *not* be empty when we see this
103 # message. It should have recorded at least the initial
104 # `Defined at ' message (for definitions) or ` at' message
105 # (for undefined symbols).
106 if {[llength $location] != 0} {
107 lappend location $expect_out(1,string)
110 # Exit this expect loop, with a result indicating failure.
114 -re "^\[\r\n\]*at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
115 # This appears after a `has no definition' message.
116 # `location' should be empty when we see it.
117 if {[string compare $definition undefined] == 0 \
118 && [llength $location] == 0} {
119 set location $expect_out(1,string)
122 # Exit this expect loop, with a result indicating failure.
126 -re "#define ${macro} (\[^\r\n\]*)\[\r\n\]" {
127 # `definition' should be empty when we see this message.
128 if {[string compare $definition ""] == 0} {
129 set definition $expect_out(1,string)
132 # Exit this expect loop, with a result indicating failure.
136 -re "has no preprocessor macro information.*$gdb_prompt $" {
137 set definition no-macro-info
139 -re "$gdb_prompt $" {
140 # Exit the expect loop; let the existing value of `definition'
141 # indicate failure or success.
144 set definition timeout
147 if {$debug_me} {exp_internal 0}
149 switch -exact -- $definition {
150 no-macro-info { return no-macro-info }
151 timeout { return timeout }
154 if {[llength $location] >= 1} {
155 return [concat $location [list $definition]]
164 # Call info_macro to show the definition of MACRO. Expect a result of
165 # EXPECTED. Use WHERE in pass/fail messages to identify the context.
166 # Return non-zero if we should abort the entire test file, or zero if
168 proc check_macro {macro expected where} {
169 set func_def [info_macro $macro]
170 if {[string compare $func_def $expected] == 0} {
171 pass "info macro $macro $where"
173 switch -exact -- $func_def {
175 xfail "executable includes no macro debugging information"
179 fail "info macro $macro $where (timeout)"
182 fail "info macro $macro $where"
190 # List the function FUNC, and then show the definition of MACRO,
191 # expecting the result EXPECTED.
192 proc list_and_check_macro {func macro expected} {
193 gdb_test "list $func" ".*${func}.*"
194 return [check_macro $macro $expected "after `list $func'"]
198 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
201 list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
202 list_and_check_macro macscp3_2 WHERE {macscp3.h macscp1.c {before macscp3_2}}
205 # Although GDB's macro table structures distinguish between multiple
206 # #inclusions of the same file, GDB's other structures don't. So the
207 # `list' command here doesn't reliably select one #inclusion or the
208 # other, even though it could. It would be nice to eventually change
209 # GDB's structures to handle this correctly.
210 gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
211 switch -exact -- [info_macro WHERE] {
212 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
213 pass "info macro WHERE after `list macscp_4_2_from_macscp2'"
215 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
216 setup_kfail *-*-* "gdb/555"
217 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (gdb/555)"
220 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
222 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
225 gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
226 switch -exact -- [info_macro WHERE] {
227 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
228 pass "info macro WHERE after `list macscp_4_2_from_macscp3'"
230 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
231 setup_kfail *-*-* "gdb/555"
232 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (gdb/555)"
235 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
237 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
241 #### Test the selection of the macro scope by the current frame.
243 ### A table of functions, in the order they will be reached, which is
244 ### also the order they appear in the preprocessed output. Each entry
245 ### has the form {FUNCNAME WHERE KFAILWHERE}, where:
246 ### - FUNCNAME is the name of the function,
247 ### - WHERE is the definition we expect to see for the macro `WHERE', as
248 ### returned by `info_macro', and
249 ### - KFAILWHERE is an alternate definition which should be reported
250 ### as a `known failure', due to GDB's inability to distinguish multiple
251 ### #inclusions of the same file.
252 ### KFAILWHERE may be omitted.
257 {macscp1.c {before macscp1_1}}
261 {macscp2.h macscp1.c {before macscp2_1}}
264 macscp4_1_from_macscp2
265 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
266 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
269 macscp4_2_from_macscp2
270 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
271 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
275 {macscp2.h macscp1.c {before macscp2_2}}
279 {macscp1.c {before macscp1_2}}
283 {macscp3.h macscp1.c {before macscp3_1}}
286 macscp4_1_from_macscp3
287 {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
288 {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
291 macscp4_2_from_macscp3
292 {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
293 {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
297 {macscp3.h macscp1.c {before macscp3_2}}
301 {macscp1.c {before macscp1_3}}
305 proc maybe_kfail { func test_name } {
306 # We can't get the right scope info when we're stopped in
307 # the macro4_ functions.
308 if {[string match macscp4_* $func]} {
309 kfail gdb/555 "$test_name"
315 # Start the program running.
316 if {! [runto_main]} {
317 fail "macro tests suppressed: couldn't run to main"
321 # Set a breakpoint on each of the functions.
322 foreach func_entry $funcs {
323 set func [lindex $func_entry 0]
324 gdb_test "break $func" "Breakpoint.*"
327 # Run to each of the breakpoints and check the definition (or lack
328 # thereof) of each macro.
329 for {set i 0} {$i < [llength $funcs]} {incr i} {
330 set func_entry [lindex $funcs $i]
331 set func [lindex $func_entry 0]
332 set expected [lindex $func_entry 1]
333 set kfail_expected [lindex $func_entry 2]
335 # Run to the breakpoint for $func.
336 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
338 # Check the macro WHERE.
339 set result [info_macro WHERE]
340 if {[string compare $result $expected] == 0} {
341 pass "info macro WHERE stopped in $func"
342 } elseif {[string compare $result $kfail_expected] == 0} {
343 setup_kfail *-*-* "gdb/555"
344 fail "info macro WHERE stopped in $func (gdb/555)"
345 } elseif {[string compare $result timeout] == 0} {
346 fail "info macro WHERE stopped in $func (timeout)"
348 fail "info macro WHERE stopped in $func"
351 # Check that the BEFORE_<func> macros for all prior functions are
352 # #defined, and that those for all subsequent functions are not.
353 for {set j 0} {$j < [llength $funcs]} {incr j} {
355 set func_j_entry [lindex $funcs $j]
356 set func_j [lindex $func_j_entry 0]
358 set before_macro "BEFORE_[string toupper $func_j]"
360 "$before_macro defined/undefined when stopped at $func"
361 set result [info_macro $before_macro]
364 if {[llength $result] >= 2 && \
365 [string compare [lindex $result end] {}] == 0} {
367 } elseif {[string compare $result timeout] == 0} {
368 fail "$test_name (timeout)"
370 maybe_kfail $func "$test_name"
373 switch -- [lindex $result end] {
374 undefined { pass $test_name }
375 timeout { fail "$test_name (timeout)" }
377 maybe_kfail $func "$test_name"
382 set until_macro "UNTIL_[string toupper $func_j]"
384 "$until_macro defined/undefined when stopped at $func"
385 set result [info_macro $until_macro]
388 switch -- [lindex $result end] {
389 undefined { pass $test_name }
390 timeout { fail "$test_name (timeout)" }
392 maybe_kfail $func "$test_name"
396 if {[llength $result] >= 2 && \
397 [string compare [lindex $result end] {}] == 0} {
399 } elseif {[string compare $result timeout] == 0} {
400 fail "$test_name (timeout)"
402 maybe_kfail $func "$test_name"