Skip to content

Commit 517f66d

Browse files
committed
Fix the crash when dumping the deserialized decls
1 parent 420c056 commit 517f66d

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

clang/docs/ReleaseNotes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ Bug Fixes in This Version
316316
- Fixed a modules crash where exception specifications were not propagated properly (#GH121245, relanded in #GH129982)
317317
- Fixed a problematic case with recursive deserialization within ``FinishedDeserializing()`` where
318318
``PassInterestingDeclsToConsumer()`` was called before the declarations were safe to be passed. (#GH129982)
319+
- Fixed a module crash with `-dump-deserialized-decls`
319320

320321
Bug Fixes to Compiler Builtins
321322
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Serialization/ASTDeserializationListener.h

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class MacroInfo;
2727
class Module;
2828
class SourceLocation;
2929

30+
// IMPORTANT: when you add a new interface to this class, please update the
31+
// DelegatingDeserializationListener in FrontendAction.cpp
3032
class ASTDeserializationListener {
3133
public:
3234
virtual ~ASTDeserializationListener();
@@ -57,6 +59,8 @@ class ASTDeserializationListener {
5759
/// A module import was read from the AST file.
5860
virtual void ModuleImportRead(serialization::SubmoduleID ID,
5961
SourceLocation ImportLoc) {}
62+
/// The deserialization of the AST file was finished.
63+
virtual void FinishedDeserializing() {}
6064
};
6165
}
6266

clang/lib/Frontend/FrontendAction.cpp

+38-6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class DelegatingDeserializationListener : public ASTDeserializationListener {
7676
if (Previous)
7777
Previous->IdentifierRead(ID, II);
7878
}
79+
void MacroRead(serialization::MacroID ID, MacroInfo *MI) override {
80+
if (Previous)
81+
Previous->MacroRead(ID, MI);
82+
}
7983
void TypeRead(serialization::TypeIdx Idx, QualType T) override {
8084
if (Previous)
8185
Previous->TypeRead(Idx, T);
@@ -93,6 +97,19 @@ class DelegatingDeserializationListener : public ASTDeserializationListener {
9397
if (Previous)
9498
Previous->MacroDefinitionRead(PPID, MD);
9599
}
100+
void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override {
101+
if (Previous)
102+
Previous->ModuleRead(ID, Mod);
103+
}
104+
void ModuleImportRead(serialization::SubmoduleID ID,
105+
SourceLocation ImportLoc) override {
106+
if (Previous)
107+
Previous->ModuleImportRead(ID, ImportLoc);
108+
}
109+
void FinishedDeserializing() override {
110+
if (Previous)
111+
Previous->FinishedDeserializing();
112+
}
96113
};
97114

98115
/// Dumps deserialized declarations.
@@ -103,15 +120,30 @@ class DeserializedDeclsDumper : public DelegatingDeserializationListener {
103120
: DelegatingDeserializationListener(Previous, DeletePrevious) {}
104121

105122
void DeclRead(GlobalDeclID ID, const Decl *D) override {
106-
llvm::outs() << "PCH DECL: " << D->getDeclKindName();
107-
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
108-
llvm::outs() << " - ";
109-
ND->printQualifiedName(llvm::outs());
123+
PendingDecls.push_back(D);
124+
DelegatingDeserializationListener::DeclRead(ID, D);
125+
}
126+
void FinishedDeserializing() override {
127+
auto Decls = std::move(PendingDecls);
128+
for (const auto *D : Decls) {
129+
llvm::outs() << "PCH DECL: " << D->getDeclKindName();
130+
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
131+
llvm::outs() << " - ";
132+
ND->printQualifiedName(llvm::outs());
133+
}
134+
llvm::outs() << "\n";
110135
}
111-
llvm::outs() << "\n";
112136

113-
DelegatingDeserializationListener::DeclRead(ID, D);
137+
if (!PendingDecls.empty()) {
138+
llvm::errs() << "Deserialized more decls while printing, total of "
139+
<< PendingDecls.size() << "\n";
140+
PendingDecls.clear();
141+
}
142+
DelegatingDeserializationListener::FinishedDeserializing();
114143
}
144+
145+
private:
146+
std::vector<const Decl *> PendingDecls;
115147
};
116148

117149
/// Checks deserialized declarations and emits error if a name

clang/lib/Serialization/ASTReader.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -10877,6 +10877,8 @@ void ASTReader::FinishedDeserializing() {
1087710877
// decls to the consumer.
1087810878
if (Consumer)
1087910879
PassInterestingDeclsToConsumer();
10880+
if (DeserializationListener)
10881+
DeserializationListener->FinishedDeserializing();
1088010882
}
1088110883
}
1088210884

0 commit comments

Comments
 (0)