(Ada) Fix Length attribute on array access

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.
This commit is contained in:
Jerome Guitton 2018-01-05 03:03:59 -05:00 committed by Joel Brobecker
parent cc0e770c0d
commit 7150d33cda
7 changed files with 166 additions and 1 deletions

View File

@ -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

View File

@ -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;

View File

@ -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.

View File

@ -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\""

View File

@ -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;

View File

@ -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;

View File

@ -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;