Skip to content

Commit bf4167f

Browse files
authored
[Clang] Don't crash if input file is not a module. (#98439)
Currently clang crashes with `-module-file-info` and input file which is not a module Emit error instead of segfaulting. Fix #98365
1 parent d4e46f0 commit bf4167f

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def err_module_map_not_found : Error<"module map file '%0' not found">,
226226
def err_missing_module_name : Error<
227227
"no module name provided; specify one with -fmodule-name=">,
228228
DefaultFatal;
229+
def err_file_is_not_module : Error<"file '%0' is not a module file">, DefaultFatal;
229230
def err_missing_module : Error<
230231
"no module named '%0' declared in module map file '%1'">, DefaultFatal;
231232
def err_no_submodule : Error<"no submodule named %0 in module '%1'">;

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,16 @@ static StringRef ModuleKindName(Module::ModuleKind MK) {
841841
}
842842

843843
void DumpModuleInfoAction::ExecuteAction() {
844-
assert(isCurrentFileAST() && "dumping non-AST?");
845-
// Set up the output file.
846844
CompilerInstance &CI = getCompilerInstance();
845+
846+
// Don't process files of type other than module to avoid crash
847+
if (!isCurrentFileAST()) {
848+
CI.getDiagnostics().Report(diag::err_file_is_not_module)
849+
<< getCurrentFile();
850+
return;
851+
}
852+
853+
// Set up the output file.
847854
StringRef OutputFileName = CI.getFrontendOpts().OutputFile;
848855
if (!OutputFileName.empty() && OutputFileName != "-") {
849856
std::error_code EC;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// RUN: not %clang_cc1 -module-file-info %s 2>&1 | FileCheck %s
2+
3+
// CHECK: fatal error: file '{{.*}}module-file-info-not-a-module.c' is not a module file

0 commit comments

Comments
 (0)