Skip to content

Commit c218323

Browse files
bob-wilsonIanWood1
authored andcommitted
[Clang] Fix crash when building a module with CC_PRINT_HEADERS_FORMAT=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.
1 parent 05f26df commit c218323

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

clang/lib/Frontend/HeaderIncludeGen.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,11 @@ void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile, const
260260

261261
void HeaderIncludesJSONCallback::EndOfMainFile() {
262262
OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID());
263-
SmallString<256> MainFile(FE->getName());
264-
SM.getFileManager().makeAbsolutePath(MainFile);
263+
SmallString<256> MainFile;
264+
if (FE) {
265+
MainFile += FE->getName();
266+
SM.getFileManager().makeAbsolutePath(MainFile);
267+
}
265268

266269
std::string Str;
267270
llvm::raw_string_ostream OS(Str);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// 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
2+
module X {}

0 commit comments

Comments
 (0)