[flang-commits] [PATCH] D157344: [flang] Foil attempt to READ a NAMELIST with an undefinable member

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Aug 7 15:28:35 PDT 2023


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

Detect and emit errors about undefinable NAMELIST group members in READ statements.

Fixes llvm-test-suite/Fortran/gfortran/regression/namelist_2.f90.


https://reviews.llvm.org/D157344

Files:
  flang/lib/Semantics/check-io.cpp
  flang/lib/Semantics/resolve-names.cpp
  flang/test/Semantics/resolve40.f90


Index: flang/test/Semantics/resolve40.f90
===================================================================
--- flang/test/Semantics/resolve40.f90
+++ flang/test/Semantics/resolve40.f90
@@ -88,3 +88,11 @@
   integer :: nl3
   nl2 = 1
 end
+
+subroutine s12(x)
+  real, intent(in) :: x
+  namelist /nl/x
+  !ERROR: NAMELIST input group must not contain undefinable item 'x'
+  !BECAUSE: 'x' is an INTENT(IN) dummy argument
+  read(*,nml=nl)
+end
Index: flang/lib/Semantics/resolve-names.cpp
===================================================================
--- flang/lib/Semantics/resolve-names.cpp
+++ flang/lib/Semantics/resolve-names.cpp
@@ -5832,6 +5832,7 @@
               ApplyImplicitRules(*symbol);
             } else if (!ConvertToObjectEntity(*symbol)) {
               SayWithDecl(name, *symbol, "'%s' is not a variable"_err_en_US);
+              context().SetError(*groupSymbol);
             }
             symbol->GetUltimate().set(Symbol::Flag::InNamelist);
             details->add_object(*symbol);
Index: flang/lib/Semantics/check-io.cpp
===================================================================
--- flang/lib/Semantics/check-io.cpp
+++ flang/lib/Semantics/check-io.cpp
@@ -1171,11 +1171,23 @@
 
 void IoChecker::CheckNamelist(const Symbol &namelist, common::DefinedIo which,
     parser::CharBlock namelistLocation) const {
-  const auto &details{namelist.GetUltimate().get<NamelistDetails>()};
-  for (const Symbol &object : details.objects()) {
-    context_.CheckIndexVarRedefine(namelistLocation, object);
-    if (auto *msg{CheckForBadIoType(object, which, namelistLocation)}) {
-      evaluate::AttachDeclaration(*msg, namelist);
+  if (!context_.HasError(namelist)) {
+    const auto &details{namelist.GetUltimate().get<NamelistDetails>()};
+    for (const Symbol &object : details.objects()) {
+      context_.CheckIndexVarRedefine(namelistLocation, object);
+      if (auto *msg{CheckForBadIoType(object, which, namelistLocation)}) {
+        evaluate::AttachDeclaration(*msg, namelist);
+      } else if (which == common::DefinedIo::ReadFormatted) {
+        if (auto why{WhyNotDefinable(namelistLocation, namelist.owner(),
+                DefinabilityFlags{}, object)}) {
+          context_
+              .Say(namelistLocation,
+                  "NAMELIST input group must not contain undefinable item '%s'"_err_en_US,
+                  object.name())
+              .Attach(std::move(*why));
+          context_.SetError(namelist);
+        }
+      }
     }
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157344.547979.patch
Type: text/x-patch
Size: 2518 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230807/da155afb/attachment-0001.bin>


More information about the flang-commits mailing list