Crate level equivalent to `rustc_interface`?

Hello.

I'm looking to use rustc_interface and rustc_driver for analysis of rust crates. I got my code working for single-file programs, but I am struggling to find a way to get it to work with an entire crate with multiple files. I have no prior experience with rustc internals, so any help will be appreciated.

I've put together small program based off of this example and information from the rustc-dev-guide. My code is almost identical to the example except for the custom analysis that runs on function signatures, so I will omit for now.

Naively pointing the input to src/lib.rs results in a runtime error because rustc doesn't have enough information about dependencies and library specific types/functions to type check lib.rs. As far as I can see from the type signature of input, it is not possible to pass in multiple files. Therefore, I would most likely need to compile various dependencies and source files in the way cargo compiles a crate. Is there a separate library to do this? (I haven't found one after googling for 30 minutes, so maybe it's not well known?) Otherwise, is it possible to the same thing with rustc_interface and rustc_driver and chain together the type context?

I have tried a few other static analysis tools for crates, but they were missing key information, so the rustc_interface/driver are kind of a last resort for me.

Thank you for your time.

Minor update:
After some more googling, I came across this.

It seems to be doing something very close to what I want to do.

I am still trying to understand the code, so if anyone has easier ideas, I am interested to hear.

Thanks!

The above code doesn't compile for rust as of 1.46.0.

Most recent solution:

  1. Do something like stupid-stats.
  2. Make cargo use that rustc replacement using the RUSTC environment variable or its equivalent in Cargo.toml.
  3. Dump out analysis as you compile.

FYI: Anyone with similar questions should ask on internals.rust-lang.org, not users.