[clang] [clang][modules] Validate input file format for GenerateModuleInterfaceAction (#132692) (PR #137711)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 28 14:14:14 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Naveen Seth Hanig (naveen-seth)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/137711.diff
4 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticFrontendKinds.td (+4)
- (modified) clang/include/clang/Frontend/FrontendActions.h (+1)
- (modified) clang/lib/Frontend/FrontendActions.cpp (+14)
- (added) clang/test/Modules/emit-module-interface-pcm-input.cpp (+13)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/137711
More information about the cfe-commits
mailing list