Skip to content
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

Show variable references in call hierarchy #272

Open
i-garrison opened this issue Feb 22, 2024 · 9 comments
Open

Show variable references in call hierarchy #272

i-garrison opened this issue Feb 22, 2024 · 9 comments
Labels
enhancement New feature or request

Comments

@i-garrison
Copy link

Currently CDT editor can show references to variable reads and writes annotated in call hierarchy.

Please add this feature to CDT LSP plugin. Since read and write access to variable is visible in function body provided by LSP it should technically be possible to bind these to function call hierarchies.

Lack of this feature can be partially worked around by wrapping variables into read/write accessor functions, but there are downsides:

  • code must be changed, which is not really an option while just exploring largish code bases
  • single call tree with all possible reads and writes is not available, there will be separate read and a write trees
@ghentschke
Copy link
Contributor

Is this what you mean:
image

@i-garrison
Copy link
Author

Yes this is what I'd like to have with CDT-LSP plugin too. Both r/w marking and also access to counter in foo call hierarchy.

@ghentschke
Copy link
Contributor

Please be aware of that the call hierarchy is (currently) limited to function calls. See this clangd issue

@i-garrison
Copy link
Author

Yes clangd does not provide that out of the box, still this can be implemented similar to what CDT editor does: it gets all variable references, finds containing functions and then stitches these to form displayed call tree. Would imagine it should be possible with CDT LSP plugin to do similar thing; you probably can store reference to function body for each variable reference and then while scanning for references build a tree going up from that function asking LSP for callers. Detecting if particular variable reference is read or write should be doable too.

@ghentschke
Copy link
Contributor

Would imagine it should be possible with CDT LSP plugin to do similar thing

IMO a better solution would be to fix it on the server side. That's what its made and used for.

@i-garrison
Copy link
Author

Would imagine it should be possible with CDT LSP plugin to do similar thing

IMO a better solution would be to fix it on the server side. That's what its made and used for.

Yes LSP server-side implementation is an option too, but looking at referenced clangd issue I do not think it will be available in clangd any time soon.

@i-garrison
Copy link
Author

i-garrison commented Nov 3, 2024

IMO a better solution would be to fix it on the server side. That's what its made and used for.

With clangd/clangd#1308 now implemented in clangd I see variable references are available in Call Hierarchy view.

There are a quite a few missing features with how references are displayed compared to CDT Call Hierarchy, for example:

  • There is no grouping by parent so there usually are a number of duplicate function name entries cluttering tree view though call hierarchy. CDT does deduplication in the view by function signature and displays single tree path for these, while still allowing to click Show Next Reference/Show Previous Reference to iterate to other entries on the same level.
  • Only function name is displayed even in case this is class member function call. CDT shows class name and function signature (i.e. arg types) which helps understanding call hierarchy a lot. CDT can also show file paths in same tree.
  • If you ask about variables, CDT can show if those are read or written by calling functions.

Is it possible to somehow feed these clangd references into CDT call hierarchy view so all those useful features are reused? I see that CDT call hierarchy implementation works off CDT Indexer contents but guess there could be some way to adapt for clangd data.

@ghentschke
Copy link
Contributor

I see that CDT call hierarchy implementation works off CDT Indexer contents but guess there could be some way to adapt for clangd data.

I'am afraid that your're right here. All these information come form the indexer.

@i-garrison
Copy link
Author

I'am afraid that your're right here. All these information come form the indexer.

Investigated this a bit, here is clangd ticket on function signatures clangd/clangd#915

Another PR about supporting outgoing call hierarchy has this small change https://github.com/llvm/llvm-project/pull/77556/files#diff-6b181f30bc00c60d34c1c294c74c3278276a3df6d72ecdfe49a680a9461656deR1755 allowing modified lsp4e to actually show function signature (you just need to fill in missing bits in CallHierarchyLabelProvider)

Regarding more compact tree representation which would fold multiple entries for same parent into single entry with N matches hint I believe this is easiest to achieve on lsp4e level. Unfortunately it is not really easy to dig into implementation details so I'm a bit lost already ))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants