Adding a library path for rustc to dump HIR/THIR/MIR

Hello!

What are you tring to do?

I want to run a static analysis over rust codes. Instead of working with raw user source, I decided to dump intermeidate represention of rust code in the form of HIR/THIR/MIR. The rustc command that emits the representation looks like this:

rustc -Zunpretty=hir-tree target_file.rs
rustc -Zunpretty=thir-tree target_file.rs
rustc -Zdump-mir target_file.rs

As you see, dumping is a unstable feature, and the rustup channel needs to be set on the nightly. Therefore, a whole command is to be:

rustup run nightly rustc -Zunpretty=hir-tree target_file.rs

There is a caveat: Even though a unit of compilation on rust is crate, dumping code is generated per file, which impiles it is even possible to dump only a part of certain crate or module.

When it comes to mod, everything works fine as long as a relative module path can be inferred. Notice that the dumped file is not a entry of lib/main/crate.

// foo.rs
pub struct fooStruct(i32);

// target.rs
mod foo;
struct Bar(foo::fooStruct);

And

rustup run nightly rustc -Zunpretty=hir-tree target.rs

But it is not the case when the dumped file has dependencies. rustc has a option that add a library path to resolve a symbol in the use(or extern crate)

The Problem

Let's say that I want to dump a file 'target.rs', which is a part of the crate, and the crate has a dependency of 'foo'. The file 'target.rs' has a code of 'use foo'.

Then it would look like this:

// target.rs
use foo;

sturct Bar(foo::something_in_foo);

to dump I tried:

rustup run nightly rustc -L crate=/relative/path/to/root/of/foo -Zunpretty=hir-tree target.rs

But it results in the error[E0432], which means "unresolved import Foo"

I guess there is a HIR representation of extern crate/use, because Enum rustc_hir::ItemKind has variants of External Crate and Use. link

What should I do?

What is not desired

The static analysis could be run on the environment that doesn't have build.rs or isn't built with the nightly compiler. So it is hard to make use of features of rustc-env/build.rs/cargo.

Instead of saving HIR or some other internal structure to a file that you then parse, have you considered using rustc's internal crates directly? You would have access to all the compiler's internal machinery, and it's just as unstable as parsing HIR/MIR.

Some links to get you started:

3 Likes

@Michael-F-Bryan Thanks for helpful links! Actually, I had tried out some of them, and for the sake of saving workloads I changed a strategy, since, analysing hirs has an advantage of not depending on api directly

I found out Syn crate has a parsing function that emits tokens of syn enums, and I think I give it a try.

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.