From 73929b49dd4ade1d0dad11e4af4c2842c99a3cae Mon Sep 17 00:00:00 2001 From: Michael Buch Date: Tue, 30 Apr 2024 11:24:39 +0100 Subject: [PATCH 1/5] [lldb][Type Completion] Ensure TypeForDecl is set on CTSD --- lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp index 26ed53050a3b4..f8e45a0ef1c76 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -1732,6 +1732,8 @@ TypeSystemClang::CreateClassTemplateSpecializationDecl( static_cast(kind)); class_template_specialization_decl->setDeclContext(decl_ctx); class_template_specialization_decl->setInstantiationOf(class_template_decl); + if (TypeSystemClang::UseRedeclCompletion()) + ast.getTypeDeclType(class_template_specialization_decl, nullptr); class_template_specialization_decl->setTemplateArgs( TemplateArgumentList::CreateCopy(ast, args)); class_template_specialization_decl->setDeclName( From 81a0cb2ab1fe5d9f98955e5e4b41375c8edf30ed Mon Sep 17 00:00:00 2001 From: Michael Buch Date: Tue, 30 Apr 2024 11:25:31 +0100 Subject: [PATCH 2/5] [clang][ASTImporter] Don't import field definitions for LLDB redecl-completion --- clang/lib/AST/ASTImporter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index dce2c9575403d..c70f87f98439f 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -2055,7 +2055,7 @@ ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) { continue; } - if (Importer.hasLLDBRedeclCompletion()) { + if (!Importer.hasLLDBRedeclCompletion()) { FieldDecl *FieldFrom = dyn_cast_or_null(From); Decl *ImportedDecl = *ImportedOrErr; FieldDecl *FieldTo = dyn_cast_or_null(ImportedDecl); From fdf30fbe42104c1f8f8fdb8b097febd00aa0cc61 Mon Sep 17 00:00:00 2001 From: Michael Buch Date: Tue, 30 Apr 2024 11:33:08 +0100 Subject: [PATCH 3/5] [lldb][Type Completion] Only create importer delegates if definition is valid --- .../Clang/ClangASTImporter.cpp | 60 ++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp index 6e0f8495d178c..fd119a9ffe035 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp @@ -824,23 +824,17 @@ bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) { if (!decl_origin.Valid()) return false; - ImporterDelegateSP delegate_sp( - GetDelegate(&decl->getASTContext(), decl_origin.ctx)); - - ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, - &decl->getASTContext()); - - if (!TypeSystemClang::UseRedeclCompletion()) { - if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl)) - return false; - - if (delegate_sp) - delegate_sp->ImportDefinitionTo(decl, decl_origin.decl); - } else { + if (TypeSystemClang::UseRedeclCompletion()) { auto *origin_def = llvm::cast(decl_origin.decl)->getDefinition(); if (!origin_def) return false; + ImporterDelegateSP delegate_sp( + GetDelegate(&decl->getASTContext(), decl_origin.ctx)); + + ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, + &decl->getASTContext()); + // This is expected to pull in a definition for result_decl (if in redecl // completion mode) llvm::Expected result = delegate_sp->Import(origin_def); @@ -859,6 +853,18 @@ bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) { if (!decl->isThisDeclarationADefinition() && result_decl != decl) if (result_decl->getPreviousDecl() == nullptr) result_decl->setPreviousDecl(decl); + } else { + if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl)) + return false; + + ImporterDelegateSP delegate_sp( + GetDelegate(&decl->getASTContext(), decl_origin.ctx)); + + ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, + &decl->getASTContext()); + + if (delegate_sp) + delegate_sp->ImportDefinitionTo(decl, decl_origin.decl); } return true; @@ -880,19 +886,7 @@ bool ClangASTImporter::CompleteObjCInterfaceDecl( if (!decl_origin.Valid()) return false; - ImporterDelegateSP delegate_sp( - GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx)); - - if (!TypeSystemClang::UseRedeclCompletion()) { - if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl)) - return false; - - if (delegate_sp) - delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl); - - if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass()) - RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0)); - } else { + if (TypeSystemClang::UseRedeclCompletion()) { ObjCInterfaceDecl *origin_decl = llvm::cast(decl_origin.decl); @@ -900,7 +894,7 @@ bool ClangASTImporter::CompleteObjCInterfaceDecl( if (!origin_decl) return false; - auto delegate_sp( + ImporterDelegateSP delegate_sp( GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx)); llvm::Expected result = delegate_sp->Import(origin_decl); @@ -915,6 +909,18 @@ bool ClangASTImporter::CompleteObjCInterfaceDecl( return false; } + if (!TypeSystemClang::GetCompleteDecl(decl_origin.ctx, decl_origin.decl)) + return false; + + ImporterDelegateSP delegate_sp( + GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx)); + + if (delegate_sp) + delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl); + + if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass()) + RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0)); + return true; } From 0e672995c222802f23f7148fbf79d7d76d143bd4 Mon Sep 17 00:00:00 2001 From: Michael Buch Date: Tue, 30 Apr 2024 11:34:16 +0100 Subject: [PATCH 4/5] [lldb][Type Completionb] Don't register decls as imported if candidate definition is not found --- .../Plugins/ExpressionParser/Clang/ClangASTImporter.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp index fd119a9ffe035..50920dbfb529d 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp @@ -1210,15 +1210,14 @@ ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) { DeclContext::lookup_result lr = dc->lookup(*dn_or_err); for (clang::Decl *candidate : lr) { if (candidate->getKind() == From->getKind()) { - RegisterImportedDecl(From, candidate); - - // If we're dealing with redecl chains. We want to find the definition, + // If we're dealing with redecl chains, we want to find the definition, // so skip if the decl is actually just a forwad decl. if (TypeSystemClang::UseRedeclCompletion()) if (auto *tag_decl = llvm::dyn_cast(candidate); !tag_decl || !tag_decl->getDefinition()) continue; + RegisterImportedDecl(From, candidate); m_decls_to_ignore.insert(candidate); return candidate; } From 9dbe7e9cdcc51a4c583495f7a551eda3f73e7837 Mon Sep 17 00:00:00 2001 From: Michael Buch Date: Tue, 30 Apr 2024 13:33:11 +0100 Subject: [PATCH 5/5] [lldb][ClangASTSource] Mark ClangASTSourceProxy as ImporterBackedASTSource --- lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.h b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.h index 0a446e45a418b..b3a7a0c9dddd1 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.h +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.h @@ -214,7 +214,7 @@ class ClangASTSource : public ImporterBackedASTSource, /// /// Clang AST contexts like to own their AST sources, so this is a state- /// free proxy object. - class ClangASTSourceProxy : public clang::ExternalASTSource { + class ClangASTSourceProxy : public ImporterBackedASTSource { public: ClangASTSourceProxy(ClangASTSource &original) : m_original(original) {}