[flang-commits] [flang] 9d075d2 - [flang] Fail more gracefully when asked to read a bogus module file (#183119)

via flang-commits flang-commits at lists.llvm.org
Thu Feb 26 07:06:27 PST 2026


Author: Peter Klausler
Date: 2026-02-26T07:06:22-08:00
New Revision: 9d075d271ff15ec153ada3413d294540206a15ed

URL: https://github.com/llvm/llvm-project/commit/9d075d271ff15ec153ada3413d294540206a15ed
DIFF: https://github.com/llvm/llvm-project/commit/9d075d271ff15ec153ada3413d294540206a15ed.diff

LOG: [flang] Fail more gracefully when asked to read a bogus module file (#183119)

Flang-new emits a torrent of Fortran tokenization error messages when
asked to read a module file from another compiler. Handle this case with
a better error message.

Added: 
    flang/test/Semantics/Inputs/modfile83.mod
    flang/test/Semantics/modfile83.f90

Modified: 
    flang/lib/Semantics/mod-file.cpp
    flang/test/Driver/intrinsic-module-path.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index a0ad6775b60b1..857bcbfafe972 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -1386,12 +1386,17 @@ std::optional<ModuleCheckSumType> ExtractCheckSum(const std::string_view &str) {
   return std::nullopt;
 }
 
+static bool VerifyMagic(llvm::ArrayRef<char> content) {
+  std::string_view sv{content.data(), content.size()};
+  return sv.substr(0, ModHeader::magicLen) == ModHeader::magic;
+}
+
 static std::optional<ModuleCheckSumType> VerifyHeader(
     llvm::ArrayRef<char> content) {
-  std::string_view sv{content.data(), content.size()};
-  if (sv.substr(0, ModHeader::magicLen) != ModHeader::magic) {
+  if (!VerifyMagic(content)) {
     return std::nullopt;
   }
+  std::string_view sv{content.data(), content.size()};
   ModuleCheckSumType checkSum{ComputeCheckSum(sv.substr(ModHeader::len))};
   std::string_view expectSum{sv.substr(ModHeader::magicLen, ModHeader::sumLen)};
   if (auto extracted{ExtractCheckSum(expectSum)};
@@ -1534,7 +1539,6 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
           context_.moduleDependences().GetRequiredHash(name.ToString(), true);
     }
   }
-
   // Look for the right module file if its hash is known
   if (requiredHash && !fatalError) {
     for (const std::string &maybe :
@@ -1557,6 +1561,9 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
         // symbol of the same name that is not a module.
         context_.SayWithDecl(
             *notAModule, name, "'%s' is not a module"_err_en_US, name);
+      } else if (sourceFile && !VerifyMagic(sourceFile->content())) {
+        Say("read", name, ancestorName,
+            "'%s' is not a module file for this compiler"_err_en_US, path);
       } else {
         for (auto &msg : parsing.messages().messages()) {
           std::string str{msg.ToString()};
@@ -1572,13 +1579,22 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
   std::optional<ModuleCheckSumType> checkSum{
       VerifyHeader(sourceFile->content())};
   if (!checkSum) {
-    Say("use", name, ancestorName, "File has invalid checksum: %s"_err_en_US,
-        sourceFile->path());
+    if (!silent) {
+      if (!VerifyMagic(sourceFile->content())) {
+        Say("read", name, ancestorName,
+            "'%s' is not a module file for this compiler"_err_en_US, path);
+      } else {
+        Say("use", name, ancestorName,
+            "File has invalid checksum: %s"_err_en_US, sourceFile->path());
+      }
+    }
     return nullptr;
   } else if (requiredHash && *requiredHash != *checkSum) {
-    Say("use", name, ancestorName,
-        "File is not the right module file for %s"_err_en_US,
-        "'"s + name.ToString() + "': "s + sourceFile->path());
+    if (!silent) {
+      Say("use", name, ancestorName,
+          "File is not the right module file for %s"_err_en_US,
+          "'"s + name.ToString() + "': "s + sourceFile->path());
+    }
     return nullptr;
   }
   llvm::raw_null_ostream NullStream;
@@ -1586,8 +1602,10 @@ Scope *ModFileReader::Read(SourceName name, std::optional<bool> isIntrinsic,
   std::optional<parser::Program> &parsedProgram{parsing.parseTree()};
   if (!parsing.messages().empty() || !parsing.consumedWholeFile() ||
       !parsedProgram) {
-    Say("parse", name, ancestorName, "Module file is corrupt: %s"_err_en_US,
-        sourceFile->path());
+    if (!silent) {
+      Say("parse", name, ancestorName, "Module file is corrupt: %s"_err_en_US,
+          sourceFile->path());
+    }
     return nullptr;
   }
   parser::Program &parseTree{context_.SaveParseTree(std::move(*parsedProgram))};

diff  --git a/flang/test/Driver/intrinsic-module-path.f90 b/flang/test/Driver/intrinsic-module-path.f90
index 615d8f9a1730a..c8b848f3efa9a 100644
--- a/flang/test/Driver/intrinsic-module-path.f90
+++ b/flang/test/Driver/intrinsic-module-path.f90
@@ -1,7 +1,7 @@
 ! Ensure argument -fintrinsic-modules-path works as expected.
 ! WITHOUT the option, the default location for the module is checked and no error generated.
 ! With the option GIVEN, the module with the same name is PREPENDED, and considered over the
-! default one, causing a CHECKSUM error.
+! default one, causing an error.
 
 !-----------------------------------------
 ! FRONTEND FLANG DRIVER (flang -fc1)
@@ -13,8 +13,8 @@
 ! WITHOUT-NOT: 'ieee_arithmetic.mod' was not found
 ! WITHOUT-NOT: 'iso_fortran_env.mod' was not found
 
-! GIVEN: error: Cannot use module file for module 'ieee_arithmetic': File has invalid checksum
-! GIVEN: error: Cannot use module file for module 'iso_fortran_env': File has invalid checksum
+! GIVEN: error: Cannot read module file for module 'ieee_arithmetic': 'ieee_arithmetic.mod' is not a module file for this compiler
+! GIVEN: error: Cannot read module file for module 'iso_fortran_env': 'iso_fortran_env.mod' is not a module file for this compiler
 
 
 program test_intrinsic_module_path

diff  --git a/flang/test/Semantics/Inputs/modfile83.mod b/flang/test/Semantics/Inputs/modfile83.mod
new file mode 100644
index 0000000000000..751010f414cbf
--- /dev/null
+++ b/flang/test/Semantics/Inputs/modfile83.mod
@@ -0,0 +1 @@
+This is not a valid module file.

diff  --git a/flang/test/Semantics/modfile83.f90 b/flang/test/Semantics/modfile83.f90
new file mode 100644
index 0000000000000..9fcb0bb7cb62d
--- /dev/null
+++ b/flang/test/Semantics/modfile83.f90
@@ -0,0 +1,4 @@
+!RUN: %python %S/test_errors.py %s %flang_fc1 -J %S/Inputs
+!ERROR: Cannot read module file for module 'modfile83': 'modfile83.mod' is not a module file for this compiler
+use modfile83
+end


        


More information about the flang-commits mailing list