From patchwork Mon Jan 8 04:57:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 25264 Received: (qmail 101265 invoked by alias); 8 Jan 2018 04:57:57 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 100742 invoked by uid 89); 8 Jan 2018 04:57:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=believes X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 08 Jan 2018 04:57:54 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 4FF44117520; Sun, 7 Jan 2018 23:57:53 -0500 (EST) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id hS7QYt05h4M7; Sun, 7 Jan 2018 23:57:53 -0500 (EST) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 3E838117510; Sun, 7 Jan 2018 23:57:53 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4233) id 39B7C50B; Sun, 7 Jan 2018 23:57:53 -0500 (EST) From: Joel Brobecker To: gdb-patches@sourceware.org Cc: Xavier Roirand Subject: [pushed/Ada] Fix print of array using non-contiguous enumeration indexes Date: Sun, 7 Jan 2018 23:57:51 -0500 Message-Id: <1515387471-146491-1-git-send-email-brobecker@adacore.com> From: Xavier Roirand Hello, Consider the following code: type Index is (Index1, Index2); Size : constant Integer := 10; for Index use (Index1 => 1, Index2 => Size); type Array_Index_Enum is array (Index) of Integer; my_table : Array_Index_Enum :=(others => 42); When compiling the code above with a compiler where the GNAT encodings are turned off (which can be temporarily emulated by using the compiler switch -fgnat-encodings=minimal), printing this table in gdb leads to: (gdb) p my_table $1 = (42, 42, 4203344, 10, -8320, 32767, 4203465, 0, 0, 0) The displayed content is wrong since the handling part believes that the length of the array is max index value (10) minus the first index value (1) i+ 1 = 10 which is wrong since index are not contiguous in this case. The right behavior is to detect that the array is using enumeration index hence parse the enumeration values in order to get the number of indexes in this array (2 indexes here). This patch fixes this issue and changes the output as follow: (gdb) p my_table $1 = (42, 42) gdb/ChangeLog: * ada-valprint.c (val_print_packed_array_elements): Use proper number of elements when printing an array indexed by an enumeration type. gdb/testsuite/ChangeLog (Joel Brobecker ): * gdb.ada/arr_enum_idx_w_gap.exp * gdb.ada/arr_enum_idx_w_gap/foo_q418_043.adb Tested on x86_64-linux and pushed to master. Thank you, diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 16b0f43..af35cb5 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2018-01-08 Xavier Roirand + + * ada-valprint.c (val_print_packed_array_elements): Use + proper number of elements when printing an array indexed + by an enumeration type. + 2018-01-07 Simon Marchi * dwarf2read.c (struct dwarf2_cu) : Remove. diff --git a/gdb/ada-valprint.c b/gdb/ada-valprint.c index f5a2c3c..a486919 100644 --- a/gdb/ada-valprint.c +++ b/gdb/ada-valprint.c @@ -141,11 +141,45 @@ val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr, { LONGEST high; + struct type *base_index_type; if (get_discrete_bounds (index_type, &low, &high) < 0) len = 1; else len = high - low + 1; + + if (TYPE_CODE (index_type) == TYPE_CODE_RANGE) + base_index_type = TYPE_TARGET_TYPE (index_type); + else + base_index_type = index_type; + + if (TYPE_CODE (base_index_type) == TYPE_CODE_ENUM) + { + LONGEST low_pos, high_pos; + + /* Non-contiguous enumerations types can by used as index types + so the array length is computed from the positions of the + first and last literal in the enumeration type, and not from + the values of these literals. */ + + if (!discrete_position (base_index_type, low, &low_pos) + || !discrete_position (base_index_type, high, &high_pos)) + { + warning (_("unable to get positions in array, use bounds instead")); + low_pos = low; + high_pos = high; + } + + /* The array length should normally be HIGH_POS - LOW_POS + 1. + But in Ada we allow LOW_POS to be greater than HIGH_POS for + empty arrays. In that situation, the array length is just zero, + not negative! */ + + if (low_pos > high_pos) + len = 0; + else + len = high_pos - low_pos + 1; + } } i = 0; diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index e87099b..baa0104 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-01-08 Joel Brobecker + + * gdb.ada/arr_enum_idx_w_gap.exp + * gdb.ada/arr_enum_idx_w_gap/foo_q418_043.adb + 2018-01-05 Pedro Alves PR gdb/18653 diff --git a/gdb/testsuite/gdb.ada/arr_enum_idx_w_gap.exp b/gdb/testsuite/gdb.ada/arr_enum_idx_w_gap.exp new file mode 100644 index 0000000..ad706d4 --- /dev/null +++ b/gdb/testsuite/gdb.ada/arr_enum_idx_w_gap.exp @@ -0,0 +1,33 @@ +# Copyright 2018 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_q418_043 + +if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug ]] != "" } { + return -1 +} + +clean_restart ${testfile} + +set bp_location [gdb_get_line_number "BREAK" ${testdir}/foo_q418_043.adb] +if ![runto "foo_q418_043.adb:$bp_location" ] then { + perror "Couldn't run ${testfile}" + return +} + +gdb_test "print A" \ + " = \\(42, 42\\)" diff --git a/gdb/testsuite/gdb.ada/arr_enum_idx_w_gap/foo_q418_043.adb b/gdb/testsuite/gdb.ada/arr_enum_idx_w_gap/foo_q418_043.adb new file mode 100644 index 0000000..66ed5b4 --- /dev/null +++ b/gdb/testsuite/gdb.ada/arr_enum_idx_w_gap/foo_q418_043.adb @@ -0,0 +1,24 @@ +-- Copyright 2018 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 . + +procedure Foo_Q418_043 is + type Index is (Index1, Index2); + Size : constant Integer := 10; + for Index use (Index1 => 1, Index2 => Size); + type Array_Index_Enum is array (Index) of Integer; + A : Array_Index_Enum :=(others => 42); +begin + A(Index2) := 4242; --BREAK +end Foo_Q418_043;