Fix comparison of array types

Message ID 20240209194545.31497-1-ssbssa@yahoo.de
State New
Headers
Series Fix comparison of array types |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Hannes Domani Feb. 9, 2024, 7:45 p.m. UTC
  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(+)
  

Comments

Hannes Domani Feb. 25, 2024, 12:02 p.m. UTC | #1
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
  
Guinevere Larsen Feb. 29, 2024, 1:26 p.m. UTC | #2
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>
  
Hannes Domani March 17, 2024, 2:12 p.m. UTC | #3
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
  
Tom Tromey March 18, 2024, 4:20 p.m. UTC | #4
>>>>> "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
  
Tom Tromey March 18, 2024, 4:26 p.m. UTC | #5
>>>>> 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
  
Hannes Domani March 20, 2024, 3:46 p.m. UTC | #6
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
  

Patch

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"