[flang] Diagnostic for shape argument in c_f_pointer

Fix #59177, add check for dimensionality for shape argument against rank of FPTR argument in c_f_pointer

Reviewed By: peixin

Differential Revision: https://reviews.llvm.org/D138743
This commit is contained in:
Qihan Cai 2022-11-26 21:46:29 +11:00
parent 47ff3042e7
commit d9df5bb8cf
2 changed files with 21 additions and 1 deletions

View File

@ -2616,6 +2616,20 @@ IntrinsicProcTable::Implementation::HandleC_F_Pointer(
} else if (!arguments[2] && fptrRank > 0) {
context.messages().Say(
"SHAPE= argument to C_F_POINTER() must appear when FPTR= is an array"_err_en_US);
} else if (arguments[2]) {
if (const auto *argExpr{arguments[2].value().UnwrapExpr()}) {
if (argExpr->Rank() > 1) {
context.messages().Say(arguments[2]->sourceLocation(),
"SHAPE= argument to C_F_POINTER() must be a rank-one array."_err_en_US);
} else if (argExpr->Rank() == 1) {
if (auto constShape{GetConstantShape(context, *argExpr)}) {
if (constShape->At(ConstantSubscripts{1}).ToInt64() != fptrRank) {
context.messages().Say(arguments[2]->sourceLocation(),
"SHAPE= argument to C_F_POINTER() must have size equal to the rank of FPTR="_err_en_US);
}
}
}
}
}
}
}

View File

@ -8,9 +8,11 @@ program test
integer, pointer :: p
end type
type(with_pointer) :: coindexed[*]
integer, pointer :: scalarIntF, arrayIntF(:)
integer, pointer :: scalarIntF, arrayIntF(:), multiDimIntF(:,:)
character(len=:), pointer :: charDeferredF
integer :: j
integer, dimension(2, 2) :: rankTwoArray
rankTwoArray = reshape([1, 2, 3, 4], shape(rankTwoArray))
call c_f_pointer(scalarC, scalarIntF) ! ok
call c_f_pointer(scalarC, arrayIntF, [1_8]) ! ok
call c_f_pointer(shape=[1_8], cptr=scalarC, fptr=arrayIntF) ! ok
@ -31,4 +33,8 @@ program test
call c_f_pointer(scalarC, coindexed[0]%p)
!ERROR: FPTR= argument to C_F_POINTER() must have a type
call c_f_pointer(scalarC, null())
!ERROR: SHAPE= argument to C_F_POINTER() must have size equal to the rank of FPTR=
call c_f_pointer(scalarC, multiDimIntF, shape=[1_8])
!ERROR: SHAPE= argument to C_F_POINTER() must be a rank-one array.
call c_f_pointer(scalarC, multiDimIntF, shape=rankTwoArray)
end program