https://github.com/naveen-seth created 
https://github.com/llvm/llvm-project/pull/137711

Fixes #132692.

`clang -cc1` crashes when generating a module interface with 
`emit-module-interface` or `emit-reduced-module-interface` using an input file 
which is already a precompiled module (`.pcm`) file. 
This commit adds validation for the input file format and Clang will now emit 
an error message instead of crashing.

>From 34108040c048dc7e586c5af161b57575f4626bd2 Mon Sep 17 00:00:00 2001
From: naveen-seth <naveen.ha...@outlook.com>
Date: Mon, 28 Apr 2025 20:30:51 +0000
Subject: [PATCH] [clang][modules] Validate input file format for
 GenerateModuleInterfaceAction  (#132692)

Fixes #132692.

`clang -cc1` crashes when generating a module interface with
`emit-module-interface` or `emit-reduced-module-interface` using an
input file which is already a precompiled module (`.pcm`) file.
This commit adds validation for the input file format and Clang will
now emit an error message instead of crashing.
---
 .../include/clang/Basic/DiagnosticFrontendKinds.td |  4 ++++
 clang/include/clang/Frontend/FrontendActions.h     |  1 +
 clang/lib/Frontend/FrontendActions.cpp             | 14 ++++++++++++++
 .../Modules/emit-module-interface-pcm-input.cpp    | 13 +++++++++++++
 4 files changed, 32 insertions(+)
 create mode 100644 clang/test/Modules/emit-module-interface-pcm-input.cpp

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 6c72775197823..8a8db27490f06 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -260,6 +260,10 @@ def err_modules_embed_file_not_found :
   DefaultFatal;
 def err_module_header_file_not_found :
   Error<"module header file '%0' not found">, DefaultFatal;
+def err_frontend_action_unsupported_input_format
+    : Error<"%0 does not support input file format of file '%1': "
+            "'%select{Source|ModuleMap|Precompiled|Unknown}2'">,
+      DefaultFatal;
 
 def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
diff --git a/clang/include/clang/Frontend/FrontendActions.h 
b/clang/include/clang/Frontend/FrontendActions.h
index a620ddfc40447..a5dfb770c58a2 100644
--- a/clang/include/clang/Frontend/FrontendActions.h
+++ b/clang/include/clang/Frontend/FrontendActions.h
@@ -156,6 +156,7 @@ class GenerateModuleFromModuleMapAction : public 
GenerateModuleAction {
 /// files) for C++20 Named Modules.
 class GenerateModuleInterfaceAction : public GenerateModuleAction {
 protected:
+  bool PrepareToExecuteAction(CompilerInstance &CI) override;
   bool BeginSourceFileAction(CompilerInstance &CI) override;
 
   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
diff --git a/clang/lib/Frontend/FrontendActions.cpp 
b/clang/lib/Frontend/FrontendActions.cpp
index e6c7b9f32c29b..51e9f2c6b39a7 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -263,6 +263,20 @@ 
GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
                                     /*ForceUseTemporary=*/true);
 }
 
+bool GenerateModuleInterfaceAction::PrepareToExecuteAction(
+    CompilerInstance &CI) {
+  for (const auto &FIF : CI.getFrontendOpts().Inputs) {
+    if (const auto InputFormat = FIF.getKind().getFormat();
+        InputFormat != InputKind::Format::Source) {
+      CI.getDiagnostics().Report(
+          diag::err_frontend_action_unsupported_input_format)
+          << "module interface compilation" << FIF.getFile() << InputFormat;
+      return false;
+    }
+  }
+  return GenerateModuleAction::PrepareToExecuteAction(CI);
+}
+
 bool GenerateModuleInterfaceAction::BeginSourceFileAction(
     CompilerInstance &CI) {
   CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
diff --git a/clang/test/Modules/emit-module-interface-pcm-input.cpp 
b/clang/test/Modules/emit-module-interface-pcm-input.cpp
new file mode 100644
index 0000000000000..9df7222068d20
--- /dev/null
+++ b/clang/test/Modules/emit-module-interface-pcm-input.cpp
@@ -0,0 +1,13 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: cd %t
+
+// Related to issue #132692
+// Verify that clang_cc1 doesn't crash when trying to generate a module
+// interface from an alreay precompiled module (`.pcm`).
+// RUN: %clang_cc1 -std=c++20 -emit-module-interface a.cppm -o a.pcm
+// RUN: not %clang_cc1 -std=c++20 -emit-module-interface a.pcm
+// RUN: not %clang_cc1 -std=c++20 -emit-reduced-module-interface a.pcm
+
+//--- a.cppm
+export module A;

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to