Commit | Line | Data |
---|---|---|
6827a8f8 | 1 | # Test macro scoping. |
6aba47ca | 2 | # Copyright 2002, 2007 Free Software Foundation, Inc. |
6827a8f8 JB |
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 2 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, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | ||
18 | # Please email any bugs, comments, and/or additions to this file to: | |
19 | # bug-gdb@prep.ai.mit.edu | |
20 | ||
21 | if $tracelevel then { | |
22 | strace $tracelevel | |
23 | } | |
24 | ||
25 | set prms_id 0 | |
26 | set bug_id 0 | |
27 | ||
28 | set testfile "macscp" | |
29 | set binfile ${objdir}/${subdir}/${testfile} | |
30 | ||
31 | if {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable {debug}] != "" } { | |
b60f0898 JB |
32 | untested macscp.exp |
33 | return -1 | |
6827a8f8 JB |
34 | } |
35 | ||
36 | gdb_exit | |
37 | gdb_start | |
38 | gdb_reinitialize_dir $srcdir/$subdir | |
39 | gdb_load ${binfile} | |
40 | ||
41 | ||
42 | # Ask GDB to show the current definition of MACRO, and return a list | |
43 | # describing the result. | |
44 | # | |
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 ... . | |
48 | # | |
49 | # If GDB says that MACRO has no definition, return the string `undefined'. | |
50 | # | |
51 | # If GDB complains that it doesn't have any information about | |
52 | # preprocessor macro definitions, return the string `no-macro-info'. | |
53 | # | |
54 | # If expect times out waiting for GDB, we return the string `timeout'. | |
55 | # | |
56 | # If GDB's output doesn't otherwise match what we're expecting, we | |
57 | # return the empty string. | |
58 | ||
59 | proc info_macro {macro} { | |
60 | global gdb_prompt | |
61 | global decimal | |
62 | ||
63 | set filepat {macscp[0-9]+\.[ch]} | |
64 | set definition {} | |
65 | set location {} | |
66 | ||
67 | send_gdb "info macro ${macro}\n" | |
68 | ||
69 | set debug_me 0 | |
70 | ||
71 | if {$debug_me} {exp_internal 1} | |
72 | gdb_expect { | |
73 | -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" { | |
74 | # `location' and `definition' should be empty when we see | |
75 | # this message. | |
76 | if {[llength $location] == 0 && [llength $definition] == 0} { | |
77 | set location $expect_out(1,string) | |
78 | exp_continue | |
79 | } else { | |
80 | # Exit this expect loop, with a result indicating failure. | |
81 | set definition {} | |
82 | } | |
83 | } | |
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 | |
86 | # this message. | |
87 | if {[llength $location] == 0 && [llength $definition] == 0} { | |
88 | set definition undefined | |
89 | exp_continue | |
90 | } else { | |
91 | # Exit this expect loop, with a result indicating failure. | |
92 | set definition {} | |
93 | } | |
94 | } | |
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) | |
102 | exp_continue | |
103 | } else { | |
104 | # Exit this expect loop, with a result indicating failure. | |
105 | set definition {} | |
106 | } | |
107 | } | |
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) | |
114 | exp_continue | |
115 | } else { | |
116 | # Exit this expect loop, with a result indicating failure. | |
117 | set definition {} | |
118 | } | |
119 | } | |
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) | |
124 | exp_continue | |
125 | } else { | |
126 | # Exit this expect loop, with a result indicating failure. | |
127 | set definition {} | |
128 | } | |
129 | } | |
130 | -re "has no preprocessor macro information.*$gdb_prompt $" { | |
131 | set definition no-macro-info | |
132 | } | |
133 | -re "$gdb_prompt $" { | |
134 | # Exit the expect loop; let the existing value of `definition' | |
135 | # indicate failure or success. | |
136 | } | |
137 | timeout { | |
138 | set definition timeout | |
139 | } | |
140 | } | |
141 | if {$debug_me} {exp_internal 0} | |
142 | ||
143 | switch -exact -- $definition { | |
144 | no-macro-info { return no-macro-info } | |
145 | timeout { return timeout } | |
146 | undefined - | |
147 | default { | |
148 | if {[llength $location] >= 1} { | |
149 | return [concat $location [list $definition]] | |
150 | } else { | |
151 | return {} | |
152 | } | |
153 | } | |
154 | } | |
155 | } | |
156 | ||
157 | ||
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 | |
161 | # we can continue. | |
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" | |
166 | } else { | |
167 | switch -exact -- $func_def { | |
168 | no-macro-info { | |
169 | xfail "executable includes no macro debugging information" | |
170 | return 1 | |
171 | } | |
172 | timeout { | |
173 | fail "info macro $macro $where (timeout)" | |
174 | } | |
175 | default { | |
176 | fail "info macro $macro $where" | |
177 | } | |
178 | } | |
179 | } | |
180 | return 0 | |
181 | } | |
182 | ||
183 | ||
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'"] | |
189 | } | |
190 | ||
191 | ||
192 | if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} { | |
193 | return 0 | |
194 | } | |
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}} | |
197 | ||
198 | ||
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'" | |
208 | } | |
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)" | |
212 | } | |
213 | timeout { | |
214 | fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)" | |
215 | } | |
216 | default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" } | |
217 | } | |
218 | ||
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'" | |
223 | } | |
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)" | |
227 | } | |
228 | timeout { | |
229 | fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)" | |
230 | } | |
231 | default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" } | |
232 | } | |
233 | ||
234 | ||
235 | #### Test the selection of the macro scope by the current frame. | |
236 | ||
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. | |
247 | ||
248 | set funcs { | |
249 | { | |
250 | macscp1_1 | |
251 | {macscp1.c {before macscp1_1}} | |
252 | } | |
253 | { | |
254 | macscp2_1 | |
255 | {macscp2.h macscp1.c {before macscp2_1}} | |
256 | } | |
257 | { | |
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}} | |
261 | } | |
262 | { | |
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}} | |
266 | } | |
267 | { | |
268 | macscp2_2 | |
269 | {macscp2.h macscp1.c {before macscp2_2}} | |
270 | } | |
271 | { | |
272 | macscp1_2 | |
273 | {macscp1.c {before macscp1_2}} | |
274 | } | |
275 | { | |
276 | macscp3_1 | |
277 | {macscp3.h macscp1.c {before macscp3_1}} | |
278 | } | |
279 | { | |
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}} | |
283 | } | |
284 | { | |
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}} | |
288 | } | |
289 | { | |
290 | macscp3_2 | |
291 | {macscp3.h macscp1.c {before macscp3_2}} | |
292 | } | |
293 | { | |
294 | macscp1_3 | |
295 | {macscp1.c {before macscp1_3}} | |
296 | } | |
297 | } | |
298 | ||
299 | ||
300 | # Start the program running. | |
301 | if {! [runto_main]} { | |
302 | fail "macro tests suppressed: couldn't run to main" | |
303 | return 0 | |
304 | } | |
305 | ||
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.*" | |
310 | } | |
311 | ||
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] | |
319 | ||
320 | # Run to the breakpoint for $func. | |
321 | gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func" | |
322 | ||
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)" | |
332 | } else { | |
333 | fail "info macro WHERE stopped in $func" | |
334 | } | |
335 | ||
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} { | |
339 | if {$j != $i} { | |
340 | set func_j_entry [lindex $funcs $j] | |
341 | set func_j [lindex $func_j_entry 0] | |
342 | ||
343 | set before_macro "BEFORE_[string toupper $func_j]" | |
344 | set test_name \ | |
345 | "$before_macro defined/undefined when stopped at $func" | |
346 | set result [info_macro $before_macro] | |
347 | ||
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)" | |
353 | } | |
354 | if {$j < $i} { | |
355 | if {[llength $result] >= 2 && \ | |
356 | [string compare [lindex $result end] {}] == 0} { | |
357 | pass $test_name | |
358 | } elseif {[string compare $result timeout] == 0} { | |
359 | fail "$test_name (timeout)" | |
360 | } else { | |
361 | fail "$test_name" | |
362 | } | |
363 | } elseif {$j > $i} { | |
364 | switch -- [lindex $result end] { | |
365 | undefined { pass $test_name } | |
366 | timeout { fail "$test_name (timeout)" } | |
367 | default { | |
368 | fail "$test_name" | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
373 | set until_macro "UNTIL_[string toupper $func_j]" | |
374 | set test_name \ | |
375 | "$until_macro defined/undefined when stopped at $func" | |
376 | set result [info_macro $until_macro] | |
377 | ||
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)" | |
383 | } | |
384 | if {$j <= $i} { | |
385 | switch -- [lindex $result end] { | |
386 | undefined { pass $test_name } | |
387 | timeout { fail "$test_name (timeout)" } | |
388 | default { | |
389 | fail "$test_name" | |
390 | } | |
391 | } | |
392 | } elseif {$j > $i} { | |
393 | if {[llength $result] >= 2 && \ | |
394 | [string compare [lindex $result end] {}] == 0} { | |
395 | pass $test_name | |
396 | } elseif {[string compare $result timeout] == 0} { | |
397 | fail "$test_name (timeout)" | |
398 | } else { | |
399 | fail "$test_name" | |
400 | } | |
401 | } | |
402 | } | |
403 | } | |
404 | } |