1# Copyright 1998-2023 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# tests for misc. C++ method stuff
17# Written by Satish Pai <pai@apollo.hp.com> 1997-07-08
18
19# This file is part of the gdb testsuite
20
21# This tests:
22# 0. method arguments are correct
23# 1. access to class data members inside method scopes
24# 2. correct param types for methods in ptype.
25# 3. const and volatile methods
26
27# (#0 and #1 above relate to an HP specific problem -- GDB must correctly
28# integrate FPARAM symbols in HP debug info into the local var list
29# for the function or method's block.)
30
31#
32# test running programs
33#
34
35if { [skip_cplus_tests] } { return }
36
37standard_testfile .cc
38
39if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++}]} {
40    return -1
41}
42
43#
44# set it up at a breakpoint so we can play with the variable values
45#
46if {![runto_main]} {
47    return
48}
49
50gdb_test "break A::foo" \
51  "Breakpoint \[0-9\]* at $hex.*file .*method.cc, line 38\\."
52
53gdb_test "continue" \
54  "Continuing\\.\r\n\r\nBreakpoint \[0-9\]*, A::foo(\\(int\\)|) \\(this=$hex, arg=13\\) at .*method\\.cc:38\r\n38\[\t \]*x \\+= arg;" \
55  "continue to A::foo"
56
57# Check ability to access this-relative stuff.
58
59gdb_test "print x" \
60   "\\$\[0-9\]* = 20" \
61   "print x in A::foo"
62
63# Check access to this pointer
64
65gdb_test "print this" \
66   "\\$\[0-9\]* = \\((class |)A *\\* *(const|)\\) $hex" \
67   "print this in A::foo"
68
69# Now do everything over again for A::bar, because sometimes processing one method
70# (the first one) is fine, but the second one's debug info gets munged beyond recognition.
71
72gdb_test "break A::bar" \
73  "Breakpoint \[0-9\]* at $hex.*file .*method.cc, line 44\\."
74
75gdb_test "continue" \
76  "Continuing\\.\r\n\r\nBreakpoint \[0-9\]*, A::bar(|\\(int\\) const| const) \\(this=$hex, arg=15\\) at .*method\\.cc:44\r\n44\[\t \]*return arg \\+ 2 \\* x;" \
77  "continue to A::bar"
78
79# Check ability to access this-relative stuff.
80
81gdb_test "print x" \
82  "\\$\[0-9\]* = 33" \
83  "print x in A::bar"
84
85# Check access to this pointer
86
87get_debug_format
88
89gdb_test_multiple "print this" "print this in A::bar" {
90    -re "\\$\[0-9\]* = \\(const (class |)A *\\* *(const|)\\) $hex\r\n$gdb_prompt $" {
91	pass "print this in A::bar"
92    }
93    -re "\\$\[0-9\]* = \\((class |)A *\\* *(const|)\\) $hex\r\n$gdb_prompt $" {
94	# gcc versions up to 3.0.4 with -gstabs+ do not emit "const"
95	# indicators, so the output is "A *".  It should be "const A
96	# *" or "const A * const".
97	setup_xfail_format "stabs"
98	fail "print this in A::bar (missing const)"
99    }
100    -re "\\$\[0-9\]* = \\(const (class |)\{\\.\\.\\.\} *\\* *(const|)\\) $hex\r\n$gdb_prompt $" {
101	# gcc versions gcc-3_1-branch%20020404 and HEAD%20020404 with -gstabs+
102	# produce good stabs, but gdb prints "const class {...} *" const.
103	# This is PR gdb/277.
104	# setup_kfail "gdb/277"
105	fail "print this in A::bar (gdb/277)"
106    }
107}
108
109# Check again with funk::getFunky (this is the original test case
110# for CHFts23426); sometimes having a constructor with no arguments
111# will nuke the debug info read in for other methods in the class.
112
113gdb_test "break 21" \
114  "Breakpoint \[0-9\]* at $hex.*file .*method.cc, line 21\\."
115
116gdb_test "continue" \
117  "Continuing\\.\r\n\r\nBreakpoint \[0-9\]*, funk::getFunky(\\(int, int\\)|) \\(this=$hex, a=1, b=2\\) at .*method\\.cc:21\r\n21\[\t \]*data_ = res;" \
118  "continue to 21"
119
120# Check ability to access this-relative stuff.
121
122gdb_test "print data_" \
123  "\\$\[0-9\]* = 33" \
124  "print data_ in funk::getFunky"
125
126# Check access to this pointer
127
128gdb_test "print this" \
129  "\\$\[0-9\]* = \\((class |)funk *\\* *(const|)\\) $hex" \
130  "print this in funk::getFunky"
131
132# Check access to local variable
133
134gdb_test "print res" \
135  "\\$\[0-9\]* = -30" \
136   "print res in funk::getFunky"
137
138# Check ptype of class -- should show const/volatile methods
139
140gdb_test_multiple "ptype A" "ptype A" {
141    -re "type = class A \{\r\n\[ \]*public:\r\n\[ \]*int x;\r\n\[ \]*int y;\r\n\r\n\[ \]*int foo\\(int\\);\r\n\[ \]*int bar\\(int\\) const;\r\n\[ \]*int baz\\(int, char\\) volatile;\r\n\[ \]*int qux\\(int, float\\) (const volatile|volatile const);\r\n\}\r\n$gdb_prompt $" {
142	pass "ptype A"
143    }
144    -re "type = class A \{\r\n\[ \]*public:\r\n\[ \]*int x;\r\n\[ \]*int y;\r\n\r\n\[ \]*A & operator=\\(A const ?&\\);\r\n\[ \]*A\\(A const ?&\\);\r\n\[ \]*A\\((void|)\\);\r\n\[ \]*int foo\\(int\\);\r\n\[ \]*int bar\\(int\\) const;\r\n\[ \]*int baz\\(int, char\\) volatile;\r\n\[ \]*int qux\\(int, float\\) (const volatile|volatile const);\r\n\}\r\n$gdb_prompt $" {
145	pass "ptype A"
146    }
147    -re "type = class A \{\r\n\[ \]*public:\r\n\[ \]*int x;\r\n\[ \]*int y;\r\n\r\n\[ \]*int foo\\(int\\);\r\n\[ \]*int bar\\(int\\) const;\r\n\[ \]*int baz\\(int, char\\) volatile;\r\n\[ \]*int qux\\(int, float\\) (const volatile|volatile const);\r\n\[ \]*A & operator=\\(A const ?&\\);\r\n\[ \]*A\\(A const ?&\\);\r\n\[ \]*A\\((void|)\\);\r\n\}\r\n$gdb_prompt $" {
148	pass "ptype A"
149    }
150    -re "type = class A \{\r\n\[ \]*public:\r\n\[ \]*int x;\r\n\[ \]*int y;\r\n\r\n\[ \]*int foo\\(int\\);\r\n\[ \]*int bar\\(int\\) const;\r\n\[ \]*int baz\\(int, char\\) volatile;\r\n\[ \]*int qux\\(int, float\\) const volatile;\r\n\}\r\n$gdb_prompt $" {
151	pass "ptype A"
152    }
153}
154
155gdb_test_multiple "cont" "finish program" {
156    -re "Continuing.\r\n$inferior_exited_re normally.*$gdb_prompt $" {
157	pass "finish program"
158    }
159    -re "Continuing.* EXIT code 0.*$inferior_exited_re normally.*$gdb_prompt $" {
160	pass "finish program (exit wrapper)"
161    }
162}
163
164