From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) by sourceware.org (Postfix) with ESMTPS id 7CAFC3990C07; Fri, 16 Jul 2021 03:46:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7CAFC3990C07 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com IronPort-SDR: Ns738VF7ft0Wfmk7ZuJXDoXPZ/DO7ZgZhmdqDlXCSB8tEuRxzB9Ci23sZX6C7yh/u34+14zEbf SJ0YzjU9/8/cROQum/qYglx1nQRYzcLV+rkwna6O/NyVTBUHiw80fD0sk0yR/+0477yeEbdz7y qMO7BGUWM1qyHc8XOqPaJxmmi/XWKdn/m8xR6Iq3YksuUaV5ghR4tY5QCT9TK0HFMEy7dtyf2r pYnXhRsOMOaTsuz11N5IiuEcX3t21XqB3EDj0n/KjfU7mQgNuWSfQuRRCZx88RvyzGFaEQJE4e rsb0+qpr5qQwS/KFD9A4jnBL X-IronPort-AV: E=Sophos;i="5.84,244,1620720000"; d="scan'208";a="63587240" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa2.mentor.iphmx.com with ESMTP; 15 Jul 2021 19:46:42 -0800 IronPort-SDR: KPn6zgjOd7UfDS60/X209NluXYpxNsP2rXA5WLtFkM+c9OnffcGKNbfH2RfYg3jGCL1uAkLkmP rm4TFUJIu9PCd8Zu11wCrcDvxBGksCNPbzIdPoTd9ngd7111l7+1QgSs/liOGSrcD/uRAw3Ztj vSFvi0SXCtmX//2qkvTzo0p5YXisiWVx30lR20PzkQk73c+8xAuHJKvoMUEnmjvSbPxzCofYFI U6KdX6nWsEUq5oWi0zlb/xn0Wqff53Ym29h+3Cs+2F4tyPG2Tivdn/Iwep0V+6wydbfSKdtIF1 c+c= To: "gcc-patches@gcc.gnu.org" , "fortran@gcc.gnu.org" From: Sandra Loosemore Subject: [PATCH, Fortran] Bind(c): CFI_signed_char is not a Fortran character type Message-ID: <13168f92-8863-cb63-9470-a6055d5da5f6@codesourcery.com> Date: Thu, 15 Jul 2021 21:46:38 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------FA7662D172897ED3B35ED357" Content-Language: en-US X-ClientProxiedBy: svr-orw-mbx-04.mgc.mentorg.com (147.34.90.204) To svr-orw-mbx-03.mgc.mentorg.com (147.34.90.203) X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Jul 2021 03:46:46 -0000 --------------FA7662D172897ED3B35ED357 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit When I was reading code in conjunction with fixing PR101317, I noticed an unrelated bug in the implementation of CFI_allocate and CFI_select_part: they were mis-handling the CFI_signed_char type as if it were a Fortran character type for the purposes of deciding whether to use the elem_len argument to those functions. It's really an integer type that has the size of signed char. I checked similar code in other functions in ISO_Fortran_binding.c and these were the only two that were incorrect. The part of the patch to add tests for this goes on top of my base TS29113 testsuite patch, which hasn't been reviewed or committed yet. I can refactor that into the same commit as the rest of the testsuite, assuming everything eventually gets approved. https://gcc.gnu.org/pipermail/gcc-patches/2021-July/574576.html -Sandra --------------FA7662D172897ED3B35ED357 Content-Type: text/x-patch; charset="UTF-8"; name="signed-char.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="signed-char.patch" commit 45190d9eb5123df77bd60a1d6712f05a3af5f42c Author: Sandra Loosemore Date: Thu Jul 15 16:51:55 2021 -0700 Bind(c): signed char is not a Fortran character type CFI_allocate and CFI_select_part were incorrectly treating CFI_type_signed_char as a Fortran character type for the purpose of deciding whether or not to use the elem_len argument. It is a Fortran integer type per table 18.2 in the 2018 Fortran standard. Other functions in ISO_Fortran_binding.c appeared to handle this case correctly already. 2021-07-15 Sandra Loosemore gcc/testsuite/ * gfortran.dg/ts29113/library/allocate-c.c (ctest): Also test handling of elem_len for CFI_type_char vs CFI_type_signed_char. * gfortran.dg/ts29113/library/select-c.c (ctest): Likewise. libgfortran/ * runtime/ISO_Fortran_binding.c (CFI_allocate) diff --git a/gcc/testsuite/gfortran.dg/ts29113/library/allocate-c.c b/gcc/testsuite/gfortran.dg/ts29113/library/allocate-c.c index 0208e5a..6343d28 100644 --- a/gcc/testsuite/gfortran.dg/ts29113/library/allocate-c.c +++ b/gcc/testsuite/gfortran.dg/ts29113/library/allocate-c.c @@ -135,5 +135,34 @@ ctest (void) CFI_deallocate (dv)); if (dv->base_addr != NULL) abort (); + + /* Signed char is not a Fortran character type. Here we expect it to + ignore the elem_len argument and use the size of the type. */ + ex[0] = 3; + ex[1] = 4; + ex[2] = 5; + check_CFI_status ("CFI_establish", + CFI_establish (dv, NULL, CFI_attribute_allocatable, + CFI_type_signed_char, 4, 3, ex)); + lb[0] = 1; + lb[1] = 2; + lb[2] = 3; + ub[0] = 10; + ub[1] = 5; + ub[2] = 10; + sm = sizeof (double); + check_CFI_status ("CFI_allocate", + CFI_allocate (dv, lb, ub, sm)); + dump_CFI_cdesc_t (dv); + if (dv->base_addr == NULL) + abort (); + if (dv->elem_len != sizeof (signed char)) + abort (); + + check_CFI_status ("CFI_deallocate", + CFI_deallocate (dv)); + if (dv->base_addr != NULL) + abort (); + } diff --git a/gcc/testsuite/gfortran.dg/ts29113/library/select-c.c b/gcc/testsuite/gfortran.dg/ts29113/library/select-c.c index df6172c..9bcbc01 100644 --- a/gcc/testsuite/gfortran.dg/ts29113/library/select-c.c +++ b/gcc/testsuite/gfortran.dg/ts29113/library/select-c.c @@ -8,6 +8,8 @@ /* Declare some source arrays. */ struct ss { + char c[4]; + signed char b[4]; int i, j, k; } s[10][5][3]; @@ -61,6 +63,31 @@ ctest (void) if (result->dim[2].sm != source->dim[2].sm) abort (); + /* Check that we use the given elem_size for char but not for + signed char, which is considered an integer type instead of a Fortran + character type. */ + check_CFI_status ("CFI_establish", + CFI_establish (result, NULL, CFI_attribute_pointer, + CFI_type_char, 4, 3, NULL)); + if (result->elem_len != 4) + abort (); + offset = offsetof (struct ss, c); + check_CFI_status ("CFI_select_part", + CFI_select_part (result, source, offset, 4)); + if (result->elem_len != 4) + abort (); + + check_CFI_status ("CFI_establish", + CFI_establish (result, NULL, CFI_attribute_pointer, + CFI_type_signed_char, 4, 3, NULL)); + if (result->elem_len != sizeof (signed char)) + abort (); + offset = offsetof (struct ss, c); + check_CFI_status ("CFI_select_part", + CFI_select_part (result, source, offset, 4)); + if (result->elem_len != sizeof (signed char)) + abort (); + /* Extract an array of character substrings. */ offset = 2; check_CFI_status ("CFI_establish", diff --git a/libgfortran/runtime/ISO_Fortran_binding.c b/libgfortran/runtime/ISO_Fortran_binding.c index 78953d0..9fe3a85 100644 --- a/libgfortran/runtime/ISO_Fortran_binding.c +++ b/libgfortran/runtime/ISO_Fortran_binding.c @@ -229,10 +229,9 @@ CFI_allocate (CFI_cdesc_t *dv, const CFI_index_t lower_bounds[], } } - /* If the type is a character, the descriptor's element length is replaced - by the elem_len argument. */ - if (dv->type == CFI_type_char || dv->type == CFI_type_ucs4_char || - dv->type == CFI_type_signed_char) + /* If the type is a Fortran character type, the descriptor's element + length is replaced by the elem_len argument. */ + if (dv->type == CFI_type_char || dv->type == CFI_type_ucs4_char) dv->elem_len = elem_len; /* Dimension information and calculating the array length. */ @@ -759,9 +758,9 @@ int CFI_select_part (CFI_cdesc_t *result, const CFI_cdesc_t *source, } } - /* Element length. */ - if (result->type == CFI_type_char || result->type == CFI_type_ucs4_char || - result->type == CFI_type_signed_char) + /* Element length is ignored unless result->type specifies a Fortran + character type. */ + if (result->type == CFI_type_char || result->type == CFI_type_ucs4_char) result->elem_len = elem_len; if (unlikely (compile_options.bounds_check)) --------------FA7662D172897ED3B35ED357--