Skip to content

Commit

Permalink
[Clang] Don't crash if input file is not a module. (#98439)
Browse files Browse the repository at this point in the history
Currently clang crashes with `-module-file-info` and input file which is
not a module
Emit error instead of segfaulting.
Fix #98365
  • Loading branch information
chestnykh authored Jul 11, 2024
1 parent d4e46f0 commit bf4167f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
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

0 comments on commit bf4167f

Please sign in to comment.