1# Copyright 2022-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# This testcase checks that if a function has the DW_AT_calling_convention
17# attribute with the value DW_CC_nocall, then GDB will not:
18# - call the function,
19# - try to access the value returned by the function when using the finish
20#   command,
21# - force a user-provided return value when using the return command.
22#
23# In every case, GDB prints a message to the user indicating the issue.  For
24# the return command, GDB asks the user to confirm if the specified value
25# should be forced.
26
27load_lib dwarf.exp
28
29# This test can only be run on targets which support DWARF-2 and use gas.
30if {![dwarf2_support]} {
31    return 0
32}
33
34standard_testfile .c .S
35
36# First compile the .c file so we can ask GDB what is sizeof(int).
37if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } {
38    untested "failed to compile"
39    return -1
40}
41
42# Make some DWARF for the test.
43set asm_file [standard_output_file $srcfile2]
44Dwarf::assemble $asm_file {
45    cu {} {
46	compile_unit {
47	    {language @DW_LANG_C}
48	    {name "calling-convention"}
49	} {
50	    declare_labels int_label
51
52	    int_label: base_type {
53		{byte_size [get_sizeof "int" 4] sdata}
54		{encoding @DW_ATE_signed}
55		{name "int"}
56	    }
57
58	    subprogram {
59		{MACRO_AT_func { foo }}
60		{type :$int_label}
61		{calling_convention @DW_CC_nocall}
62	    }
63
64	    subprogram {
65		{MACRO_AT_func { main }}
66		{type :$int_label}
67	    }
68	}
69    }
70}
71
72if { [prepare_for_testing "failed to prepare" ${testfile} \
73	  [list $srcfile $asm_file] {nodebug}] } {
74    return -1
75}
76
77if {![runto_main]} {
78    return -1
79}
80
81gdb_test "call foo ()" \
82    "Cannot call the function 'foo' which does not follow the target calling convention."
83gdb_breakpoint "foo"
84gdb_continue_to_breakpoint "foo"
85
86gdb_test "return 35" \
87       "Not confirmed" \
88       "return 35" \
89       [multi_line \
90           "Function 'foo' does not follow the target calling convention\\." \
91           "If you continue, setting the return value will probably lead to unpredictable behaviors\\." \
92           "Make foo return now\\? \\(y or n\\) $"] \
93       "n"
94
95gdb_test "finish" [multi_line \
96    "Run till exit from #0  $hex in foo \\(\\)" \
97    "warning: Function 'foo' does not follow the target calling convention, cannot determine its returned value\." \
98    "$hex in main \\(\\)" \
99    "Value returned has type: int. Cannot determine contents"]
100