Skip to content

Commit a9e249f

Browse files
authored
[C++20][Modules][Serialization] Delay marking pending incomplete decl chains until the end of finishPendingActions. (#121245)
The call to `hasBody` inside `finishPendingActions` that bumps the `PendingIncompleteDeclChains` size from `0` to `1`, and also sets the `LazyVal->LastGeneration` to `6` which matches the `LazyVal->ExternalSource->getGeneration()` value of `6`. Later, the iterations over `redecls()` (which calls `getNextRedeclaration`) is expected to trigger the reload, but it **does not** since the generation numbers match. The proposed solution is to perform the marking of incomplete decl chains at the end of `finishPendingActions`. This way, **all** of the incomplete decls are marked incomplete as a post-condition of `finishPendingActions`. It's also safe to delay this operation since any operation being done within `finishPendingActions` has `NumCurrentElementsDeserializing == 1`, which means that any calls to `CompleteDeclChain` would simply add to the `PendingIncompleteDeclChains` without doing anything anyway.
1 parent fc3ec13 commit a9e249f

File tree

2 files changed

+105
-13
lines changed

2 files changed

+105
-13
lines changed

clang/lib/Serialization/ASTReader.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -10186,12 +10186,12 @@ void ASTReader::visitTopLevelModuleMaps(
1018610186
}
1018710187

1018810188
void ASTReader::finishPendingActions() {
10189-
while (
10190-
!PendingIdentifierInfos.empty() || !PendingDeducedFunctionTypes.empty() ||
10191-
!PendingDeducedVarTypes.empty() || !PendingIncompleteDeclChains.empty() ||
10192-
!PendingDeclChains.empty() || !PendingMacroIDs.empty() ||
10193-
!PendingDeclContextInfos.empty() || !PendingUpdateRecords.empty() ||
10194-
!PendingObjCExtensionIvarRedeclarations.empty()) {
10189+
while (!PendingIdentifierInfos.empty() ||
10190+
!PendingDeducedFunctionTypes.empty() ||
10191+
!PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||
10192+
!PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
10193+
!PendingUpdateRecords.empty() ||
10194+
!PendingObjCExtensionIvarRedeclarations.empty()) {
1019510195
// If any identifiers with corresponding top-level declarations have
1019610196
// been loaded, load those declarations now.
1019710197
using TopLevelDeclsMap =
@@ -10239,13 +10239,6 @@ void ASTReader::finishPendingActions() {
1023910239
}
1024010240
PendingDeducedVarTypes.clear();
1024110241

10242-
// For each decl chain that we wanted to complete while deserializing, mark
10243-
// it as "still needs to be completed".
10244-
for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
10245-
markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
10246-
}
10247-
PendingIncompleteDeclChains.clear();
10248-
1024910242
// Load pending declaration chains.
1025010243
for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
1025110244
loadPendingDeclChain(PendingDeclChains[I].first,
@@ -10483,6 +10476,12 @@ void ASTReader::finishPendingActions() {
1048310476
for (auto *ND : PendingMergedDefinitionsToDeduplicate)
1048410477
getContext().deduplicateMergedDefinitonsFor(ND);
1048510478
PendingMergedDefinitionsToDeduplicate.clear();
10479+
10480+
// For each decl chain that we wanted to complete while deserializing, mark
10481+
// it as "still needs to be completed".
10482+
for (Decl *D : PendingIncompleteDeclChains)
10483+
markIncompleteDeclChain(D);
10484+
PendingIncompleteDeclChains.clear();
1048610485
}
1048710486

1048810487
void ASTReader::diagnoseOdrViolations() {

clang/test/Modules/pr121245.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// If this test fails, it should be investigated under Debug builds.
2+
// Before the PR, this test was encountering an `llvm_unreachable()`.
3+
4+
// RUN: rm -rf %t
5+
// RUN: mkdir -p %t
6+
// RUN: split-file %s %t
7+
// RUN: cd %t
8+
9+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-01.h \
10+
// RUN: -fcxx-exceptions -o %t/hu-01.pcm
11+
12+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-02.h \
13+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
14+
// RUN: -fmodule-file=%t/hu-01.pcm -o %t/hu-02.pcm
15+
16+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-03.h \
17+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
18+
// RUN: -fmodule-file=%t/hu-01.pcm -o %t/hu-03.pcm
19+
20+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-04.h \
21+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
22+
// RUN: -fmodule-file=%t/hu-01.pcm -o %t/hu-04.pcm
23+
24+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header %t/hu-05.h \
25+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
26+
// RUN: -fmodule-file=%t/hu-03.pcm -fmodule-file=%t/hu-04.pcm \
27+
// RUN: -fmodule-file=%t/hu-01.pcm -o %t/hu-05.pcm
28+
29+
// RUN: %clang_cc1 -std=c++20 -emit-obj %t/main.cpp \
30+
// RUN: -Wno-experimental-header-units -fcxx-exceptions \
31+
// RUN: -fmodule-file=%t/hu-02.pcm -fmodule-file=%t/hu-05.pcm \
32+
// RUN: -fmodule-file=%t/hu-04.pcm -fmodule-file=%t/hu-03.pcm \
33+
// RUN: -fmodule-file=%t/hu-01.pcm
34+
35+
//--- hu-01.h
36+
template <typename T>
37+
struct A {
38+
A() {}
39+
~A() {}
40+
};
41+
42+
template <typename T>
43+
struct EBO : T {
44+
EBO() = default;
45+
};
46+
47+
template <typename T>
48+
struct HT : EBO<A<T>> {};
49+
50+
//--- hu-02.h
51+
import "hu-01.h";
52+
53+
inline void f() {
54+
HT<int>();
55+
}
56+
57+
//--- hu-03.h
58+
import "hu-01.h";
59+
60+
struct C {
61+
C();
62+
63+
HT<long> _;
64+
};
65+
66+
//--- hu-04.h
67+
import "hu-01.h";
68+
69+
void g(HT<long> = {});
70+
71+
//--- hu-05.h
72+
import "hu-03.h";
73+
import "hu-04.h";
74+
import "hu-01.h";
75+
76+
struct B {
77+
virtual ~B() = default;
78+
79+
virtual void f() {
80+
HT<long>();
81+
}
82+
};
83+
84+
//--- main.cpp
85+
import "hu-02.h";
86+
import "hu-05.h";
87+
import "hu-03.h";
88+
89+
int main() {
90+
f();
91+
C();
92+
B();
93+
}

0 commit comments

Comments
 (0)