Skip to content

Commit

Permalink
Update 3.1-frontend.md
Browse files Browse the repository at this point in the history
  • Loading branch information
hxuhack authored Aug 3, 2024
1 parent 9b58bc6 commit ef2ab64
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/3.1-frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,25 @@ Cargo will automatically search the binaries named cargo-toolname from the paths
Note that we cannot directly invoke rap in the first round but through cargo check because we need cargo to manage the project-level compilation and append detailed compilation options for launching rustc. However, we want to hook rustc execution and execute rap instead for analysis. Therefore, we set [RUSTC_WRAPPER](https://doc.rust-lang.org/cargo/reference/environment-variables.html) with the value of cargo-rap. In this way, cargo check will actually run `cargo-rap rustc appended_options`. We then dispath the execution to rap with appended options.

## Register Analysis Callbacks
Supposing the purpose is to execute a function named my_analysis, developers should design a new struct and implement the [Callbacks Trait](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/trait.Callbacks.html) for the struct.
```
pub struct MyCallback {...}
impl Callbacks for MyCallback {
fn after_analysis<'tcx>(&mut self, compiler: &Compiler, queries: &'tcx Queries<'tcx>) -> Compilation {
compiler.session().abort_if_errors();
queries.global_ctxt().unwrap().enter(
|tcx| my_analysis(tcx, *self) // the analysis function to execute after compilation.
);
compiler.session().abort_if_errors();
Compilation::Continue
}
}
```
To execute the compiler and callback function, developers can employ the APIs [rustc_driver::RunCompiler](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/struct.RunCompiler.html) provided by Rust.
```
let mut callback = MyCallback::default();
let run_compiler = rustc_driver::RunCompiler::new(&args, callback);
let exit_code = rustc_driver::catch_with_exit_code(move || run_compiler.run());
```


0 comments on commit ef2ab64

Please sign in to comment.