diff --git a/clang/include/clang/AST/ASTImporterSharedState.h b/clang/include/clang/AST/ASTImporterSharedState.h index 7be6f1460a85..a64c9d6be455 100644 --- a/clang/include/clang/AST/ASTImporterSharedState.h +++ b/clang/include/clang/AST/ASTImporterSharedState.h @@ -38,6 +38,9 @@ class ASTImporterSharedState { /// never cleared (like ImportedFromDecls). llvm::DenseMap ImportErrors; + /// Set of the newly created declarations. + llvm::DenseSet NewDecls; + // FIXME put ImportedFromDecls here! // And from that point we can better encapsulate the lookup table. @@ -73,6 +76,10 @@ public: void setImportDeclError(Decl *To, ImportError Error) { ImportErrors[To] = Error; } + + bool isNewDecl(const Decl *ToD) const { return NewDecls.count(ToD); } + + void markAsNewDecl(Decl *ToD) { NewDecls.insert(ToD); } }; } // namespace clang diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index e2294088908c..9c4f60511fb2 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -285,6 +285,7 @@ namespace clang { ToD = CreateFun(std::forward(args)...); // Keep track of imported Decls. Importer.RegisterImportedDecl(FromD, ToD); + Importer.SharedState->markAsNewDecl(ToD); InitializeImportedDecl(FromD, ToD); return false; // A new Decl is created. } diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index 856010cd4d03..896e3cb7a956 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -7698,6 +7698,38 @@ TEST_P(ASTImporterOptionSpecificTestBase, EXPECT_TRUE(ToX->getInClassInitializer()); } +TEST_P(ASTImporterOptionSpecificTestBase, isNewDecl) { + Decl *FromTU = getTuDecl( + R"( + int bar() { + return 0; + } + void other() { + bar(); + } + )", + Lang_CXX11); + Decl *ToTU = getToTuDecl( + R"( + int bar() { + return 0; + } + )", + Lang_CXX11); + auto *FromOther = FirstDeclMatcher().match( + FromTU, functionDecl(hasName("other"))); + ASSERT_TRUE(FromOther); + + auto *ToOther = Import(FromOther, Lang_CXX11); + ASSERT_TRUE(ToOther); + + auto *ToBar = FirstDeclMatcher().match( + ToTU, functionDecl(hasName("bar"))); + + EXPECT_TRUE(SharedStatePtr->isNewDecl(ToOther)); + EXPECT_FALSE(SharedStatePtr->isNewDecl(ToBar)); +} + INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions);