Skip to content

[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

Merged
merged 1 commit into from
Apr 22, 2025

Conversation

bob-wilson
Copy link
Contributor

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

…=json

There is no main file when building a module, so the code in
HeaderIncludesJSONCallback::EndOfMainFile() needs to check for that
to avoid crashing.
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Apr 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 17, 2025

@llvm/pr-subscribers-clang

Author: Bob Wilson (bob-wilson)

Changes

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 --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 {}

@jansvoboda11
Copy link
Contributor

Since this is trying to make CC_PRINT_HEADERS_FORMAT work for modules, I have to ask: should it report module map files too?

@bob-wilson
Copy link
Contributor Author

I'm still investigating what to do for modules. It's not yet clear if that should be handled along with CC_PRINT_HEADERS_FORMAT=json or some new option. I figured it would be good to start by fixing the crashes.

Copy link
Contributor

@jansvoboda11 jansvoboda11 left a 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.

@jansvoboda11 jansvoboda11 merged commit 68bfb17 into llvm:main Apr 22, 2025
13 checks passed
@bob-wilson bob-wilson deleted the header-include-json-crash branch April 23, 2025 23:50
bob-wilson added a commit to swiftlang/llvm-project that referenced this pull request Apr 29, 2025
…=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.
cyndyishida pushed a commit to swiftlang/llvm-project that referenced this pull request Apr 30, 2025
…=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.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…=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.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…=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.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
…=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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants