Skip to content

Commit c34478f

Browse files
committed
[lldb][NFC] Move ClangExpressionDeclMap's persistent decl search into its own function
Searching persistent decls is a small subset of the things FindExternalVisibleDecls does. It should be its own function instead of being encapsulated in this `do { } while(false);` pattern.
1 parent b80e483 commit c34478f

File tree

2 files changed

+73
-53
lines changed

2 files changed

+73
-53
lines changed

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -771,19 +771,64 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls(
771771
ClangASTSource::FindExternalVisibleDecls(context);
772772
}
773773

774+
void ClangExpressionDeclMap::MaybeRegisterFunctionBody(
775+
FunctionDecl *copied_function_decl) {
776+
if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
777+
clang::DeclGroupRef decl_group_ref(copied_function_decl);
778+
m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
779+
}
780+
}
781+
782+
void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
783+
const ConstString name,
784+
unsigned int current_id) {
785+
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
786+
Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
787+
if (!target)
788+
return;
789+
790+
ClangASTContext *scratch_clang_ast_context =
791+
target->GetScratchClangASTContext();
792+
793+
if (!scratch_clang_ast_context)
794+
return;
795+
796+
ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
797+
798+
if (!scratch_ast_context)
799+
return;
800+
801+
NamedDecl *persistent_decl =
802+
m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
803+
804+
if (!persistent_decl)
805+
return;
806+
807+
Decl *parser_persistent_decl = CopyDecl(persistent_decl);
808+
809+
if (!parser_persistent_decl)
810+
return;
811+
812+
NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
813+
814+
if (!parser_named_decl)
815+
return;
816+
817+
if (clang::FunctionDecl *parser_function_decl =
818+
llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
819+
MaybeRegisterFunctionBody(parser_function_decl);
820+
}
821+
822+
LLDB_LOGF(log, " CEDM::FEVD[%u] Found persistent decl %s", current_id,
823+
name.GetCString());
824+
825+
context.AddNamedDecl(parser_named_decl);
826+
}
774827
void ClangExpressionDeclMap::FindExternalVisibleDecls(
775828
NameSearchContext &context, lldb::ModuleSP module_sp,
776829
CompilerDeclContext &namespace_decl, unsigned int current_id) {
777830
assert(m_ast_context);
778831

779-
std::function<void(clang::FunctionDecl *)> MaybeRegisterFunctionBody =
780-
[this](clang::FunctionDecl *copied_function_decl) {
781-
if (copied_function_decl->getBody() && m_parser_vars->m_code_gen) {
782-
DeclGroupRef decl_group_ref(copied_function_decl);
783-
m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
784-
}
785-
};
786-
787832
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
788833

789834
SymbolContextList sc_list;
@@ -802,51 +847,8 @@ void ClangExpressionDeclMap::FindExternalVisibleDecls(
802847
lldb::eSymbolContextBlock);
803848

804849
// Try the persistent decls, which take precedence over all else.
805-
if (!namespace_decl) {
806-
do {
807-
if (!target)
808-
break;
809-
810-
ClangASTContext *scratch_clang_ast_context =
811-
target->GetScratchClangASTContext();
812-
813-
if (!scratch_clang_ast_context)
814-
break;
815-
816-
ASTContext *scratch_ast_context =
817-
scratch_clang_ast_context->getASTContext();
818-
819-
if (!scratch_ast_context)
820-
break;
821-
822-
NamedDecl *persistent_decl =
823-
m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
824-
825-
if (!persistent_decl)
826-
break;
827-
828-
Decl *parser_persistent_decl = CopyDecl(persistent_decl);
829-
830-
if (!parser_persistent_decl)
831-
break;
832-
833-
NamedDecl *parser_named_decl =
834-
dyn_cast<NamedDecl>(parser_persistent_decl);
835-
836-
if (!parser_named_decl)
837-
break;
838-
839-
if (clang::FunctionDecl *parser_function_decl =
840-
llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
841-
MaybeRegisterFunctionBody(parser_function_decl);
842-
}
843-
844-
LLDB_LOGF(log, " CEDM::FEVD[%u] Found persistent decl %s", current_id,
845-
name.GetCString());
846-
847-
context.AddNamedDecl(parser_named_decl);
848-
} while (false);
849-
}
850+
if (!namespace_decl)
851+
SearchPersistenDecls(context, name, current_id);
850852

851853
if (name.GetCString()[0] == '$' && !namespace_decl) {
852854
static ConstString g_lldb_class_name("$__lldb_class");

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,24 @@ class ClangExpressionDeclMap : public ClangASTSource {
372372
/// from persistent variables.
373373
uint64_t GetParserID() { return (uint64_t) this; }
374374

375+
/// Should be called on all copied functions.
376+
void MaybeRegisterFunctionBody(clang::FunctionDecl *copied_function_decl);
377+
378+
/// Searches the persistent decls of the target for entities with the
379+
/// given name.
380+
///
381+
/// \param[in] context
382+
/// The NameSearchContext that can construct Decls for this name.
383+
///
384+
/// \param[in] name
385+
/// The name of the entities that need to be found.
386+
///
387+
/// \param[in] current_id
388+
/// The ID for the current FindExternalVisibleDecls invocation,
389+
/// for logging purposes.
390+
void SearchPersistenDecls(NameSearchContext &context, const ConstString name,
391+
unsigned int current_id);
392+
375393
/// Given a target, find a variable that matches the given name and type.
376394
///
377395
/// \param[in] target

0 commit comments

Comments
 (0)