Skip to content

[Clang] Don't crash if input file is not a module. #98439

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 3 commits into from
Jul 11, 2024
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
1 change: 1 addition & 0 deletions clang/include/clang/Basic/DiagnosticFrontendKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def err_module_map_not_found : Error<"module map file '%0' not found">,
def err_missing_module_name : Error<
"no module name provided; specify one with -fmodule-name=">,
DefaultFatal;
def err_file_is_not_module : Error<"file '%0' is not a module file">, DefaultFatal;
def err_missing_module : Error<
"no module named '%0' declared in module map file '%1'">, DefaultFatal;
def err_no_submodule : Error<"no submodule named %0 in module '%1'">;
Expand Down
11 changes: 9 additions & 2 deletions clang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,16 @@ static StringRef ModuleKindName(Module::ModuleKind MK) {
}

void DumpModuleInfoAction::ExecuteAction() {
assert(isCurrentFileAST() && "dumping non-AST?");
// Set up the output file.
CompilerInstance &CI = getCompilerInstance();

// Don't process files of type other than module to avoid crash
if (!isCurrentFileAST()) {
CI.getDiagnostics().Report(diag::err_file_is_not_module)
<< getCurrentFile();
return;
}

// Set up the output file.
StringRef OutputFileName = CI.getFrontendOpts().OutputFile;
if (!OutputFileName.empty() && OutputFileName != "-") {
std::error_code EC;
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Frontend/module-file-info-not-a-module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// RUN: not %clang_cc1 -module-file-info %s 2>&1 | FileCheck %s

// CHECK: fatal error: file '{{.*}}module-file-info-not-a-module.c' is not a module file
Loading