[clang] 68bfb17 - [Clang] Fix crash when building a module with CC_PRINT_HEADERS_FORMAT=json (#136227)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 22 10:27:10 PDT 2025


Author: Bob Wilson
Date: 2025-04-22T10:27:07-07:00
New Revision: 68bfb17f1dd15f3b0b2754c8bce1d1c395e553c1

URL: https://github.com/llvm/llvm-project/commit/68bfb17f1dd15f3b0b2754c8bce1d1c395e553c1
DIFF: https://github.com/llvm/llvm-project/commit/68bfb17f1dd15f3b0b2754c8bce1d1c395e553c1.diff

LOG: [Clang] Fix crash when building a module with CC_PRINT_HEADERS_FORMAT=json (#136227)

There is no main file when building a module, so the code in
HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that to
avoid crashing.

Added: 
    clang/test/Preprocessor/print-header-crash.modulemap

Modified: 
    clang/lib/Frontend/HeaderIncludeGen.cpp

Removed: 
    


################################################################################
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