-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Clang] Fix crash when building a module with CC_PRINT_HEADERS_FORMAT=json #136227
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
…=json There is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing.
@llvm/pr-subscribers-clang Author: Bob Wilson (bob-wilson) ChangesThere is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing. Full diff: https://github.com/llvm/llvm-project/pull/136227.diff 2 Files Affected:
diff --git a/clang/lib/Frontend/HeaderIncludeGen.cpp b/clang/lib/Frontend/HeaderIncludeGen.cpp
index 992c2670260e5..792526083b1e6 100644
--- a/clang/lib/Frontend/HeaderIncludeGen.cpp
+++ b/clang/lib/Frontend/HeaderIncludeGen.cpp
@@ -260,8 +260,11 @@ void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile, const
void HeaderIncludesJSONCallback::EndOfMainFile() {
OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID());
- SmallString<256> MainFile(FE->getName());
- SM.getFileManager().makeAbsolutePath(MainFile);
+ SmallString<256> MainFile;
+ if (FE) {
+ MainFile += FE->getName();
+ SM.getFileManager().makeAbsolutePath(MainFile);
+ }
std::string Str;
llvm::raw_string_ostream OS(Str);
diff --git a/clang/test/Preprocessor/print-header-crash.modulemap b/clang/test/Preprocessor/print-header-crash.modulemap
new file mode 100644
index 0000000000000..5919c70780a95
--- /dev/null
+++ b/clang/test/Preprocessor/print-header-crash.modulemap
@@ -0,0 +1,2 @@
+// RUN: %clang_cc1 -header-include-format=json -header-include-filtering=only-direct-system -header-include-file %t.txt -emit-module -x c -fmodules -fmodule-name=X %s -o /dev/null
+module X {}
|
Since this is trying to make |
I'm still investigating what to do for modules. It's not yet clear if that should be handled along with |
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, LGTM as a targeted fix.
…=json (llvm#136227) There is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing.
…=json (llvm#136227) There is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing.
…=json (llvm#136227) There is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing.
…=json (llvm#136227) There is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing.
…=json (llvm#136227) There is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing.
There is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing.