-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang][Modules] Add added implicit members to record upon deserialization #161933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…ation 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
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: John Hui (j-hui) ChangesPreviously, 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:
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();
|
`pthread_mutexattr_t` should be getting its implicit default constructor instantiated by Clang. Due to a Clang bug, the constructor was being instantiated, `needsImplicitDefaultConstructor` was being set to `false`, but the constructor wasn't being added to `decl->members()`. This prevented Swift from correctly importing the constructor. This relies on John's change: llvm/llvm-project#161933. rdar://161999293
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