]> git.lizzy.rs Git - rust.git/blob - benches/helpers/miri_helper.rs
fix for GlobalCtxt changes
[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, Queries};
11 use rustc_driver::Compilation;
12 use crate::test::Bencher;
13
14 struct MiriCompilerCalls<'a> {
15     bencher: &'a mut Bencher,
16 }
17
18 impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
19     fn after_analysis<'tcx>(&mut self, compiler: &interface::Compiler, queries: &'tcx Queries<'tcx>) -> Compilation {
20         compiler.session().abort_if_errors();
21
22         queries.global_ctxt().unwrap().peek_mut().enter(|tcx| {
23             let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect(
24                 "no main or start function found",
25             );
26
27             self.bencher.iter(|| {
28                 let config = miri::MiriConfig {
29                     validate: true,
30                     communicate: false,
31                     excluded_env_vars: vec![],
32                     args: vec![],
33                     seed: None,
34                 };
35                 eval_main(tcx, entry_def_id, config);
36             });
37         });
38
39         compiler.session().abort_if_errors();
40
41         Compilation::Stop
42     }
43 }
44
45 fn find_sysroot() -> String {
46     // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
47     let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
48     let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
49     match (home, toolchain) {
50         (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
51         _ => {
52             option_env!("RUST_SYSROOT")
53                 .expect(
54                     "need to specify RUST_SYSROOT env var or use rustup or multirust",
55                 )
56                 .to_owned()
57         }
58     }
59 }
60
61 pub fn run(filename: &str, bencher: &mut Bencher) {
62     let args = &[
63         "miri".to_string(),
64         format!("benches/helpers/{}.rs", filename),
65         "--sysroot".to_string(),
66         find_sysroot(),
67     ];
68     rustc_driver::run_compiler(args, &mut MiriCompilerCalls { bencher }, None, None).unwrap()
69 }