summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/macscp.exp
blob: 90c2d9531f338b4e5ce16c035a45801ac2e49e97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Test macro scoping.
# Copyright 2002, 2007, 2008 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@prep.ai.mit.edu

if $tracelevel then {
    strace $tracelevel
}

set prms_id 0
set bug_id 0

set srcfile macscp1.c
set testfile "macscp"
set binfile ${objdir}/${subdir}/${testfile}

set options { debug }

get_compiler_info ${binfile}
if [test_compiler_info gcc*] {
    lappend options additional_flags=-g3
}

if  {[gdb_compile "${srcdir}/${subdir}/macscp1.c" "${binfile}" executable $options] != "" } {
    untested macscp.exp
    return -1
}

gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_load ${binfile}


# Ask GDB to show the current definition of MACRO, and return a list
# describing the result.
#
# The return value has the form {FILE1 FILE2 ... DEF}, which means
# that MACRO has the definition `DEF', and was defined in `FILE1',
# which was included from `FILE2', included from ... .
#
# If GDB says that MACRO has no definition, return the string `undefined'.
#
# If GDB complains that it doesn't have any information about
# preprocessor macro definitions, return the string `no-macro-info'.
# 
# If expect times out waiting for GDB, we return the string `timeout'.
#
# If GDB's output doesn't otherwise match what we're expecting, we
# return the empty string.

proc info_macro {macro} {
    global gdb_prompt
    global decimal

    set filepat {macscp[0-9]+\.[ch]}
    set definition {}
    set location {}

    send_gdb "info macro ${macro}\n"

    set debug_me 0

    if {$debug_me} {exp_internal 1}
    gdb_expect {
        -re "Defined at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
            # `location' and `definition' should be empty when we see
            # this message.
            if {[llength $location] == 0 && [llength $definition] == 0} {
                set location $expect_out(1,string)
                exp_continue
            } else {
                # Exit this expect loop, with a result indicating failure.
                set definition {}
            }
        }
        -re "The symbol `${macro}' has no definition as a C/C\\+\\+ preprocessor macro\[^\r\n\]*\[\r\n\]" {
            # `location' and `definition' should be empty when we see
            # this message.
            if {[llength $location] == 0 && [llength $definition] == 0} {
                set definition undefined
                exp_continue
            } else {
                # Exit this expect loop, with a result indicating failure.
                set definition {}
            }
        }
        -re "^\[\r\n\]*  included at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
            # `location' should *not* be empty when we see this
            # message.  It should have recorded at least the initial
            # `Defined at ' message (for definitions) or ` at' message
            # (for undefined symbols).
            if {[llength $location] != 0} {
                lappend location $expect_out(1,string)
                exp_continue
            } else {
                # Exit this expect loop, with a result indicating failure.
                set definition {}
            }
        }
        -re "^\[\r\n\]*at \[^\r\n\]*(${filepat}):${decimal}\[\r\n\]" {
            # This appears after a `has no definition' message.
            # `location' should be empty when we see it.
            if {[string compare $definition undefined] == 0 \
                    && [llength $location] == 0} {
                set location $expect_out(1,string)
                exp_continue
            } else {
                # Exit this expect loop, with a result indicating failure.
                set definition {}
            }
        }
        -re "#define ${macro} (\[^\r\n\]*)\[\r\n\]" {
            # `definition' should be empty when we see this message.
            if {[string compare $definition ""] == 0} {
                set definition $expect_out(1,string)
                exp_continue
            } else {
                # Exit this expect loop, with a result indicating failure.
                set definition {}
            }
        }
        -re "has no preprocessor macro information.*$gdb_prompt $" {
            set definition no-macro-info
        }
        -re "$gdb_prompt $" {
            # Exit the expect loop; let the existing value of `definition'
            # indicate failure or success.
        }
        timeout {
            set definition timeout
        }
    }
    if {$debug_me} {exp_internal 0}

    switch -exact -- $definition {
        no-macro-info { return no-macro-info }
        timeout { return timeout }
        undefined -
        default {
            if {[llength $location] >= 1} {
                return [concat $location [list $definition]]
            } else {
                return {}
            }
        }
    }
}


# Call info_macro to show the definition of MACRO.  Expect a result of
# EXPECTED.  Use WHERE in pass/fail messages to identify the context.
# Return non-zero if we should abort the entire test file, or zero if
# we can continue.
proc check_macro {macro expected where} {
    set func_def [info_macro $macro]
    if {[string compare $func_def $expected] == 0} {
        pass "info macro $macro $where"
    } else {
        switch -exact -- $func_def {
            no-macro-info {
                xfail "executable includes no macro debugging information"
                return 1
            }
            timeout {
                fail "info macro $macro $where (timeout)"
            }
            default {
                fail "info macro $macro $where"
            }
        }
    }
    return 0
}
    

# List the function FUNC, and then show the definition of MACRO,
# expecting the result EXPECTED.
proc list_and_check_macro {func macro expected} {
    gdb_test "list $func" ".*${func}.*"
    return [check_macro $macro $expected "after `list $func'"]
}


if {[list_and_check_macro main WHERE {macscp1.c {before macscp1_3}}]} {
    return 0
}
list_and_check_macro macscp2_2 WHERE {macscp2.h macscp1.c {before macscp2_2}}
list_and_check_macro macscp3_2 WHERE {macscp3.h macscp1.c {before macscp3_2}}


# Although GDB's macro table structures distinguish between multiple
# #inclusions of the same file, GDB's other structures don't.  So the
# `list' command here doesn't reliably select one #inclusion or the
# other, even though it could.  It would be nice to eventually change
# GDB's structures to handle this correctly.
gdb_test "list macscp4_2_from_macscp2" ".*macscp4_2_, MACSCP4_INCLUSION.*"
switch -exact -- [info_macro WHERE] {
    {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
        pass "info macro WHERE after `list macscp_4_2_from_macscp2'"
    }
    {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
        setup_kfail *-*-* "gdb/555"
        fail "info macro WHERE after `list macscp_4_2_from_macscp2' (gdb/555)"
    }
    timeout { 
        fail "info macro WHERE after `list macscp_4_2_from_macscp2' (timeout)"
    }
    default { fail "info macro WHERE after `list macscp_4_2_from_macscp2'" }
}

gdb_test "list macscp4_2_from_macscp3" ".*macscp4_2_, MACSCP4_INCLUSION.*"
switch -exact -- [info_macro WHERE] {
    {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}} {
        pass "info macro WHERE after `list macscp_4_2_from_macscp3'"
    }
    {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}} {
        setup_kfail *-*-* "gdb/555"
        fail "info macro WHERE after `list macscp_4_2_from_macscp3' (gdb/555)"
    }
    timeout {
        fail "info macro WHERE after `list macscp_4_2_from_macscp3' (timeout)"
    }
    default { fail "info macro WHERE after `list macscp_4_2_from_macscp3'" }
}


#### Test the selection of the macro scope by the current frame.

### A table of functions, in the order they will be reached, which is
### also the order they appear in the preprocessed output.  Each entry
### has the form {FUNCNAME WHERE KFAILWHERE}, where:
### - FUNCNAME is the name of the function,
### - WHERE is the definition we expect to see for the macro `WHERE', as
###   returned by `info_macro', and
### - KFAILWHERE is an alternate definition which should be reported
###   as a `known failure', due to GDB's inability to distinguish multiple
###   #inclusions of the same file.
### KFAILWHERE may be omitted.

