Skip to content

Commit 7ce0f5a

Browse files
authored
[clang][modules] Validate input file format for GenerateModuleInterfaceAction (#132692) (#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.
1 parent a37e475 commit 7ce0f5a

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ def err_modules_embed_file_not_found :
260260
DefaultFatal;
261261
def err_module_header_file_not_found :
262262
Error<"module header file '%0' not found">, DefaultFatal;
263+
def err_frontend_action_unsupported_input_format
264+
: Error<"%0 does not support input file format of file '%1': "
265+
"'%select{Source|ModuleMap|Precompiled|Unknown}2'">,
266+
DefaultFatal;
263267

264268
def err_test_module_file_extension_version : Error<
265269
"test module file extension '%0' has different version (%1.%2) than expected "

clang/include/clang/Frontend/FrontendActions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
156156
/// files) for C++20 Named Modules.
157157
class GenerateModuleInterfaceAction : public GenerateModuleAction {
158158
protected:
159+
bool PrepareToExecuteAction(CompilerInstance &CI) override;
159160
bool BeginSourceFileAction(CompilerInstance &CI) override;
160161

161162
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
263263
/*ForceUseTemporary=*/true);
264264
}
265265

266+
bool GenerateModuleInterfaceAction::PrepareToExecuteAction(
267+
CompilerInstance &CI) {
268+
for (const auto &FIF : CI.getFrontendOpts().Inputs) {
269+
if (const auto InputFormat = FIF.getKind().getFormat();
270+
InputFormat != InputKind::Format::Source) {
271+
CI.getDiagnostics().Report(
272+
diag::err_frontend_action_unsupported_input_format)
273+
<< "module interface compilation" << FIF.getFile() << InputFormat;
274+
return false;
275+
}
276+
}
277+
return GenerateModuleAction::PrepareToExecuteAction(CI);
278+
}
279+
266280
bool GenerateModuleInterfaceAction::BeginSourceFileAction(
267281
CompilerInstance &CI) {
268282
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: cd %t
4+
5+
// Related to issue #132692
6+
// Verify that clang_cc1 doesn't crash when trying to generate a module
7+
// interface from an alreay precompiled module (`.pcm`).
8+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface a.cppm -o a.pcm
9+
// RUN: not %clang_cc1 -std=c++20 -emit-module-interface a.pcm
10+
// RUN: not %clang_cc1 -std=c++20 -emit-reduced-module-interface a.pcm
11+
12+
//--- a.cppm
13+
export module A;

0 commit comments

Comments
 (0)