Skip to content

Commit bff8c71

Browse files
committed
Fix Ada "ptype" bug with array types
Using ptype on an array type in Ada can sometimes show an incorrect high bound. This happens because ada_evaluate_subexp will create an array with an incorrect upper bound in the EVAL_AVOID_SIDE_EFFECTS case. This patch fixes the problem by arranging to always create such an array with valid bounds. Tested on x86-64 Fedora 29. gdb/ChangeLog 2019-03-18 Tom Tromey <tromey@adacore.com> * ada-lang.c (empty_array): Add "high" parameter. (ada_evaluate_subexp): Update. gdb/testsuite/ChangeLog 2019-03-18 Joel Brobecker <brobecker@adacore.com> Tom Tromey <tromey@adacore.com> * gdb.ada/ptype_array/pck.adb: New file. * gdb.ada/ptype_array/pck.ads: New file. * gdb.ada/ptype_array/foo.adb: New file. * gdb.ada/ptype_array.exp: New file.
1 parent af60449 commit bff8c71

File tree

7 files changed

+131
-7
lines changed

7 files changed

+131
-7
lines changed

Diff for: gdb/ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2019-03-18 Tom Tromey <tromey@adacore.com>
2+
3+
* ada-lang.c (empty_array): Add "high" parameter.
4+
(ada_evaluate_subexp): Update.
5+
16
2019-03-17 Sergei Trofimovich <siarheit@google.com>
27

38
* unittests/string_view-selftests.c: Define

Diff for: gdb/ada-lang.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -3173,16 +3173,18 @@ ada_array_length (struct value *arr, int n)
31733173
return high - low + 1;
31743174
}
31753175

3176-
/* An empty array whose type is that of ARR_TYPE (an array type),
3177-
with bounds LOW to LOW-1. */
3176+
/* An array whose type is that of ARR_TYPE (an array type), with
3177+
bounds LOW to HIGH, but whose contents are unimportant. If HIGH is
3178+
less than LOW, then LOW-1 is used. */
31783179

31793180
static struct value *
3180-
empty_array (struct type *arr_type, int low)
3181+
empty_array (struct type *arr_type, int low, int high)
31813182
{
31823183
struct type *arr_type0 = ada_check_typedef (arr_type);
31833184
struct type *index_type
31843185
= create_static_range_type
3185-
(NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (arr_type0)), low, low - 1);
3186+
(NULL, TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (arr_type0)), low,
3187+
high < low ? low - 1 : high);
31863188
struct type *elt_type = ada_array_element_type (arr_type0, 1);
31873189

31883190
return allocate_value (create_array_type (NULL, elt_type, index_type));
@@ -11033,7 +11035,8 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
1103311035
if (noside == EVAL_AVOID_SIDE_EFFECTS
1103411036
&& ada_is_array_descriptor_type (ada_check_typedef
1103511037
(value_type (array))))
11036-
return empty_array (ada_type_of_array (array, 0), low_bound);
11038+
return empty_array (ada_type_of_array (array, 0), low_bound,
11039+
high_bound);
1103711040

1103811041
array = ada_coerce_to_simple_array_ptr (array);
1103911042

@@ -11057,7 +11060,7 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
1105711060
struct type *type0 = ada_check_typedef (value_type (array));
1105811061

