llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: John Hui (j-hui) <details> <summary>Changes</summary> 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 --- Full diff: https://github.com/llvm/llvm-project/pull/161933.diff 1 Files Affected: - (modified) clang/lib/Serialization/ASTReader.cpp (+64) ``````````diff diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 6acf79acea111..7593cb498a440 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -10651,6 +10651,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(); `````````` </details> https://github.com/llvm/llvm-project/pull/161933 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
