Skip to content

[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

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticFrontendKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Frontend/FrontendActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions clang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions clang/test/Modules/emit-module-interface-pcm-input.cpp
Original file line number Diff line number Diff line change
@@ -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;