Author: Raphael Isemann Date: 2020-08-06T17:37:29+02:00 New Revision: f6913e74400aa932b3edc7cc765495247799fcb0
URL: https://github.com/llvm/llvm-project/commit/f6913e74400aa932b3edc7cc765495247799fcb0 DIFF: https://github.com/llvm/llvm-project/commit/f6913e74400aa932b3edc7cc765495247799fcb0.diff LOG: [lldb][NFC] Document and encapsulate OriginMap in ASTContextMetadata Just adds the respective accessor functions to ASTContextMetadata instead of directly exposing the OriginMap to the whole world. Added: Modified: lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.h Removed: ################################################################################ diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp index ac16738933ac..6d8773779a69 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.cpp @@ -245,9 +245,9 @@ class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener { m_decls_already_completed.insert(decl); // We should only complete decls coming from the source context. - assert(to_context_md->m_origins[decl].ctx == m_src_ctx); + assert(to_context_md->getOrigin(decl).ctx == m_src_ctx); - Decl *original_decl = to_context_md->m_origins[decl].decl; + Decl *original_decl = to_context_md->getOrigin(decl).decl; // Complete the decl now. TypeSystemClang::GetCompleteDecl(m_src_ctx, original_decl); @@ -266,7 +266,7 @@ class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener { container_decl->setHasExternalVisibleStorage(false); } - to_context_md->m_origins.erase(decl); + to_context_md->removeOrigin(decl); } // Stop listening to imported decls. We do this after clearing the @@ -581,10 +581,7 @@ bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); - OriginMap &origins = context_md->m_origins; - - origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl); - + context_md->setOrigin(decl, DeclOrigin(origin_ast_ctx, origin_decl)); return true; } @@ -721,29 +718,14 @@ ClangASTImporter::DeclOrigin ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) { ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); - OriginMap &origins = context_md->m_origins; - - OriginMap::iterator iter = origins.find(decl); - - if (iter != origins.end()) - return iter->second; - return DeclOrigin(); + return context_md->getOrigin(decl); } void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl, clang::Decl *original_decl) { ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); - - OriginMap &origins = context_md->m_origins; - - OriginMap::iterator iter = origins.find(decl); - - if (iter != origins.end()) { - iter->second.decl = original_decl; - iter->second.ctx = &original_decl->getASTContext(); - return; - } - origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl); + context_md->setOrigin( + decl, DeclOrigin(&original_decl->getASTContext(), original_decl)); } void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl, @@ -817,14 +799,7 @@ void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast, return; md->m_delegates.erase(src_ast); - - for (OriginMap::iterator iter = md->m_origins.begin(); - iter != md->m_origins.end();) { - if (iter->second.ctx == src_ast) - md->m_origins.erase(iter++); - else - ++iter; - } + md->removeOriginsWithContext(src_ast); } ClangASTImporter::MapCompleter::~MapCompleter() { return; } @@ -1100,37 +1075,31 @@ void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from, m_master.MaybeGetContextMetadata(m_source_ctx); if (from_context_md) { - OriginMap &origins = from_context_md->m_origins; + DeclOrigin origin = from_context_md->getOrigin(from); - OriginMap::iterator origin_iter = origins.find(from); - - if (origin_iter != origins.end()) { - if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() || - user_id != LLDB_INVALID_UID) { - if (origin_iter->second.ctx != &to->getASTContext()) - to_context_md->m_origins[to] = origin_iter->second; - } + if (origin.Valid()) { + if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID) + if (origin.ctx != &to->getASTContext()) + to_context_md->setOrigin(to, origin); ImporterDelegateSP direct_completer = - m_master.GetDelegate(&to->getASTContext(), origin_iter->second.ctx); + m_master.GetDelegate(&to->getASTContext(), origin.ctx); if (direct_completer.get() != this) - direct_completer->ASTImporter::Imported(origin_iter->second.decl, to); + direct_completer->ASTImporter::Imported(origin.decl, to); LLDB_LOG(log, " [ClangASTImporter] Propagated origin " "(Decl*){0}/(ASTContext*){1} from (ASTContext*){2} to " "(ASTContext*){3}", - origin_iter->second.decl, origin_iter->second.ctx, - &from->getASTContext(), &to->getASTContext()); + origin.decl, origin.ctx, &from->getASTContext(), + &to->getASTContext()); } else { if (m_new_decl_listener) m_new_decl_listener->NewDeclImported(from, to); - if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() || - user_id != LLDB_INVALID_UID) { - to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from); - } + if (!to_context_md->hasOrigin(to) || user_id != LLDB_INVALID_UID) + to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from)); LLDB_LOG(log, " [ClangASTImporter] Decl has no origin information in " @@ -1151,7 +1120,7 @@ void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from, namespace_map_iter->second; } } else { - to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from); + to_context_md->setOrigin(to, DeclOrigin(m_source_ctx, from)); LLDB_LOG(log, " [ClangASTImporter] Sourced origin " diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.h b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.h index 6ceec774914b..b8c751479d4b 100644 --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.h +++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangASTImporter.h @@ -166,8 +166,6 @@ class ClangASTImporter { clang::Decl *decl; }; - typedef llvm::DenseMap<const clang::Decl *, DeclOrigin> OriginMap; - /// Listener interface used by the ASTImporterDelegate to inform other code /// about decls that have been imported the first time. struct NewDeclListener { @@ -259,17 +257,61 @@ class ClangASTImporter { typedef llvm::DenseMap<const clang::NamespaceDecl *, NamespaceMapSP> NamespaceMetaMap; - struct ASTContextMetadata { - ASTContextMetadata(clang::ASTContext *dst_ctx) - : m_dst_ctx(dst_ctx), m_delegates(), m_origins(), m_namespace_maps(), - m_map_completer(nullptr) {} + class ASTContextMetadata { + typedef llvm::DenseMap<const clang::Decl *, DeclOrigin> OriginMap; + + public: + ASTContextMetadata(clang::ASTContext *dst_ctx) : m_dst_ctx(dst_ctx) {} clang::ASTContext *m_dst_ctx; DelegateMap m_delegates; - OriginMap m_origins; NamespaceMetaMap m_namespace_maps; - MapCompleter *m_map_completer; + MapCompleter *m_map_completer = nullptr; + + /// Sets the DeclOrigin for the given Decl and overwrites any existing + /// DeclOrigin. + void setOrigin(const clang::Decl *decl, DeclOrigin origin) { + m_origins[decl] = origin; + } + + /// Removes any tracked DeclOrigin for the given decl. + void removeOrigin(const clang::Decl *decl) { m_origins.erase(decl); } + + /// Remove all DeclOrigin entries that point to the given ASTContext. + /// Useful when an ASTContext is about to be deleted and all the dangling + /// pointers to it need to be removed. + void removeOriginsWithContext(clang::ASTContext *ctx) { + for (OriginMap::iterator iter = m_origins.begin(); + iter != m_origins.end();) { + if (iter->second.ctx == ctx) + m_origins.erase(iter++); + else + ++iter; + } + } + + /// Returns the DeclOrigin for the given Decl or an invalid DeclOrigin + /// instance if there no known DeclOrigin for the given Decl. + DeclOrigin getOrigin(const clang::Decl *decl) const { + auto iter = m_origins.find(decl); + if (iter == m_origins.end()) + return DeclOrigin(); + return iter->second; + } + + /// Returns true there is a known DeclOrigin for the given Decl. + bool hasOrigin(const clang::Decl *decl) const { + return getOrigin(decl).Valid(); + } + + private: + /// Maps declarations to the ASTContext/Decl from which they were imported + /// from. If a declaration is from an ASTContext which has been deleted + /// since the declaration was imported or the declaration wasn't created by + /// the ASTImporter, then it doesn't have a DeclOrigin and will not be + /// tracked here. + OriginMap m_origins; }; typedef std::shared_ptr<ASTContextMetadata> ASTContextMetadataSP; _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits