1 # Tests for anonymous union support.
2 # Copyright 1998, 1999, 2003, 2004, 2007, 2008, 2009, 2010
3 # Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Written by Satish Pai <pai@apollo.hp.com> 1997-08-19
20 # This file is part of the gdb testsuite
27 # test running programs
33 if { [skip_cplus_tests] } { continue }
35 set testfile "anon-union"
36 set srcfile ${testfile}.cc
37 set binfile ${objdir}/${subdir}/${testfile}
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug c++}] != "" } {
40 perror "Testcase compile failed" 0
44 # Start with a fresh gdb
47 gdb_reinitialize_dir $srcdir/$subdir
51 if ![runto_main] then {
52 perror "couldn't run to breakpoint"
56 send_gdb "set width 0\n"
57 gdb_expect -re "$gdb_prompt $"
59 #send_gdb "ptype foo\n"
61 # -re "\r\n$gdb_prompt $" {
64 # -re ".*$gdb_prompt $" { fail "ptype foo" }
65 # timeout { fail "(timeout) ptype foo" }
68 #send_gdb "ptype bar\n"
70 # -re "\r\n$gdb_prompt $" {
73 # -re ".*$gdb_prompt $" { fail "ptype foo" }
74 # timeout { fail "(timeout) ptype foo" }
77 # NOTE: Add -- ptype foo.x, etc. when things are working
82 -re "40\[ \t\]*foo.paper = 33;\r\n$gdb_prompt $" {
85 -re ".*$gdb_prompt $" { fail "next 1" }
86 timeout { fail "(timeout) next 1" }
89 # Print out the big anon union.
90 send_gdb "print foo\n"
92 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 0, x = \{rock = 0, rock2 = 0\}, \{qux = 0, mux = 0\}, boulder = 0\}, \{paper = 0, cloth = 0\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
95 -re ".*$gdb_prompt $" { fail "print foo 1" }
96 timeout { fail "(timeout) print foo 1" }
99 # Step over assignment to member
103 -re "41\[ \t\]*foo.pebble = 44;\r\n$gdb_prompt $" {
106 -re ".*$gdb_prompt $" { fail "next 1" }
107 timeout { fail "(timeout) next 1" }
110 # Now print out anon union again
111 send_gdb "print foo\n"
113 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 0, x = \{rock = 0, rock2 = 0\}, \{qux = 0, mux = 0\}, boulder = 0\}, \{paper = 33, cloth = 33\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
116 -re ".*$gdb_prompt $" { fail "print foo 2" }
117 timeout { fail "(timeout) print foo 2" }
120 # Modify the member just set
121 send_gdb "set var foo.cloth = 35\n"
123 -re "\r\n$gdb_prompt $" {
124 pass "set var foo.cloth"
126 timeout { fail "(timeout) set var foo.cloth" }
129 # Now print out anon union again to see if the right member was set
130 send_gdb "print foo\n"
132 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 0, x = \{rock = 0, rock2 = 0\}, \{qux = 0, mux = 0\}, boulder = 0\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
135 -re ".*$gdb_prompt $" { fail "print foo 3" }
136 timeout { fail "(timeout) print foo 3" }
140 # Step over next assignment to member
144 -re "42\[ \t\]*foo.mux = 55;\r\n$gdb_prompt $" {
147 -re ".*$gdb_prompt $" { fail "next 2" }
148 timeout { fail "(timeout) next 2" }
151 # Now print out anon union again
152 send_gdb "print foo\n"
154 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 44, x = \{rock = 44, rock2 = 0\}, \{qux = 44, mux = 44\}, boulder = 44\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
157 -re ".*$gdb_prompt $" { fail "print foo 4" }
158 timeout { fail "(timeout) print foo 4" }
161 # Modify the member just set
162 send_gdb "set var foo.pebble = 45\n"
164 -re "\r\n$gdb_prompt $" {
165 pass "set var foo.pebble"
167 timeout { fail "(timeout) set var foo.pebble" }
170 # Now print out anon union again to see if the right member was set
171 send_gdb "print foo\n"
173 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 45, x = \{rock = 45, rock2 = 0\}, \{qux = 45, mux = 45\}, boulder = 45\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
176 -re ".*$gdb_prompt $" { fail "print foo 5" }
177 timeout { fail "(timeout) print foo 5" }
180 # Modify another member at a different level
181 send_gdb "set var foo.qux = 46\n"
183 -re "\r\n$gdb_prompt $" {
184 pass "set var foo.qux"
186 timeout { fail "(timeout) set var foo.qux" }
189 # Now print out anon union again to see if the right member was set
190 send_gdb "print foo\n"
192 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 46, x = \{rock = 46, rock2 = 0\}, \{qux = 46, mux = 46\}, boulder = 46\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
195 -re ".*$gdb_prompt $" { fail "print foo 6" }
196 timeout { fail "(timeout) print foo 6" }
199 # Modify the member at another level, but not the first one in the union
200 send_gdb "set var foo.mux = 47\n"
202 -re "\r\n$gdb_prompt $" {
203 pass "set var foo.mux"
205 timeout { fail "(timeout) set var foo.mux" }
208 # Now print out anon union again to see if things worked
209 send_gdb "print foo\n"
211 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 47, x = \{rock = 47, rock2 = 0\}, \{qux = 47, mux = 47\}, boulder = 47\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
214 -re ".*$gdb_prompt $" { fail "print foo 7" }
215 timeout { fail "(timeout) print foo 7" }
218 # Modify a member of a struct in an anon union
219 send_gdb "set var foo.x.rock = 48\n"
221 -re "\r\n$gdb_prompt $" {
222 pass "set var foo.x.rock"
224 timeout { fail "(timeout) set var foo.x.rock" }
227 # Now print out anon union again to see if things worked
228 send_gdb "print foo\n"
230 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 48, x = \{rock = 48, rock2 = 0\}, \{qux = 48, mux = 48\}, boulder = 48\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
233 -re ".*$gdb_prompt $" { fail "print foo 8" }
234 timeout { fail "(timeout) print foo 8" }
237 # Modify a member of a struct in an anon union, but something
238 # that doesn't alias to some other union member
239 send_gdb "set var foo.x.rock2 = 49\n"
241 -re "\r\n$gdb_prompt $" {
242 pass "set var foo.x.rock2"
244 timeout { fail "(timeout) set var foo.x.rock2" }
247 # Now print out anon union again to see if things worked
248 send_gdb "print foo\n"
250 -re "\\$\[0-9\]* = \{num1 = \{zero = 0, one = 0\}, \{pebble = 48, x = \{rock = 48, rock2 = 49\}, \{qux = 48, mux = 48\}, boulder = 48\}, \{paper = 35, cloth = 35\}, num2 = \{two = 0, three = 0\}\}\r\n$gdb_prompt $" {
253 -re ".*$gdb_prompt $" { fail "print foo 9" }
254 timeout { fail "(timeout) print foo 9" }
258 # Step over next four assignments
261 -re "53\[ \t\]*w = 45;\r\n$gdb_prompt $" {
264 -re ".*$gdb_prompt $" { fail "next 3" }
265 timeout { fail "(timeout) next 3" }
268 # Tests for anon unions that are not members of a class or struct
272 -re "\\$\[0-9\]* = 0\r\n$gdb_prompt $" {
275 -re ".*$gdb_prompt $" { fail "print w 1" }
276 timeout { fail "(timeout) print w 1" }
281 -re "\\$\[0-9\]* = 0\r\n$gdb_prompt $" {
284 -re ".*$gdb_prompt $" { fail "print z 1" }
285 timeout { fail "(timeout) print z 1" }
288 # Step over next assignment to w
291 -re "55\[ \t\]*int j = 0;\r\n$gdb_prompt $" {
294 -re ".*$gdb_prompt $" { fail "next 4" }
295 timeout { fail "(timeout) next 4" }
298 # See if the change in value is noticed
301 -re "\\$\[0-9\]* = 45\r\n$gdb_prompt $" {
304 -re ".*$gdb_prompt $" { fail "print w 2" }
305 timeout { fail "(timeout) print w 2" }
308 # See if z shows the same value
311 -re "\\$\[0-9\]* = 45\r\n$gdb_prompt $" {
314 -re ".*$gdb_prompt $" { fail "print z 2" }
315 timeout { fail "(timeout) print z 2" }
318 # Set the anon union member
319 send_gdb "set var z = 27\n"
321 -re "\r\n$gdb_prompt $" {
324 timeout { fail "(timeout) set var z" }
327 # See if the change in value is noticed
330 -re "\\$\[0-9\]* = 27\r\n$gdb_prompt $" {
333 -re ".*$gdb_prompt $" { fail "print w 3" }
334 timeout { fail "(timeout) print w 3" }
337 # See if z shows the same value
340 -re "\\$\[0-9\]* = 27\r\n$gdb_prompt $" {
343 -re ".*$gdb_prompt $" { fail "print z 3" }
344 timeout { fail "(timeout) print z 3" }