From patchwork Tue Apr 9 21:23:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 88255 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3894E3846410 for ; Tue, 9 Apr 2024 21:24:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by sourceware.org (Postfix) with ESMTPS id 8E7523858D39; Tue, 9 Apr 2024 21:23:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8E7523858D39 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8E7523858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.227.15.15 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712697840; cv=none; b=ekLq1ncIse/mwYVln6Tzneso19Iv5plN7/bvtHKbUzLHPIITDVClzBv7S03H7FNNwrVOK70b/YlKmSl0JMW88d3DAO/QP2uyI5wpkkI4Nb7KF3ntK/SpUm13O2C3gZWRe+IlKrRIiKTtpzmczQoym+rWwiHEiom+HTNtUu6nibc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712697840; c=relaxed/simple; bh=h2A5ZxT/HcuuswjWduSWPmmmWh969fbiT6OBEAucTek=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=n48ZDzpJWbmRWy6FkFX1V+jZEyQt4atLajs4z4iTGZ5shsHCQWfnot5uc5kQD5xf76ZL5SCpPE9LG6tPXz/yq2OtYFOp5LkXUqbLwwVnmN9YxOHvSeclGhVOPwHetyljAGEsTr8gi74jEkBiz9FRf2jFB6hNRqBZ0K31Mix/1aw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1712697835; x=1713302635; i=anlauf@gmx.de; bh=95zceCJTRzF4TCS2jt4+mLgcT4P7Z/XhKWILHAM4rE0=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From: In-Reply-To; b=MGE19Nis9O/aaZVM3ESXYB5Obg9pv+cAS5mTKeFMTSqb1jGH1kQnQoa69F6Jmq5N DGwmTTAzDjTu9ayGR8SmrJSjFTmUnLxcTkqsv7MeHiQAXAhA/nXSOLUJabkFPpwg4 yCc9NY9kBc41dCegwu8zjqpVwutA2LVgZM2bfD+o28Zvr4WQwi/eQ8yQRJE+IeE7A DhgnoYOadY5gMkDQFJcgMRYt0h1lZbzMzYxJ+mVMgKNX+aMogq4JSpz3xyj6nqWGW xd0212EnvF/G/zszpWXBvh+V31P4PRl193iBiH7Lmoh/mjeXxrmJ0HVBDnNLHkEu1 +V7GURdxIDaFNSJzfg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.178.29] ([79.232.151.151]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1Ma24s-1sFDyR2EYG-00W0eI; Tue, 09 Apr 2024 23:23:55 +0200 Message-ID: <2337004b-dfd8-45d1-b349-7c08e17353d9@gmx.de> Date: Tue, 9 Apr 2024 23:23:54 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH, v2] Fortran: fix argument checking of intrinsics C_SIZEOF, C_F_POINTER [PR106500] To: FX Coudert Cc: fortran , gcc-patches Newsgroups: gmane.comp.gcc.patches,gmane.comp.gcc.fortran References: <1BADB9B4-62BF-43FE-845F-545128503616@gmail.com> Content-Language: en-US From: Harald Anlauf In-Reply-To: <1BADB9B4-62BF-43FE-845F-545128503616@g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eBUwlBWTI3aiDGPF8/A5NZM9KbMHdp73a4Y8+Aaq 1C00+Vn8WbXm9WsIdCiwF487/UxKxqrkUvr2MVVvlRt9M= X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hi FX! On 4/9/24 09:32, FX Coudert wrote: > Hi Harald, > > Thanks for the patch. > > >> + if (attr.function) >> + { >> + gfc_error ("FPTR at %L to C_F_POINTER is a function returning a pointer", >> + &fptr->where); >> + return false; >> + } >> + >> if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true)) >> return gfc_notify_std (GFC_STD_F2018, "Noninteroperable array FPTR " >> "at %L to C_F_POINTER: %s", &fptr->where, msg); > > > In both of these gfc_error(), could we change our error message to say "FPTR argument” instead of “FPTR”? “FPTR to C_F_POINTER” does not really make sense to me. > > This would be more in line with what the generally do: > >> Error: 'x' argument of 'sqrt' intrinsic at (1) must be REAL or COMPLEX > > So maybe “FPTR argument to C_F_POINTER at %L” ? That’s much more readable to me. Good point! I did indeed feel a little uncomfortable with the text and adjusted both messages accordingly to your suggestion. I also forgot to add one update of a pattern, and found a cornercase where the tightening of checks for C_F_POINTER was too strong. Corrected and now covered in an extension of the corresponding testcase. > Otherwise, OK. > > FX Thanks for the review! If there are no further comments, I will commit tomorrow. Thanks, Harald From 5983a07f11c88d920241141732fa742735cdb8ea Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Tue, 9 Apr 2024 23:07:59 +0200 Subject: [PATCH] Fortran: fix argument checking of intrinsics C_SIZEOF, C_F_POINTER [PR106500] The interpretation of the F2018 standard regarding valid arguments to the intrinsic C_SIZEOF(X) was clarified in an edit to 18-007r1: https://j3-fortran.org/doc/year/22/22-101r1.txt loosening restrictions and giving examples. The F2023 text has: ! F2023:18.2.3.8 C_SIZEOF (X) ! ! X shall be a data entity with interoperable type and type parameters, ! and shall not be an assumed-size array, an assumed-rank array that ! is associated with an assumed-size array, an unallocated allocatable ! variable, or a pointer that is not associated. where ! 3.41 data entity ! data object, result of the evaluation of an expression, or the ! result of the execution of a function reference Update the checking code for interoperable arguments accordingly, and extend to reject functions returning pointer as FPTR argument to C_F_POINTER. gcc/fortran/ChangeLog: PR fortran/106500 * check.cc (is_c_interoperable): Fix checks for C_SIZEOF. (gfc_check_c_f_pointer): Reject function returning a pointer as FPTR, and improve an error message. gcc/testsuite/ChangeLog: PR fortran/106500 * gfortran.dg/c_sizeof_6.f90: Remove wrong dg-error. * gfortran.dg/sizeof_2.f90: Adjust pattern. * gfortran.dg/c_f_pointer_tests_9.f90: New test. * gfortran.dg/c_sizeof_7.f90: New test. --- gcc/fortran/check.cc | 26 +++++++----- .../gfortran.dg/c_f_pointer_tests_9.f90 | 37 ++++++++++++++++ gcc/testsuite/gfortran.dg/c_sizeof_6.f90 | 2 +- gcc/testsuite/gfortran.dg/c_sizeof_7.f90 | 42 +++++++++++++++++++ gcc/testsuite/gfortran.dg/sizeof_2.f90 | 2 +- 5 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/c_f_pointer_tests_9.f90 create mode 100644 gcc/testsuite/gfortran.dg/c_sizeof_7.f90 diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc index db74dcf3f40..2f50d84b876 100644 --- a/gcc/fortran/check.cc +++ b/gcc/fortran/check.cc @@ -5299,18 +5299,14 @@ is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr) return false; } - if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY) + /* Checks for C_SIZEOF need to take into account edits to 18-007r1, see + https://j3-fortran.org/doc/year/22/22-101r1.txt . */ + if (!c_loc && !c_f_ptr && expr->rank > 0 && expr->expr_type == EXPR_VARIABLE) { gfc_array_ref *ar = gfc_find_array_ref (expr); - if (ar->type != AR_FULL) + if (ar->type == AR_FULL && ar->as->type == AS_ASSUMED_SIZE) { - *msg = "Only whole-arrays are interoperable"; - return false; - } - if (!c_f_ptr && ar->as->type != AS_EXPLICIT - && ar->as->type != AS_ASSUMED_SIZE) - { - *msg = "Only explicit-size and assumed-size arrays are interoperable"; + *msg = "Assumed-size arrays are not interoperable"; return false; } } @@ -5475,9 +5471,17 @@ gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape) return false; } + if (fptr->ts.type == BT_PROCEDURE && attr.function) + { + gfc_error ("FPTR argument to C_F_POINTER at %L is a function " + "returning a pointer", &fptr->where); + return false; + } + if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true)) - return gfc_notify_std (GFC_STD_F2018, "Noninteroperable array FPTR " - "at %L to C_F_POINTER: %s", &fptr->where, msg); + return gfc_notify_std (GFC_STD_F2018, + "Noninteroperable array FPTR argument to " + "C_F_POINTER at %L: %s", &fptr->where, msg); return true; } diff --git a/gcc/testsuite/gfortran.dg/c_f_pointer_tests_9.f90 b/gcc/testsuite/gfortran.dg/c_f_pointer_tests_9.f90 new file mode 100644 index 00000000000..8c8b4a713a4 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/c_f_pointer_tests_9.f90 @@ -0,0 +1,37 @@ +! { dg-do compile } +! +! A function returning a pointer cannot be interoperable +! and cannot be used as FPTR argument to C_F_POINTER. + +subroutine s () + use, intrinsic :: iso_c_binding + implicit none + type(c_ptr) :: cPtr + call c_f_pointer (cPtr, p0) ! { dg-error "function returning a pointer" } + call c_f_pointer (cPtr, p1, shape=[2]) ! { dg-error "function returning a pointer" } +contains + function p0 () + integer, pointer :: p0 + nullify (p0) + end + function p1 () + integer, pointer :: p1(:) + nullify (p1) + end + function fp0 () + integer, pointer :: fp0 + call c_f_pointer (cPtr, fp0) ! valid here + end + function fp1 () + integer, pointer :: fp1(:) + call c_f_pointer (cPtr, fp1, shape=[2]) ! valid here + end + function ffp0 () result (fp0) + integer, pointer :: fp0 + call c_f_pointer (cPtr, fp0) ! valid here + end + function ffp1 () result (fp1) + integer, pointer :: fp1(:) + call c_f_pointer (cPtr, fp1, shape=[2]) ! valid here + end +end diff --git a/gcc/testsuite/gfortran.dg/c_sizeof_6.f90 b/gcc/testsuite/gfortran.dg/c_sizeof_6.f90 index a676a5b8986..7043ac6ca99 100644 --- a/gcc/testsuite/gfortran.dg/c_sizeof_6.f90 +++ b/gcc/testsuite/gfortran.dg/c_sizeof_6.f90 @@ -8,7 +8,7 @@ program foo character(kind=c_char,len=1),parameter :: str2(4) = ["a","b","c","d"] - i = c_sizeof(str2(1:3)) ! { dg-error "must be an interoperable data" } + i = c_sizeof(str2(1:3)) if (i /= 3) STOP 1 diff --git a/gcc/testsuite/gfortran.dg/c_sizeof_7.f90 b/gcc/testsuite/gfortran.dg/c_sizeof_7.f90 new file mode 100644 index 00000000000..04a0bddbcaa --- /dev/null +++ b/gcc/testsuite/gfortran.dg/c_sizeof_7.f90 @@ -0,0 +1,42 @@ +! { dg-do compile } +! PR fortran/106500 - fix checking of arguments to C_SIZEOF +! +! Check support of the following EDIT to 18-007r1: +! https://j3-fortran.org/doc/year/22/22-101r1.txt + +subroutine foo (n, x, y, z, w, u) + use, intrinsic :: iso_c_binding + implicit none + integer, intent(in) :: n + real :: x(n) + real :: y(:) + real :: z(2,*) + real :: w(..) + real, allocatable :: a(:) + real, pointer :: b(:) + type t + real, allocatable :: a(:) + end type t + type(t) :: u + + print *, c_sizeof (x) + print *, c_sizeof (x(::2)) + print *, c_sizeof (x+1) + print *, c_sizeof (y) + print *, c_sizeof (y(1:2)) + print *, c_sizeof (z(:,1:2)) + print *, c_sizeof (w) + print *, c_sizeof (1._c_float) + ! + allocate (a(n)) + allocate (b(n)) + if (.not. allocated (u%a)) allocate (u%a(n)) + print *, c_sizeof (a) + print *, c_sizeof (b) + ! + print *, c_sizeof (u%a) + print *, c_sizeof (u%a(1:2)) + ! + print *, c_sizeof (z) ! { dg-error "Assumed-size arrays are not interoperable" } + print *, c_sizeof (u) ! { dg-error "Expression is a noninteroperable derived type" } +end diff --git a/gcc/testsuite/gfortran.dg/sizeof_2.f90 b/gcc/testsuite/gfortran.dg/sizeof_2.f90 index e6661a56b30..d1655c63449 100644 --- a/gcc/testsuite/gfortran.dg/sizeof_2.f90 +++ b/gcc/testsuite/gfortran.dg/sizeof_2.f90 @@ -15,7 +15,7 @@ subroutine foo(x, y) ii = storage_size (x) ! { dg-error "Assumed-type argument at .1. is not permitted as actual argument to the intrinsic storage_size" } ii = sizeof (y) ! { dg-error "shall not be an assumed-size array" } - ii = c_sizeof (y) ! { dg-error "shall not be an assumed-size array" } + ii = c_sizeof (y) ! { dg-error "\[Aa\]ssumed-size array" } ii = storage_size (y) ! okay, element-size is known ii = sizeof (proc) ! { dg-error "shall not be a procedure" } -- 2.35.3