1 extern crate rustc_driver;
2 extern crate rustc_hir;
3 extern crate rustc_interface;
5 use rustc_hir::def_id::LOCAL_CRATE;
6 use rustc_driver::Compilation;
7 use rustc_interface::{interface, Queries};
9 use crate::test::Bencher;
11 struct MiriCompilerCalls<'a> {
12 bencher: &'a mut Bencher,
15 impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
16 fn after_analysis<'tcx>(
18 compiler: &interface::Compiler,
19 queries: &'tcx Queries<'tcx>,
21 compiler.session().abort_if_errors();
23 queries.global_ctxt().unwrap().peek_mut().enter(|tcx| {
24 let (entry_def_id, _) =
25 tcx.entry_fn(LOCAL_CRATE).expect("no main or start function found");
27 self.bencher.iter(|| {
28 let config = miri::MiriConfig::default();
29 miri::eval_main(tcx, entry_def_id, config);
33 compiler.session().abort_if_errors();
39 fn find_sysroot() -> String {
40 // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
41 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
42 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
43 match (home, toolchain) {
44 (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
45 _ => option_env!("RUST_SYSROOT")
46 .expect("need to specify RUST_SYSROOT env var or use rustup or multirust")
51 pub fn run(filename: &str, bencher: &mut Bencher) {
54 format!("benches/helpers/{}.rs", filename),
55 "--sysroot".to_string(),
58 rustc_driver::run_compiler(args, &mut MiriCompilerCalls { bencher }, None, None).unwrap()