[clang] [Clang] Fix crash when building a module with CC_PRINT_HEADERS_FORMAT=json (PR #136227)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 17 16:40:00 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Bob Wilson (bob-wilson)
<details>
<summary>Changes</summary>
There 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:
- (modified) clang/lib/Frontend/HeaderIncludeGen.cpp (+5-2)
- (added) clang/test/Preprocessor/print-header-crash.modulemap (+2)
``````````diff
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 {}
``````````
</details>
https://github.com/llvm/llvm-project/pull/136227
More information about the cfe-commits
mailing list