public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix comparison of array types
       [not found] <20240209194545.31497-1-ssbssa.ref@yahoo.de>
@ 2024-02-09 19:45 ` Hannes Domani
  2024-02-25 12:02   ` Hannes Domani
                     ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Hannes Domani @ 2024-02-09 19:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Keith Seitz

Currently it's not possible to call functions if an argument is a
pointer to an array:
```
(gdb) l f
1       int f (int (*x)[2])
2       {
3         return x[0][1];
4       }
5
6       int main()
7       {
8         int a[2][2] = {{0, 1}, {2, 3}};
9         return f (a);
10      }
(gdb) p f(a)
Cannot resolve function f to any overloaded instance
```

This happens because types_equal doesn't handle array types, so the
function is never even considered as a possibility.

With array type handling added, by comparing element types and array
bounds, the same works:
```
(gdb) p f(a)
$1 = 1
```

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=15398
Co-Authored-By: Keith Seitz <keiths@redhat.com>
---
 gdb/gdbtypes.c                    | 13 +++++++++++++
 gdb/testsuite/gdb.cp/converts.exp |  1 +
 2 files changed, 14 insertions(+)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index dcd7321d979..6c0d20b2daf 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -4202,6 +4202,19 @@ types_equal (struct type *a, struct type *b)
       return true;
     }
 
+  /* Two array types are the same if they have the same element types
+     and array bounds.  */
+  if (a->code () == TYPE_CODE_ARRAY)
+    {
+      if (!types_equal (a->target_type (), b->target_type ()))
+	return false;
+
+      if (*a->bounds () != *b->bounds ())
+	return false;
+
+      return true;
+    }
+
   return false;
 }
 \f
diff --git a/gdb/testsuite/gdb.cp/converts.exp b/gdb/testsuite/gdb.cp/converts.exp
index bf608bdcccd..6ea21fec563 100644
--- a/gdb/testsuite/gdb.cp/converts.exp
+++ b/gdb/testsuite/gdb.cp/converts.exp
@@ -48,6 +48,7 @@ gdb_test "p foo1_8 (bp)" "Using non-standard.*" "pointer to long int"
 gdb_test "p foo1_5 (b)" "= 15"             "pointer pointer to void pointer"
 gdb_test "p foo2_1 (b)" "= 21"             "pointer pointer to pointer pointer"
 gdb_test "p foo2_2 (b)" "Cannot resolve.*" "pointer pointer to array of arrays"
+gdb_test "p foo2_2 (ba)" "= 22"            "array of arrays to array of arrays"
 gdb_test "p foo2_3 (b)" "= 23"             "pointer pointer to array of pointers"
 gdb_test "p foo2_4 (b)" "Cannot resolve.*" "pointer pointer to array of wrong pointers"
 
-- 
2.35.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix comparison of array types
  2024-02-09 19:45 ` [PATCH] Fix comparison of array types Hannes Domani
@ 2024-02-25 12:02   ` Hannes Domani
  2024-02-29 13:26   ` Guinevere Larsen
  2024-03-18 16:20   ` Tom Tromey
  2 siblings, 0 replies; 7+ messages in thread
From: Hannes Domani @ 2024-02-25 12:02 UTC (permalink / raw)
  To: gdb-patches; +Cc: Keith Seitz

 Ping.


Am Freitag, 9. Februar 2024 um 20:46:35 MEZ hat Hannes Domani <ssbssa@yahoo.de> Folgendes geschrieben:

> Currently it's not possible to call functions if an argument is a
> pointer to an array:
> ```
> (gdb) l f
> 1      int f (int (*x)[2])
> 2      {
> 3        return x[0][1];
> 4      }
> 5
> 6      int main()
> 7      {
> 8        int a[2][2] = {{0, 1}, {2, 3}};
> 9        return f (a);
> 10      }
> (gdb) p f(a)
> Cannot resolve function f to any overloaded instance
> ```
>
> This happens because types_equal doesn't handle array types, so the
> function is never even considered as a possibility.
>
> With array type handling added, by comparing element types and array
> bounds, the same works:
> ```
> (gdb) p f(a)
> $1 = 1
> ```
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=15398
> Co-Authored-By: Keith Seitz <keiths@redhat.com>
> ---
> gdb/gdbtypes.c                    | 13 +++++++++++++
> gdb/testsuite/gdb.cp/converts.exp |  1 +
> 2 files changed, 14 insertions(+)
>
> diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
> index dcd7321d979..6c0d20b2daf 100644
> --- a/gdb/gdbtypes.c
> +++ b/gdb/gdbtypes.c
> @@ -4202,6 +4202,19 @@ types_equal (struct type *a, struct type *b)
>       return true;
>     }
>
> +  /* Two array types are the same if they have the same element types
> +    and array bounds.  */
> +  if (a->code () == TYPE_CODE_ARRAY)
> +    {
> +      if (!types_equal (a->target_type (), b->target_type ()))
> +    return false;
> +
> +      if (*a->bounds () != *b->bounds ())
> +    return false;
> +
> +      return true;
> +    }
> +
>   return false;
> }
>
> diff --git a/gdb/testsuite/gdb.cp/converts.exp b/gdb/testsuite/gdb.cp/converts.exp
> index bf608bdcccd..6ea21fec563 100644
> --- a/gdb/testsuite/gdb.cp/converts.exp
> +++ b/gdb/testsuite/gdb.cp/converts.exp
> @@ -48,6 +48,7 @@ gdb_test "p foo1_8 (bp)" "Using non-standard.*" "pointer to long int"
> gdb_test "p foo1_5 (b)" "= 15"            "pointer pointer to void pointer"
> gdb_test "p foo2_1 (b)" "= 21"            "pointer pointer to pointer pointer"
> gdb_test "p foo2_2 (b)" "Cannot resolve.*" "pointer pointer to array of arrays"
> +gdb_test "p foo2_2 (ba)" "= 22"            "array of arrays to array of arrays"
> gdb_test "p foo2_3 (b)" "= 23"            "pointer pointer to array of pointers"
> gdb_test "p foo2_4 (b)" "Cannot resolve.*" "pointer pointer to array of wrong pointers"
>
> --
> 2.35.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix comparison of array types
  2024-02-09 19:45 ` [PATCH] Fix comparison of array types Hannes Domani
  2024-02-25 12:02   ` Hannes Domani
