[flang-commits] [flang] 41d5fed - [flang][Semantics] set scope even for module subroutines outside modules (#109009)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 18 02:40:44 PDT 2024


Author: Tom Eccles
Date: 2024-09-18T10:40:41+01:00
New Revision: 41d5fed09e7d31922e7869c72116a4c3adc11a4a

URL: https://github.com/llvm/llvm-project/commit/41d5fed09e7d31922e7869c72116a4c3adc11a4a
DIFF: https://github.com/llvm/llvm-project/commit/41d5fed09e7d31922e7869c72116a4c3adc11a4a.diff

LOG: [flang][Semantics] set scope even for module subroutines outside modules (#109009)

The missing scope information led to a crash in OpenMP semantic checks
run before printing the error that was already discovered in the code.

The following block has to be skipped for this invalid code so that we
don't emit a second spurious error.

Fixes #82913

Added: 
    flang/test/Semantics/OpenMP/bad_module_subroutine.f90

Modified: 
    flang/lib/Semantics/resolve-names.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index b99f308e1c7fab..7c692440d24730 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -4351,15 +4351,18 @@ bool SubprogramVisitor::BeginSubprogram(const parser::Name &name,
     Symbol::Flag subpFlag, bool hasModulePrefix,
     const parser::LanguageBindingSpec *bindingSpec,
     const ProgramTree::EntryStmtList *entryStmts) {
+  bool isValid{true};
   if (hasModulePrefix && !currScope().IsModule() &&
       !currScope().IsSubmodule()) { // C1547
     Say(name,
         "'%s' is a MODULE procedure which must be declared within a "
         "MODULE or SUBMODULE"_err_en_US);
-    return false;
+    // Don't return here because it can be useful to have the scope set for
+    // other semantic checks run before we print the errors
+    isValid = false;
   }
   Symbol *moduleInterface{nullptr};
-  if (hasModulePrefix && !inInterfaceBlock()) {
+  if (isValid && hasModulePrefix && !inInterfaceBlock()) {
     moduleInterface = FindSeparateModuleProcedureInterface(name);
     if (moduleInterface && &moduleInterface->owner() == &currScope()) {
       // Subprogram is MODULE FUNCTION or MODULE SUBROUTINE with an interface

diff  --git a/flang/test/Semantics/OpenMP/bad_module_subroutine.f90 b/flang/test/Semantics/OpenMP/bad_module_subroutine.f90
new file mode 100644
index 00000000000000..339d6bf27e7dfd
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/bad_module_subroutine.f90
@@ -0,0 +1,6 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+! Test that we don't crash on this code inside of openmp semantics checks
+
+!ERROR: 'e' is a MODULE procedure which must be declared within a MODULE or SUBMODULE
+impure elemental module subroutine e()
+end subroutine


        


More information about the flang-commits mailing list