gdb
[external/binutils.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 srcfile macscp1.c
28 set testfile "macscp"
29 set binfile ${objdir}/${subdir}/${testfile}
30
31 set options { debug }
32
33 get_compiler_info ${binfile}
34 if [test_compiler_info gcc*] {
35     lappend options additional_flags=-g3
36 }
37
38 if  {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable $options] != "" } {
39     untested macscp.exp
40     return -1
41 }
42
43 gdb_exit
44 gdb_start
45 gdb_reinitialize_dir $srcdir/$subdir
46 gdb_load ${binfile}
47
48
49 # Ask GDB to show the current definition of MACRO, and return a list
50 # describing the result.
51 #
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 ... .
55 #
56 # If GDB says that MACRO has no definition, return the string `undefined'.
57 #
58 # If GDB complains that it doesn't have any information about
59 # preprocessor macro definitions, return the string `no-macro-info'.
60
61 # If expect times out waiting for GDB, we return the string `timeout'.
62 #
63 # If GDB's output doesn't otherwise match what we're expecting, we
64 # return the empty string.
65
66 proc info_macro {macro} {
67     global gdb_prompt
68     global decimal
69
70     set filepat {macscp[0-9]+\.[ch]}
71     set definition {}
72     set location {}
73
74     send_gdb "info macro ${macro}\n"
75
76     set debug_me 0
77
78     if {$debug_me} {exp_internal 1}
79     gdb_expect {
80         -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
81             # `location' and `definition' should be empty when we see
82             # this message.
83             if {[llength $location] == 0 && [llength $definition] == 0} {
84                 set location $expect_out(1,string)
85                 exp_continue
86             } else {
87                 # Exit this expect loop, with a result indicating failure.
88                 set definition {}
89             }
90         }
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
93             # this message.
94             if {[llength $location] == 0 && [llength $definition] == 0} {
95                 set definition undefined
96                 exp_continue
97             } else {
98                 # Exit this expect loop, with a result indicating failure.
99                 set definition {}
100             }
101         }
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)
109                 exp_continue
110             } else {
111                 # Exit this expect loop, with a result indicating failure.
112                 set definition {}
113             }
114         }
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)
121                 exp_continue
122             } else {
123                 # Exit this expect loop, with a result indicating failure.
124                 set definition {}
125             }
126         }
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)
131                 exp_continue
132             } else {
133                 # Exit this expect loop, with a result indicating failure.
134                 set definition {}
135             }
136         }
137         -re "has no preprocessor macro information.*$gdb_prompt $" {
138             set definition no-macro-info
139         }
140         -re "$gdb_prompt $" {
141             # Exit the expect loop; let the existing value of `definition'
142             # indicate failure or success.
143         }
144         timeout {
145             set definition timeout
146         }
147     }
148     if {$debug_me} {exp_internal 0}
149
150     switch -exact -- $definition {
151         no-macro-info { return no-macro-info }
152         timeout { return timeout }
153         undefined -
154         default {
155             if {[llength $location] >= 1} {
156                 return [concat $location [list $definition]]
157             } else {
158                 return {}
159             }
160         }
161     }
162 }
163
164
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
168 # we can continue.
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"
173     } else {
174         switch -exact -- $func_def {
175             no-macro-info {
176                 xfail "executable includes no macro debugging information"
177                 return 1
178             }
179             timeout {
180                 fail "info macro $macro $where (timeout)"
181             }
182             default {
183                 fail "info macro $macro $where"
184             }
185         }
186     }
187     return 0
188 }
189     
190
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'"]
196 }
197
198
199 if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
200     return 0
201 }
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}}
204
205
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'"
215     }
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)"
219     }
220     timeout { 
221         fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
222     }
223     default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
224 }
225
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'"
230     }
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)"
234     }
235     timeout {
236         fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
237     }
238     default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
239 }
240
241
242 #### Test the selection of the macro scope by the current frame.
243
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.
254
255 set funcs {
256     {
257         macscp1_1
258         {macscp1.c {before macscp1_1}}
259     }
260     {
261         macscp2_1
262         {macscp2.h macscp1.c {before macscp2_1}}
263     }
264     {
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}}
268     }
269     {
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}}
273     }
274     {
275         macscp2_2
276         {macscp2.h macscp1.c {before macscp2_2}}
277     }
278     {
279         macscp1_2
280         {macscp1.c {before macscp1_2}}
281     }
282     {
283         macscp3_1
284         {macscp3.h macscp1.c {before macscp3_1}}
285     }
286     {
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}}
290     }
291     {
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}}
295     }
296     {
297         macscp3_2
298         {macscp3.h macscp1.c {before macscp3_2}}
299     }
300     {
301         macscp1_3
302         {macscp1.c {before macscp1_3}}
303     }
304 }
305
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"
311     } else {
312         fail "$test_name"
313     }
314 }
315
316 # Start the program running.
317 if {! [runto_main]} {
318     fail "macro tests suppressed: couldn't run to main"
319     return 0
320 }
321
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.*"
326 }
327
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]
335
336     # Run to the breakpoint for $func.
337     gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"
338
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)"
348     } else {
349         fail "info macro WHERE stopped in $func"
350     }
351
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} {
355         if {$j != $i} {
356             set func_j_entry [lindex $funcs $j]
357             set func_j [lindex $func_j_entry 0]
358
359             set before_macro "BEFORE_[string toupper $func_j]"
360             set test_name \
361                     "$before_macro defined/undefined when stopped at $func"
362             set result [info_macro $before_macro]
363
364             if {$j < $i} {
365                 if {[llength $result] >= 2 && \
366                         [string compare [lindex $result end] {}] == 0} {
367                     pass $test_name
368                 } elseif {[string compare $result timeout] == 0} {
369                     fail "$test_name (timeout)"
370                 } else {
371                     maybe_kfail $func "$test_name"
372                 }
373             } elseif {$j > $i} {
374                 switch -- [lindex $result end] {
375                     undefined { pass $test_name }
376                     timeout { fail "$test_name (timeout)" }
377                     default { 
378                         maybe_kfail $func "$test_name"
379                     }
380                 }
381             }
382
383             set until_macro "UNTIL_[string toupper $func_j]"
384             set test_name \
385                     "$until_macro defined/undefined when stopped at $func"
386             set result [info_macro $until_macro]
387
388             if {$j <= $i} {
389                 switch -- [lindex $result end] {
390                     undefined { pass $test_name }
391                     timeout { fail "$test_name (timeout)" }
392                     default { 
393                         maybe_kfail $func "$test_name"
394                     }
395                 }
396             } elseif {$j > $i} {
397                 if {[llength $result] >= 2 && \
398                         [string compare [lindex $result end] {}] == 0} {
399                     pass $test_name
400                 } elseif {[string compare $result timeout] == 0} {
401                     fail "$test_name (timeout)"
402                 } else {
403                     maybe_kfail $func "$test_name"
404                 }
405             }
406         }
407     }
408 }
409
410 gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
411     "Breakpoint.*at.* file .*, line.*" \
412     "breakpoint macscp_expr"
413
414 gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"
415
416 gdb_test "print M" \
417     "No symbol \"M\" in current context\." \
418     "print expression with macro before define."
419
420 gdb_test "next" "foo = 1;" "next to definition"
421
422 gdb_test "print M" \
423     " = 0" \
424     "print expression with macro in scope."
425
426 gdb_test "macro define M 72" \
427   "" \
428   "user macro override"
429
430 gdb_test "print M" \
431   " = 72" \
432   "choose user macro"
433
434 gdb_test "macro undef M" \
435   "" \
436   "remove user override"
437
438 gdb_test "print M" \
439     " = 0" \
440     "print expression with macro after removing override"
441
442 gdb_test "next" "foo = 2;" "next to definition"
443
444 gdb_test "print M" \
445     "No symbol \"M\" in current context\." \
446     "print expression with macro after undef."
447
448 gdb_test "macro define M 5" \
449   "" \
450   "basic macro define"
451
452 gdb_test "print M" \
453   " = 5" \
454   "expansion of defined macro"
455
456 gdb_test "macro list" \
457   "macro define M 5" \
458   "basic macro list"
459
460 gdb_test "macro define M(x) x" \
461   "" \
462   "basic redefine, macro with args"
463
464 gdb_test "print M (7)" \
465   " = 7" \
466   "expansion of macro with arguments"
467
468 gdb_test "macro undef M" \
469   "" \
470   "basic macro undef"
471
472 gdb_test "print M" \
473     "No symbol \"M\" in current context\." \
474     "print expression with macro after user undef."