Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Keith Seitz <keiths@redhat.com>
To: "Aktemur, Tankut Baris" <tankut.baris.aktemur@intel.com>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: Re: [PATCH v2] infcall: Add support for integer literals as reference function paramters
Date: Tue, 27 Jan 2026 10:43:54 -0800	[thread overview]
Message-ID: <e9d06af7-96c9-41ea-82c6-3f77cb801b31@redhat.com> (raw)
In-Reply-To: <DM4PR11MB73034523C1A76887D4235DEDC494A@DM4PR11MB7303.namprd11.prod.outlook.com>

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


  reply	other threads:[~2026-01-27 18:44 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-16 15:48 [PATCH] " Keith Seitz
2025-10-17 14:49 ` Tom Tromey
2025-10-20 19:00   ` Keith Seitz
2025-10-20 19:36 ` [PATCH v2] " Keith Seitz
2025-10-21 20:19   ` Tom Tromey
2025-10-22 12:05     ` Andrew Burgess
2025-10-22 13:21       ` Tom Tromey
2026-01-22 19:05       ` Keith Seitz
2026-01-23 14:07         ` Aktemur, Tankut Baris
2026-01-27 18:43           ` Keith Seitz [this message]
2026-01-27 19:01 ` [PATCH v4] " Keith Seitz
2026-01-28  8:24   ` Aktemur, Tankut Baris
2026-01-28 13:54   ` Andrew Burgess
2026-02-02 17:05     ` Aktemur, Tankut Baris
2026-02-02 17:21     ` Keith Seitz
2026-01-30 20:59   ` Tom Tromey
2026-02-02 16:58     ` Keith Seitz
2026-02-02 17:22       ` Aktemur, Tankut Baris
2026-02-12 16:31       ` Tom Tromey
2026-03-12 14:14 ` [PATCH v5] infcall: Add support for integer literals as reference function parameters Keith Seitz
2026-03-12 16:23   ` Tom de Vries
2026-03-12 16:45     ` Keith Seitz
2026-03-12 17:12 ` [PATCH v6] " Keith Seitz
2026-03-17 14:10   ` Andrew Burgess
2026-03-17 18:11     ` Keith Seitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e9d06af7-96c9-41ea-82c6-3f77cb801b31@redhat.com \
    --to=keiths@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tankut.baris.aktemur@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox