Skip to content

[Syntax Highlighting] Add name and parameters syntax highlighting in Swift backtraces #10710

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

Open
wants to merge 6 commits into
base: swift/release/6.2
Choose a base branch
from

Conversation

charles-zablit
Copy link

This PR adds function name syntax highlighting in LLDB Swift backtraces.

Motivation

In this patch, @Michael137 implemented name highlighting for methods in the C++ plugin of LLDB. This results in better readability when reading backtraces of functions with long scopes.

Please find below an example of the same feature, ported to Swift (which this patch implements):
Before:
Screenshot 2025-05-19 at 18 25 55

After:
Screenshot 2025-05-19 at 18 25 34

Implementation details

swiftlang/swift#81511 implements a way to override the NodePrinter, which allows to track relevant ranges. Once the correct ranges are tracked, we return a slice of the demangled string for the relevant part of the highlighting query.

The rest of the implementation is a close copy of @Michael137 's work.

Testing

The tests are implemented using the manglings.txt file in the swift repo.

Follow ups

  • This patch only tracks the name and parameters of the function. In order to display the scope and qualifiers of the function, we mark them in the catch all prefix and suffix ranges. We could track those ranges as well to grey out some information that are not important, eg the fact the function is fileprivate.

@charles-zablit charles-zablit requested a review from a team as a code owner May 20, 2025 16:36
@charles-zablit charles-zablit changed the title Charles zablit/lldb/swift mangling [Syntax Highlighting] Add name and parameters syntax highlighting in Swift backtraces May 20, 2025
@adrian-prantl
Copy link

Are there some bits (such as the prefix and suffix) that we could do upstream? Even with an empty default implementation, to document the settings etc?

@charles-zablit
Copy link
Author

charles-zablit commented May 27, 2025

Are there some bits (such as the prefix and suffix) that we could do upstream? Even with an empty default implementation, to document the settings etc?

llvm#140762 implements the "generic" parts of the PR that are a good fit for upstream.

Michael137 pushed a commit to llvm/llvm-project that referenced this pull request May 28, 2025
This PR is a subset of the commits made in
swiftlang#10710.

The most notable change is the addition of `PrefixRange` and
`SuffixRange` which are a catch-all to track anything after or before a
function's demangled name. In the case of Swift, this allows to add
support for name highlighting without having to track the range of the
scope and specifiers of a function (this will come in another PR).
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request May 28, 2025
This PR is a subset of the commits made in
swiftlang/llvm-project#10710.

The most notable change is the addition of `PrefixRange` and
`SuffixRange` which are a catch-all to track anything after or before a
function's demangled name. In the case of Swift, this allows to add
support for name highlighting without having to track the range of the
scope and specifiers of a function (this will come in another PR).
return false;
}

bool SwiftLanguage::GetFunctionDisplayArgs(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add this helper function in a separate patch. Makes it easier to review

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is needed for the implementation of bool SwiftLanguage::HandleFrameFormatVariable. I don't think I can remove it without copy pasting the logic at the call site.

Say this is PR A. Would it be OK to create a new PR B which introduces this helper function, merge PR B and then rebase the PR A on the changes introduced by PR B?

/// Returns \c true if this object holds a valid basename range.
bool hasBasename() const {
return BasenameRange.second > BasenameRange.first &&
BasenameRange.second > 0;
return BasenameRange.second > BasenameRange.first;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry if you've previously answered this, but why do we need this change?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do require it, we should have that upstream too

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not needed but since I added the hasArguments method I wanted to remove the redundant >0 check.

Since the start and end range are unsigned, if end > start, then end is always greater than 0, which is why I removed the second check. I might be missing something.

I'm happy to remove the change to minimize changes in the PR.

/// Indicates the [start, end) of the function's prefix. This is a
/// catch-all range for anything that is not tracked by the rest of
/// the pairs.
std::pair<size_t, size_t> PrefixRange{};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialization here is redundant

Copy link

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On quick read-through LGTM. I think you just need to rebase on top of some upstream changes (e.g., the MangledTest changes are outdated i think). Also left some minor comments

@charles-zablit charles-zablit force-pushed the charles-zablit/lldb/swift-mangling branch from e2ae90b to 7113755 Compare May 29, 2025 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants