Skip to content

Commit bc08e69

Browse files
authored
[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.
1 parent a22d1c2 commit bc08e69

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4374,8 +4374,7 @@ class ASTDeclContextNameLookupTrait
43744374
// parent of parent. We DON'T remove the enum constant from its parent. So
43754375
// we don't need to care about merging problems here.
43764376
if (auto *ECD = dyn_cast<EnumConstantDecl>(D);
4377-
ECD && DC.isFileContext() && ECD->getOwningModule() &&
4378-
ECD->getTopLevelOwningNamedModule()->isNamedModule()) {
4377+
ECD && DC.isFileContext() && ECD->getTopLevelOwningNamedModule()) {
43794378
if (llvm::all_of(
43804379
DC.noload_lookup(
43814380
cast<EnumDecl>(ECD->getDeclContext())->getDeclName()),
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Fixes #165445
2+
3+
// RUN: rm -rf %t
4+
// RUN: mkdir -p %t
5+
// RUN: split-file %s %t
6+
//
7+
// RUN: %clang_cc1 -std=c++20 -x c++-user-header %t/header.h \
8+
// RUN: -emit-header-unit -o %t/header.pcm
9+
//
10+
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -fmodule-file=%t/header.pcm \
11+
// RUN: -emit-module-interface -o %t/A.pcm
12+
//
13+
// RUN: %clang_cc1 -std=c++20 %t/B.cppm -fmodule-file=%t/header.pcm \
14+
// RUN: -emit-module-interface -o %t/B.pcm
15+
//
16+
// RUN: %clang_cc1 -std=c++20 %t/use.cpp \
17+
// RUN: -fmodule-file=A=%t/A.pcm -fmodule-file=B=%t/B.pcm \
18+
// RUN: -fmodule-file=%t/header.pcm \
19+
// RUN: -verify -fsyntax-only
20+
21+
//--- enum.h
22+
enum E { Value };
23+
24+
//--- header.h
25+
#include "enum.h"
26+
27+
//--- A.cppm
28+
module;
29+
#include "enum.h"
30+
export module A;
31+
32+
auto e = Value;
33+
34+
//--- B.cppm
35+
export module B;
36+
import "header.h";
37+
38+
auto e = Value;
39+
40+
//--- use.cpp
41+
// expected-no-diagnostics
42+
import A;
43+
import B;
44+
#include "enum.h"
45+
46+
auto e = Value;

0 commit comments

Comments
 (0)