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