This revision was automatically updated to reflect the committed changes. Closed by commit rGab8b22d1c2d9: [lldb] Don't create duplicate declarations when completing a forward… (authored by teemperor).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D73345/new/ https://reviews.llvm.org/D73345 Files: lldb/source/Symbol/ClangASTImporter.cpp lldb/unittests/Symbol/TestClangASTImporter.cpp Index: lldb/unittests/Symbol/TestClangASTImporter.cpp =================================================================== --- lldb/unittests/Symbol/TestClangASTImporter.cpp +++ lldb/unittests/Symbol/TestClangASTImporter.cpp @@ -88,6 +88,33 @@ EXPECT_EQ(origin.decl, source.record_decl); } +TEST_F(TestClangASTImporter, CompleteFwdDeclWithOtherOrigin) { + // Create an AST with a full type that is defined. + clang_utils::SourceASTWithRecord source_with_definition; + + // Create an AST with a type thst is only a forward declaration with the + // same name as the one in the the other source. + std::unique_ptr<TypeSystemClang> fwd_decl_source = clang_utils::createAST(); + CompilerType fwd_decl_type = clang_utils::createRecord( + *fwd_decl_source, source_with_definition.record_decl->getName()); + + // Create a target and import the forward decl. + std::unique_ptr<TypeSystemClang> target = clang_utils::createAST(); + ClangASTImporter importer; + CompilerType imported = importer.CopyType(*target, fwd_decl_type); + ASSERT_TRUE(imported.IsValid()); + EXPECT_FALSE(imported.IsDefined()); + + // Now complete the forward decl with the definition from the other source. + // This should define the decl and give it the fields of the other origin. + clang::TagDecl *imported_tag_decl = ClangUtil::GetAsTagDecl(imported); + importer.CompleteTagDeclWithOrigin(imported_tag_decl, + source_with_definition.record_decl); + ASSERT_TRUE(imported.IsValid()); + EXPECT_TRUE(imported.IsDefined()); + EXPECT_EQ(1U, imported.GetNumFields()); +} + TEST_F(TestClangASTImporter, DeportDeclTagDecl) { // Tests that the ClangASTImporter::DeportDecl completely copies TagDecls. clang_utils::SourceASTWithRecord source; Index: lldb/source/Symbol/ClangASTImporter.cpp =================================================================== --- lldb/source/Symbol/ClangASTImporter.cpp +++ lldb/source/Symbol/ClangASTImporter.cpp @@ -882,6 +882,14 @@ void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo( clang::Decl *to, clang::Decl *from) { + // We might have a forward declaration from a shared library that we + // gave external lexical storage so that Clang asks us about the full + // definition when it needs it. In this case the ASTImporter isn't aware + // that the forward decl from the shared library is the actual import + // target but would create a second declaration that would then be defined. + // We want that 'to' is actually complete after this function so let's + // tell the ASTImporter that 'to' was imported from 'from'. + MapImported(from, to); ASTImporter::Imported(from, to); /*
Index: lldb/unittests/Symbol/TestClangASTImporter.cpp =================================================================== --- lldb/unittests/Symbol/TestClangASTImporter.cpp +++ lldb/unittests/Symbol/TestClangASTImporter.cpp @@ -88,6 +88,33 @@ EXPECT_EQ(origin.decl, source.record_decl); } +TEST_F(TestClangASTImporter, CompleteFwdDeclWithOtherOrigin) { + // Create an AST with a full type that is defined. + clang_utils::SourceASTWithRecord source_with_definition; + + // Create an AST with a type thst is only a forward declaration with the + // same name as the one in the the other source. + std::unique_ptr<TypeSystemClang> fwd_decl_source = clang_utils::createAST(); + CompilerType fwd_decl_type = clang_utils::createRecord( + *fwd_decl_source, source_with_definition.record_decl->getName()); + + // Create a target and import the forward decl. + std::unique_ptr<TypeSystemClang> target = clang_utils::createAST(); + ClangASTImporter importer; + CompilerType imported = importer.CopyType(*target, fwd_decl_type); + ASSERT_TRUE(imported.IsValid()); + EXPECT_FALSE(imported.IsDefined()); + + // Now complete the forward decl with the definition from the other source. + // This should define the decl and give it the fields of the other origin. + clang::TagDecl *imported_tag_decl = ClangUtil::GetAsTagDecl(imported); + importer.CompleteTagDeclWithOrigin(imported_tag_decl, + source_with_definition.record_decl); + ASSERT_TRUE(imported.IsValid()); + EXPECT_TRUE(imported.IsDefined()); + EXPECT_EQ(1U, imported.GetNumFields()); +} + TEST_F(TestClangASTImporter, DeportDeclTagDecl) { // Tests that the ClangASTImporter::DeportDecl completely copies TagDecls. clang_utils::SourceASTWithRecord source; Index: lldb/source/Symbol/ClangASTImporter.cpp =================================================================== --- lldb/source/Symbol/ClangASTImporter.cpp +++ lldb/source/Symbol/ClangASTImporter.cpp @@ -882,6 +882,14 @@ void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo( clang::Decl *to, clang::Decl *from) { + // We might have a forward declaration from a shared library that we + // gave external lexical storage so that Clang asks us about the full + // definition when it needs it. In this case the ASTImporter isn't aware + // that the forward decl from the shared library is the actual import + // target but would create a second declaration that would then be defined. + // We want that 'to' is actually complete after this function so let's + // tell the ASTImporter that 'to' was imported from 'from'. + MapImported(from, to); ASTImporter::Imported(from, to); /*
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits