Re: [PATCH v2] infcall: Add support for integer literals as reference function paramters

Keith Seitz
2026-01-27T18:44:27Z

Hi!

On 1/23/26 6:07 AM, Aktemur, Tankut Baris wrote:
> On Thursday, January 22, 2026 8:06 PM, Keith Seitz wrote:
>> diff --git a/gdb/infcall.c b/gdb/infcall.c
>> index dcbae679d07..b836a868d15 100644
>> --- a/gdb/infcall.c
>> +++ b/gdb/infcall.c
>> @@ -276,10 +276,23 @@ value_arg_coerce (struct gdbarch *gdbarch, struct
>> value *arg,
>> return value_cast_pointers (type, arg, 0);
>>
>> /* Cast the value to the reference's target type, and then
>> - convert it back to a reference. This will issue an error
>> - if the value was not previously in memory - in some cases
>> - we should clearly be allowing this, but how? */
>> + convert it back to a reference. For C++ reference parameters,
>> + if the value is not already in memory (e.g., a literal), we
>> + need to allocate space in the inferior and copy the value there.
>> + If the value is already in memory, we can use its address
>> + directly. */
>> new_value = value_cast (type->target_type (), arg);
>> + if (new_value->lval () != lval_memory
>> + && language_pass_by_reference (new_value->type ())
>> + .trivially_copyable)
>> + {
>> + LONGEST length = check_typedef (new_value->type ())->length ();
>> + struct value *addr_val = value_allocate_space_in_inferior
>> (length);
>
> This uses malloc to allocate the space. I was wondering if allocating
> space on the stack wouldn't be better. That's how arguments that are
> implicitly pass-by-reference are passed. Also struct return values
> are forced to lval in stack-allocated memory.
>
> Furthermore, malloc would not be possible everywhere (e.g. on GPUs);
> stack allocation would be supported by more platforms.
>
> There is gdbarch_reserve_stack_space to allocate space on stack,
> but it needs to be passed the current stack pointer. I don't know
> how easy it is to get and set the SP in this context.

Yes, I think we can use stack for this -- a lot of
call_function_by_hand_dummy does this already. I've made this change.

>> + CORE_ADDR addr = value_as_address (addr_val);
>> + write_memory (addr, new_value->contents ().data (), length);
>> + new_value = value_at_lazy (new_value->type (), addr);
>
> IMHO, we could use
>
> CORE_ADDR addr = allocate_space_in_inferior (length); // Or stack space.
> new_value->force_lval (addr);

I've made this change.

>> diff --git a/gdb/testsuite/gdb.cp/ref-params.exp
>> b/gdb/testsuite/gdb.cp/ref-params.exp
>> index b61055e9f50..a94927fae56 100644
>> --- a/gdb/testsuite/gdb.cp/ref-params.exp
>> +++ b/gdb/testsuite/gdb.cp/ref-params.exp

[snip]

>> +gdb_test "print const_ref_func(global_obj)" \
>> + "= 20" \
>> + "call function with const ref param and global object"
>
> Another potentially interesting testcase would be passing
> a "literal" object, like this:
>
> print const_ref_func((TestClass) {42})
>
> This case is also fixed with your patch.

I've added these tests.
>
>> +# Test functions taking non-const reference parameter.
>> +gdb_test "print ref_func(10)" \
>> + "Cannot resolve function ref_func to any overloaded instance" \
>> + "call function with non-const ref param and literal"
>> +
>> +gdb_test "print ref_func(global_int)" \
>> + " = 43" \
>> + "call function with non-const ref param and global variable"
>> +
>> +gdb_test "print ref_func(local_var)" \
>> + " = 16" \
>> + "call function with non-const ref param and local variable"
>> +
>> +gdb_test "print ref_func(obj)" \
>> + "= 6" \
>> + "call function with non-const ref param and object"
>> +
>> +gdb_test "print ref_func(global_obj)" \
>> + "= 11" \
>> + "call function with non-const ref param and global object"
>> +
>> +# Test methods taking constant reference parameter.
>> +gdb_test "print obj.const_ref_method(5)" \
>> + "= 10" \
>> + "call const method with const ref param and literal"
>> +
>> +gdb_test "print obj.const_ref_method(global_int)" \
>> + "= 47" \
>> + "call const method with const ref param and global variable"
>> +
>> +gdb_test "print obj.const_ref_method(local_var)" \
>> + "= 20" \
>> + "call const method with const ref param and local variable"
>> +
>> +gdb_test "print obj.const_ref_method (obj)" \
>
> Nit: The other tests don't have space before parenthesis.
>

You are correct, and trailing parentheses are not permitted in test
names like this (or used to anyway). I've corrected this typo.
Good eye!

[snip]

>> +gdb_test "print global_obj.ref_method(global_obj)" \
>> + "= 30" \
>> + "call global method with non-const ref param and global object"
>> \ No newline at end of file
>
> Could you please add a new line at the end?

Done.

I will send v4 [note I have an off-by-one error in my versioning].

Thank you for your review!
Keith