For a given method name x, I want to find all the method names of methods that call x. I want to recursively do this until I reach the entry point for my code. I found a way to do this for C# code using Roslyn in this post, but I want the solution to be as language-agnostic as possible. It should work for C#, C++, Python, ... I'm writing this tool in C# and need a programmatic solution. Any help is greatly appreciated. Thanks!
-
2Is this for working in the IDE, or do you need this at runtime? If you don't need to programmatically find this, VS has a tool called the call hierarchy that you can use for this? – AotN Nov 30 '20 at 17:41
-
I was looking for a programmatic solution, but that's actually very helpful too. I can't believe I wasn't aware of this tool. Do you know if there is a way to programmatically extract the information from the call hierarchy? I basically want a string representing the entire path. – Serpemes Nov 30 '20 at 18:02
-
I'm not too sure about that as I'm not well versed in VS extension development. You may be able to develop your own extension. Otherwise, since references are more of a compiler feature, you may have to work with Roslyn via Microsoft.CodeAnalysis. Unfortunately, working with Roslyn isn't my strong suit.. But here's a question that may be helpful in getting started in the right direction maybe? https://stackoverflow.com/questions/32974118/c-sharp-recursively-find-references-in-all-projects-in-solution – AotN Nov 30 '20 at 18:36
1 Answers
I don't believe there is any language-agnostic way in which you could possibly do this. The tool has to know at least something of the structure of the language, what a function or method looks like, and what a function or method call looks like.
Edited to note: You might find Dr. Terence Parr's ANTLR parser generator useful:
ANTLR can target C#, and there are a ton of grammars written for it, including C++ and python.
Using that would let you slot in the parser of choice at runtime. The AST (abstract syntax tree) produced by the parser would have the same structure, so it would be a tree walk to identify the function/method calls.
Probably need adapters for each parser's output so that the AST produced by each parser used common names for the things you care about.

- 71,308
- 16
- 93
- 135
-
I am aware of this. What I meant by as language-agnostic as possible was that there might be a solution that supports multiple languages that are similar in design. – Serpemes Nov 30 '20 at 20:33
-
See my updated answer for a suggestion as to tooling you might use for this. – Nicholas Carey Dec 01 '20 at 20:21