Author: John Hui
Date: 2026-10-12T10:15:54+01:00
New Revision: 5631c3a2d09c44302473b4435a9258856ecb285e

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

LOG: [Clang][Modules] Add added implicit members to record upon deserialization 
(#161933)

Previously, we were only called RD->addedMember(MD) to notify RD that MD
(an implicit member) was added, but MD is never actually added as a
child of RD. (Interestingly, MD still thinks RD is its parent.)

We end up with a broken AST that Clang itself seems to be robust to, but
causes issues for users of libclang like Swift's ClangImporter.

This patch tries to maintain a well-formed AST by replacing calls to
RD->addedMember(MD) with RD->addHiddenDecl(MD) for member function decls
(addHiddenDecl() internally calls addedMember() to keep member metadata
up to date). Most of the code is there to check that RD doesn't already
contain the same decl as MD, which may be declared in and deserialized
from another module. We fall back to the original behavior of calling
addedMember() for non-function members: it's unclear what scenario leads
to that (if any), nor how we should best handle that.

Note that we call addHiddenDecl() instead of addDecl() because the
latter can sometimes lead to an assertion failure.

rdar://161931408

Added: 
    clang/test/Modules/added-implicit-members.cpp

Modified: 
    clang/lib/Serialization/ASTReader.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Serialization/ASTReader.cpp 
b/clang/lib/Serialization/ASTReader.cpp
index 0a897a49d1165..16ec15179e5b3 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -11048,6 +11048,70 @@ void ASTReader::finishPendingActions() {
 
   // Inform any classes that had members added that they now have more members.
   for (auto [RD, MD] : PendingAddedClassMembers) {
+    // The module we just imported added MD to RD so we should do the same, but
+    // only RD doesn't already contain the same member. This can happen when 
two
+    // modules independent add and serialize the same implicit member.
+
+    // Fast path: we can check for the presence of implicit special members
+    // (default/copy/move constructors + copy/move assignments + destructors)
+    // with RD's needsImplicit*() methods (which are constant time).
+    if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(MD)) {
+      if (!Ctor->isInheritingConstructor()) {
+        if (Ctor->isDefaultConstructor()) {
+          if (RD->needsImplicitDefaultConstructor())
+            RD->addHiddenDecl(MD);
+          continue;
+        }
+        if (Ctor->isCopyConstructor()) {
+          if (RD->needsImplicitCopyConstructor())
+            RD->addHiddenDecl(MD);
+          continue;
+        }
+        if (Ctor->isMoveConstructor()) {
+          if (RD->needsImplicitMoveConstructor())
+            RD->addHiddenDecl(MD);
+          continue;
+        }
+      }
+    } else if (const auto *Mthd = dyn_cast<CXXMethodDecl>(MD)) {
+      if (isa<CXXDestructorDecl>(Mthd)) {
+        if (RD->needsImplicitDestructor())
+          RD->addHiddenDecl(MD);
+        continue;
+      }
+      if (Mthd->isCopyAssignmentOperator()) {
+        if (RD->needsImplicitCopyAssignment())
+          RD->addHiddenDecl(MD);
+        continue;
+      }
+      if (Mthd->isMoveAssignmentOperator()) {
+        if (RD->needsImplicitMoveAssignment())
+          RD->addHiddenDecl(MD);
+        continue;
+      }
+    }
+
+    if (auto *FD = dyn_cast<FunctionDecl>(MD)) {
+      // Slow path: this is some other implicit added member (e.g., inherited
+      // constructor). We need iterate through all of RD's members and compare
+      // their ODR hash against MD's.
+      auto needToAddMD = true;
+      for (auto *RDD : RD->noload_decls()) {
+        if (auto *RDFD = dyn_cast<FunctionDecl>(RDD)) {
+          if (FD->getODRHash() == RDFD->getODRHash()) {
+            needToAddMD = false;
+            break;
+          }
+        }
+      }
+      if (needToAddMD)
+        RD->addHiddenDecl(MD);
+      continue;
+    }
+
+    // This branch is only reachable if MD is not a function decl (which means
+    // it doesn't have an ODR hash to compare). Call addedMember() to preserve
+    // historical behavior.
     RD->addedMember(MD);
   }
   PendingAddedClassMembers.clear();

diff  --git a/clang/test/Modules/added-implicit-members.cpp 
b/clang/test/Modules/added-implicit-members.cpp
new file mode 100644
index 0000000000000..a0c23e038c146
--- /dev/null
+++ b/clang/test/Modules/added-implicit-members.cpp
@@ -0,0 +1,80 @@
+// Check that implicit members that a module adds to a class imported from
+// another module appear as members of that class once deserialized,
+// and appear once even if several modules add the same member.
+
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+// RUN: %clang_cc1 -std=c++17 -fmodules -fimplicit-module-maps \
+// RUN:   -fmodules-cache-path=%t/cache -I %t/include %t/main.cpp \
+// RUN:   -ast-dump-all -ast-dump-filter Copyable \
+// RUN:   | FileCheck %s --check-prefix=COPYABLE \
+// RUN:       --implicit-check-not=CXXConstructorDecl \
+// RUN:       --implicit-check-not=CXXDestructorDecl \
+// RUN:       --implicit-check-not=CXXMethodDecl
+// RUN: %clang_cc1 -std=c++17 -fmodules -fimplicit-module-maps \
+// RUN:   -fmodules-cache-path=%t/cache -I %t/include %t/main.cpp \
+// RUN:   -ast-dump-all -ast-dump-filter Derived \
+// RUN:   | FileCheck %s --check-prefix=DERIVED \
+// RUN:       --implicit-check-not=CXXConstructorDecl
+
+//--- include/module.modulemap
+module A { header "a.h" export * }
+module B { header "b.h" export * }
+module C { header "c.h" export * }
+
+//--- include/a.h
+struct NonTrivial { ~NonTrivial(); };
+
+// All special members are implicit; none are declared in A.
+struct Copyable { NonTrivial m; };
+
+struct Base { Base(int); };
+struct Derived : Base { using Base::Base; };
+
+//--- include/b.h
+#include "a.h"
+inline void useB() {
+  Copyable a;
+  Copyable b(a);
+  Copyable c(static_cast<Copyable &&>(a));
+  b = a;
+  c = static_cast<Copyable &&>(a);
+  Derived d(1);
+}
+
+//--- include/c.h
+#include "a.h"
+inline void useC() {
+  Copyable a;
+  Copyable b(a);
+  Copyable c(static_cast<Copyable &&>(a));
+  b = a;
+  c = static_cast<Copyable &&>(a);
+  Derived d(1);
+}
+
+//--- main.cpp
+#include "b.h"
+#include "c.h"
+
+// COPYABLE-LABEL: Dumping Copyable:
+// COPYABLE-NEXT:  CXXRecordDecl {{.*}} imported in A {{.*}}struct Copyable 
definition
+// COPYABLE:       CXXConstructorDecl {{.*}} implicit {{.*}}Copyable 'void 
(const Copyable &){{.*}}'
+// COPYABLE:       CXXConstructorDecl {{.*}} implicit {{.*}}Copyable 'void 
(Copyable &&){{.*}}'
+// COPYABLE:       CXXMethodDecl {{.*}} implicit {{.*}}operator= 'Copyable 
&(Copyable &&){{.*}}'
+// COPYABLE:       CXXDestructorDecl {{.*}} implicit {{.*}}~Copyable 'void 
(){{.*}}'
+// COPYABLE:       CXXConstructorDecl {{.*}} implicit {{.*}}Copyable 'void 
(){{.*}}'
+// COPYABLE:       CXXMethodDecl {{.*}} implicit {{.*}}operator= 'Copyable 
&(const Copyable &){{.*}}'
+
+// DERIVED-LABEL: Dumping Derived:
+// DERIVED-NEXT:  CXXRecordDecl {{.*}} imported in A {{.*}}struct Derived 
definition
+// Targets of the ConstructorUsingShadowDecls, declared in A.
+// DERIVED:       CXXConstructorDecl {{.*}}Base 'void (int)'
+// DERIVED:       CXXConstructorDecl {{.*}} implicit {{.*}}Base 'void (const 
Base &)'
+// DERIVED:       CXXConstructorDecl {{.*}} implicit {{.*}}Base 'void (Base 
&&)'
+// Implicit members added by B.
+// DERIVED:       CXXConstructorDecl {{.*}} implicit {{.*}}Derived 'void ()'
+// DERIVED:       CXXConstructorDecl {{.*}} implicit {{.*}}Derived 'void 
(const Derived &)'
+// DERIVED:       CXXConstructorDecl {{.*}} implicit {{.*}}Derived 'void 
(Derived &&)'
+// Inheriting constructors are named after the base class.
+// DERIVED:       CXXConstructorDecl {{.*}} implicit {{.*}}Base 'void 
(int){{.*}}'


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to