Skip to content

[lldb][SymbolFileDWARF][NFC] Add FindFunctionDefinition helper #152733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lldb/source/Expression/IRExecutionUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ ResolveFunctionCallLabel(const FunctionCallLabel &label,
auto sc_or_err = symbol_file->ResolveFunctionCallLabel(label);
if (!sc_or_err)
return llvm::joinErrors(
llvm::createStringError("failed to resolve function by UID"),
llvm::createStringError("failed to resolve function by UID:"),
sc_or_err.takeError());

SymbolContextList sc_list;
Expand Down
54 changes: 30 additions & 24 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,30 @@ bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die,
return false;
}

DWARFDIE
SymbolFileDWARF::FindFunctionDefinition(const FunctionCallLabel &label) {
DWARFDIE definition;
Module::LookupInfo info(ConstString(label.lookup_name),
lldb::eFunctionNameTypeFull,
lldb::eLanguageTypeUnknown);

m_index->GetFunctions(info, *this, {}, [&](DWARFDIE entry) {
if (entry.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_declaration, 0))
return IterationAction::Continue;

// We don't check whether the specification DIE for this function
// corresponds to the declaration DIE because the declaration might be in
// a type-unit but the definition in the compile-unit (and it's
// specifcation would point to the declaration in the compile-unit). We
// rely on the mangled name within the module to be enough to find us the
// unique definition.
definition = entry;
return IterationAction::Stop;
});

return definition;
}

llvm::Expected<SymbolContext>
SymbolFileDWARF::ResolveFunctionCallLabel(const FunctionCallLabel &label) {
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Expand All @@ -2495,37 +2519,19 @@ SymbolFileDWARF::ResolveFunctionCallLabel(const FunctionCallLabel &label) {
// Label was created using a declaration DIE. Need to fetch the definition
// to resolve the function call.
if (die.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_declaration, 0)) {
Module::LookupInfo info(ConstString(label.lookup_name),
lldb::eFunctionNameTypeFull,
lldb::eLanguageTypeUnknown);

m_index->GetFunctions(info, *this, {}, [&](DWARFDIE entry) {
if (entry.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_declaration, 0))
return IterationAction::Continue;

// We don't check whether the specification DIE for this function
// corresponds to the declaration DIE because the declaration might be in
// a type-unit but the definition in the compile-unit (and it's
// specifcation would point to the declaration in the compile-unit). We
// rely on the mangled name within the module to be enough to find us the
// unique definition.
die = entry;
return IterationAction::Stop;
});
auto definition = FindFunctionDefinition(label);
if (!definition)
return llvm::createStringError("failed to find definition DIE");

if (die.GetAttributeValueAsUnsigned(llvm::dwarf::DW_AT_declaration, 0))
return llvm::createStringError(
llvm::formatv("failed to find definition DIE for {0}", label));
die = std::move(definition);
}

SymbolContextList sc_list;
if (!ResolveFunction(die, /*include_inlines=*/false, sc_list))
return llvm::createStringError(
llvm::formatv("failed to resolve function for {0}", label));
return llvm::createStringError("failed to resolve function");

if (sc_list.IsEmpty())
return llvm::createStringError(
llvm::formatv("failed to find function for {0}", label));
return llvm::createStringError("failed to find function");

assert(sc_list.GetSize() == 1);

Expand Down
7 changes: 7 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ class SymbolFileDWARF : public SymbolFileCommon {
/// Returns the DWARFIndex for this symbol, if it exists.
DWARFIndex *getIndex() { return m_index.get(); }

private:
/// Find the definition DIE for the specified \c label in this
/// SymbolFile.
///
/// \returns A valid definition DIE on success.
DWARFDIE FindFunctionDefinition(const FunctionCallLabel &label);

protected:
SymbolFileDWARF(const SymbolFileDWARF &) = delete;
const SymbolFileDWARF &operator=(const SymbolFileDWARF &) = delete;
Expand Down
Loading