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