How to extract function call infomation

I am using librustdoc to get some function signature and function call information. I have got signature by using Function structure in librustdoc/clean/types. But how can I get the body infomation of a function to extract what functions it calls? Thanks.

I don't think you can do that with rustdoc. You could probably use an attribute procedural macro to achieve that for a single function (inner attribute macros are currently not supported), but I assume you want to build some sort of call graph for your whole program. In that case you probably want to create a program that takes your program as input (similar to rustdoc), or hack into the build process with a build.rs script. There is the unmaintained rust-callgraph you could either try using or take as a starting point for building such a tool yourself.

1 Like

Note that call graph in a typical Rust program is much less important than in a typical OOP program.

Without implementation inheritance it's not possible to have “hidden connections” between various types, if callback is passed to you procedure it's done explicitly.

The only exceptions are default methods in traits, but these are usually (but not always) short and simple.

I got it. Thank you. Let me have a try!

How can I got the full name of the callee function, such as crate1::mod1::mod2::func1?

May I ask you for some context here? What is your approach? If you are using the TokenStream from an attribute macro, you can't expand an item to its full path. This post summarizes the use-cases of syn quite nicely.

Sorry, I didn't make it clear.
I'm building a tool to parse code and extract fragments of code to create another valid program.
I try to use hir or mir to parse a rust program. and get all the call sites of every functions, including the call to functions in manifest dependencies. For example, there is a program crate2:

use crate1::mod1::mod2;
fn main(){
    mod2::mod3::func1();
}

In manifest of crate2, there are a dependency called crate1. The program above call func1 in crate1. So I want to get the full path of func1 such as crate1::mod1::mod2::mod3::func1() so that I can call func1() in program I generate.
Are there any util APIs in mir or hir to achieve.... There are so many apis that I can't find it.

Very interesting project. Never worked with any of the rustc crates, but the HIR-Map sounds promising for the resolution of identifiers.

Thanks a lot! I have learnt the documentation and the corresponding source code more deeply. It seems that Hir and Mir modules provide many helpful apis!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.