Use CXXCOMPILE in gold/testsuite/Makefile for c++ testcases
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.compile / compile-cplus.exp
CommitLineData
b811d2c2 1# Copyright 2014-2020 Free Software Foundation, Inc.
078a0207
KS
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
16load_lib compile-support.exp
17
18standard_testfile .c compile-shlib.c compile-constvar.S compile-nodebug.c
19
20get_compiler_info
21set options {}
678048e8 22if { [test_compiler_info gcc*] || [test_compiler_info clang*] } {
078a0207
KS
23 lappend options additional_flags=-g3
24 lappend options additional_flags=-std=gnu++11
25 lappend options c++
26}
678048e8
GB
27if [test_compiler_info clang*] {
28 # Treating C input as C++ is deprecated in Clang, so
29 # the build will fail without disabling -Wdeprecated.
30 lappend options additional_flags=-Wno-deprecated
31}
078a0207
KS
32
33if { ![istarget x86_64-*-* ] || ![is_lp64_target] } {
34 verbose "Skipping x86_64 LOC_CONST test."
35 set srcfile3 ""
36}
37
38set srcfilesoptions [list ${srcfile} ${options}]
39if { $srcfile3 != "" } {
40 lappend srcfilesoptions $srcfile3 ${options}
41}
678048e8
GB
42set srcfile4options "nodebug c++"
43if [test_compiler_info clang*] {
44 # Treating C input as C++ is deprecated in Clang, so
45 # the build will fail without disabling -Wdeprecated.
46 set srcfile4options "$srcfile4options additional_flags=-Wno-deprecated"
47}
48lappend srcfilesoptions $srcfile4 $srcfile4options
078a0207
KS
49if { [eval build_executable_from_specs ${testfile}.exp $testfile {$options} ${srcfilesoptions}] } {
50 return -1
51}
52
53clean_restart ${testfile}
54
55#
56# FIXME: Right now, for C++ we just duplicate the C tests, but force
57# the language to C++
58#
59gdb_test_no_output "set language c++" \
60 "Set language to C++"
61
62if ![runto_main] {
63 return -1
64}
65
66if {[skip_compile_feature_tests]} {
67 untested "compile command not supported (could not find libcc1 shared library?)"
68 return -1
69}
70
71#
72# Test delimiter for code, and arguments.
73#
74
75
76gdb_test_no_output "compile code globalvar = SOME_MACRO;" \
77 "set variable from macro"
78gdb_test "p globalvar" " = 23" "expect 23"
79
80gdb_test_no_output "compile code globalvar = ARG_MACRO(0, 0);" \
81 "set variable from function-like macro"
82gdb_test "p globalvar" " = -1" "expect -1"
83
84gdb_test_no_output "compile code globalvar = 42;" "set variable"
85gdb_test "p globalvar" " = 42" "expect 42"
86
87gdb_test_no_output "compile code globalvar *= 2;" "modify variable"
88gdb_test "p globalvar" " = 84" "expect 84"
89
90gdb_test_no_output "compile file -r ${srcdir}/${subdir}/${testfile}-mod.c" \
91 "use external source file"
92gdb_test "p globalvar" " = 7" "expect 7"
93
94gdb_test_no_output "compile code func_static (2);" "call static function"
95gdb_test "p globalvar" " = 9" "expect 9"
96gdb_test_no_output "compile code func_global (1);" "call global function"
97gdb_test "p globalvar" " = 8" "expect 8"
98
99gdb_test_no_output \
100 "compile code globalvar = (sizeof (ulonger) == sizeof (long))" \
101 "compute size of ulonger"
102gdb_test "p globalvar" " = 1" "check size of ulonger"
103gdb_test_no_output \
104 "compile code globalvar = (sizeof (longer) == sizeof (long))" \
105 "compute size of longer"
106gdb_test "p globalvar" " = 1" "check size of longer"
107gdb_test_no_output "compile code globalvar = MINUS_1"
108gdb_test "p globalvar" " = -1" "check MINUS_1"
109
110gdb_test_no_output "compile code globalvar = static_local"
111gdb_test "p globalvar" " = 77000" "check static_local"
112
113gdb_test_no_output \
114 "compile code static int staticvar = 5; intptr = &staticvar" \
115 "do not keep jit in memory"
116gdb_test "p *intptr" "Cannot access memory at address 0x\[0-9a-f\]+" \
117 "expect 5"
118
119gdb_test "compile code func_doesnotexist ();" "error: \'func_doesnotexist\' was not declared in this scope.*"
120
121gdb_test "compile code *(volatile int *) 0 = 0;" \
122 "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB remains in the frame where the signal was received\\.\r\n.*" \
123 "compile code segfault first"
124gdb_test "bt" \
125 "\r\n#0 \[^\r\n\]* in _gdb_expr \[^\r\n\]*\r\n#1 <function called from gdb>\r\n.*"
126
127set test "p/x \$pc"
128set infcall_pc 0
129gdb_test_multiple $test $test {
130 -re " = (0x\[0-9a-f\]+)\r\n$gdb_prompt $" {
131 set infcall_pc $expect_out(1,string)
132 pass $test
133 }
134}
135
136gdb_test "info sym $infcall_pc" "\r\n_gdb_expr.*" "info sym found"
137gdb_test "return" "\r\n#0 main .*" "return" \
138 "Make _gdb_expr\\(__gdb_regs\\*\\) return now\\? \\(y or n\\) " "y"
139gdb_test "info sym $infcall_pc" "\r\nNo symbol matches .*" "info sym not found"
140
141gdb_test_no_output "set unwindonsignal on"
142gdb_test "compile code *(volatile int *) 0 = 0;" \
143 "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB has restored the context to what it was before the call\\.\r\n.*" \
144 "compile code segfault second"
145
146gdb_breakpoint [gdb_get_line_number "break-here"]
147gdb_continue_to_breakpoint "break-here" ".* break-here .*"
148
149# C++ Specific tests.
150## Public methods and members
151
152gdb_test "print foovar.public_var" "42" \
153 "Test compile code foovar.public_var = 42 setting."
154gdb_test_no_output "compile code foovar.public_var = 43;" \
155 "set foobar.public_var to 43"
156gdb_test "print foovar.public_var" "43" \
157 "Test compile code foovar.public_var = 43 setting."
158gdb_test "print foovar.public_method ()" "43" \
159 "Test compile code foovar.public_method = 43 setting."
160
161## Private methods and members
162gdb_test_no_output "compile code foovar.set_private_var (84);" \
163 "Call class function to set private_var"
164gdb_test "print foovar.private_var" "84" \
165 "Test compile code foovar.set_private_var = 84 setting."
166gdb_test_no_output "compile code foovar.private_var = 85" \
167 "Directly set a private member in GDB compile5"
168gdb_test "print foovar.private_var" "85" \
169 "Test compile code foovar.set_private_var = 85 setting."
170
171## Simple inheritance
172CompileExpression::new "var"
173CompileExpression::test "class Baz: public Foo {public: int z = 12;}; Baz bazvar; bazvar.z = 24; var = bazvar.z" 24 -explicit
174## Multiple inheritance
175CompileExpression::test "class MI: public Base, public Base2 {int pure_virt () {return 42;}}; MI MIVar; var = MIVar.pure_virt();" 42 -explicit
176CompileExpression::test "class MI: public Base, public Base2 {int pure_virt () {return Base::return_value() + 42;}}; MI MIVar; var = MIVar.pure_virt();" 43 -explicit
177CompileExpression::test "class Base3 {public: int z = 99;}; class MI: public Base, public Base3 {int pure_virt () {return Base3::z + 42;}}; MI MIVar; var = MIVar.pure_virt();" 141 -explicit
178
179gdb_test "p localvar" " = 50" "expect localvar 50"
180
181gdb_test_no_output "compile code localvar = 12;" "set localvar"
182gdb_test "p localvar" " = 12" "expect 12"
183
184gdb_test_no_output "compile code localvar *= 2;" "modify localvar"
185gdb_test "p localvar" " = 24" "expect 24"
186
187gdb_test_no_output "compile code localvar = shadowed" \
188 "test shadowing"
189gdb_test "p localvar" " = 52" "expect 52"
190
191gdb_test_no_output "compile code localvar = externed"
192gdb_test "p localvar" " = 7" "test extern in inner scope"
193
194gdb_test_no_output "compile code vla\[2\] = 7"
195gdb_test "p vla\[2\]" " = 7"
196gdb_test_no_output \
197 "compile code localvar = (sizeof (vla) == bound * sizeof (vla\[0\]))"
198gdb_test "p localvar" " = 1"
199
200#
201# Test setting fields and also many different types.
202#
203
204gdb_test_no_output "compile code struct_object.selffield = (struct_type*)&struct_object"
205gdb_test "print struct_object.selffield == &struct_object" " = true"
206
207gdb_test_no_output "compile code struct_object.charfield = 1"
208gdb_test "print struct_object.charfield" " = 1 '\\\\001'"
209gdb_test_no_output "compile code struct_object.ucharfield = 1"
210gdb_test "print struct_object.ucharfield" " = 1 '\\\\001'"
211
212foreach {field value} {
213 shortfield -5
214 ushortfield 5
215 intfield -7
216 uintfield 7
217 bitfield 2
218 longfield -9
219 ulongfield 9
220 enumfield ONE
221 floatfield 1
222 doublefield 2
223} {
224 gdb_test_no_output "compile code struct_object.$field = $value"
225 gdb_test "print struct_object.$field" " = $value"
226}
227
228gdb_test_no_output "compile code struct_object.arrayfield\[2\] = 7"
229gdb_test "print struct_object.arrayfield" \
230 " = \\{0, 0, 7, 0, 0\\}"
231
232gdb_test_no_output "compile code struct_object.complexfield = 7 + 5i"
981c08ce 233gdb_test "print struct_object.complexfield" " = 7 \\+ 5i"
078a0207
KS
234
235gdb_test_no_output "compile code struct_object.boolfield = 1"
236gdb_test "print struct_object.boolfield" " = true"
237
238gdb_test_no_output "compile code struct_object.vectorfield\[2\] = 7"
239gdb_test "print struct_object.vectorfield" \
240 " = \\{0, 0, 7, 0\\}"
241
242gdb_test_no_output "compile code union_object.typedeffield = 7"
243gdb_test "print union_object.typedeffield" " = 7"
244gdb_test "print union_object.intfield" " = 7"
245
246
247# LOC_UNRESOLVED tests.
248
249gdb_test "print unresolved" " = 20"
250gdb_test "compile code globalvar = unresolved;"
251gdb_test "print globalvar" " = 20" "print unresolved value"
252
253# Test shadowing with global and static variables.
254
255gdb_test_no_output "compile code globalshadow += 1;"
256gdb_test "print globalshadow" " = 101"
257gdb_test_no_output "compile code extern int globalshadow; globalshadow += 5;"
258gdb_test "print 'compile-cplus.c'::globalshadow" " = 15"
259gdb_test "print globalshadow" " = 101" "print globalshadow second time"
260gdb_test_no_output "compile code staticshadow += 2;"
261gdb_test "print staticshadow" " = 202"
262# "extern int staticshadow;" cannot access static variable.
263
264# Raw code cannot refer to locals.
265# As it references global variable we need the #pragma.
266# For #pragma we need multiline input.
267gdb_test_multiple "compile code -r" "compile code -r multiline 1" { -re "\r\n>$" {} }
268gdb_test_multiple "void _gdb_expr(void) {" "compile code -r multiline 2" { -re "\r\n>$" {} }
269gdb_test_multiple "#pragma GCC push_user_expression" "compile code -r multiline 3" { -re "\r\n>$" {} }
270gdb_test_multiple " globalshadow = 77000;" "compile code -r multiline 4" { -re "\r\n>$" {} }
271gdb_test_multiple "#pragma GCC pop_user_expression" "compile code -r multiline 5" { -re "\r\n>$" {} }
272gdb_test_multiple "}" "compile code -r multiline 6" { -re "\r\n>$" {} }
273gdb_test_no_output "end" "compile code -r multiline 7"
274gdb_test "print 'compile-cplus.c'::globalshadow" " = 77000" \
275 "check globalshadow with -r"
276
277# Test GOT vs. resolving jit function pointers.
278
279gdb_test_no_output "compile -raw -- extern \"C\" void abort(); int func(){return 21;} void _gdb_expr(){int (*funcp)()=func; if (funcp()!=21) abort();}" \
280 "pointer to jit function"
281
282#
283# Test the case where the registers structure would not normally have
284# any fields.
285#
286
287gdb_breakpoint [gdb_get_line_number "no_args_or_locals breakpoint"]
288gdb_continue_to_breakpoint "no_args_or_locals"
289
290gdb_test_no_output "compile code globalvar = 77;" "set variable to 77"
291gdb_test "p globalvar" " = 77" "expect 77"
292
293
294# Test reference to minimal_symbol, not (full) symbol.
295
296setup_kfail compile/23585 *-*-*
297gdb_test_no_output "compile code globalvar = func_nodebug (75);" \
298 "call func_nodebug"
299
300setup_kfail compile/23585 *-*-*
301gdb_test "p globalvar" " = -75" "expect -75"
302
303setup_kfail compile/23585 *-*-*
304gdb_test_no_output \
305 "compile code int (*funcp) (int) = (int(*)(int))func_nodebug; globalvar = funcp (76);" \
306 "call func_nodebug indirectly"
307setup_kfail compile/23585 *-*-*
308gdb_test "p globalvar" " = -76" "expect -76"
309
310
311# Test compiled module memory protection.
312
313gdb_test_no_output "set debug compile on"
314gdb_test "compile code static const int readonly = 1; *(int *) &readonly = 2;" \
315 "The program being debugged was signaled while in a function called from GDB\\.\r\nGDB has restored the context to what it was before the call\\.\r\n.*"
316gdb_test_no_output "set debug compile off"
317
318
319#
320# Some simple coverage tests.
321#
322
323gdb_test "show debug compile" "Compile debugging is .*"
324gdb_test "show compile-args" \
325 "Compile command command-line arguments are .*"
326gdb_test "compile code -z" "Unknown argument.*"
327
328gdb_test "set lang rust" \
329 "Warning: the current language does not match this frame."
330gdb_test "compile code globalvar" "No compiler support for language rust\."
331gdb_test_no_output "set lang auto"
332
333gdb_test_no_output "compile code union union_type newdecl_u"
334gdb_test_no_output "compile code struct struct_type newdecl_s"
335gdb_test_no_output "compile code inttypedef newdecl_i"
336
337gdb_test "compile file" \
338 "You must provide a filename for this command.*" \
339 "Test compile file without a filename"
340gdb_test "compile file -r" \
341 "You must provide a filename with the raw option set.*" \
342 "Test compile file and raw option without a filename"
343gdb_test "compile file -z" \
344 "Unknown argument.*" \
345 "Test compile file with unknown argument"
346
347
348# LOC_CONST tests.
349
350if { $srcfile3 != "" } {
351 gdb_test "p constvar" " = 3"
352 gdb_test "info addr constvar" {Symbol "constvar" is constant\.}
353
354 gdb_test_no_output "compile code globalvar = constvar;"
355 gdb_test "print globalvar" " = 3" "print constvar value"
356} else {
357 untested "print constvar value"
358}
This page took 0.262661 seconds and 4 git commands to generate.