[flang-commits] [flang] [flang] Fix crash with USE of hermetic module file (PR #138785)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue May 6 17:01:32 PDT 2025


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

When one hermetic module file uses another, a later compilation may crash in semantics when it itself is used, since the module file reader sets the "current hermetic module file scope" to null after reading one rather than saving and restoring that pointer.

>From ffb0d3a53a0f072ab59f9b12e8b84be4b73f4873 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 6 May 2025 16:57:36 -0700
Subject: [PATCH] [flang] Fix crash with USE of hermetic module file

When one hermetic module file uses another, a later compilation
may crash in semantics when it itself is used, since the module
file reader sets the "current hermetic module file scope" to
null after reading one rather than saving and restoring that
pointer.
---
 flang/lib/Semantics/mod-file.cpp   |  3 ++-
 flang/test/Semantics/modfile75.F90 | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Semantics/modfile75.F90

diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index ee356e56e4458..3df229cc85587 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -1537,6 +1537,7 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
   // created under -fhermetic-module-files?  If so, process them first in
   // their own nested scope that will be visible only to USE statements
   // within the module file.
+  Scope *previousHermetic{context_.currentHermeticModuleFileScope()};
   if (parseTree.v.size() > 1) {
     parser::Program hermeticModules{std::move(parseTree.v)};
     parseTree.v.emplace_back(std::move(hermeticModules.v.front()));
@@ -1552,7 +1553,7 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
   GetModuleDependences(context_.moduleDependences(), sourceFile->content());
   ResolveNames(context_, parseTree, topScope);
   context_.foldingContext().set_moduleFileName(wasModuleFileName);
-  context_.set_currentHermeticModuleFileScope(nullptr);
+  context_.set_currentHermeticModuleFileScope(previousHermetic);
   if (!moduleSymbol) {
     // Submodule symbols' storage are owned by their parents' scopes,
     // but their names are not in their parents' dictionaries -- we
diff --git a/flang/test/Semantics/modfile75.F90 b/flang/test/Semantics/modfile75.F90
new file mode 100644
index 0000000000000..e8cfb13552437
--- /dev/null
+++ b/flang/test/Semantics/modfile75.F90
@@ -0,0 +1,17 @@
+!RUN: (%flang -c -DWHICH=1 %s && %flang -c -DWHICH=2 %s && %flang_fc1 -fdebug-unparse %s) | FileCheck %s
+
+#if WHICH == 1
+module m1
+  use iso_c_binding
+end
+#elif WHICH == 2
+module m2
+  use m1
+end
+#else
+program test
+  use m2
+!CHECK: INTEGER(KIND=4_4) n
+  integer(c_int) n
+end
+#endif



More information about the flang-commits mailing list