1105911062
if (high_bound < low_bound || noside == EVAL_AVOID_SIDE_EFFECTS)
11060-
return empty_array (TYPE_TARGET_TYPE (type0), low_bound);
11063+
return empty_array (TYPE_TARGET_TYPE (type0), low_bound, high_bound);
1106111064
else
1106211065
{
1106311066
struct type *arr_type0 =
@@ -11071,7 +11074,7 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
1107111074
else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1107211075
return array;
1107311076
else if (high_bound < low_bound)
11074-
return empty_array (value_type (array), low_bound);
11077+
return empty_array (value_type (array), low_bound, high_bound);
1107511078
else
1107611079
return ada_value_slice (array, longest_to_int (low_bound),
1107711080
longest_to_int (high_bound));

Diff for: gdb/testsuite/ChangeLog

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2019-03-18 Joel Brobecker <brobecker@adacore.com>
2+
Tom Tromey <tromey@adacore.com>
3+
4+
* gdb.ada/ptype_array/pck.adb: New file.
5+
* gdb.ada/ptype_array/pck.ads: New file.
6+
* gdb.ada/ptype_array/foo.adb: New file.
7+
* gdb.ada/ptype_array.exp: New file.
8+
19
2019-03-14 Tom Tromey <tromey@adacore.com>
210

311
* gdb.base/style.exp: Add "set style sources" test.

Diff for: gdb/testsuite/gdb.ada/ptype_array.exp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2019 Free Software Foundation, Inc.
2+
#
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
load_lib "ada.exp"
17+
18+
standard_ada_testfile foo
19+
20+
if {[gdb_compile_ada "${srcfile}" "${binfile}" executable [list debug additional_flags=-gnat05 ]] != "" } {
21+
return -1
22+
}
23+
24+
clean_restart ${testfile}
25+
26+
runto_main
27+
28+
gdb_test "ptype pck.W.G(1,5).m(2 .. 5)" \
29+
"type = array \\(2 \\.\\. 5\\) of character" \
30+
"ptype 2..5"
31+
32+
gdb_test "ptype pck.W.G(1,5).m(3 .. 5)" \
33+
"type = array \\(3 \\.\\. 5\\) of character" \
34+
"ptype 3..5"

Diff for: gdb/testsuite/gdb.ada/ptype_array/foo.adb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- Copyright 2019 Free Software Foundation, Inc.
2+
--
3+
-- This program is free software; you can redistribute it and/or modify
4+
-- it under the terms of the GNU General Public License as published by
5+
-- the Free Software Foundation; either version 3 of the License, or
6+
-- (at your option) any later version.
7+
--
8+
-- This program is distributed in the hope that it will be useful,
9+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
-- GNU General Public License for more details.
12+
--
13+
-- You should have received a copy of the GNU General Public License
14+
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
with Pck;
17+
18+
procedure Foo is
19+
begin
20+
Pck.Do_Nothing (Pck.W.G'Address);
21+
end Foo;

Diff for: gdb/testsuite/gdb.ada/ptype_array/pck.adb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Copyright 2019 Free Software Foundation, Inc.
2+
--
3+
-- This program is free software; you can redistribute it and/or modify
4+
-- it under the terms of the GNU General Public License as published by
5+
-- the Free Software Foundation; either version 3 of the License, or
6+
-- (at your option) any later version.
7+
--
8+
-- This program is distributed in the hope that it will be useful,
9+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
-- GNU General Public License for more details.
12+
--
13+
-- You should have received a copy of the GNU General Public License
14+
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
package body Pck is
17+
procedure Do_Nothing (A : System.Address) is
18+
begin
19+
null;
20+
end Do_Nothing;
21+
end Pck;
22+
23+

Diff for: gdb/testsuite/gdb.ada/ptype_array/pck.ads

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-- Copyright 2019 Free Software Foundation, Inc.
2+
--
3+
-- This program is free software; you can redistribute it and/or modify
4+
-- it under the terms of the GNU General Public License as published by
5+
-- the Free Software Foundation; either version 3 of the License, or
6+
-- (at your option) any later version.
7+
--
8+
-- This program is distributed in the hope that it will be useful,
9+
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
-- GNU General Public License for more details.
12+
--
13+
-- You should have received a copy of the GNU General Public License
14+
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
with System;
17+
18+
package Pck is
19+
package W is
20+
type P is record
21+
M : String (2 .. 5);
22+
end record;
23+
24+
type R is array (1 .. 10, 1 .. 20) of P;
25+
26+
G : R;
27+
end W;
28+
29+
procedure Do_Nothing (A : System.Address);
30+
end Pck;

0 commit comments

Comments
 (0)