Skip to content

add range tracking when demangling a name #82298

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 2 commits into
base: release/6.2
Choose a base branch
from

Conversation

charles-zablit
Copy link
Contributor

@charles-zablit charles-zablit commented Jun 17, 2025

This PR implements range tracking for the name and parameters of a demangled function.

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.

I am working on implementing the same feature for the Swift plugin in LLDB. Please see an example below:
Before:
lldb-swift-without-highlighting

After:
lldb-swift-with-highlighting

Notice how the name and parameters of the functions are highlighted differently than the rest.
(Please note that the difference in text contents are temporary, and should disappear before for the final patch).

Implementation details

To implement this feature in the Swift plugin of LLDB, we need to provide LLDB with the ranges of the "components" of the demangled name. For instance the baseName of bar.foo() spans 4...7, while the parameters span 7...9.

This information allows LLDB to build the name that is displayed in the backtraces.

Original implementation

This patch introduces a new class called TrackingDemanglerPrinter which stores the information needed to track the ranges. It can later be extended to track more ranges. The methods it defines are called in the print method of NodePrinter.cpp.

The previous behavior is still available when calling DemangleSymbolAsString without a printer, as this will result in a no op when startName is called for instance.

Current implementation

2 methods were made virtual in NodePrinter, so that any consumer can override them and track the ranges they need. This is less lldb specific than the previous method.

Testing (removed, relevant only for the original implementation)

To implement the tests for the range tracking, I used the manglings.txt file. It now contains the ranges for the name and parameters where that's relevant. The format I chose to serialize the ranges is arbitrary and I'm happy to discuss any change, especially when considering we might want to add more tracked ranges.

I also developed a Python script to help review the test cases by coloring them. Please find it here for reference. It could be interesting to add it to the repo.

Follow ups

  • We will need to track more than just the name and the parameters to implement the changes in the LLDB plugin. We will also need the qualifiers, return type and probably a catch all "suffix".
  • It could be interesting to implement syntax highlighting in swift-demangle, as these changes make it fairly trivial to implement, and it would greatly help with readability.

Please note that it was originally opened here #81511 but I moved to a different target branch.

This PR depends on #82303

@charles-zablit charles-zablit requested a review from a team as a code owner June 17, 2025 15:27
@charles-zablit charles-zablit self-assigned this Jun 17, 2025
@charles-zablit charles-zablit added the demangling Area → compiler: The `libswiftDemangling` library label Jun 17, 2025
@charles-zablit charles-zablit force-pushed the charles-zablit/mangling branch from c95ffe3 to 51fd790 Compare June 17, 2025 16:33
@adrian-prantl
Copy link
Contributor

Are these cherry-picks from main? If not, can you rebase this PR to main and then create another PR that just cherry-picks this to the release branch?

(In case you are wondering: yes, this is exactly the inverse workflow that LLDB uses).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
demangling Area → compiler: The `libswiftDemangling` library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants