[flang-commits] [PATCH] D145089: [flang] Warn about dangerous actual argument association with TARGET dummy arguments

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Mar 1 10:53:03 PST 2023


klausler created this revision.
klausler added a reviewer: vzakhari.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

The actual argument associated with a dummy argument with the TARGET attribute is
not required to be itself a target or pointer, or even to be a variable, but in
those cases, any pointer that is associated with the dummy argument during the
execution of the procedure is either going to be invalid afterwards because it
points to temporary storage that has since been deallocated or an optimization
time bomb because it aliases an object that isn't a target.  Add warnings for
these cases.


https://reviews.llvm.org/D145089

Files:
  flang/lib/Semantics/check-call.cpp
  flang/test/Semantics/call34.f90


Index: flang/test/Semantics/call34.f90
===================================================================
--- /dev/null
+++ flang/test/Semantics/call34.f90
@@ -0,0 +1,22 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -Werror
+module m
+ contains
+  subroutine foo(a)
+    real, intent(in), target :: a(:)
+  end subroutine
+end module
+
+program test
+  use m
+  real, target :: a(1)
+  real :: b(1)
+  call foo(a) ! ok
+  !WARNING: Any pointer associated with TARGET dummy argument 'a=' during this call must not be used afterwards, as 'b' is not a target
+  call foo(b)
+  !WARNING: Any pointer associated with TARGET dummy argument 'a=' during this call will not be associated with the value of '(a)' afterwards
+  call foo((a))
+  !WARNING: Any pointer associated with TARGET dummy argument 'a=' during this call will not be associated with the value of 'a([INTEGER(8)::1_8])' afterwards
+  call foo(a([1]))
+  !ERROR: Scalar actual argument may not be associated with assumed-shape dummy argument 'a='
+  call foo(a(1))
+end
Index: flang/lib/Semantics/check-call.cpp
===================================================================
--- flang/lib/Semantics/check-call.cpp
+++ flang/lib/Semantics/check-call.cpp
@@ -441,7 +441,8 @@
   // technically legal but worth emitting a warning
   // llvm-project issue #58973: constant actual argument passed in where dummy
   // argument is marked volatile
-  if (dummyIsVolatile && !IsVariable(actual)) {
+  bool actualIsVariable{evaluate::IsVariable(actual)};
+  if (dummyIsVolatile && !actualIsVariable) {
     messages.Say(
         "actual argument associated with VOLATILE %s is not a variable"_warn_en_US,
         dummyName);
@@ -599,6 +600,24 @@
         "Actual argument associated with %s may not be null pointer %s"_err_en_US,
         dummyName, actual.AsFortran());
   }
+
+  // Warn about dubious actual argument association with a TARGET dummy argument
+  if (dummy.attrs.test(characteristics::DummyDataObject::Attr::Target)) {
+    bool actualIsTemp{!actualIsVariable || HasVectorSubscript(actual) ||
+        evaluate::ExtractCoarrayRef(actual)};
+    if (actualIsTemp) {
+      messages.Say(
+          "Any pointer associated with TARGET %s during this call will not be associated with the value of '%s' afterwards"_warn_en_US,
+          dummyName, actual.AsFortran());
+    } else {
+      auto actualSymbolVector{GetSymbolVector(actual)};
+      if (!evaluate::GetLastTarget(actualSymbolVector)) {
+        messages.Say(
+            "Any pointer associated with TARGET %s during this call must not be used afterwards, as '%s' is not a target"_warn_en_US,
+            dummyName, actual.AsFortran());
+      }
+    }
+  }
 }
 
 static void CheckProcedureArg(evaluate::ActualArgument &arg,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145089.501581.patch
Type: text/x-patch
Size: 2765 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230301/c9f0f5c5/attachment-0001.bin>


More information about the flang-commits mailing list