@ 2024-02-29 13:26   ` Guinevere Larsen
  2024-03-17 14:12     ` Hannes Domani
  2024-03-18 16:26     ` Tom Tromey
  2024-03-18 16:20   ` Tom Tromey
  2 siblings, 2 replies; 7+ messages in thread
From: Guinevere Larsen @ 2024-02-29 13:26 UTC (permalink / raw)
  To: Hannes Domani, gdb-patches; +Cc: Keith Seitz

On 09/02/2024 20:45, Hannes Domani wrote:
> Currently it's not possible to call functions if an argument is a
> pointer to an array:
> ```
> (gdb) l f
> 1       int f (int (*x)[2])
> 2       {
> 3         return x[0][1];
> 4       }
> 5
> 6       int main()
> 7       {
> 8         int a[2][2] = {{0, 1}, {2, 3}};
> 9         return f (a);
> 10      }
> (gdb) p f(a)
> Cannot resolve function f to any overloaded instance
> ```
>
> This happens because types_equal doesn't handle array types, so the
> function is never even considered as a possibility.
>
> With array type handling added, by comparing element types and array
> bounds, the same works:
> ```
> (gdb) p f(a)
> $1 = 1
> ```
>
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=15398
> Co-Authored-By: Keith Seitz <keiths@redhat.com>

Hi!

This patch fixes the problem (for both gcc and clang) and introduces no 
regressions.

My one nitpick is that I think this code fits more naturally right after 
the pointer check, instead of the last check in the function, but feel 
free to disagree. Either way: Reviewed-By: Guinevere Larsen 
<blarsen@redhat.com>

-- 
Cheers,
Guinevere Larsen
She/Her/Hers

> ---
>   gdb/gdbtypes.c                    | 13 +++++++++++++
>   gdb/testsuite/gdb.cp/converts.exp |  1 +
>   2 files changed, 14 insertions(+)
>
> diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
> index dcd7321d979..6c0d20b2daf 100644
> --- a/gdb/gdbtypes.c
> +++ b/gdb/gdbtypes.c
> @@ -4202,6 +4202,19 @@ types_equal (struct type *a, struct type *b)
>         return true;
>       }
>   
> +  /* Two array types are the same if they have the same element types
> +     and array bounds.  */
> +  if (a->code () == TYPE_CODE_ARRAY)
> +    {
> +      if (!types_equal (a->target_type (), b->target_type ()))
> +	return false;
> +
> +      if (*a->bounds () != *b->bounds ())
> +	return false;
> +
> +      return true;
> +    }
> +
>     return false;
>   }
>   \f
> diff --git a/gdb/testsuite/gdb.cp/converts.exp b/gdb/testsuite/gdb.cp/converts.exp
> index bf608bdcccd..6ea21fec563 100644
> --- a/gdb/testsuite/gdb.cp/converts.exp
> +++ b/gdb/testsuite/gdb.cp/converts.exp
> @@ -48,6 +48,7 @@ gdb_test "p foo1_8 (bp)" "Using non-standard.*" "pointer to long int"
>   gdb_test "p foo1_5 (b)" "= 15"             "pointer pointer to void pointer"
>   gdb_test "p foo2_1 (b)" "= 21"             "pointer pointer to pointer pointer"
>   gdb_test "p foo2_2 (b)" "Cannot resolve.*" "pointer pointer to array of arrays"
> +gdb_test "p foo2_2 (ba)" "= 22"            "array of arrays to array of arrays"
>   gdb_test "p foo2_3 (b)" "= 23"             "pointer pointer to array of pointers"
>   gdb_test "p foo2_4 (b)" "Cannot resolve.*" "pointer pointer to array of wrong pointers"
>   



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix comparison of array types
  2024-02-29 13:26   ` Guinevere Larsen
@ 2024-03-17 14:12     ` Hannes Domani
  2024-03-18 16:26     ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Hannes Domani @ 2024-03-17 14:12 UTC (permalink / raw)
  To: gdb-patches, Guinevere Larsen; +Cc: Keith Seitz

 Am Donnerstag, 29. Februar 2024 um 14:26:16 MEZ hat Guinevere Larsen <blarsen@redhat.com> Folgendes geschrieben:

