[Ada] 'first/'last/'length of array whose bound is a discriminant
authorJoel Brobecker <brobecker@adacore.com>
Thu, 15 Jan 2015 06:09:32 +0000 (10:09 +0400)
committerJoel Brobecker <brobecker@adacore.com>
Thu, 15 Jan 2015 08:53:33 +0000 (12:53 +0400)
Consider the following code:

   type Table is array (Positive range <>) of Integer;
   type Object (N : Integer) is record
       Data : Table (1 .. N);
   end record;
   My_Object : Object := (N => 3, Data => (3, 5, 8));

Trying to print the range and length of the My_Object.Data array yields:

    (gdb) print my_object.data'first
    $1 = 1
    (gdb) print my_object.data'last
    $2 = 0
    (gdb) print my_object.data'length
    $3 = 0

The first one is correct, and that is thanks to the fact that
the lower bound is statically known.  However, for the upper
bound, and consequently the array's length, the values are incorrect.
It should be:

    (gdb) print my_object.data'last
    $2 = 3
    (gdb) print my_object.data'length
    $3 = 3

What happens here is that ada_array_bound_from_type sees that
our array has a parallel "___XA" type, and therefore tries to
use it.  In particular, it described our array's index type as:
[...]___XDLU_1__n, which means lower bound = 1, and upper bound
is value of "n". Unfortunately, ada_array_bound_from_type does
not have access to the discriminant, and is therefore unable to
compute the bound correctly.

Fortunately, at this stage, the bound has already been computed
a while ago, and therefore doesn't need to be re-computed here.
This patch fixes the issue by ignoring that ___XA type if the array
is marked as already fixed.

This also fixes the same issue with packed arrays.

gdb/ChangeLog:

        * ada-lang.c (ada_array_bound_from_type): Ignore array's parallel
        ___XA type if the array has already been fixed.

gdb/testsuite/ChangeLog:

        * gdb.ada/var_arr_attrs: New testcase.

gdb/ChangeLog
gdb/ada-lang.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.ada/var_arr_attrs.exp [new file with mode: 0644]
gdb/testsuite/gdb.ada/var_arr_attrs/foo_o115_002.adb [new file with mode: 0644]
gdb/testsuite/gdb.ada/var_arr_attrs/pck.adb [new file with mode: 0644]
gdb/testsuite/gdb.ada/var_arr_attrs/pck.ads [new file with mode: 0644]

index 4557c3ebfde12d2c204b02e0bb5404d62e6a5e9d..cddae3c3b2e1379bec112ee8983d8d8d4c3d8247 100644 (file)
@@ -1,3 +1,8 @@
+2015-01-15  Joel Brobecker  <brobecker@adacore.com>
+
+       * ada-lang.c (ada_array_bound_from_type): Ignore array's parallel
+       ___XA type if the array has already been fixed.
+
 2015-01-14  Yao Qi  <yao@codesourcery.com>
 
        * Makefile.in (ppc-linux.o): New rule.
index ec066933b5321ca377160f3c092686759e635bde..f5753f13fcb7613fb1f1c739e73ec99116714895 100644 (file)
@@ -2928,8 +2928,19 @@ ada_array_bound_from_type (struct type *arr_type, int n, int which)
   else
     type = arr_type;
 
-  index_type_desc = ada_find_parallel_type (type, "___XA");
-  ada_fixup_array_indexes_type (index_type_desc);
+  if (TYPE_FIXED_INSTANCE (type))
+    {
+      /* The array has already been fixed, so we do not need to
+        check the parallel ___XA type again.  That encoding has
+        already been applied, so ignore it now.  */
+      index_type_desc = NULL;
+    }
+  else
+    {
+      index_type_desc = ada_find_parallel_type (type, "___XA");
+      ada_fixup_array_indexes_type (index_type_desc);
+    }
+
   if (index_type_desc != NULL)
     index_type = to_fixed_range_type (TYPE_FIELD_TYPE (index_type_desc, n - 1),
                                      NULL);
index 2f747b5d721d2959eee71f38b35cf295a4657bbd..a71ee988eab5ac1a45c9e1e04a4cb73d8903479f 100644 (file)
@@ -1,3 +1,7 @@
+2015-01-15  Joel Brobecker  <brobecker@adacore.com>
+
+       * gdb.ada/var_arr_attrs: New testcase.
+
 2015-01-14  Pedro Alves  <palves@redhat.com>
            Joel Brobecker  <brobecker@adacore.com>
 
diff --git a/gdb/testsuite/gdb.ada/var_arr_attrs.exp b/gdb/testsuite/gdb.ada/var_arr_attrs.exp
new file mode 100644 (file)
index 0000000..34caeac
--- /dev/null
@@ -0,0 +1,39 @@
+# Copyright 2015 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 foo_o115_002
+
+if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug]] != "" } {
+  return -1
+}
+
+clean_restart ${testfile}
+
+set bp_location [gdb_get_line_number "STOP" ${testdir}/foo_o115_002.adb]
+runto "foo_o115_002.adb:$bp_location"
+
+gdb_test "print my_object.data'first" " = 1"
+
+gdb_test "print my_object.data'last" " = 3"
+
+gdb_test "print my_object.data'length" " = 3"
+
+gdb_test "print my_small_object.data'first" " = 1"
+
+gdb_test "print my_small_object.data'last" " = 3"
+
+gdb_test "print my_small_object.data'length" " = 3"
diff --git a/gdb/testsuite/gdb.ada/var_arr_attrs/foo_o115_002.adb b/gdb/testsuite/gdb.ada/var_arr_attrs/foo_o115_002.adb
new file mode 100644 (file)
index 0000000..1b5909e
--- /dev/null
@@ -0,0 +1,25 @@
+--  Copyright 2015 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 Pck; use Pck;
+
+procedure Foo_O115_002 is
+   My_Object : Object := (N => 3, Data => (3, 5, 8));
+   My_Small_Object : Small_Object := (N => 3, Data => (3, 5, 8));
+begin
+   Do_Nothing (My_Object'Address);             -- STOP
+   Do_Nothing (My_Small_Object'Address);
+end Foo_O115_002;
+
diff --git a/gdb/testsuite/gdb.ada/var_arr_attrs/pck.adb b/gdb/testsuite/gdb.ada/var_arr_attrs/pck.adb
new file mode 100644 (file)
index 0000000..3f490ee
--- /dev/null
@@ -0,0 +1,21 @@
+--  Copyright 2015 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 Pck is
+   procedure Do_Nothing (A : System.Address) is
+   begin
+      null;
+   end Do_Nothing;
+end Pck;
diff --git a/gdb/testsuite/gdb.ada/var_arr_attrs/pck.ads b/gdb/testsuite/gdb.ada/var_arr_attrs/pck.ads
new file mode 100644 (file)
index 0000000..79af546
--- /dev/null
@@ -0,0 +1,36 @@
+--  Copyright 2015 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 System;
+package Pck is
+
+   type Table is array (Positive range <>) of Integer;
+
+   type Object (N : Integer) is record
+       Data : Table (1 .. N);
+   end record;
+
+   type Small is new Integer range 0 .. 255;
+   for Small'Size use 8;
+
+   type Small_Table is array (Positive range <>) of Small;
+   pragma Pack (Small_Table);
+
+   type Small_Object (N : Integer) is record
+       Data : Table (1 .. N);
+   end record;
+
+   procedure Do_Nothing (A : System.Address);
+end Pck;
This page took 0.041718 seconds and 4 git commands to generate.