[clang] [Clang] Fix crash when building a module with CC_PRINT_HEADERS_FORMAT=json (PR #136227)
Bob Wilson via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 17 16:39:28 PDT 2025
https://github.com/bob-wilson created https://github.com/llvm/llvm-project/pull/136227
There is no main file when building a module, so the code in HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to avoid crashing.
>From 160e490852d9bcb85da0cf398a72698438ec5e77 Mon Sep 17 00:00:00 2001
From: Bob Wilson <bob.wilson at apple.com>
Date: Thu, 17 Apr 2025 16:18:59 -0700
Subject: [PATCH] [Clang] Fix crash when building a module with
CC_PRINT_HEADERS_FORMAT=json
There is no main file when building a module, so the code in
HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that
to avoid crashing.
---
clang/lib/Frontend/HeaderIncludeGen.cpp | 7 +++++--
clang/test/Preprocessor/print-header-crash.modulemap | 2 ++
2 files changed, 7 insertions(+), 2 deletions(-)
create mode 100644 clang/test/Preprocessor/print-header-crash.modulemap
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 {}
More information about the cfe-commits
mailing list