]> git.lizzy.rs Git - rust.git/blob - benches/helpers/miri_helper.rs
Merge remote-tracking branch 'origin/master' into mut-visitor
[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 test;
6
7 use self::miri::eval_main;
8 use self::rustc_driver::{driver, Compilation};
9 use std::cell::RefCell;
10 use std::rc::Rc;
11 use crate::test::Bencher;
12
13 pub struct MiriCompilerCalls<'a>(Rc<RefCell<&'a mut Bencher>>);
14
15 fn find_sysroot() -> String {
16     // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
17     let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
18     let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
19     match (home, toolchain) {
20         (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
21         _ => {
22             option_env!("RUST_SYSROOT")
23                 .expect(
24                     "need to specify RUST_SYSROOT env var or use rustup or multirust",
25                 )
26                 .to_owned()
27         }
28     }
29 }
30
31 pub fn run(filename: &str, bencher: &mut Bencher) {
32     let args = &[
33         "miri".to_string(),
34         format!("benches/helpers/{}.rs", filename),
35         "--sysroot".to_string(),
36         find_sysroot(),
37     ];
38     let bencher = RefCell::new(bencher);
39
40     let mut control = driver::CompileController::basic();
41
42     control.after_analysis.stop = Compilation::Stop;
43     control.after_analysis.callback = Box::new(move |state| {
44         state.session.abort_if_errors();
45
46         let tcx = state.tcx.unwrap();
47         let (entry_node_id, _, _) = state.session.entry_fn.borrow().expect(
48             "no main or start function found",
49         );
50         let entry_def_id = tcx.hir.local_def_id(entry_node_id);
51
52         bencher.borrow_mut().iter(|| {
53             eval_main(tcx, entry_def_id, false);
54         });
55
56         state.session.abort_if_errors();
57     });
58
59     rustc_driver::run_compiler(args, Box::new(control), None, None);
60 }