diff options
Diffstat (limited to 'lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp')
-rw-r--r-- | lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp | 1154 |
1 files changed, 444 insertions, 710 deletions
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp index 42927ab6cc8a..6fe85a1298fc 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp @@ -1,4 +1,4 @@ -//===-- ClangASTSource.cpp ---------------------------------------*- C++-*-===// +//===-- ClangASTSource.cpp ------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -13,8 +13,6 @@ #include "lldb/Core/Module.h" #include "lldb/Core/ModuleList.h" -#include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Symbol/ClangUtil.h" #include "lldb/Symbol/CompilerDeclContext.h" #include "lldb/Symbol/Function.h" #include "lldb/Symbol/SymbolFile.h" @@ -23,8 +21,11 @@ #include "lldb/Utility/Log.h" #include "clang/AST/ASTContext.h" #include "clang/AST/RecordLayout.h" +#include "clang/Basic/SourceManager.h" +#include "Plugins/ExpressionParser/Clang/ClangUtil.h" #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" +#include "Plugins/TypeSystem/Clang/TypeSystemClang.h" #include <memory> #include <vector> @@ -49,14 +50,16 @@ private: }; } -ClangASTSource::ClangASTSource(const lldb::TargetSP &target, - const lldb::ClangASTImporterSP &importer) - : m_import_in_progress(false), m_lookups_enabled(false), m_target(target), - m_ast_context(nullptr), m_active_lexical_decls(), m_active_lookups() { - m_ast_importer_sp = importer; +ClangASTSource::ClangASTSource( + const lldb::TargetSP &target, + const std::shared_ptr<ClangASTImporter> &importer) + : m_lookups_enabled(false), m_target(target), m_ast_context(nullptr), + m_ast_importer_sp(importer), m_active_lexical_decls(), + m_active_lookups() { + assert(m_ast_importer_sp && "No ClangASTImporter passed to ClangASTSource?"); } -void ClangASTSource::InstallASTContext(ClangASTContext &clang_ast_context) { +void ClangASTSource::InstallASTContext(TypeSystemClang &clang_ast_context) { m_ast_context = &clang_ast_context.getASTContext(); m_clang_ast_context = &clang_ast_context; m_file_manager = &m_ast_context->getSourceManager().getFileManager(); @@ -64,18 +67,15 @@ void ClangASTSource::InstallASTContext(ClangASTContext &clang_ast_context) { } ClangASTSource::~ClangASTSource() { - if (!m_ast_importer_sp) - return; - m_ast_importer_sp->ForgetDestination(m_ast_context); if (!m_target) return; // We are in the process of destruction, don't create clang ast context on // demand by passing false to - // Target::GetScratchClangASTContext(create_on_demand). - ClangASTContext *scratch_clang_ast_context = - ClangASTContext::GetScratch(*m_target, false); + // Target::GetScratchTypeSystemClang(create_on_demand). + TypeSystemClang *scratch_clang_ast_context = + TypeSystemClang::GetScratch(*m_target, false); if (!scratch_clang_ast_context) return; @@ -103,16 +103,8 @@ bool ClangASTSource::FindExternalVisibleDeclsByName( return false; } - if (GetImportInProgress()) { - SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); - return false; - } - std::string decl_name(clang_decl_name.getAsString()); - // if (m_decl_map.DoingASTImport ()) - // return DeclContext::lookup_result(); - // switch (clang_decl_name.getNameKind()) { // Normal identifiers. case DeclarationName::Identifier: { @@ -141,7 +133,7 @@ bool ClangASTSource::FindExternalVisibleDeclsByName( case DeclarationName::ObjCMultiArgSelector: { llvm::SmallVector<NamedDecl *, 1> method_decls; - NameSearchContext method_search_context(*this, method_decls, + NameSearchContext method_search_context(*m_clang_ast_context, method_decls, clang_decl_name, decl_ctx); FindObjCMethodDecls(method_search_context); @@ -179,154 +171,134 @@ bool ClangASTSource::FindExternalVisibleDeclsByName( return false; } m_active_lookups.insert(uniqued_const_decl_name); - // static uint32_t g_depth = 0; - // ++g_depth; - // printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, - // uniqued_const_decl_name); llvm::SmallVector<NamedDecl *, 4> name_decls; - NameSearchContext name_search_context(*this, name_decls, clang_decl_name, - decl_ctx); + NameSearchContext name_search_context(*m_clang_ast_context, name_decls, + clang_decl_name, decl_ctx); FindExternalVisibleDecls(name_search_context); SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, name_decls); - // --g_depth; m_active_lookups.erase(uniqued_const_decl_name); return (name_decls.size() != 0); } -void ClangASTSource::CompleteType(TagDecl *tag_decl) { +TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) { Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - static unsigned int invocation_id = 0; - unsigned int current_id = invocation_id++; - - if (log) { - LLDB_LOGF(log, - " CompleteTagDecl[%u] on (ASTContext*)%p Completing " - "(TagDecl*)%p named %s", - current_id, static_cast<void *>(m_ast_context), - static_cast<void *>(tag_decl), tag_decl->getName().str().c_str()); - - LLDB_LOG(log, " CTD[%u] Before:\n{0}", current_id, - ClangUtil::DumpDecl(tag_decl)); - } - - auto iter = m_active_lexical_decls.find(tag_decl); - if (iter != m_active_lexical_decls.end()) - return; - m_active_lexical_decls.insert(tag_decl); - ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl); + if (const NamespaceDecl *namespace_context = + dyn_cast<NamespaceDecl>(decl->getDeclContext())) { + ClangASTImporter::NamespaceMapSP namespace_map = + m_ast_importer_sp->GetNamespaceMap(namespace_context); - if (!m_ast_importer_sp) { - return; - } + LLDB_LOGV(log, " CTD Inspecting namespace map{0} ({1} entries)", + namespace_map.get(), namespace_map->size()); - if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) { - // We couldn't complete the type. Maybe there's a definition somewhere - // else that can be completed. - - LLDB_LOGF(log, - " CTD[%u] Type could not be completed in the module in " - "which it was first found.", - current_id); + if (!namespace_map) + return nullptr; - bool found = false; + for (const ClangASTImporter::NamespaceMapItem &item : *namespace_map) { + LLDB_LOG(log, " CTD Searching namespace {0} in module {1}", + item.second.GetName(), item.first->GetFileSpec().GetFilename()); - DeclContext *decl_ctx = tag_decl->getDeclContext(); + TypeList types; - if (const NamespaceDecl *namespace_context = - dyn_cast<NamespaceDecl>(decl_ctx)) { - ClangASTImporter::NamespaceMapSP namespace_map = - m_ast_importer_sp->GetNamespaceMap(namespace_context); + ConstString name(decl->getName()); - if (log && log->GetVerbose()) - LLDB_LOGF(log, " CTD[%u] Inspecting namespace map %p (%d entries)", - current_id, static_cast<void *>(namespace_map.get()), - static_cast<int>(namespace_map->size())); + item.first->FindTypesInNamespace(name, item.second, UINT32_MAX, types); - if (!namespace_map) - return; + for (uint32_t ti = 0, te = types.GetSize(); ti != te; ++ti) { + lldb::TypeSP type = types.GetTypeAtIndex(ti); - for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), - e = namespace_map->end(); - i != e && !found; ++i) { - LLDB_LOGF(log, " CTD[%u] Searching namespace %s in module %s", - current_id, i->second.GetName().AsCString(), - i->first->GetFileSpec().GetFilename().GetCString()); + if (!type) + continue; - TypeList types; + CompilerType clang_type(type->GetFullCompilerType()); - ConstString name(tag_decl->getName().str().c_str()); + if (!ClangUtil::IsClangType(clang_type)) + continue; - i->first->FindTypesInNamespace(name, &i->second, UINT32_MAX, types); + const TagType *tag_type = + ClangUtil::GetQualType(clang_type)->getAs<TagType>(); - for (uint32_t ti = 0, te = types.GetSize(); ti != te && !found; ++ti) { - lldb::TypeSP type = types.GetTypeAtIndex(ti); + if (!tag_type) + continue; - if (!type) - continue; + TagDecl *candidate_tag_decl = + const_cast<TagDecl *>(tag_type->getDecl()); - CompilerType clang_type(type->GetFullCompilerType()); + if (TypeSystemClang::GetCompleteDecl( + &candidate_tag_decl->getASTContext(), candidate_tag_decl)) + return candidate_tag_decl; + } + } + } else { + TypeList types; - if (!ClangUtil::IsClangType(clang_type)) - continue; + ConstString name(decl->getName()); - const TagType *tag_type = - ClangUtil::GetQualType(clang_type)->getAs<TagType>(); + const ModuleList &module_list = m_target->GetImages(); - if (!tag_type) - continue; + bool exact_match = false; + llvm::DenseSet<SymbolFile *> searched_symbol_files; + module_list.FindTypes(nullptr, name, exact_match, UINT32_MAX, + searched_symbol_files, types); - TagDecl *candidate_tag_decl = - const_cast<TagDecl *>(tag_type->getDecl()); + for (uint32_t ti = 0, te = types.GetSize(); ti != te; ++ti) { + lldb::TypeSP type = types.GetTypeAtIndex(ti); - if (m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, - candidate_tag_decl)) - found = true; - } - } - } else { - TypeList types; + if (!type) + continue; - ConstString name(tag_decl->getName().str().c_str()); + CompilerType clang_type(type->GetFullCompilerType()); - const ModuleList &module_list = m_target->GetImages(); + if (!ClangUtil::IsClangType(clang_type)) + continue; - bool exact_match = false; - llvm::DenseSet<SymbolFile *> searched_symbol_files; - module_list.FindTypes(nullptr, name, exact_match, UINT32_MAX, - searched_symbol_files, types); + const TagType *tag_type = + ClangUtil::GetQualType(clang_type)->getAs<TagType>(); - for (uint32_t ti = 0, te = types.GetSize(); ti != te && !found; ++ti) { - lldb::TypeSP type = types.GetTypeAtIndex(ti); + if (!tag_type) + continue; - if (!type) - continue; + TagDecl *candidate_tag_decl = const_cast<TagDecl *>(tag_type->getDecl()); - CompilerType clang_type(type->GetFullCompilerType()); + // We have found a type by basename and we need to make sure the decl + // contexts are the same before we can try to complete this type with + // another + if (!TypeSystemClang::DeclsAreEquivalent(const_cast<TagDecl *>(decl), + candidate_tag_decl)) + continue; - if (!ClangUtil::IsClangType(clang_type)) - continue; + if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(), + candidate_tag_decl)) + return candidate_tag_decl; + } + } + return nullptr; +} - const TagType *tag_type = - ClangUtil::GetQualType(clang_type)->getAs<TagType>(); +void ClangASTSource::CompleteType(TagDecl *tag_decl) { + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - if (!tag_type) - continue; + if (log) { + LLDB_LOG(log, + " CompleteTagDecl on (ASTContext*){0} Completing " + "(TagDecl*){1} named {2}", + m_clang_ast_context->getDisplayName(), tag_decl, + tag_decl->getName()); - TagDecl *candidate_tag_decl = - const_cast<TagDecl *>(tag_type->getDecl()); + LLDB_LOG(log, " CTD Before:\n{0}", ClangUtil::DumpDecl(tag_decl)); + } - // We have found a type by basename and we need to make sure the decl - // contexts are the same before we can try to complete this type with - // another - if (!ClangASTContext::DeclsAreEquivalent(tag_decl, candidate_tag_decl)) - continue; + auto iter = m_active_lexical_decls.find(tag_decl); + if (iter != m_active_lexical_decls.end()) + return; + m_active_lexical_decls.insert(tag_decl); + ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl); - if (m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, - candidate_tag_decl)) - found = true; - } - } + if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) { + // We couldn't complete the type. Maybe there's a definition somewhere + // else that can be completed. + if (TagDecl *alternate = FindCompleteType(tag_decl)) + m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, alternate); } LLDB_LOG(log, " [CTD] After:\n{0}", ClangUtil::DumpDecl(tag_decl)); @@ -335,19 +307,14 @@ void ClangASTSource::CompleteType(TagDecl *tag_decl) { void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) { Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - LLDB_LOGF(log, - " [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing " - "an ObjCInterfaceDecl named %s", - static_cast<void *>(m_ast_context), - interface_decl->getName().str().c_str()); + LLDB_LOG(log, + " [CompleteObjCInterfaceDecl] on (ASTContext*){0} '{1}' " + "Completing an ObjCInterfaceDecl named {1}", + m_ast_context, m_clang_ast_context->getDisplayName(), + interface_decl->getName()); LLDB_LOG(log, " [COID] Before:\n{0}", ClangUtil::DumpDecl(interface_decl)); - if (!m_ast_importer_sp) { - lldbassert(0 && "No mechanism for completing a type!"); - return; - } - ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl); if (original.Valid()) { @@ -368,10 +335,8 @@ void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) { interface_decl->getSuperClass() != interface_decl) CompleteType(interface_decl->getSuperClass()); - if (log) { - LLDB_LOGF(log, " [COID] After:"); - LLDB_LOG(log, " [COID] {0}", ClangUtil::DumpDecl(interface_decl)); - } + LLDB_LOG(log, " [COID] After:"); + LLDB_LOG(log, " [COID] {0}", ClangUtil::DumpDecl(interface_decl)); } clang::ObjCInterfaceDecl *ClangASTSource::GetCompleteObjCInterface( @@ -420,9 +385,6 @@ void ClangASTSource::FindExternalLexicalDecls( llvm::function_ref<bool(Decl::Kind)> predicate, llvm::SmallVectorImpl<Decl *> &decls) { - if (!m_ast_importer_sp) - return; - Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); const Decl *context_decl = dyn_cast<Decl>(decl_context); @@ -436,29 +398,27 @@ void ClangASTSource::FindExternalLexicalDecls( m_active_lexical_decls.insert(context_decl); ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl); - static unsigned int invocation_id = 0; - unsigned int current_id = invocation_id++; - if (log) { if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) - LLDB_LOGF( - log, - "FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p", - current_id, static_cast<void *>(m_ast_context), - context_named_decl->getNameAsString().c_str(), - context_decl->getDeclKindName(), - static_cast<const void *>(context_decl)); + LLDB_LOG(log, + "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in " + "'{2}' (%sDecl*){3}", + m_ast_context, m_clang_ast_context->getDisplayName(), + context_named_decl->getNameAsString().c_str(), + context_decl->getDeclKindName(), + static_cast<const void *>(context_decl)); else if (context_decl) - LLDB_LOGF( - log, "FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p", - current_id, static_cast<void *>(m_ast_context), - context_decl->getDeclKindName(), - static_cast<const void *>(context_decl)); + LLDB_LOG(log, + "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in " + "({2}Decl*){3}", + m_ast_context, m_clang_ast_context->getDisplayName(), + context_decl->getDeclKindName(), + static_cast<const void *>(context_decl)); else - LLDB_LOGF( - log, - "FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context", - current_id, static_cast<const void *>(m_ast_context)); + LLDB_LOG(log, + "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in a " + "NULL context", + m_ast_context, m_clang_ast_context->getDisplayName()); } ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(context_decl); @@ -466,10 +426,10 @@ void ClangASTSource::FindExternalLexicalDecls( if (!original.Valid()) return; - LLDB_LOG( - log, " FELD[{0}] Original decl (ASTContext*){1:x} (Decl*){2:x}:\n{3}", - current_id, static_cast<void *>(original.ctx), - static_cast<void *>(original.decl), ClangUtil::DumpDecl(original.decl)); + LLDB_LOG(log, " FELD Original decl {0} (Decl*){1:x}:\n{2}", + static_cast<void *>(original.ctx), + static_cast<void *>(original.decl), + ClangUtil::DumpDecl(original.decl)); if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original.decl)) { @@ -499,10 +459,7 @@ void ClangASTSource::FindExternalLexicalDecls( // Indicates whether we skipped any Decls of the original DeclContext. bool SkippedDecls = false; - for (TagDecl::decl_iterator iter = original_decl_context->decls_begin(); - iter != original_decl_context->decls_end(); ++iter) { - Decl *decl = *iter; - + for (Decl *decl : original_decl_context->decls()) { // The predicate function returns true if the passed declaration kind is // the one we are looking for. // See clang::ExternalASTSource::FindExternalLexicalDecls() @@ -511,13 +468,13 @@ void ClangASTSource::FindExternalLexicalDecls( std::string ast_dump = ClangUtil::DumpDecl(decl); if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) - LLDB_LOGF(log, " FELD[%d] Adding [to %sDecl %s] lexical %sDecl %s", - current_id, context_named_decl->getDeclKindName(), - context_named_decl->getNameAsString().c_str(), - decl->getDeclKindName(), ast_dump.c_str()); + LLDB_LOG(log, " FELD Adding [to {0}Decl {1}] lexical {2}Decl {3}", + context_named_decl->getDeclKindName(), + context_named_decl->getName(), decl->getDeclKindName(), + ast_dump); else - LLDB_LOGF(log, " FELD[%d] Adding lexical %sDecl %s", current_id, - decl->getDeclKindName(), ast_dump.c_str()); + LLDB_LOG(log, " FELD Adding lexical {0}Decl {1}", + decl->getDeclKindName(), ast_dump); } Decl *copied_decl = CopyDecl(decl); @@ -556,56 +513,29 @@ void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) { Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - static unsigned int invocation_id = 0; - unsigned int current_id = invocation_id++; - if (log) { if (!context.m_decl_context) - LLDB_LOGF(log, - "ClangASTSource::FindExternalVisibleDecls[%u] on " - "(ASTContext*)%p for '%s' in a NULL DeclContext", - current_id, static_cast<void *>(m_ast_context), - name.GetCString()); + LLDB_LOG(log, + "ClangASTSource::FindExternalVisibleDecls on " + "(ASTContext*){0} '{1}' for '{2}' in a NULL DeclContext", + m_ast_context, m_clang_ast_context->getDisplayName(), name); else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context)) - LLDB_LOGF(log, - "ClangASTSource::FindExternalVisibleDecls[%u] on " - "(ASTContext*)%p for '%s' in '%s'", - current_id, static_cast<void *>(m_ast_context), - name.GetCString(), - context_named_decl->getNameAsString().c_str()); + LLDB_LOG(log, + "ClangASTSource::FindExternalVisibleDecls on " + "(ASTContext*){0} '{1}' for '{2}' in '{3}'", + m_ast_context, m_clang_ast_context->getDisplayName(), name, + context_named_decl->getName()); else - LLDB_LOGF(log, - "ClangASTSource::FindExternalVisibleDecls[%u] on " - "(ASTContext*)%p for '%s' in a '%s'", - current_id, static_cast<void *>(m_ast_context), - name.GetCString(), context.m_decl_context->getDeclKindName()); + LLDB_LOG(log, + "ClangASTSource::FindExternalVisibleDecls on " + "(ASTContext*){0} '{1}' for '{2}' in a '{3}'", + m_ast_context, m_clang_ast_context->getDisplayName(), name, + context.m_decl_context->getDeclKindName()); } - context.m_namespace_map = std::make_shared<ClangASTImporter::NamespaceMap>(); - - if (const NamespaceDecl *namespace_context = - dyn_cast<NamespaceDecl>(context.m_decl_context)) { - ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer_sp ? - m_ast_importer_sp->GetNamespaceMap(namespace_context) : nullptr; - - if (log && log->GetVerbose()) - LLDB_LOGF(log, " CAS::FEVD[%u] Inspecting namespace map %p (%d entries)", - current_id, static_cast<void *>(namespace_map.get()), - static_cast<int>(namespace_map->size())); - - if (!namespace_map) - return; - - for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), - e = namespace_map->end(); - i != e; ++i) { - LLDB_LOGF(log, " CAS::FEVD[%u] Searching namespace %s in module %s", - current_id, i->second.GetName().AsCString(), - i->first->GetFileSpec().GetFilename().GetCString()); - - FindExternalVisibleDecls(context, i->first, i->second, current_id); - } + if (isa<NamespaceDecl>(context.m_decl_context)) { + LookupInNamespace(context); } else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) { FindObjCPropertyAndIvarDecls(context); } else if (!isa<TranslationUnitDecl>(context.m_decl_context)) { @@ -614,18 +544,15 @@ void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) { } else { CompilerDeclContext namespace_decl; - LLDB_LOGF(log, " CAS::FEVD[%u] Searching the root namespace", current_id); + LLDB_LOG(log, " CAS::FEVD Searching the root namespace"); - FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl, - current_id); + FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl); } if (!context.m_namespace_map->empty()) { if (log && log->GetVerbose()) - LLDB_LOGF(log, - " CAS::FEVD[%u] Registering namespace map %p (%d entries)", - current_id, static_cast<void *>(context.m_namespace_map.get()), - static_cast<int>(context.m_namespace_map->size())); + LLDB_LOG(log, " CAS::FEVD Registering namespace map {0} ({1} entries)", + context.m_namespace_map.get(), context.m_namespace_map->size()); NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map); @@ -658,7 +585,7 @@ bool ClangASTSource::IgnoreName(const ConstString name, void ClangASTSource::FindExternalVisibleDecls( NameSearchContext &context, lldb::ModuleSP module_sp, - CompilerDeclContext &namespace_decl, unsigned int current_id) { + CompilerDeclContext &namespace_decl) { assert(m_ast_context); Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); @@ -672,196 +599,113 @@ void ClangASTSource::FindExternalVisibleDecls( if (!m_target) return; - if (module_sp && namespace_decl) { - CompilerDeclContext found_namespace_decl; - - if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) { - found_namespace_decl = symbol_file->FindNamespace(name, &namespace_decl); + FillNamespaceMap(context, module_sp, namespace_decl); - if (found_namespace_decl) { - context.m_namespace_map->push_back( - std::pair<lldb::ModuleSP, CompilerDeclContext>( - module_sp, found_namespace_decl)); - - LLDB_LOGF(log, " CAS::FEVD[%u] Found namespace %s in module %s", - current_id, name.GetCString(), - module_sp->GetFileSpec().GetFilename().GetCString()); - } - } - } else { - const ModuleList &target_images = m_target->GetImages(); - std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex()); + if (context.m_found_type) + return; - for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) { - lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); + TypeList types; + const bool exact_match = true; + llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; + if (module_sp && namespace_decl) + module_sp->FindTypesInNamespace(name, namespace_decl, 1, types); + else { + m_target->GetImages().FindTypes(module_sp.get(), name, exact_match, 1, + searched_symbol_files, types); + } - if (!image) - continue; + if (size_t num_types = types.GetSize()) { + for (size_t ti = 0; ti < num_types; ++ti) { + lldb::TypeSP type_sp = types.GetTypeAtIndex(ti); - CompilerDeclContext found_namespace_decl; + if (log) { + const char *name_string = type_sp->GetName().GetCString(); - SymbolFile *symbol_file = image->GetSymbolFile(); + LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\": {1}", name, + (name_string ? name_string : "<anonymous>")); + } - if (!symbol_file) - continue; + CompilerType full_type = type_sp->GetFullCompilerType(); - found_namespace_decl = symbol_file->FindNamespace(name, &namespace_decl); + CompilerType copied_clang_type(GuardedCopyType(full_type)); - if (found_namespace_decl) { - context.m_namespace_map->push_back( - std::pair<lldb::ModuleSP, CompilerDeclContext>( - image, found_namespace_decl)); + if (!copied_clang_type) { + LLDB_LOG(log, " CAS::FEVD - Couldn't export a type"); - LLDB_LOGF(log, " CAS::FEVD[%u] Found namespace %s in module %s", - current_id, name.GetCString(), - image->GetFileSpec().GetFilename().GetCString()); + continue; } - } - } - do { - if (context.m_found.type) - break; + context.AddTypeDecl(copied_clang_type); - TypeList types; - const bool exact_match = true; - llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; - if (module_sp && namespace_decl) - module_sp->FindTypesInNamespace(name, &namespace_decl, 1, types); - else { - m_target->GetImages().FindTypes(module_sp.get(), name, exact_match, 1, - searched_symbol_files, types); + context.m_found_type = true; + break; } + } - if (size_t num_types = types.GetSize()) { - for (size_t ti = 0; ti < num_types; ++ti) { - lldb::TypeSP type_sp = types.GetTypeAtIndex(ti); - - if (log) { - const char *name_string = type_sp->GetName().GetCString(); + if (!context.m_found_type) { + // Try the modules next. + FindDeclInModules(context, name); + } - LLDB_LOGF(log, " CAS::FEVD[%u] Matching type found for \"%s\": %s", - current_id, name.GetCString(), - (name_string ? name_string : "<anonymous>")); - } + if (!context.m_found_type) { + FindDeclInObjCRuntime(context, name); + } +} - CompilerType full_type = type_sp->GetFullCompilerType(); +void ClangASTSource::FillNamespaceMap( + NameSearchContext &context, lldb::ModuleSP module_sp, + const CompilerDeclContext &namespace_decl) { + const ConstString name(context.m_decl_name.getAsString().c_str()); + if (IgnoreName(name, true)) + return; - CompilerType copied_clang_type(GuardedCopyType(full_type)); + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - if (!copied_clang_type) { - LLDB_LOGF(log, " CAS::FEVD[%u] - Couldn't export a type", - current_id); + if (module_sp && namespace_decl) { + CompilerDeclContext found_namespace_decl; - continue; - } + if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) { + found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl); - context.AddTypeDecl(copied_clang_type); + if (found_namespace_decl) { + context.m_namespace_map->push_back( + std::pair<lldb::ModuleSP, CompilerDeclContext>( + module_sp, found_namespace_decl)); - context.m_found.type = true; - break; + LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name, + module_sp->GetFileSpec().GetFilename()); } } + return; + } - if (!context.m_found.type) { - // Try the modules next. - - do { - if (ClangModulesDeclVendor *modules_decl_vendor = - m_target->GetClangModulesDeclVendor()) { - bool append = false; - uint32_t max_matches = 1; - std::vector<clang::NamedDecl *> decls; - - if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) - break; - - if (log) { - LLDB_LOGF(log, - " CAS::FEVD[%u] Matching entity found for \"%s\" in " - "the modules", - current_id, name.GetCString()); - } - - clang::NamedDecl *const decl_from_modules = decls[0]; - - if (llvm::isa<clang::TypeDecl>(decl_from_modules) || - llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) || - llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) { - clang::Decl *copied_decl = CopyDecl(decl_from_modules); - clang::NamedDecl *copied_named_decl = - copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; - - if (!copied_named_decl) { - LLDB_LOGF( - log, - " CAS::FEVD[%u] - Couldn't export a type from the modules", - current_id); - - break; - } - - context.AddNamedDecl(copied_named_decl); - - context.m_found.type = true; - } - } - } while (false); - } - - if (!context.m_found.type) { - do { - // Couldn't find any types elsewhere. Try the Objective-C runtime if - // one exists. - - lldb::ProcessSP process(m_target->GetProcessSP()); - - if (!process) - break; - - ObjCLanguageRuntime *language_runtime( - ObjCLanguageRuntime::Get(*process)); - - if (!language_runtime) - break; - - DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); + const ModuleList &target_images = m_target->GetImages(); + std::lock_guard<std::recursive_mutex> guard(target_images.GetMutex()); - if (!decl_vendor) - break; + for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) { + lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); - bool append = false; - uint32_t max_matches = 1; - std::vector<clang::NamedDecl *> decls; + if (!image) + continue; - auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor); - if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls)) - break; + CompilerDeclContext found_namespace_decl; - if (log) { - LLDB_LOGF( - log, - " CAS::FEVD[%u] Matching type found for \"%s\" in the runtime", - current_id, name.GetCString()); - } + SymbolFile *symbol_file = image->GetSymbolFile(); - clang::Decl *copied_decl = CopyDecl(decls[0]); - clang::NamedDecl *copied_named_decl = - copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; + if (!symbol_file) + continue; - if (!copied_named_decl) { - LLDB_LOGF(log, - " CAS::FEVD[%u] - Couldn't export a type from the runtime", - current_id); + found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl); - break; - } + if (found_namespace_decl) { + context.m_namespace_map->push_back( + std::pair<lldb::ModuleSP, CompilerDeclContext>(image, + found_namespace_decl)); - context.AddNamedDecl(copied_named_decl); - } while (false); + LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name, + image->GetFileSpec().GetFilename()); } - - } while (false); + } } template <class D> class TaggedASTDecl { @@ -915,8 +759,8 @@ DeclFromParser<D> DeclFromUser<D>::Import(ClangASTSource &source) { } bool ClangASTSource::FindObjCMethodDeclsWithOrigin( - unsigned int current_id, NameSearchContext &context, - ObjCInterfaceDecl *original_interface_decl, const char *log_info) { + NameSearchContext &context, ObjCInterfaceDecl *original_interface_decl, + const char *log_info) { const DeclarationName &decl_name(context.m_decl_name); clang::ASTContext *original_ctx = &original_interface_decl->getASTContext(); @@ -951,7 +795,7 @@ bool ClangASTSource::FindObjCMethodDeclsWithOrigin( llvm::SmallVector<NamedDecl *, 1> methods; - ClangASTContext::GetCompleteDecl(original_ctx, original_interface_decl); + TypeSystemClang::GetCompleteDecl(original_ctx, original_interface_decl); if (ObjCMethodDecl *instance_method_decl = original_interface_decl->lookupInstanceMethod(original_selector)) { @@ -987,7 +831,7 @@ bool ClangASTSource::FindObjCMethodDeclsWithOrigin( Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - LLDB_LOG(log, " CAS::FOMD[{0}] found ({1}) {2}", current_id, log_info, + LLDB_LOG(log, " CAS::FOMD found ({0}) {1}", log_info, ClangUtil::DumpDecl(copied_method_decl)); context.AddNamedDecl(copied_method_decl); @@ -996,11 +840,91 @@ bool ClangASTSource::FindObjCMethodDeclsWithOrigin( return true; } -void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { +void ClangASTSource::FindDeclInModules(NameSearchContext &context, + ConstString name) { Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - static unsigned int invocation_id = 0; - unsigned int current_id = invocation_id++; + ClangModulesDeclVendor *modules_decl_vendor = + m_target->GetClangModulesDeclVendor(); + if (!modules_decl_vendor) + return; + + bool append = false; + uint32_t max_matches = 1; + std::vector<clang::NamedDecl *> decls; + + if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) + return; + + LLDB_LOG(log, " CAS::FEVD Matching entity found for \"{0}\" in the modules", + name); + + clang::NamedDecl *const decl_from_modules = decls[0]; + + if (llvm::isa<clang::TypeDecl>(decl_from_modules) || + llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) || + llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) { + clang::Decl *copied_decl = CopyDecl(decl_from_modules); + clang::NamedDecl *copied_named_decl = + copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; + + if (!copied_named_decl) { + LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the modules"); + + return; + } + + context.AddNamedDecl(copied_named_decl); + + context.m_found_type = true; + } +} + +void ClangASTSource::FindDeclInObjCRuntime(NameSearchContext &context, + ConstString name) { + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); + + lldb::ProcessSP process(m_target->GetProcessSP()); + + if (!process) + return; + + ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process)); + + if (!language_runtime) + return; + + DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); + + if (!decl_vendor) + return; + + bool append = false; + uint32_t max_matches = 1; + std::vector<clang::NamedDecl *> decls; + + auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor); + if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls)) + return; + + LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\" in the runtime", + name); + + clang::Decl *copied_decl = CopyDecl(decls[0]); + clang::NamedDecl *copied_named_decl = + copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr; + + if (!copied_named_decl) { + LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the runtime"); + + return; + } + + context.AddNamedDecl(copied_named_decl); +} + +void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); const DeclarationName &decl_name(context.m_decl_name); const DeclContext *decl_ctx(context.m_decl_context); @@ -1020,8 +944,8 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original.decl); - if (FindObjCMethodDeclsWithOrigin(current_id, context, - original_interface_decl, "at origin")) + if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl, + "at origin")) return; // found it, no need to look any further } while (false); @@ -1046,12 +970,11 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { ConstString selector_name(ss.GetString()); - LLDB_LOGF(log, - "ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p " - "for selector [%s %s]", - current_id, static_cast<void *>(m_ast_context), - interface_decl->getNameAsString().c_str(), - selector_name.AsCString()); + LLDB_LOG(log, + "ClangASTSource::FindObjCMethodDecls on (ASTContext*){0} '{1}' " + "for selector [{2} {3}]", + m_ast_context, m_clang_ast_context->getDisplayName(), + interface_decl->getName(), selector_name); SymbolContextList sc_list; const bool include_symbols = false; @@ -1148,7 +1071,7 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { continue; ObjCMethodDecl *method_decl = - ClangASTContext::DeclContextGetAsObjCMethodDecl(function_decl_ctx); + TypeSystemClang::DeclContextGetAsObjCMethodDecl(function_decl_ctx); if (!method_decl) continue; @@ -1171,7 +1094,7 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { if (!copied_method_decl) continue; - LLDB_LOG(log, " CAS::FOMD[{0}] found (in symbols)\n{1}", current_id, + LLDB_LOG(log, " CAS::FOMD found (in symbols)\n{0}", ClangUtil::DumpDecl(copied_method_decl)); context.AddNamedDecl(copied_method_decl); @@ -1199,13 +1122,12 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { if (complete_interface_decl == interface_decl) break; // already checked this one - LLDB_LOGF(log, - "CAS::FOPD[%d] trying origin " - "(ObjCInterfaceDecl*)%p/(ASTContext*)%p...", - current_id, static_cast<void *>(complete_interface_decl), - static_cast<void *>(&complete_iface_decl->getASTContext())); + LLDB_LOG(log, + "CAS::FOPD trying origin " + "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...", + complete_interface_decl, &complete_iface_decl->getASTContext()); - FindObjCMethodDeclsWithOrigin(current_id, context, complete_interface_decl, + FindObjCMethodDeclsWithOrigin(context, complete_interface_decl, "in debug info"); return; @@ -1232,8 +1154,8 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { if (!interface_decl_from_modules) break; - if (FindObjCMethodDeclsWithOrigin( - current_id, context, interface_decl_from_modules, "in modules")) + if (FindObjCMethodDeclsWithOrigin(context, interface_decl_from_modules, + "in modules")) return; } } while (false); @@ -1273,13 +1195,13 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { if (!runtime_interface_decl) break; - FindObjCMethodDeclsWithOrigin(current_id, context, runtime_interface_decl, + FindObjCMethodDeclsWithOrigin(context, runtime_interface_decl, "in runtime"); } while (false); } static bool FindObjCPropertyAndIvarDeclsWithOrigin( - unsigned int current_id, NameSearchContext &context, ClangASTSource &source, + NameSearchContext &context, ClangASTSource &source, DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) { Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); @@ -1301,7 +1223,7 @@ static bool FindObjCPropertyAndIvarDeclsWithOrigin( DeclFromParser<ObjCPropertyDecl> parser_property_decl( origin_property_decl.Import(source)); if (parser_property_decl.IsValid()) { - LLDB_LOG(log, " CAS::FOPD[{0}] found\n{1}", current_id, + LLDB_LOG(log, " CAS::FOPD found\n{0}", ClangUtil::DumpDecl(parser_property_decl.decl)); context.AddNamedDecl(parser_property_decl.decl); @@ -1316,10 +1238,8 @@ static bool FindObjCPropertyAndIvarDeclsWithOrigin( DeclFromParser<ObjCIvarDecl> parser_ivar_decl( origin_ivar_decl.Import(source)); if (parser_ivar_decl.IsValid()) { - if (log) { - LLDB_LOG(log, " CAS::FOPD[{0}] found\n{1}", current_id, - ClangUtil::DumpDecl(parser_ivar_decl.decl)); - } + LLDB_LOG(log, " CAS::FOPD found\n{0}", + ClangUtil::DumpDecl(parser_ivar_decl.decl)); context.AddNamedDecl(parser_ivar_decl.decl); found = true; @@ -1332,9 +1252,6 @@ static bool FindObjCPropertyAndIvarDeclsWithOrigin( void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - static unsigned int invocation_id = 0; - unsigned int current_id = invocation_id++; - DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl( cast<ObjCInterfaceDecl>(context.m_decl_context)); DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl( @@ -1342,23 +1259,20 @@ void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { ConstString class_name(parser_iface_decl->getNameAsString().c_str()); - LLDB_LOGF(log, - "ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on " - "(ASTContext*)%p for '%s.%s'", - current_id, static_cast<void *>(m_ast_context), - parser_iface_decl->getNameAsString().c_str(), - context.m_decl_name.getAsString().c_str()); + LLDB_LOG(log, + "ClangASTSource::FindObjCPropertyAndIvarDecls on " + "(ASTContext*){0} '{1}' for '{2}.{3}'", + m_ast_context, m_clang_ast_context->getDisplayName(), + parser_iface_decl->getName(), context.m_decl_name.getAsString()); - if (FindObjCPropertyAndIvarDeclsWithOrigin( - current_id, context, *this, origin_iface_decl)) + if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, origin_iface_decl)) return; - LLDB_LOGF(log, - "CAS::FOPD[%d] couldn't find the property on origin " - "(ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching " - "elsewhere...", - current_id, static_cast<const void *>(origin_iface_decl.decl), - static_cast<void *>(&origin_iface_decl->getASTContext())); + LLDB_LOG(log, + "CAS::FOPD couldn't find the property on origin " + "(ObjCInterfaceDecl*){0}/(ASTContext*){1}, searching " + "elsewhere...", + origin_iface_decl.decl, &origin_iface_decl->getASTContext()); SymbolContext null_sc; TypeList type_list; @@ -1379,14 +1293,12 @@ void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { if (complete_iface_decl.decl == origin_iface_decl.decl) break; // already checked this one - LLDB_LOGF(log, - "CAS::FOPD[%d] trying origin " - "(ObjCInterfaceDecl*)%p/(ASTContext*)%p...", - current_id, static_cast<const void *>(complete_iface_decl.decl), - static_cast<void *>(&complete_iface_decl->getASTContext())); + LLDB_LOG(log, + "CAS::FOPD trying origin " + "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...", + complete_iface_decl.decl, &complete_iface_decl->getASTContext()); - FindObjCPropertyAndIvarDeclsWithOrigin(current_id, context, *this, - complete_iface_decl); + FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, complete_iface_decl); return; } while (false); @@ -1414,14 +1326,13 @@ void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { if (!interface_decl_from_modules.IsValid()) break; - LLDB_LOGF( - log, - "CAS::FOPD[%d] trying module " - "(ObjCInterfaceDecl*)%p/(ASTContext*)%p...", - current_id, static_cast<const void *>(interface_decl_from_modules.decl), - static_cast<void *>(&interface_decl_from_modules->getASTContext())); + LLDB_LOG(log, + "CAS::FOPD[{0}] trying module " + "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...", + interface_decl_from_modules.decl, + &interface_decl_from_modules->getASTContext()); - if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id, context, *this, + if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, interface_decl_from_modules)) return; } while (false); @@ -1459,19 +1370,43 @@ void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { if (!interface_decl_from_runtime.IsValid()) break; - LLDB_LOGF( - log, - "CAS::FOPD[%d] trying runtime " - "(ObjCInterfaceDecl*)%p/(ASTContext*)%p...", - current_id, static_cast<const void *>(interface_decl_from_runtime.decl), - static_cast<void *>(&interface_decl_from_runtime->getASTContext())); + LLDB_LOG(log, + "CAS::FOPD[{0}] trying runtime " + "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...", + interface_decl_from_runtime.decl, + &interface_decl_from_runtime->getASTContext()); - if (FindObjCPropertyAndIvarDeclsWithOrigin( - current_id, context, *this, interface_decl_from_runtime)) + if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, + interface_decl_from_runtime)) return; } while (false); } +void ClangASTSource::LookupInNamespace(NameSearchContext &context) { + const NamespaceDecl *namespace_context = + dyn_cast<NamespaceDecl>(context.m_decl_context); + + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); + + ClangASTImporter::NamespaceMapSP namespace_map = + m_ast_importer_sp->GetNamespaceMap(namespace_context); + + LLDB_LOGV(log, " CAS::FEVD Inspecting namespace map {0} ({1} entries)", + namespace_map.get(), namespace_map->size()); + + if (!namespace_map) + return; + + for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), + e = namespace_map->end(); + i != e; ++i) { + LLDB_LOG(log, " CAS::FEVD Searching namespace {0} in module {1}", + i->second.GetName(), i->first->GetFileSpec().GetFilename()); + + FindExternalVisibleDecls(context, i->first, i->second); + } +} + typedef llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsetMap; typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetMap; @@ -1557,17 +1492,14 @@ bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size, FieldOffsetMap &field_offsets, BaseOffsetMap &base_offsets, BaseOffsetMap &virtual_base_offsets) { - static unsigned int invocation_id = 0; - unsigned int current_id = invocation_id++; Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - LLDB_LOGF(log, - "LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p " - "[name = '%s']", - current_id, static_cast<void *>(m_ast_context), - static_cast<const void *>(record), - record->getNameAsString().c_str()); + LLDB_LOG(log, + "LayoutRecordType on (ASTContext*){0} '{1}' for (RecordDecl*)" + "{3} [name = '{4}']", + m_ast_context, m_clang_ast_context->getDisplayName(), record, + record->getName()); DeclFromParser<const RecordDecl> parser_record(record); DeclFromUser<const RecordDecl> origin_record( @@ -1580,7 +1512,7 @@ bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size, BaseOffsetMap origin_base_offsets; BaseOffsetMap origin_virtual_base_offsets; - ClangASTContext::GetCompleteDecl( + TypeSystemClang::GetCompleteDecl( &origin_record->getASTContext(), const_cast<RecordDecl *>(origin_record.decl)); @@ -1631,25 +1563,23 @@ bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size, m_ast_context->getCharWidth(); if (log) { - LLDB_LOGF(log, "LRT[%u] returned:", current_id); - LLDB_LOGF(log, "LRT[%u] Original = (RecordDecl*)%p", current_id, - static_cast<const void *>(origin_record.decl)); - LLDB_LOGF(log, "LRT[%u] Size = %" PRId64, current_id, size); - LLDB_LOGF(log, "LRT[%u] Alignment = %" PRId64, current_id, alignment); - LLDB_LOGF(log, "LRT[%u] Fields:", current_id); + LLDB_LOG(log, "LRT returned:"); + LLDB_LOG(log, "LRT Original = (RecordDecl*)%p", + static_cast<const void *>(origin_record.decl)); + LLDB_LOG(log, "LRT Size = %" PRId64, size); + LLDB_LOG(log, "LRT Alignment = %" PRId64, alignment); + LLDB_LOG(log, "LRT Fields:"); for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end(); fi != fe; ++fi) { - LLDB_LOGF(log, - "LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 - " bits", - current_id, static_cast<void *>(*fi), - fi->getNameAsString().c_str(), field_offsets[*fi]); + LLDB_LOG(log, + "LRT (FieldDecl*){0}, Name = '{1}', Offset = {2} bits", + *fi, fi->getName(), field_offsets[*fi]); } DeclFromParser<const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record); if (parser_cxx_record.IsValid()) { - LLDB_LOGF(log, "LRT[%u] Bases:", current_id); + LLDB_LOG(log, "LRT Bases:"); for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end(); @@ -1662,19 +1592,17 @@ bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size, DeclFromParser<CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record); - LLDB_LOGF( - log, - "LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 - " chars", - current_id, (is_virtual ? "Virtual " : ""), - static_cast<void *>(base_cxx_record.decl), - base_cxx_record.decl->getNameAsString().c_str(), - (is_virtual - ? virtual_base_offsets[base_cxx_record.decl].getQuantity() - : base_offsets[base_cxx_record.decl].getQuantity())); + LLDB_LOG(log, + "LRT {0}(CXXRecordDecl*){1}, Name = '{2}', Offset = " + "{3} chars", + (is_virtual ? "Virtual " : ""), base_cxx_record.decl, + base_cxx_record.decl->getName(), + (is_virtual + ? virtual_base_offsets[base_cxx_record.decl].getQuantity() + : base_offsets[base_cxx_record.decl].getQuantity())); } } else { - LLDB_LOGF(log, "LRD[%u] Not a CXXRecord, so no bases", current_id); + LLDB_LOG(log, "LRD Not a CXXRecord, so no bases"); } } @@ -1684,25 +1612,21 @@ bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size, void ClangASTSource::CompleteNamespaceMap( ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name, ClangASTImporter::NamespaceMapSP &parent_map) const { - static unsigned int invocation_id = 0; - unsigned int current_id = invocation_id++; Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); if (log) { if (parent_map && parent_map->size()) - LLDB_LOGF(log, - "CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for " - "namespace %s in namespace %s", - current_id, static_cast<void *>(m_ast_context), - name.GetCString(), - parent_map->begin()->second.GetName().AsCString()); + LLDB_LOG(log, + "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching " + "for namespace {2} in namespace {3}", + m_ast_context, m_clang_ast_context->getDisplayName(), name, + parent_map->begin()->second.GetName()); else - LLDB_LOGF(log, - "CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for " - "namespace %s", - current_id, static_cast<void *>(m_ast_context), - name.GetCString()); + LLDB_LOG(log, + "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching " + "for namespace {2}", + m_ast_context, m_clang_ast_context->getDisplayName(), name); } if (parent_map) { @@ -1720,7 +1644,7 @@ void ClangASTSource::CompleteNamespaceMap( continue; found_namespace_decl = - symbol_file->FindNamespace(name, &module_parent_namespace_decl); + symbol_file->FindNamespace(name, module_parent_namespace_decl); if (!found_namespace_decl) continue; @@ -1728,9 +1652,8 @@ void ClangASTSource::CompleteNamespaceMap( namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>( module_sp, found_namespace_decl)); - LLDB_LOGF(log, " CMN[%u] Found namespace %s in module %s", current_id, - name.GetCString(), - module_sp->GetFileSpec().GetFilename().GetCString()); + LLDB_LOG(log, " CMN Found namespace {0} in module {1}", name, + module_sp->GetFileSpec().GetFilename()); } } else { const ModuleList &target_images = m_target->GetImages(); @@ -1752,7 +1675,7 @@ void ClangASTSource::CompleteNamespaceMap( continue; found_namespace_decl = - symbol_file->FindNamespace(name, &null_namespace_decl); + symbol_file->FindNamespace(name, null_namespace_decl); if (!found_namespace_decl) continue; @@ -1760,9 +1683,8 @@ void ClangASTSource::CompleteNamespaceMap( namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>( image, found_namespace_decl)); - LLDB_LOGF(log, " CMN[%u] Found namespace %s in module %s", current_id, - name.GetCString(), - image->GetFileSpec().GetFilename().GetCString()); + LLDB_LOG(log, " CMN[{0}] Found namespace {0} in module {1}", name, + image->GetFileSpec().GetFilename()); } } } @@ -1776,11 +1698,11 @@ NamespaceDecl *ClangASTSource::AddNamespace( const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second; clang::ASTContext *src_ast = - ClangASTContext::DeclContextGetClangASTContext(namespace_decl); + TypeSystemClang::DeclContextGetTypeSystemClang(namespace_decl); if (!src_ast) return nullptr; clang::NamespaceDecl *src_namespace_decl = - ClangASTContext::DeclContextGetAsNamespaceDecl(namespace_decl); + TypeSystemClang::DeclContextGetAsNamespaceDecl(namespace_decl); if (!src_namespace_decl) return nullptr; @@ -1804,42 +1726,21 @@ NamespaceDecl *ClangASTSource::AddNamespace( } clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) { - if (m_ast_importer_sp) { - return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl); - } else { - lldbassert(0 && "No mechanism for copying a decl!"); - return nullptr; - } + return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl); } ClangASTImporter::DeclOrigin ClangASTSource::GetDeclOrigin(const clang::Decl *decl) { - if (m_ast_importer_sp) { - return m_ast_importer_sp->GetDeclOrigin(decl); - } else { - // this can happen early enough that no ExternalASTSource is installed. - return ClangASTImporter::DeclOrigin(); - } + return m_ast_importer_sp->GetDeclOrigin(decl); } CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) { - ClangASTContext *src_ast = - llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem()); + TypeSystemClang *src_ast = + llvm::dyn_cast_or_null<TypeSystemClang>(src_type.GetTypeSystem()); if (src_ast == nullptr) return CompilerType(); - SetImportInProgress(true); - - QualType copied_qual_type; - - if (m_ast_importer_sp) { - copied_qual_type = ClangUtil::GetQualType( - m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type)); - } else { - lldbassert(0 && "No mechanism for copying a type!"); - return CompilerType(); - } - - SetImportInProgress(false); + QualType copied_qual_type = ClangUtil::GetQualType( + m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type)); if (copied_qual_type.getAsOpaquePtr() && copied_qual_type->getCanonicalTypeInternal().isNull()) @@ -1849,170 +1750,3 @@ CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) { return m_clang_ast_context->GetType(copied_qual_type); } - -clang::NamedDecl *NameSearchContext::AddVarDecl(const CompilerType &type) { - assert(type && "Type for variable must be valid!"); - - if (!type.IsValid()) - return nullptr; - - ClangASTContext *lldb_ast = - llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); - if (!lldb_ast) - return nullptr; - - IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo(); - - clang::ASTContext &ast = lldb_ast->getASTContext(); - - clang::NamedDecl *Decl = VarDecl::Create( - ast, const_cast<DeclContext *>(m_decl_context), SourceLocation(), - SourceLocation(), ii, ClangUtil::GetQualType(type), nullptr, SC_Static); - m_decls.push_back(Decl); - - return Decl; -} - -clang::NamedDecl *NameSearchContext::AddFunDecl(const CompilerType &type, - bool extern_c) { - assert(type && "Type for variable must be valid!"); - - if (!type.IsValid()) - return nullptr; - - if (m_function_types.count(type)) - return nullptr; - - ClangASTContext *lldb_ast = - llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); - if (!lldb_ast) - return nullptr; - - m_function_types.insert(type); - - QualType qual_type(ClangUtil::GetQualType(type)); - - clang::ASTContext &ast = lldb_ast->getASTContext(); - - const bool isInlineSpecified = false; - const bool hasWrittenPrototype = true; - const bool isConstexprSpecified = false; - - clang::DeclContext *context = const_cast<DeclContext *>(m_decl_context); - - if (extern_c) { - context = LinkageSpecDecl::Create( - ast, context, SourceLocation(), SourceLocation(), - clang::LinkageSpecDecl::LanguageIDs::lang_c, false); - } - - // Pass the identifier info for functions the decl_name is needed for - // operators - clang::DeclarationName decl_name = - m_decl_name.getNameKind() == DeclarationName::Identifier - ? m_decl_name.getAsIdentifierInfo() - : m_decl_name; - - clang::FunctionDecl *func_decl = FunctionDecl::Create( - ast, context, SourceLocation(), SourceLocation(), decl_name, qual_type, - nullptr, SC_Extern, isInlineSpecified, hasWrittenPrototype, - isConstexprSpecified ? CSK_constexpr : CSK_unspecified); - - // We have to do more than just synthesize the FunctionDecl. We have to - // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do - // this, we raid the function's FunctionProtoType for types. - - const FunctionProtoType *func_proto_type = - qual_type.getTypePtr()->getAs<FunctionProtoType>(); - - if (func_proto_type) { - unsigned NumArgs = func_proto_type->getNumParams(); - unsigned ArgIndex; - - SmallVector<ParmVarDecl *, 5> parm_var_decls; - - for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) { - QualType arg_qual_type(func_proto_type->getParamType(ArgIndex)); - - parm_var_decls.push_back( - ParmVarDecl::Create(ast, const_cast<DeclContext *>(context), - SourceLocation(), SourceLocation(), nullptr, - arg_qual_type, nullptr, SC_Static, nullptr)); - } - - func_decl->setParams(ArrayRef<ParmVarDecl *>(parm_var_decls)); - } else { - Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); - - LLDB_LOGF(log, "Function type wasn't a FunctionProtoType"); - } - - // If this is an operator (e.g. operator new or operator==), only insert the - // declaration we inferred from the symbol if we can provide the correct - // number of arguments. We shouldn't really inject random decl(s) for - // functions that are analyzed semantically in a special way, otherwise we - // will crash in clang. - clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; - if (func_proto_type && - ClangASTContext::IsOperator(decl_name.getAsString().c_str(), op_kind)) { - if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( - false, op_kind, func_proto_type->getNumParams())) - return nullptr; - } - m_decls.push_back(func_decl); - - return func_decl; -} - -clang::NamedDecl *NameSearchContext::AddGenericFunDecl() { - FunctionProtoType::ExtProtoInfo proto_info; - - proto_info.Variadic = true; - - QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType( - m_ast_source.m_ast_context->UnknownAnyTy, // result - ArrayRef<QualType>(), // argument types - proto_info)); - - return AddFunDecl( - m_ast_source.m_clang_ast_context->GetType(generic_function_type), true); -} - -clang::NamedDecl * -NameSearchContext::AddTypeDecl(const CompilerType &clang_type) { - if (ClangUtil::IsClangType(clang_type)) { - QualType qual_type = ClangUtil::GetQualType(clang_type); - - if (const TypedefType *typedef_type = - llvm::dyn_cast<TypedefType>(qual_type)) { - TypedefNameDecl *typedef_name_decl = typedef_type->getDecl(); - - m_decls.push_back(typedef_name_decl); - - return (NamedDecl *)typedef_name_decl; - } else if (const TagType *tag_type = qual_type->getAs<TagType>()) { - TagDecl *tag_decl = tag_type->getDecl(); - - m_decls.push_back(tag_decl); - - return tag_decl; - } else if (const ObjCObjectType *objc_object_type = - qual_type->getAs<ObjCObjectType>()) { - ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface(); - - m_decls.push_back((NamedDecl *)interface_decl); - - return (NamedDecl *)interface_decl; - } - } - return nullptr; -} - -void NameSearchContext::AddLookupResult(clang::DeclContextLookupResult result) { - for (clang::NamedDecl *decl : result) - m_decls.push_back(decl); -} - -void NameSearchContext::AddNamedDecl(clang::NamedDecl *decl) { - m_decls.push_back(decl); -} |