-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-clang Author: Dmitriy Chestnykh (chestnykh) ChangesCurrently clang crashes with Full diff: https://github.com/llvm/llvm-project/pull/98439.diff 3 Files Affected:
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85c32e55bdab3..029ae6457699e 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -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">, 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'">;
diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp
index 4f064321997a2..ddefa07569c55 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -841,9 +841,15 @@ 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;
diff --git a/clang/test/Frontend/module-file-info-not-a-module.c b/clang/test/Frontend/module-file-info-not-a-module.c
new file mode 100644
index 0000000000000..67eba2a27051b
--- /dev/null
+++ b/clang/test/Frontend/module-file-info-not-a-module.c
@@ -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
|
CC: @ChuanqiXu9 @shafik |
Currently clang crashes with `-module-file-info` and input file which is not a module Emit error instead of segfaulting. Fix llvm#98365
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -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">, DefaultFatal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def err_file_is_not_module : Error<"file '%0' is not a module">, DefaultFatal; | |
def err_file_is_not_module : Error<"file '%0' is not a module file">, DefaultFatal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Extended the description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChuanqiXu9 FYI i don't have commit access so when PR is ready please commit it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll commit it.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/123/builds/1610 Here is the relevant piece of the build log for the reference:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this fix!
I don't think the build error is related to this change. |
Currently clang crashes with `-module-file-info` and input file which is not a module Emit error instead of segfaulting. Fix llvm#98365
Currently clang crashes with
-module-file-info
and input file which is not a moduleEmit error instead of segfaulting.
Fix #98365