[flang-commits] [flang] 5b9bd88 - [flang] Fix crash with USE of hermetic module file (#138785)
via flang-commits
flang-commits at lists.llvm.org
Mon May 12 12:15:49 PDT 2025
Author: Peter Klausler
Date: 2025-05-12T12:15:46-07:00
New Revision: 5b9bd8838842896b482fea20dce56906d42cc7b1
URL: https://github.com/llvm/llvm-project/commit/5b9bd8838842896b482fea20dce56906d42cc7b1
DIFF: https://github.com/llvm/llvm-project/commit/5b9bd8838842896b482fea20dce56906d42cc7b1.diff
LOG: [flang] Fix crash with USE of hermetic module file (#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.
Added:
flang/test/Semantics/modfile75.F90
Modified:
flang/lib/Semantics/mod-file.cpp
Removed:
################################################################################
diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index 3ea37ceddd056..a1ec956562204 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -1548,6 +1548,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()));
@@ -1563,7 +1564,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..aba00ffac848a
--- /dev/null
+++ b/flang/test/Semantics/modfile75.F90
@@ -0,0 +1,17 @@
+!RUN: %flang -c -fhermetic-module-files -DWHICH=1 %s && %flang -c -fhermetic-module-files -DWHICH=2 %s && %flang_fc1 -fdebug-unparse %s | FileCheck %s
+
+#if WHICH == 1
+module modfile75a
+ use iso_c_binding
+end
+#elif WHICH == 2
+module modfile75b
+ use modfile75a
+end
+#else
+program test
+ use modfile75b
+!CHECK: INTEGER(KIND=4_4) n
+ integer(c_int) n
+end
+#endif
More information about the flang-commits
mailing list