From patchwork Thu Dec 14 04:53:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Brobecker X-Patchwork-Id: 24927 Received: (qmail 42356 invoked by alias); 14 Dec 2017 04:53:40 -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 42314 invoked by uid 89); 14 Dec 2017 04:53:40 -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, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= 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; Thu, 14 Dec 2017 04:53:39 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id C0866116E6D; Wed, 13 Dec 2017 23:53:37 -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 XrV4Tx872PC7; Wed, 13 Dec 2017 23:53:37 -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 B1777116E6B; Wed, 13 Dec 2017 23:53:37 -0500 (EST) Received: by tron.gnat.com (Postfix, from userid 4233) id B04D74A4; Wed, 13 Dec 2017 23:53:37 -0500 (EST) From: Joel Brobecker To: gdb-patches@sourceware.org Cc: Xavier Roirand Subject: [commit/Ada 2/2] slices of arrays with dynamic strides Date: Wed, 13 Dec 2017 23:53:33 -0500 Message-Id: <1513227213-37871-2-git-send-email-brobecker@adacore.com> In-Reply-To: <1513227213-37871-1-git-send-email-brobecker@adacore.com> References: <1513227213-37871-1-git-send-email-brobecker@adacore.com> Hello, This is a patch which fixes an isses with array slices in Ada when the array has a dynamic stride. It depends on the patch adding support for DW_AT_byte_stride, so I will push after the first one gets approved. Consider the following Ada code: procedure Nested (L, U : Integer) is subtype Small_Type is Integer range L .. U; type Record_Type (I : Small_Type := L) is record S : String (1 .. I); end record; type Array_Type is array (Integer range <>) of Record_Type; A1 : Array_Type := (1 => (I => 0, S => <>), 2 => (I => 1, S => "A"), 3 => (I => 2, S => "AB")); procedure Discard (R : Record_Type) is begin null; end Discard; begin Discard (A1 (1)); -- STOP end; Trying to print a slice of that array currently yields: (gdb) p a1(1..3) $1 = ((i => 0, s => ""), (i => 0, s => ""), (i => 0, s => "")) We expected instead: (gdb) p a1(1..3) $1 = ((i => 0, s => ""), (i => 1, s => "A"), (i => 2, s => "AB")) This is because the functions we use in ada-lang.c to create the type of the array slice (ada_value_slice and ada_value_slice_from_ptr) was not taking into account the stride of the array. This patch fixes this. gdb/ChangeLog: * ada-lang.c (ada_value_slice_from_ptr): Take array stride into account when creating the array type of the slice. (ada_value_slice): Likewise. gdb/testsuite/ChangeLog: * gdb.ada/dyn_stride.exp: Add slice test. Note that, with the current use of ada_value_slice, the enhancement to handle dynamic array strides seems unnecessary, because I do not see how an array with a dynamic stride can be referenced by either by reference or pointer. Since references are coerced to array pointers, in both cases, the slice is performed by ada_value_slice_from_ptr. But ada_value_slice is enhanced nonetheless, in the spirit of making the code more robust, in case we missed something, and also as similar as possible with its from_ptr counterpart. tested on x86_64-linux. Thanks, diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 9e637eb..c3b1cd3 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -2910,8 +2910,10 @@ ada_value_slice_from_ptr (struct value *array_ptr, struct type *type, struct type *base_index_type = TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type0)); struct type *index_type = create_static_range_type (NULL, base_index_type, low, high); - struct type *slice_type = - create_array_type (NULL, TYPE_TARGET_TYPE (type0), index_type); + struct type *slice_type = create_array_type_with_stride + (NULL, TYPE_TARGET_TYPE (type0), index_type, + get_dyn_prop (DYN_PROP_BYTE_STRIDE, type0), + TYPE_FIELD_BITSIZE (type0, 0)); int base_low = ada_discrete_type_low_bound (TYPE_INDEX_TYPE (type0)); LONGEST base_low_pos, low_pos; CORE_ADDR base; @@ -2938,8 +2940,10 @@ ada_value_slice (struct value *array, int low, int high) struct type *base_index_type = TYPE_TARGET_TYPE (TYPE_INDEX_TYPE (type)); struct type *index_type = create_static_range_type (NULL, TYPE_INDEX_TYPE (type), low, high); - struct type *slice_type = - create_array_type (NULL, TYPE_TARGET_TYPE (type), index_type); + struct type *slice_type = create_array_type_with_stride + (NULL, TYPE_TARGET_TYPE (type), index_type, + get_dyn_prop (DYN_PROP_BYTE_STRIDE, type), + TYPE_FIELD_BITSIZE (type, 0)); LONGEST low_pos, high_pos; if (!discrete_position (base_index_type, low, &low_pos) diff --git a/gdb/testsuite/gdb.ada/dyn_stride.exp b/gdb/testsuite/gdb.ada/dyn_stride.exp index 4dd1177..19be9cb 100644 --- a/gdb/testsuite/gdb.ada/dyn_stride.exp +++ b/gdb/testsuite/gdb.ada/dyn_stride.exp @@ -36,3 +36,6 @@ gdb_test "print A1(2)" \ gdb_test "print A1(3)" \ "\\(i => 2, s => \"AB\"\\)" + +gdb_test "print A1(1..3)" \ + "\\(\\(i => 0, s => \"\"\\), \\(i => 1, s => \"A\"\\), \\(i => 2, s => \"AB\"\\)\\)"