[clang][modules] Fix crash in enum visibility lookup for C++20 header units#166272
Merged
ChuanqiXu9 merged 1 commit intollvm:mainfrom Nov 4, 2025
Merged
Conversation
… units Fixes llvm#165445. Fixes a crash when ASTWriter::GenerateNameLookupTable processes enum constants from C++20 header units. The special handling for enum constants, introduced in fccc6ee, doesn't account for declarations whose owning module is a C++20 header unit. It calls isNamedModule() on the result of getTopLevelOwningNamedModule(), which returns null for header units, causing a null pointer dereference.
Member
|
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: Naveen Seth Hanig (naveen-seth) ChangesFixes #165445. Fixes a crash when The special handling for enum constants, introduced in fccc6ee, doesn't account for declarations whose owning module is a C++20 header unit. It calls 2 Files Affected:
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 3ac338e013deb..b1fd151790d96 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -4374,8 +4374,7 @@ class ASTDeclContextNameLookupTrait
// parent of parent. We DON'T remove the enum constant from its parent. So
// we don't need to care about merging problems here.
if (auto *ECD = dyn_cast<EnumConstantDecl>(D);
- ECD && DC.isFileContext() && ECD->getOwningModule() &&
- ECD->getTopLevelOwningNamedModule()->isNamedModule()) {
+ ECD && DC.isFileContext() && ECD->getTopLevelOwningNamedModule()) {
if (llvm::all_of(
DC.noload_lookup(
cast<EnumDecl>(ECD->getDeclContext())->getDeclName()),
diff --git a/clang/test/Modules/crash-enum-visibility-with-header-unit.cppm b/clang/test/Modules/crash-enum-visibility-with-header-unit.cppm
new file mode 100644
index 0000000000000..90c57796dcf7e
--- /dev/null
+++ b/clang/test/Modules/crash-enum-visibility-with-header-unit.cppm
@@ -0,0 +1,46 @@
+// Fixes #165445
+
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %clang_cc1 -std=c++20 -x c++-user-header %t/header.h \
+// RUN: -emit-header-unit -o %t/header.pcm
+//
+// RUN: %clang_cc1 -std=c++20 %t/A.cppm -fmodule-file=%t/header.pcm \
+// RUN: -emit-module-interface -o %t/A.pcm
+//
+// RUN: %clang_cc1 -std=c++20 %t/B.cppm -fmodule-file=%t/header.pcm \
+// RUN: -emit-module-interface -o %t/B.pcm
+//
+// RUN: %clang_cc1 -std=c++20 %t/use.cpp \
+// RUN: -fmodule-file=A=%t/A.pcm -fmodule-file=B=%t/B.pcm \
+// RUN: -fmodule-file=%t/header.pcm \
+// RUN: -verify -fsyntax-only
+
+//--- enum.h
+enum E { Value };
+
+//--- header.h
+#include "enum.h"
+
+//--- A.cppm
+module;
+#include "enum.h"
+export module A;
+
+auto e = Value;
+
+//--- B.cppm
+export module B;
+import "header.h";
+
+auto e = Value;
+
+//--- use.cpp
+// expected-no-diagnostics
+import A;
+import B;
+#include "enum.h"
+
+auto e = Value;
|
ChuanqiXu9
approved these changes
Nov 4, 2025
Member
|
/cherry-pick bc08e69 |
Member
|
/pull-request #166701 |
dyung
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Nov 12, 2025
… units (llvm#166272) Fixes llvm#165445. Fixes a crash when `ASTWriter::GenerateNameLookupTable` processes enum constants from C++20 header units. The special handling for enum constants, introduced in fccc6ee, doesn't account for declarations whose owning module is a C++20 header unit. It calls `isNamedModule()` on the result of `getTopLevelOwningNamedModule()`, which returns null for header units, causing a null pointer dereference. (cherry picked from commit bc08e69)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #165445.
Fixes a crash when
ASTWriter::GenerateNameLookupTableprocesses enum constants from C++20 header units.The special handling for enum constants, introduced in fccc6ee, doesn't account for declarations whose owning module is a C++20 header unit. It calls
isNamedModule()on the result ofgetTopLevelOwningNamedModule(), which returns null for header units, causing a null pointer dereference.