xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.cp/formatted-ref.exp (revision eceb233b9bd0dfebb902ed73b531ae6964fa3f9b)
1# Copyright 2007-2019 Free Software Foundation, Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# Author: P. N. Hilfinger, AdaCore, Inc.
17
18# This test checks the behavior of formatted print when applied to a
19# reference value.  The intended behavior is that a formatted print of
20# such a value should display the same value as a plain print,
21# modulo format, of course.  Older versions of GDB would instead print
22# the reference's address value itself when doing a formatted print,
23# rather than printing both that and the dereferenced value.  We also
24# check that the (non-standard) expression &(&x), where x is of type T&,
25# yields an appropriate value.
26# This also tests that some other arithmetic operations on references
27# work properly: condition expression using a reference object as one of its
28# operand.
29
30
31if { [skip_cplus_tests] } { continue }
32
33standard_testfile .cc
34
35if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
36    return -1
37}
38
39proc get_address { var } {
40    global expect_out
41    global gdb_prompt
42
43    gdb_test_multiple "print &$var" "find address of $var" {
44	-re "\\$\[0-9\]+ = \\(.*\\) (0x\[0-9a-f\]+).*$gdb_prompt $" {
45            return $expect_out(1,string)
46 	}
47        timeout {
48	    perror "couldn't find address of $var"
49	    return ""
50        }
51    }
52    return ""
53}
54
55proc test_p_x { var type val addr } {
56    global gdb_prompt
57
58    set test "print/x $var"
59    gdb_test_multiple $test $test {
60        -re "\\$\[0-9\]+ = \\([string_to_regexp $type]\\) @0x\[a-f0-9\]+: [string_to_regexp $val].*$gdb_prompt $" {
61	    pass $test
62	}
63	-re "\\$\[0-9\]+ = $addr.*$gdb_prompt $" {
64            fail "$test (prints just address)"
65        }
66	-re "\\$\[0-9\]+ = 0x\[a-f0-9\]+.*$gdb_prompt $" {
67            fail "$test (prints unexpected address)"
68        }
69    }
70    return 0
71}
72
73proc test_p_x_addr { var addr } {
74    global gdb_prompt
75
76    set test "print/x &$var"
77    gdb_test_multiple $test $test {
78        -re "\\$\[0-9\]+ = $addr.*$gdb_prompt $" {
79	    pass $test
80	}
81	-re "\\$\[0-9\]+ = 0x\[a-f0-9+\]+.*$gdb_prompt $" {
82            fail "$test (prints unexpected address)"
83        }
84    }
85    return 0
86}
87
88proc test_p_x_ref_addr { var addr } {
89    global gdb_prompt
90
91    set test "print/x *(&(&$var))"
92    gdb_test_multiple $test $test {
93        -re "\\$\[0-9\]+ = $addr.*$gdb_prompt $" {
94	    pass $test
95	}
96	-re "Attempt to take address of value not located in memory.*$gdb_prompt $" {
97	    # The reference might be in a register.  At least we parsed
98	    # correctly...
99	    pass $test
100	}
101	-re "\\$\[0-9\]+ = 0x\[a-f0-9+\]+.*$gdb_prompt $" {
102            fail "$test (prints unexpected address)"
103        }
104    }
105    return 0
106}
107
108proc test_p_op1_equals_op2 {op1 op2} {
109    set test "print $op1 == $op2"
110    gdb_test $test "\\$\[0-9\]+ = true"
111}
112
113runto ${srcfile}:[gdb_get_line_number "marker here"]
114
115set s1_address  [get_address "s1"]
116set e1_address  [get_address "e1"]
117set i1_address  [get_address "i1"]
118
119test_p_x "s" "Struct1 &" "{x = 0xd, y = 0x13}" $s1_address
120test_p_x "e" "Enum1 &" "0xb" $e1_address
121test_p_x "i" "int &" "0x17" $i1_address
122
123test_p_x_addr "s" $s1_address
124test_p_x_addr "e" $e1_address
125test_p_x_addr "i" $i1_address
126
127test_p_x_ref_addr "s" $s1_address
128test_p_x_ref_addr "i" $i1_address
129test_p_x_ref_addr "e" $e1_address
130
131test_p_op1_equals_op2 "s.x" "13"
132