Author: Haojian Wu
Date: 2025-03-28T15:31:21+01:00
New Revision: db04c3e4b3d9b79d99c5bbeb7fffaa887cd4352e

URL: 
https://github.com/llvm/llvm-project/commit/db04c3e4b3d9b79d99c5bbeb7fffaa887cd4352e
DIFF: 
https://github.com/llvm/llvm-project/commit/db04c3e4b3d9b79d99c5bbeb7fffaa887cd4352e.diff

LOG: [clang] Implement some missing interfaces for 
DelegatingDeserializationListener (#133424)

Split from the https://github.com/llvm/llvm-project/pull/133395 per the
review comment.

This patch also moves the `DelegatingDeserializationListener` close to
`ASTDeserializationListener`.

Added: 
    

Modified: 
    clang/include/clang/Serialization/ASTDeserializationListener.h
    clang/lib/Frontend/FrontendAction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Serialization/ASTDeserializationListener.h 
b/clang/include/clang/Serialization/ASTDeserializationListener.h
index ea96faa07c191..7728ce04856f5 100644
--- a/clang/include/clang/Serialization/ASTDeserializationListener.h
+++ b/clang/include/clang/Serialization/ASTDeserializationListener.h
@@ -27,6 +27,8 @@ class MacroInfo;
 class Module;
 class SourceLocation;
 
+// IMPORTANT: when you add a new interface to this class, please update the
+// DelegatingDeserializationListener below.
 class ASTDeserializationListener {
 public:
   virtual ~ASTDeserializationListener();
@@ -44,6 +46,11 @@ class ASTDeserializationListener {
   ///        unqualified.
   virtual void TypeRead(serialization::TypeIdx Idx, QualType T) { }
   /// A decl was deserialized from the AST file.
+  //
+  // Note: Implementors should be cautious when introducing additional
+  // serialization (e.g., printing the qualified name of the declaration) 
within
+  // the callback. Doing so may lead to unintended and complex side effects, or
+  // even cause a crash.
   virtual void DeclRead(GlobalDeclID ID, const Decl *D) {}
   /// A predefined decl was built during the serialization.
   virtual void PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) {}
@@ -58,6 +65,70 @@ class ASTDeserializationListener {
   virtual void ModuleImportRead(serialization::SubmoduleID ID,
                                 SourceLocation ImportLoc) {}
 };
-}
+
+class DelegatingDeserializationListener : public ASTDeserializationListener {
+  ASTDeserializationListener *Previous;
+  bool DeletePrevious;
+
+public:
+  explicit DelegatingDeserializationListener(
+      ASTDeserializationListener *Previous, bool DeletePrevious)
+      : Previous(Previous), DeletePrevious(DeletePrevious) {}
+  ~DelegatingDeserializationListener() override {
+    if (DeletePrevious)
+      delete Previous;
+  }
+
+  DelegatingDeserializationListener(const DelegatingDeserializationListener &) 
=
+      delete;
+  DelegatingDeserializationListener &
+  operator=(const DelegatingDeserializationListener &) = delete;
+
+  void ReaderInitialized(ASTReader *Reader) override {
+    if (Previous)
+      Previous->ReaderInitialized(Reader);
+  }
+  void IdentifierRead(serialization::IdentifierID ID,
+                      IdentifierInfo *II) override {
+    if (Previous)
+      Previous->IdentifierRead(ID, II);
+  }
+  void MacroRead(serialization::MacroID ID, MacroInfo *MI) override {
+    if (Previous)
+      Previous->MacroRead(ID, MI);
+  }
+  void TypeRead(serialization::TypeIdx Idx, QualType T) override {
+    if (Previous)
+      Previous->TypeRead(Idx, T);
+  }
+  void DeclRead(GlobalDeclID ID, const Decl *D) override {
+    if (Previous)
+      Previous->DeclRead(ID, D);
+  }
+  void PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) override {
+    if (Previous)
+      Previous->PredefinedDeclBuilt(ID, D);
+  }
+  void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
+    if (Previous)
+      Previous->SelectorRead(ID, Sel);
+  }
+  void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
+                           MacroDefinitionRecord *MD) override {
+    if (Previous)
+      Previous->MacroDefinitionRead(PPID, MD);
+  }
+  void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override {
+    if (Previous)
+      Previous->ModuleRead(ID, Mod);
+  }
+  void ModuleImportRead(serialization::SubmoduleID ID,
+                        SourceLocation ImportLoc) override {
+    if (Previous)
+      Previous->ModuleImportRead(ID, ImportLoc);
+  }
+};
+
+} // namespace clang
 
 #endif

diff  --git a/clang/lib/Frontend/FrontendAction.cpp 
b/clang/lib/Frontend/FrontendAction.cpp
index 9f789f093f55d..f6ad7c8dbd7ca 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -49,52 +49,6 @@ LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
 
 namespace {
 
-class DelegatingDeserializationListener : public ASTDeserializationListener {
-  ASTDeserializationListener *Previous;
-  bool DeletePrevious;
-
-public:
-  explicit DelegatingDeserializationListener(
-      ASTDeserializationListener *Previous, bool DeletePrevious)
-      : Previous(Previous), DeletePrevious(DeletePrevious) {}
-  ~DelegatingDeserializationListener() override {
-    if (DeletePrevious)
-      delete Previous;
-  }
-
-  DelegatingDeserializationListener(const DelegatingDeserializationListener &) 
=
-      delete;
-  DelegatingDeserializationListener &
-  operator=(const DelegatingDeserializationListener &) = delete;
-
-  void ReaderInitialized(ASTReader *Reader) override {
-    if (Previous)
-      Previous->ReaderInitialized(Reader);
-  }
-  void IdentifierRead(serialization::IdentifierID ID,
-                      IdentifierInfo *II) override {
-    if (Previous)
-      Previous->IdentifierRead(ID, II);
-  }
-  void TypeRead(serialization::TypeIdx Idx, QualType T) override {
-    if (Previous)
-      Previous->TypeRead(Idx, T);
-  }
-  void DeclRead(GlobalDeclID ID, const Decl *D) override {
-    if (Previous)
-      Previous->DeclRead(ID, D);
-  }
-  void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
-    if (Previous)
-      Previous->SelectorRead(ID, Sel);
-  }
-  void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
-                           MacroDefinitionRecord *MD) override {
-    if (Previous)
-      Previous->MacroDefinitionRead(PPID, MD);
-  }
-};
-
 /// Dumps deserialized declarations.
 class DeserializedDeclsDumper : public DelegatingDeserializationListener {
 public:


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to