[flang-commits] [flang] d9df5bb - [flang] Diagnostic for shape argument in c_f_pointer

Qihan Cai via flang-commits flang-commits at lists.llvm.org
Sun Dec 4 19:09:11 PST 2022


Author: Qihan Cai
Date: 2022-12-05T14:09:04+11:00
New Revision: d9df5bb8cf1971fac165a718cef2d435b71bab4b

URL: https://github.com/llvm/llvm-project/commit/d9df5bb8cf1971fac165a718cef2d435b71bab4b
DIFF: https://github.com/llvm/llvm-project/commit/d9df5bb8cf1971fac165a718cef2d435b71bab4b.diff

LOG: [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

Added: 
    

Modified: 
    flang/lib/Evaluate/intrinsics.cpp
    flang/test/Semantics/c_f_pointer.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index a944217ed22c..308e3e9d6185 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -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);
+              }
+            }
+          }
+        }
       }
     }
   }

diff  --git a/flang/test/Semantics/c_f_pointer.f90 b/flang/test/Semantics/c_f_pointer.f90
index 2d780334e2e0..2613a4de8d32 100644
--- a/flang/test/Semantics/c_f_pointer.f90
+++ b/flang/test/Semantics/c_f_pointer.f90
@@ -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


        


More information about the flang-commits mailing list