Skip to content

[lldb][Mangled] Move SuffixRange computation into TrackingOutputBuffer #152483

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 2 commits into from
Aug 7, 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
11 changes: 11 additions & 0 deletions lldb/source/Core/DemangledNameInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ void TrackingOutputBuffer::finalizeEnd() {
if (NameInfo.ScopeRange.first > NameInfo.ScopeRange.second)
NameInfo.ScopeRange.second = NameInfo.ScopeRange.first;
NameInfo.BasenameRange.first = NameInfo.ScopeRange.second;

// We call anything past the FunctionEncoding the "suffix".
// In practice this would be nodes like `DotSuffix` that wrap
// a FunctionEncoding.
NameInfo.SuffixRange.first = getCurrentPosition();
}

ScopedOverride<unsigned> TrackingOutputBuffer::enterFunctionTypePrinting() {
Expand Down Expand Up @@ -138,6 +143,9 @@ void TrackingOutputBuffer::printLeft(const Node &N) {
default:
OutputBuffer::printLeft(N);
}

// Keep updating suffix until we reach the end.
NameInfo.SuffixRange.second = getCurrentPosition();
}

void TrackingOutputBuffer::printRight(const Node &N) {
Expand All @@ -151,6 +159,9 @@ void TrackingOutputBuffer::printRight(const Node &N) {
default:
OutputBuffer::printRight(N);
}

// Keep updating suffix until we reach the end.
NameInfo.SuffixRange.second = getCurrentPosition();
}

void TrackingOutputBuffer::printLeftImpl(const FunctionType &N) {
Expand Down
3 changes: 0 additions & 3 deletions lldb/source/Core/Mangled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ GetItaniumDemangledStr(const char *M) {

TrackingOutputBuffer OB(demangled_cstr, demangled_size);
demangled_cstr = ipd.finishDemangle(&OB);
// TODO: we should set the SuffixRange inside the TrackingOutputBuffer.
OB.NameInfo.SuffixRange.first = OB.NameInfo.QualifiersRange.second;
OB.NameInfo.SuffixRange.second = std::string_view(OB).size();
info = std::move(OB.NameInfo);

assert(demangled_cstr &&
Expand Down
25 changes: 12 additions & 13 deletions lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,16 @@ GetDemangledScope(const SymbolContext &sc) {
return CPlusPlusLanguage::GetDemangledScope(demangled_name, info);
}

/// Handles anything printed after the FunctionEncoding ItaniumDemangle
/// node. Most notably the DotSuffix node.
///
/// FIXME: the suffix should also have an associated
/// CPlusPlusLanguage::GetDemangledFunctionSuffix
/// once we start setting the `DemangledNameInfo::SuffixRange`
/// from inside the `TrackingOutputBuffer`.
llvm::Expected<llvm::StringRef>
CPlusPlusLanguage::GetDemangledFunctionSuffix(llvm::StringRef demangled,
const DemangledNameInfo &info) {
if (!info.hasSuffix())
return llvm::createStringError("Suffix range for '%s' is invalid.",
demangled.data());

return demangled.slice(info.SuffixRange.first, info.SuffixRange.second);
}

static llvm::Expected<llvm::StringRef>
GetDemangledFunctionSuffix(const SymbolContext &sc) {
auto info_or_err = GetAndValidateInfo(sc);
Expand All @@ -407,11 +410,7 @@ GetDemangledFunctionSuffix(const SymbolContext &sc) {

auto [demangled_name, info] = *info_or_err;

if (!info.hasSuffix())
return llvm::createStringError("Suffix range for '%s' is invalid.",
demangled_name.data());

return demangled_name.slice(info.SuffixRange.first, info.SuffixRange.second);
return CPlusPlusLanguage::GetDemangledFunctionSuffix(demangled_name, info);
}

llvm::Expected<llvm::StringRef>
Expand Down Expand Up @@ -2424,7 +2423,7 @@ bool CPlusPlusLanguage::HandleFrameFormatVariable(
return true;
}
case FormatEntity::Entry::Type::FunctionSuffix: {
auto suffix_or_err = GetDemangledFunctionSuffix(sc);
auto suffix_or_err = ::GetDemangledFunctionSuffix(sc);
if (!suffix_or_err) {
LLDB_LOG_ERROR(
GetLog(LLDBLog::Language), suffix_or_err.takeError(),
Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class CPlusPlusLanguage : public Language {
GetDemangledFunctionArguments(llvm::StringRef demangled,
const DemangledNameInfo &info);

static llvm::Expected<llvm::StringRef>
GetDemangledFunctionSuffix(llvm::StringRef demangled,
const DemangledNameInfo &info);

// Extract C++ context and identifier from a string using heuristic matching
// (as opposed to
// CPlusPlusLanguage::CxxMethodName which has to have a fully qualified C++
Expand Down
8 changes: 4 additions & 4 deletions lldb/unittests/Core/MangledTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,10 +889,10 @@ TEST_P(DemanglingInfoCorrectnessTestFixutre, Correctness) {
EXPECT_THAT_EXPECTED(qualifiers, llvm::Succeeded());
reconstructed_name += *qualifiers;

// TODO: should retrieve suffix using the plugin too.
auto suffix = tracked_name.slice(OB->NameInfo.QualifiersRange.second,
llvm::StringRef::npos);
reconstructed_name += suffix;
auto suffix =
CPlusPlusLanguage::GetDemangledFunctionSuffix(tracked_name, OB->NameInfo);
EXPECT_THAT_EXPECTED(suffix, llvm::Succeeded());
reconstructed_name += *suffix;

EXPECT_EQ(reconstructed_name, demangled);
}
Expand Down
Loading