set funcs {
    {
        macscp1_1
        {macscp1.c {before macscp1_1}}
    }
    {
        macscp2_1
        {macscp2.h macscp1.c {before macscp2_1}}
    }
    {
        macscp4_1_from_macscp2
        {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
        {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
    }
    {
        macscp4_2_from_macscp2
        {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
        {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
    }
    {
        macscp2_2
        {macscp2.h macscp1.c {before macscp2_2}}
    }
    {
        macscp1_2
        {macscp1.c {before macscp1_2}}
    }
    {
        macscp3_1
        {macscp3.h macscp1.c {before macscp3_1}}
    }
    {
        macscp4_1_from_macscp3
        {macscp4.h macscp3.h macscp1.c {before macscp4_1_..., from macscp3.h}}
        {macscp4.h macscp2.h macscp1.c {before macscp4_1_..., from macscp2.h}}
    }
    {
        macscp4_2_from_macscp3
        {macscp4.h macscp3.h macscp1.c {before macscp4_2_..., from macscp3.h}}
        {macscp4.h macscp2.h macscp1.c {before macscp4_2_..., from macscp2.h}}
    }
    {
        macscp3_2
        {macscp3.h macscp1.c {before macscp3_2}}
    }
    {
        macscp1_3
        {macscp1.c {before macscp1_3}}
    }
}

proc maybe_kfail { func test_name } {
    # We can't get the right scope info when we're stopped in
    # the macro4_ functions.
    if {[string match macscp4_* $func]} {
	kfail gdb/555 "$test_name"
    } else {
	fail "$test_name"
    }
}

# Start the program running.
if {! [runto_main]} {
    fail "macro tests suppressed: couldn't run to main"
    return 0
}

# Set a breakpoint on each of the functions.
foreach func_entry $funcs {
    set func [lindex $func_entry 0]
    gdb_test "break $func" "Breakpoint.*"
}

# Run to each of the breakpoints and check the definition (or lack
# thereof) of each macro.
for {set i 0} {$i < [llength $funcs]} {incr i} {
    set func_entry [lindex $funcs $i]
    set func [lindex $func_entry 0]
    set expected [lindex $func_entry 1]
    set kfail_expected [lindex $func_entry 2]

    # Run to the breakpoint for $func.
    gdb_test "continue" "Breakpoint $decimal, $func .*" "continue to $func"

    # Check the macro WHERE.
    set result [info_macro WHERE]
    if {[string compare $result $expected] == 0} {
        pass "info macro WHERE stopped in $func"
    } elseif {[string compare $result $kfail_expected] == 0} {
        setup_kfail *-*-* "gdb/555"
        fail "info macro WHERE stopped in $func (gdb/555)"
    } elseif {[string compare $result timeout] == 0} {
        fail "info macro WHERE stopped in $func (timeout)"
    } else {
        fail "info macro WHERE stopped in $func"
    }

    # Check that the BEFORE_<func> macros for all prior functions are
    # #defined, and that those for all subsequent functions are not.
    for {set j 0} {$j < [llength $funcs]} {incr j} {
        if {$j != $i} {
            set func_j_entry [lindex $funcs $j]
            set func_j [lindex $func_j_entry 0]

            set before_macro "BEFORE_[string toupper $func_j]"
            set test_name \
                    "$before_macro defined/undefined when stopped at $func"
            set result [info_macro $before_macro]

            if {$j < $i} {
                if {[llength $result] >= 2 && \
                        [string compare [lindex $result end] {}] == 0} {
                    pass $test_name
                } elseif {[string compare $result timeout] == 0} {
                    fail "$test_name (timeout)"
                } else {
                    maybe_kfail $func "$test_name"
                }
            } elseif {$j > $i} {
                switch -- [lindex $result end] {
                    undefined { pass $test_name }
                    timeout { fail "$test_name (timeout)" }
                    default { 
                        maybe_kfail $func "$test_name"
                    }
                }
            }

            set until_macro "UNTIL_[string toupper $func_j]"
            set test_name \
                    "$until_macro defined/undefined when stopped at $func"
            set result [info_macro $until_macro]

            if {$j <= $i} {
                switch -- [lindex $result end] {
                    undefined { pass $test_name }
                    timeout { fail "$test_name (timeout)" }
                    default { 
                        maybe_kfail $func "$test_name"
                    }
                }
            } elseif {$j > $i} {
                if {[llength $result] >= 2 && \
                        [string compare [lindex $result end] {}] == 0} {
                    pass $test_name
                } elseif {[string compare $result timeout] == 0} {
                    fail "$test_name (timeout)"
                } else {
                    maybe_kfail $func "$test_name"
                }
            }
        }
    }
}

gdb_test "break [gdb_get_line_number "set breakpoint here"]" \
    "Breakpoint.*at.* file .*, line.*" \
    "breakpoint macscp_expr"

gdb_test "continue" "foo = 0;.*" "continue to macsp_expr"

gdb_test "print M" \
    "No symbol \"M\" in current context\." \
    "print expression with macro before define."

gdb_test "next" "foo = 1;" "next to definition"

gdb_test "print M" \
    " = 0" \
    "print expression with macro in scope."

gdb_test "macro define M 72" \
  "" \
  "user macro override"

gdb_test "print M" \
  " = 72" \
  "choose user macro"

gdb_test "macro undef M" \
  "" \
  "remove user override"

gdb_test "print M" \
    " = 0" \
    "print expression with macro after removing override"

gdb_test "next" "foo = 2;" "next to definition"

gdb_test "print M" \
    "No symbol \"M\" in current context\." \
    "print expression with macro after undef."

gdb_test "macro define M 5" \
  "" \
  "basic macro define"

gdb_test "print M" \
  " = 5" \
  "expansion of defined macro"

gdb_test "macro list" \
  "macro define M 5" \
  "basic macro list"

gdb_test "macro define M(x) x" \
  "" \
  "basic redefine, macro with args"

gdb_test "print M (7)" \
  " = 7" \
  "expansion of macro with arguments"

gdb_test "macro undef M" \
  "" \
  "basic macro undef"

gdb_test "print M" \
    "No symbol \"M\" in current context\." \
    "print expression with macro after user undef."