Ada: unable to compare strings (Attempt to compare array with non-array)

Consider the following Ada Code:

   type Str is new String (1 .. 4);
   My_str : Str := "ABCD";

This simply declares a 4-character string type. Trying to perform
equality tests using it currently yield an error:

    (gdb) p my_str = my_str
    Attempt to compare array with non-array
    (gdb) p my_str = "ABCD"
    Attempt to compare array with non-array

The error occurs because my_str is defined as an object whose
type is a typdef to a TYPE_CODE_ARRAY, which ada_value_equal
is not expecting at all (yet). This patch fixes this oversight.

gdb/ChangeLog:

        * ada-lang.c (ada_value_equal): Add handling of typedef types
        when comparing array objects.

gdb/testsuite/ChangeLog:

        * gdb.ada/str_binop_equal: New testcase.

Tested on x86_64-linux.
This commit is contained in:
Joel Brobecker
2017-12-14 00:05:24 -05:00
parent e05fa6f9df
commit 79e8fcaafa
7 changed files with 124 additions and 7 deletions

View File

@@ -9729,23 +9729,28 @@ ada_value_equal (struct value *arg1, struct value *arg2)
if (ada_is_direct_array_type (value_type (arg1))
|| ada_is_direct_array_type (value_type (arg2)))
{
struct type *arg1_type, *arg2_type;
/* Automatically dereference any array reference before
we attempt to perform the comparison. */
arg1 = ada_coerce_ref (arg1);
arg2 = ada_coerce_ref (arg2);
arg1 = ada_coerce_to_simple_array (arg1);
arg2 = ada_coerce_to_simple_array (arg2);
if (TYPE_CODE (value_type (arg1)) != TYPE_CODE_ARRAY
|| TYPE_CODE (value_type (arg2)) != TYPE_CODE_ARRAY)
arg1_type = ada_check_typedef (value_type (arg1));
arg2_type = ada_check_typedef (value_type (arg2));
if (TYPE_CODE (arg1_type) != TYPE_CODE_ARRAY
|| TYPE_CODE (arg2_type) != TYPE_CODE_ARRAY)
error (_("Attempt to compare array with non-array"));
/* FIXME: The following works only for types whose
representations use all bits (no padding or undefined bits)
and do not have user-defined equality. */
return
TYPE_LENGTH (value_type (arg1)) == TYPE_LENGTH (value_type (arg2))
&& memcmp (value_contents (arg1), value_contents (arg2),
TYPE_LENGTH (value_type (arg1))) == 0;
return (TYPE_LENGTH (arg1_type) == TYPE_LENGTH (arg2_type)
&& memcmp (value_contents (arg1), value_contents (arg2),
TYPE_LENGTH (arg1_type)) == 0);
}
return value_equal (arg1, arg2);
}