[flang-commits] [flang] [flang] Allow use of COMMON in PURE (PR #76741)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Jan 2 10:45:53 PST 2024


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/76741

Although COMMON variables implicitly have the SAVE attribute, and variables with the SAVE attribute are generally disallowed in PURE subprograms, we must allow the use of COMMON in PURE as an exception.  F'2023 constraint C1598 applies only to local variables.

>From d561df85d0f993dd8cdb5c08f24da311e330f0cf Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 2 Jan 2024 10:41:56 -0800
Subject: [PATCH] [flang] Allow use of COMMON in PURE

Although COMMON variables implicitly have the SAVE attribute,
and variables with the SAVE attribute are generally disallowed
in PURE subprograms, we must allow the use of COMMON in PURE
as an exception.  F'2023 constraint C1598 applies only to local
variables.
---
 flang/lib/Semantics/check-declarations.cpp | 16 +++++++---------
 flang/test/Semantics/call10.f90            |  4 ----
 2 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index 777e6a9f23fbf8..93acb603297a47 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -317,15 +317,13 @@ void CheckHelper::Check(const Symbol &symbol) {
       // are not pertinent to the characteristics of the procedure.
       // Restrictions on entities in pure procedure interfaces don't need
       // enforcement.
-    } else {
-      if (IsSaved(symbol)) {
-        if (IsInitialized(symbol)) {
-          messages_.Say(
-              "A pure subprogram may not initialize a variable"_err_en_US);
-        } else {
-          messages_.Say(
-              "A pure subprogram may not have a variable with the SAVE attribute"_err_en_US);
-        }
+    } else if (!FindCommonBlockContaining(symbol) && IsSaved(symbol)) {
+      if (IsInitialized(symbol)) {
+        messages_.Say(
+            "A pure subprogram may not initialize a variable"_err_en_US);
+      } else {
+        messages_.Say(
+            "A pure subprogram may not have a variable with the SAVE attribute"_err_en_US);
       }
     }
     if (symbol.attrs().test(Attr::VOLATILE) &&
diff --git a/flang/test/Semantics/call10.f90 b/flang/test/Semantics/call10.f90
index 2a840e111e2ab2..dab2f874a9a953 100644
--- a/flang/test/Semantics/call10.f90
+++ b/flang/test/Semantics/call10.f90
@@ -109,10 +109,6 @@ pure subroutine s05 ! C1589
     !ERROR: A pure subprogram may not initialize a variable
     real :: v3
     data v3/0./
-    !ERROR: A pure subprogram may not have a variable with the SAVE attribute
-    real :: v4
-    common /blk/ v4
-    save /blk/
     block
     !ERROR: A pure subprogram may not have a variable with the SAVE attribute
       real, save :: v5



More information about the flang-commits mailing list