]> git.lizzy.rs Git - rust.git/blob - benches/helpers/miri_helper.rs
Use the new rustc interface
[rust.git] / benches / helpers / miri_helper.rs
1 extern crate getopts;
2 extern crate miri;
3 extern crate rustc;
4 extern crate rustc_driver;
5 extern crate rustc_interface;
6 extern crate test;
7
8 use self::miri::eval_main;
9 use rustc::hir::def_id::LOCAL_CRATE;
10 use rustc_interface::interface;
11 use crate::test::Bencher;
12
13 struct MiriCompilerCalls<'a> {
14     bencher: &'a mut Bencher,
15 }
16
17 impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
18     fn after_analysis(&mut self, compiler: &interface::Compiler<'_>) -> bool {
19         compiler.session().abort_if_errors();
20
21         compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
22             let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect(
23                 "no main or start function found",
24             );
25
26             self.bencher.iter(|| {
27                 let config = MiriConfig { validate: true, args: vec![] };
28                 eval_main(tcx, entry_def_id, config);
29             });
30         });
31
32         compiler.session().abort_if_errors();
33
34         // Don't continue execution
35         false
36     }
37 }
38
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         _ => {
46             option_env!("RUST_SYSROOT")
47                 .expect(
48                     "need to specify RUST_SYSROOT env var or use rustup or multirust",
49                 )
50                 .to_owned()
51         }
52     }
53 }
54
55 pub fn run(filename: &str, bencher: &mut Bencher) {
56     let args = &[
57         "miri".to_string(),
58         format!("benches/helpers/{}.rs", filename),
59         "--sysroot".to_string(),
60         find_sysroot(),
61     ];
62     rustc_driver::run_compiler(args, &mut MiriCompilerCalls { bencher }, None, None);
63 }