[PATCH] D156461: [clang][ASTImporter] Merge implicit ctors with definition

Ding Fei via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 27 10:49:47 PDT 2023


danix800 updated this revision to Diff 544854.
danix800 edited the summary of this revision.
Herald added a subscriber: pengfei.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156461/new/

https://reviews.llvm.org/D156461

Files:
  clang/lib/AST/ASTImporter.cpp
  clang/unittests/AST/ASTImporterTest.cpp


Index: clang/unittests/AST/ASTImporterTest.cpp
===================================================================
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -3175,6 +3175,39 @@
   testNoImportOf(cxxMethodDecl(hasName("f")), Code);
 }
 
+TEST_P(ImportImplicitMethods, MergeImplicitMethodWithDefinition) {
+  Decl *From, *To;
+  std::tie(From, To) = getImportedDecl(
+      R"s(
+        struct A {
+          A() : m() {}
+          int m;
+        };
+
+        A foo() { A a; return a; }
+        A bar() { return {}; }
+      )s",
+      Lang_CXX17,
+      R"s(
+        struct A {
+          A() : m() {}
+          int m;
+        };
+        A baz() { return {}; }
+      )s",
+      Lang_CXX17, "A");
+
+  MatchVerifier<Decl> Verifier;
+  auto HasCopyCtor = has(cxxConstructorDecl(isCopyConstructor(), isImplicit()));
+  auto HasCtorInit =
+      hasAnyConstructorInitializer(cxxCtorInitializer(isMemberInitializer()));
+  auto HasMoveCtor =
+      has(cxxConstructorDecl(isMoveConstructor(), isImplicit(), HasCtorInit));
+  auto Pattern = cxxRecordDecl(HasCopyCtor, HasMoveCtor);
+  ASSERT_TRUE(Verifier.match(From, Pattern));
+  EXPECT_TRUE(Verifier.match(To, Pattern));
+}
+
 TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentRecord) {
   Decl *ToR1;
   {
Index: clang/lib/AST/ASTImporter.cpp
===================================================================
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -3734,9 +3734,14 @@
 
   // Connect the redecl chain.
   if (FoundByLookup) {
-    auto *Recent = const_cast<FunctionDecl *>(
-          FoundByLookup->getMostRecentDecl());
-    ToFunction->setPreviousDecl(Recent);
+    if (auto *Ctor = dyn_cast<CXXConstructorDecl>(D);
+        Ctor && Ctor->isImplicit())
+      DC->removeDecl(FoundByLookup);
+    else {
+      auto *Recent =
+          const_cast<FunctionDecl *>(FoundByLookup->getMostRecentDecl());
+      ToFunction->setPreviousDecl(Recent);
+    }
     // FIXME Probably we should merge exception specifications.  E.g. In the
     // "To" context the existing function may have exception specification with
     // noexcept-unevaluated, while the newly imported function may have an


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156461.544854.patch
Type: text/x-patch
Size: 2241 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230727/221a6b10/attachment.bin>


More information about the cfe-commits mailing list