> On 09/02/2024 20:45, Hannes Domani wrote:
> > Currently it's not possible to call functions if an argument is a
> > pointer to an array:
> > ```
> > (gdb) l f
> > 1      int f (int (*x)[2])
> > 2      {
> > 3        return x[0][1];
> > 4      }
> > 5
> > 6      int main()
> > 7      {
> > 8        int a[2][2] = {{0, 1}, {2, 3}};
> > 9        return f (a);
> > 10      }
> > (gdb) p f(a)
> > Cannot resolve function f to any overloaded instance
> > ```
> >
> > This happens because types_equal doesn't handle array types, so the
> > function is never even considered as a possibility.
> >
> > With array type handling added, by comparing element types and array
> > bounds, the same works:
> > ```
> > (gdb) p f(a)
> > $1 = 1
> > ```
> >
> > Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=15398
> > Co-Authored-By: Keith Seitz <keiths@redhat.com>
>
> Hi!
>
> This patch fixes the problem (for both gcc and clang) and introduces no
> regressions.
>
> My one nitpick is that I think this code fits more naturally right after
> the pointer check, instead of the last check in the function, but feel
> free to disagree. Either way: Reviewed-By: Guinevere Larsen
> <blarsen@redhat.com>

Either position is fine for me.


Hannes

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix comparison of array types
  2024-02-09 19:45 ` [PATCH] Fix comparison of array types Hannes Domani
  2024-02-25 12:02   ` Hannes Domani
  2024-02-29 13:26   ` Guinevere Larsen
@ 2024-03-18 16:20   ` Tom Tromey
  2024-03-20 15:46     ` Hannes Domani
  2 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2024-03-18 16:20 UTC (permalink / raw)
  To: Hannes Domani; +Cc: gdb-patches, Keith Seitz

>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

Hannes> This happens because types_equal doesn't handle array types, so the
Hannes> function is never even considered as a possibility.

Hannes> With array type handling added, by comparing element types and array
Hannes> bounds, the same works:

Sorry about the delay on this.  This is OK.

Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix comparison of array types
  2024-02-29 13:26   ` Guinevere Larsen
  2024-03-17 14:12     ` Hannes Domani
@ 2024-03-18 16:26     ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2024-03-18 16:26 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: Hannes Domani, gdb-patches, Keith Seitz

>>>>> Guinevere Larsen <blarsen@redhat.com> writes:

> My one nitpick is that I think this code fits more naturally right
> after the pointer check, instead of the last check in the function,
> but feel free to disagree. Either way: Reviewed-By: Guinevere Larsen
> <blarsen@redhat.com>

This would make sense too, but I'm not sure how much it matters.
In most languages, array types don't have a name anyway.

Also I wonder if we really want to keep all these different
type-equality functions around.  Right now there is types_equal,
types_deeply_equal, and also ada_type_match.

The Ada one is maybe hard to remove as long as GNAT encodings are
relevant.  However maybe we could replace types_equal with
types_deeply_equal everywhere.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fix comparison of array types
  2024-03-18 16:20   ` Tom Tromey
@ 2024-03-20 15:46     ` Hannes Domani
  0 siblings, 0 replies; 7+ messages in thread
From: Hannes Domani @ 2024-03-20 15:46 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches, Keith Seitz

 Am Montag, 18. März 2024 um 17:20:38 MEZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:

> >>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:
>
> Hannes> This happens because types_equal doesn't handle array types, so the
> Hannes> function is never even considered as a possibility.
>
> Hannes> With array type handling added, by comparing element types and array
> Hannes> bounds, the same works:
>
> Sorry about the delay on this.  This is OK.
>
> Approved-By: Tom Tromey <tom@tromey.com>

Pushed, thanks.


Hannes

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-03-20 15:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20240209194545.31497-1-ssbssa.ref@yahoo.de>
2024-02-09 19:45 ` [PATCH] Fix comparison of array types Hannes Domani
2024-02-25 12:02   ` Hannes Domani
2024-02-29 13:26   ` Guinevere Larsen
2024-03-17 14:12     ` Hannes Domani
2024-03-18 16:26     ` Tom Tromey
2024-03-18 16:20   ` Tom Tromey
2024-03-20 15:46     ` Hannes Domani

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).