[clang][modules] Fix crash in enum visibility lookup for C++20 header units (#166272)

Fixes #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.
This commit is contained in:
Naveen Seth Hanig 2025-11-04 07:24:56 +05:30 committed by GitHub
parent a22d1c2225
commit bc08e69959
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 2 deletions

View File

@ -4374,8 +4374,7 @@ private:
// 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()),

View File

@ -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;