From: Joel Brobecker Date: Thu, 14 Dec 2017 05:05:24 +0000 (-0500) Subject: Ada: unable to compare strings (Attempt to compare array with non-array) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=79e8fcaafa92e7b4a74e510c5487e87c60ff1945;p=binutils-gdb.git 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. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index cca1af9fc58..d15b732b983 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2017-12-14 Joel Brobecker + + * ada-lang.c (ada_value_equal): Add handling of typedef types + when comparing array objects. + 2017-12-14 Joel Brobecker * ada-tasks.c (read_atcb): Properly set task_info->ptid diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 9e637ebab6b..44f219f986a 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -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); } diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 53c37800094..50f371b3273 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2017-12-14 Joel Brobecker + + * gdb.ada/str_binop_equal: New testcase. + 2017-12-14 Joel Brobecker * gdb.ada/task_switch_in_core: New testcase. diff --git a/gdb/testsuite/gdb.ada/str_binop_equal.exp b/gdb/testsuite/gdb.ada/str_binop_equal.exp new file mode 100644 index 00000000000..2481bacedac --- /dev/null +++ b/gdb/testsuite/gdb.ada/str_binop_equal.exp @@ -0,0 +1,39 @@ +# Copyright 2017 Free Software Foundation, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +load_lib "ada.exp" + +standard_ada_testfile foo_p211_061 + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug]] != "" } { + return -1 +} + +clean_restart ${testfile} + +set bp_location [gdb_get_line_number "STOP" ${testdir}/foo_p211_061.adb] +runto "foo_p211_061.adb:$bp_location" + +gdb_test "print my_str = my_str" \ + " = true" + +gdb_test "print my_str = \"ABCD\"" \ + " = true" + +gdb_test "print my_str = \"EFGH\"" \ + " = false" + +gdb_test "print my_str = \"AB\"" \ + " = false" diff --git a/gdb/testsuite/gdb.ada/str_binop_equal/foo_p211_061.adb b/gdb/testsuite/gdb.ada/str_binop_equal/foo_p211_061.adb new file mode 100644 index 00000000000..29d6ef893cf --- /dev/null +++ b/gdb/testsuite/gdb.ada/str_binop_equal/foo_p211_061.adb @@ -0,0 +1,22 @@ +-- Copyright 2017 Free Software Foundation, Inc. +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +with Pck; use Pck; + +procedure Foo_P211_061 is + My_Str : Str := "ABCD"; +begin + Do_Nothing (My_Str'Address); -- STOP +end Foo_P211_061; diff --git a/gdb/testsuite/gdb.ada/str_binop_equal/pck.adb b/gdb/testsuite/gdb.ada/str_binop_equal/pck.adb new file mode 100644 index 00000000000..15180757cca --- /dev/null +++ b/gdb/testsuite/gdb.ada/str_binop_equal/pck.adb @@ -0,0 +1,22 @@ +-- Copyright 2017 Free Software Foundation, Inc. +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +package body Pck is + procedure Do_Nothing (A : System.Address) is + begin + null; + end Do_Nothing; +end pck; + diff --git a/gdb/testsuite/gdb.ada/str_binop_equal/pck.ads b/gdb/testsuite/gdb.ada/str_binop_equal/pck.ads new file mode 100644 index 00000000000..8578b957965 --- /dev/null +++ b/gdb/testsuite/gdb.ada/str_binop_equal/pck.ads @@ -0,0 +1,20 @@ +-- Copyright 2017 Free Software Foundation, Inc. +-- +-- This program is free software; you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation; either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +with System; +package Pck is + type Str is new String (1 .. 4); + procedure Do_Nothing (A : System.Address); +end pck;