* gdb.base/macscp.exp: Enable kfails. Compile with -g3 for GCC.
[binutils-gdb.git] / gdb / testsuite / gdb.base / macscp.exp
1 # Test macro scoping.
2 # Copyright 2002, 2007, 2008 Free Software Foundation, Inc.
3
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.
8 #
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.
13 #
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/>.
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
19
20 if $tracelevel then {
21 strace $tracelevel
22 }
23
24 set prms_id 0
25 set bug_id 0
26
27 set testfile "macscp"
28 set binfile ${objdir}/${subdir}/${testfile}
29
30 set options { debug }
31
32 get_compiler_info ${binfile}
33 if [test_compiler_info gcc*] {
34 lappend options additional_flags=-g3
35 }
36
37 if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable $options] != "" } {
38 untested macscp.exp
39 return -1
40 }
41
42 gdb_exit
43 gdb_start
44 gdb_reinitialize_dir $srcdir/$subdir
45 gdb_load ${binfile}
46
47
48 # Ask GDB to show the current definition of MACRO, and return a list
49 # describing the result.
50 #
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 ... .
54 #
55 # If GDB says that MACRO has no definition, return the string `undefined'.
56 #
57 # If GDB complains that it doesn't have any information about
58 # preprocessor macro definitions, return the string `no-macro-info'.
59 #
60 # If expect times out waiting for GDB, we return the string `timeout'.
61 #
62 # If GDB's output doesn't otherwise match what we're expecting, we
63 # return the empty string.
64
65 proc info_macro {macro} {
66 global gdb_prompt
67 global decimal
68
69 set filepat {macscp[0-9]+\.[ch]}
70 set definition {}
71 set location {}
72
73 send_gdb "info macro ${macro}\n"
74
75 set debug_me 0
76
77 if {$debug_me} {exp_internal 1}
78 gdb_expect {
79 -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
80 # `location' and `definition' should be empty when we see
81 # this message.
82 if {[llength $location] == 0 && [llength $definition] == 0} {
83 set location $expect_out(1,string)
84 exp_continue
85 } else {
86 # Exit this expect loop, with a result indicating failure.
87 set definition {}
88 }
89 }
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
92 # this message.
93 if {[llength $location] == 0 && [llength $definition] == 0} {
94 set definition undefined
95 exp_continue
96 } else {
97 # Exit this expect loop, with a result indicating failure.
98 set definition {}
99 }
100 }
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)
108 exp_continue
109 } else {
110 # Exit this expect loop, with a result indicating failure.
111 set definition {}
112 }
113 }
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)
120 exp_continue
121 } else {
122 # Exit this expect loop, with a result indicating failure.
123 set definition {}
124 }
125 }
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)
130 exp_continue
131 } else {
132 # Exit this expect loop, with a result indicating failure.
133 set definition {}
134 }
135 }
136 -re "has no preprocessor macro information.*$gdb_prompt $" {
137 set definition no-macro-info
138 }
139 -re "$gdb_prompt $" {
140 # Exit the expect loop; let the existing value of `definition'
141 # indicate failure or success.
142 }
143 timeout {
144 set definition timeout
145 }
146 }
147 if {$debug_me} {exp_internal 0}
148
149 switch -exact -- $definition {
150 no-macro-info { return no-macro-info }
151 timeout { return timeout }
152 undefined -
153 default {
154 if {[llength $location] >= 1} {
155 return [concat $location [list $definition]]
156 } else {
157 return {}
158 }
159 }
160 }
161 }
162
163
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
167 # we can continue.
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"
172 } else {
173 switch -exact -- $func_def {
174 no-macro-info {
175 xfail "executable includes no macro debugging information"
176 return 1
177 }
178 timeout {
179 fail "info macro $macro $where (timeout)"
180 }
181 default {
182 fail "info macro $macro $where"
183 }
184 }
185 }
186 return 0
187 }
188
189
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'"]
195 }
196
197
198 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
199 return 0
200 }
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}}
203
204
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'"
214 }
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)"
218 }
219 timeout {
220 fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
221 }
222 default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
223 }
224
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'"
229 }
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)"
233 }
234 timeout {
235 fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
236 }
237 default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
238 }
239
240
241 #### Test the selection of the macro scope by the current frame.
242
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.
253
254 set funcs {
255 {
256 macscp1_1
257 {macscp1.c {before macscp1_1}}
258 }
259 {
260 macscp2_1
261 {macscp2.h macscp1.c {before macscp2_1}}
262 }
263 {
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}}
267 }
268 {
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}}
272 }
273 {
274 macscp2_2
275 {macscp2.h macscp1.c {before macscp2_2}}
276 }
277 {
278 macscp1_2
279 {macscp1.c {before macscp1_2}}
280 }
281 {
282 macscp3_1
283 {macscp3.h macscp1.c {before macscp3_1}}
284 }
285 {
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}}
289 }
290 {
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}}
294 }
295 {
296 macscp3_2
297 {macscp3.h macscp1.c {before macscp3_2}}
298 }
299 {
300 macscp1_3
301 {macscp1.c {before macscp1_3}}
302 }
303 }
304
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"
310 } else {
311 fail "$test_name"
312 }
313 }
314
315 # Start the program running.
316 if {! [runto_main]} {
317 fail "macro tests suppressed: couldn't run to main"
318 return 0
319 }
320
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.*"
325 }
326
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]
334
335 # Run to the breakpoint for $func.
336 gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
337
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)"
347 } else {
348 fail "info macro WHERE stopped in $func"
349 }
350
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} {
354 if {$j != $i} {
355 set func_j_entry [lindex $funcs $j]
356 set func_j [lindex $func_j_entry 0]
357
358 set before_macro "BEFORE_[string toupper $func_j]"
359 set test_name \
360 "$before_macro defined/undefined when stopped at $func"
361 set result [info_macro $before_macro]
362
363 if {$j < $i} {
364 if {[llength $result] >= 2 && \
365 [string compare [lindex $result end] {}] == 0} {
366 pass $test_name
367 } elseif {[string compare $result timeout] == 0} {
368 fail "$test_name (timeout)"
369 } else {
370 maybe_kfail $func "$test_name"
371 }
372 } elseif {$j > $i} {
373 switch -- [lindex $result end] {
374 undefined { pass $test_name }
375 timeout { fail "$test_name (timeout)" }
376 default {
377 maybe_kfail $func "$test_name"
378 }
379 }
380 }
381
382 set until_macro "UNTIL_[string toupper $func_j]"
383 set test_name \
384 "$until_macro defined/undefined when stopped at $func"
385 set result [info_macro $until_macro]
386
387 if {$j <= $i} {
388 switch -- [lindex $result end] {
389 undefined { pass $test_name }
390 timeout { fail "$test_name (timeout)" }
391 default {
392 maybe_kfail $func "$test_name"
393 }
394 }
395 } elseif {$j > $i} {
396 if {[llength $result] >= 2 && \
397 [string compare [lindex $result end] {}] == 0} {
398 pass $test_name
399 } elseif {[string compare $result timeout] == 0} {
400 fail "$test_name (timeout)"
401 } else {
402 maybe_kfail $func "$test_name"
403 }
404 }
405 }
406 }
407 }