]> git.lizzy.rs Git - rust.git/blob - src/bin/miri.rs
Remove inception test for now.
[rust.git] / src / bin / miri.rs
1 #![feature(rustc_private)]
2
3 extern crate getopts;
4 extern crate miri;
5 extern crate rustc;
6 extern crate rustc_driver;
7 extern crate env_logger;
8 extern crate log_settings;
9 extern crate syntax;
10 #[macro_use] extern crate log;
11
12 use miri::{
13     EvalContext,
14     CachedMir,
15     step,
16     EvalError,
17     Frame,
18 };
19 use rustc::session::Session;
20 use rustc_driver::{driver, CompilerCalls};
21 use rustc::ty::{TyCtxt, subst};
22 use rustc::mir::mir_map::MirMap;
23 use rustc::hir::def_id::DefId;
24
25 struct MiriCompilerCalls;
26
27 impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
28     fn build_controller(
29         &mut self,
30         _: &Session,
31         _: &getopts::Matches
32     ) -> driver::CompileController<'a> {
33         let mut control = driver::CompileController::basic();
34
35         control.after_analysis.callback = Box::new(|state| {
36             state.session.abort_if_errors();
37             interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap());
38         });
39
40         control
41     }
42 }
43
44
45
46 fn interpret_start_points<'a, 'tcx>(
47     tcx: TyCtxt<'a, 'tcx, 'tcx>,
48     mir_map: &MirMap<'tcx>,
49 ) {
50     let initial_indentation = ::log_settings::settings().indentation;
51     for (&id, mir) in &mir_map.map {
52         for attr in tcx.map.attrs(id) {
53             use syntax::attr::AttrMetaMethods;
54             if attr.check_name("miri_run") {
55                 let item = tcx.map.expect_item(id);
56
57                 ::log_settings::settings().indentation = initial_indentation;
58
59                 debug!("Interpreting: {}", item.name);
60
61                 let mut ecx = EvalContext::new(tcx, mir_map);
62                 let substs = tcx.mk_substs(subst::Substs::empty());
63                 let return_ptr = ecx.alloc_ret_ptr(mir.return_ty, substs);
64
65                 ecx.push_stack_frame(tcx.map.local_def_id(id), mir.span, CachedMir::Ref(mir), substs, return_ptr);
66
67                 loop {
68                     match step(&mut ecx) {
69                         Ok(true) => {}
70                         Ok(false) => {
71                             match return_ptr {
72                                 Some(ptr) => if log_enabled!(::log::LogLevel::Debug) {
73                                     ecx.memory().dump(ptr.alloc_id);
74                                 },
75                                 None => warn!("diverging function returned"),
76                             }
77                             break;
78                         }
79                         // FIXME: diverging functions can end up here in some future miri
80                         Err(e) => {
81                             report(tcx, &ecx, e);
82                             break;
83                         }
84                     }
85                 }
86             }
87         }
88     }
89 }
90
91 fn report(tcx: TyCtxt, ecx: &EvalContext, e: EvalError) {
92     let frame = ecx.stack().last().expect("stackframe was empty");
93     let block = &frame.mir.basic_blocks()[frame.next_block];
94     let span = if frame.stmt < block.statements.len() {
95         block.statements[frame.stmt].source_info.span
96     } else {
97         block.terminator().source_info.span
98     };
99     let mut err = tcx.sess.struct_span_err(span, &e.to_string());
100     for &Frame { def_id, substs, span, .. } in ecx.stack().iter().rev() {
101         // FIXME(solson): Find a way to do this without this Display impl hack.
102         use rustc::util::ppaux;
103         use std::fmt;
104         struct Instance<'tcx>(DefId, &'tcx subst::Substs<'tcx>);
105         impl<'tcx> fmt::Display for Instance<'tcx> {
106             fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107                 ppaux::parameterized(f, self.1, self.0, ppaux::Ns::Value, &[],
108                     |tcx| Some(tcx.lookup_item_type(self.0).generics))
109             }
110         }
111         err.span_note(span, &format!("inside call to {}", Instance(def_id, substs)));
112     }
113     err.emit();
114 }
115
116 fn main() {
117     init_logger();
118     let args: Vec<String> = std::env::args().collect();
119     rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
120 }
121
122 fn init_logger() {
123     const NSPACES: usize = 40;
124     let format = |record: &log::LogRecord| {
125         // prepend spaces to indent the final string
126         let indentation = log_settings::settings().indentation;
127         format!("{lvl}:{module}{depth:2}{indent:<indentation$} {text}",
128             lvl = record.level(),
129             module = record.location().module_path(),
130             depth = indentation / NSPACES,
131             indentation = indentation % NSPACES,
132             indent = "",
133             text = record.args())
134     };
135
136     let mut builder = env_logger::LogBuilder::new();
137     builder.format(format).filter(None, log::LogLevelFilter::Info);
138
139     if std::env::var("MIRI_LOG").is_ok() {
140         builder.parse(&std::env::var("MIRI_LOG").unwrap());
141     }
142
143     builder.init().unwrap();
144 }