| 1 |
! { dg-do compile } |
| 2 |
! { dg-additional-options "-fdump-tree-original" } |
| 3 |
! |
| 4 |
! PR fortran/108621 |
| 5 |
! |
| 6 |
! If the bind(C) procedure's dummy argument is a POINTER with INTENT(OUT), |
| 7 |
! avoid converting the array bounds for the CFI descriptor before the call. |
| 8 |
! |
| 9 |
! Rational: Fewer code and, esp. for undefined pointers, there might be a |
| 10 |
! compile-time warning or a runtime error due to the 'extent' arithmentic |
| 11 |
! and integer overflows (i.e. random values and -fsanitize=undefined). |
| 12 |
! |
| 13 |
! (For disassociated pointers, it would/should be only pointless code as |
| 14 |
! the bound setting is guarded by a != NULL condtion. However, as the PR shows, |
| 15 |
! a bogus may-use-uninitialized-memory warning might still be shown in that case.) |
| 16 |
! |
| 17 |
! Without 'intent' (but still intent(out) internally), the same applies but |
| 18 |
! there is nothing the compiler can do on the caller side. |
| 19 |
! Still, as only uninit memory and not invalid memory it accessed, it should still |
| 20 |
! work (at least when run-time checking is turned off). |
| 21 |
! |
| 22 |
subroutine demo(f) |
| 23 |
use, intrinsic :: iso_c_binding, only : c_int |
| 24 |
implicit none |
| 25 |
|
| 26 |
interface |
| 27 |
subroutine fun(f_p) bind(c) |
| 28 |
import c_int |
| 29 |
integer(c_int), pointer, intent(out) :: f_p(:) |
| 30 |
end subroutine |
| 31 |
end interface |
| 32 |
|
| 33 |
integer(c_int), pointer :: f(:) |
| 34 |
|
| 35 |
call fun(f) |
| 36 |
end |
| 37 |
|
| 38 |
! The following ones must be present even with intent(out): |
| 39 |
! |
| 40 |
! { dg-final { scan-tree-dump "cfi...version = 1;" "original" } } |
| 41 |
! { dg-final { scan-tree-dump "cfi...rank = 1;" "original" } } |
| 42 |
! { dg-final { scan-tree-dump "cfi...type = 1025;" "original" } } |
| 43 |
! { dg-final { scan-tree-dump "cfi...attribute = 0;" "original" } } |
| 44 |
! { dg-final { scan-tree-dump "cfi...elem_len = 4;" "original" } } |
| 45 |
|
| 46 |
|
| 47 |
! The following is not needed - but user code might expect that an incoming pointer is NULL |
| 48 |
! in this case. - At least the GCC testsuite expects this in the C code at |
| 49 |
! gfortran.dg/c-interop/section-{1,2}.f90 |
| 50 |
! Thus, it is kept as it does not cause any harm: |
| 51 |
! |
| 52 |
! { dg-final { scan-tree-dump "cfi...base_addr = f->data;" "original" } } |
| 53 |
|
| 54 |
|
| 55 |
! The following ones are not need with intent(out) and, therefore, shouldn't be there: |
| 56 |
! |
| 57 |
! cfi.0.dim[idx.1].lower_bound = f->dim[idx.1].lbound; |
| 58 |
! cfi.0.dim[idx.1].extent = (f->dim[idx.1].ubound - f->dim[idx.1].lbound) + 1; |
| 59 |
! cfi.0.dim[idx.1].sm = f->dim[idx.1].stride * f->span; |
| 60 |
! |
| 61 |
! Now match those - but using a rather generic pattern as it is a ...-not scan: |
| 62 |
! |
| 63 |
! { dg-final { scan-tree-dump-not "lower_bound = " "original" } } |
| 64 |
! { dg-final { scan-tree-dump-not "extent = " "original" } } |
| 65 |
! { dg-final { scan-tree-dump-not "sm = " "original" } } |