-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][modules] Validate input file format for GenerateModuleInterfaceAction (#132692) #137711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][modules] Validate input file format for GenerateModuleInterfaceAction (#132692) #137711
Conversation
…ceAction (llvm#132692) Fixes llvm#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.
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: Naveen Seth Hanig (naveen-seth) ChangesFixes #132692.
Full diff: https://github.com/llvm/llvm-project/pull/137711.diff 4 Files Affected:
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;
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…ceAction (llvm#132692) (llvm#137711) Fixes llvm#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.
…ceAction (llvm#132692) (llvm#137711) Fixes llvm#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.
…ceAction (llvm#132692) (llvm#137711) Fixes llvm#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.
…ceAction (llvm#132692) (llvm#137711) Fixes llvm#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.
…ceAction (llvm#132692) (llvm#137711) Fixes llvm#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.
…ceAction (llvm#132692) (llvm#137711) Fixes llvm#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.
Fixes #132692.
clang -cc1
crashes when generating a module interface withemit-module-interface
oremit-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.