Patchwork [pushed] (Ada) Fix Length attribute on array access

login
register
mail settings
Submitter Joel Brobecker
Date Jan. 5, 2018, 8:16 a.m.
Message ID <1515140162-9232-1-git-send-email-brobecker@adacore.com>
Download mbox | patch
Permalink /patch/25223/
State New
Headers show

Comments

Joel Brobecker - Jan. 5, 2018, 8:16 a.m.
From: Jerome Guitton <guitton@adacore.com>

Hello,

Consider the following variable "Indexed_By_Enum", declared as
an access to an array whose index type is an enumerated type
whose underlying values have "gaps":

   type Enum_With_Gaps is (LIT0, LIT1, LIT2, LIT3, LIT4);
   for Enum_With_Gaps use (LIT0 => 3,
                           LIT1 => 5,
                           LIT2 => 8,
                           LIT3 => 13,
                           LIT4 => 21);
   for Enum_With_Gaps'size use 16;

   type MyWord is range 0 .. 16#FFFF# ;
   for MyWord'Size use 16;

   type AR is array (Enum_With_Gaps range <>) of MyWord;
   type AR_Access is access AR;

   Indexed_By_Enum : AR_Access :=
     new AR'(LIT1 => 1,  LIT2 => 43, LIT3 => 42, LIT4 => 41);

Trying to print the length (number of elements) of this array using
the 'Length attribute does not work:

    (gdb) print indexed_by_enum'length
    'POS only defined on discrete types

The problem occurs while trying to get the array's index type.
It was using TYPE_INDEX_TYPE for that. It does not work for Ada arrays
in general; use ada_index_type instead.

gdb/ChangeLog:

	* ada-lang.c (ada_array_length): Use ada_index_type instead of
	TYPE_INDEX_TYPE.

gdb/testsuite/ChangeLog:

        * gdb.ada/arr_acc_idx_w_gap: New testcase.

Tested on x86_64-linux. Pushed to master.

Thank you,

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 3efb697..95d7b5a 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@ 
+2018-01-05  Jerome Guitton  <guitton@adacore.com>
+
+	* ada-lang.c (ada_array_length): Use ada_index_type instead of
+	TYPE_INDEX_TYPE.
+
 2018-01-05  Joel Brobecker  <brobecker@adacore.com>
 
 	* ada-lang.c (ada_to_fixed_value_create): Add handling of
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 85e50cc..846cf8c 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -3179,7 +3179,7 @@  ada_array_length (struct value *arr, int n)
     }
 
   arr_type = check_typedef (arr_type);
-  index_type = TYPE_INDEX_TYPE (arr_type);
+  index_type = ada_index_type (arr_type, n, "length");
   if (index_type != NULL)
     {
       struct type *base_type;
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index ecc7110..c79dffc 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@ 
+2018-01-05  Jerome Guitton  <guitton@adacore.com>
+
+	* gdb.ada/arr_acc_idx_w_gap: New testcase.
+
 2018-01-05  Joel Brobecker  <brobecker@adacore.com>
 
 	* gdb.ada/convvar_comp: New testcase.
diff --git a/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap.exp b/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap.exp
new file mode 100644
index 0000000..a95f4fa
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap.exp
@@ -0,0 +1,55 @@ 
+# 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 <http://www.gnu.org/licenses/>.
+
+load_lib "ada.exp"
+
+standard_ada_testfile enum_with_gap_main
+
+if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug ]] != "" } {
+  return -1
+}
+
+clean_restart ${testfile}
+
+set bp_location [gdb_get_line_number "BREAK" ${testdir}/enum_with_gap_main.adb]
+if ![runto "enum_with_gap_main.adb:$bp_location" ] then {
+  perror "Couldn't run ${testfile}"
+  return
+}
+
+gdb_test "print indexed_by_enum.all" \
+         " = \\(lit1 => 1, 43, 42, 41\\)"
+gdb_test "print s.all" \
+         " = \"Hello!\""
+
+gdb_test "print indexed_by_enum'length" \
+         " = 4"
+gdb_test "print s'length" \
+         " = 6"
+
+gdb_test "print indexed_by_enum'first" \
+         " = lit1"
+gdb_test "print s'first" \
+         " = 1"
+
+gdb_test "print indexed_by_enum'last" \
+         " = lit4"
+gdb_test "print s'last" \
+         " = 6"
+
+gdb_test "print indexed_by_enum(lit2..lit4)" \
+         " = \\(lit2 => 43, 42, 41\\)"
+gdb_test "print s(2..4)" \
+         " = \"ell\""
diff --git a/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap.adb b/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap.adb
new file mode 100644
index 0000000..f03b995
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap.adb
@@ -0,0 +1,28 @@ 
+--  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 <http://www.gnu.org/licenses/>.
+
+package body Enum_With_Gap is
+
+   procedure Do_Nothing (E : AR_Access) is
+   begin
+      null;
+   end Do_Nothing;
+
+   procedure Do_Nothing (E : String_Access) is
+   begin
+      null;
+   end Do_Nothing;
+
+end Enum_With_Gap;
diff --git a/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap.ads b/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap.ads
new file mode 100644
index 0000000..3c67e6c
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap.ads
@@ -0,0 +1,48 @@ 
+--  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 <http://www.gnu.org/licenses/>.
+
+package Enum_With_Gap is
+
+   type Enum_With_Gaps is
+     (
+      LIT0,
+      LIT1,
+      LIT2,
+      LIT3,
+      LIT4
+     );
+
+   for Enum_With_Gaps use
+     (
+      LIT0 => 3,
+      LIT1 => 5,
+      LIT2 => 8,
+      LIT3 => 13,
+      LIT4 => 21
+     );
+   for Enum_With_Gaps'size use 16;
+
+   type MyWord is range 0 .. 16#FFFF# ;
+   for MyWord'Size use 16;
+
+   type AR is array (Enum_With_Gaps range <>) of MyWord;
+   type AR_Access is access AR;
+   
+   type String_Access is access String;
+   
+   procedure Do_Nothing (E : AR_Access);
+   procedure Do_Nothing (E : String_Access);
+
+end Enum_With_Gap;
diff --git a/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap_main.adb b/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap_main.adb
new file mode 100644
index 0000000..f954a28
--- /dev/null
+++ b/gdb/testsuite/gdb.ada/arr_acc_idx_w_gap/enum_with_gap_main.adb
@@ -0,0 +1,25 @@ 
+--  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 <http://www.gnu.org/licenses/>.
+
+with Enum_With_Gap; use Enum_With_Gap;
+
+procedure Enum_With_Gap_Main is
+   Indexed_By_Enum : AR_Access :=
+     new AR'(LIT1 => 1,  LIT2 => 43, LIT3 => 42, LIT4 => 41);
+   S : String_Access := new String'("Hello!");
+begin
+   Do_Nothing (Indexed_By_Enum); --  BREAK
+   Do_Nothing (S);
+end Enum_With_Gap_Main;