]> git.lizzy.rs Git - rust.git/blob - src/main.rs
Merge pull request #950 from oli-obk/split3
[rust.git] / src / main.rs
1 // error-pattern:yummy
2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4
5 extern crate rustc_driver;
6 extern crate getopts;
7 extern crate rustc;
8 extern crate syntax;
9 extern crate rustc_plugin;
10 extern crate clippy_lints;
11
12 use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
13 use rustc::session::{config, Session};
14 use rustc::session::config::{Input, ErrorOutputType};
15 use syntax::diagnostics;
16 use std::path::PathBuf;
17 use std::process::Command;
18
19 struct ClippyCompilerCalls(RustcDefaultCalls);
20
21 impl std::default::Default for ClippyCompilerCalls {
22     fn default() -> Self {
23         Self::new()
24     }
25 }
26
27 impl ClippyCompilerCalls {
28     fn new() -> Self {
29         ClippyCompilerCalls(RustcDefaultCalls)
30     }
31 }
32
33 impl<'a> CompilerCalls<'a> for ClippyCompilerCalls {
34     fn early_callback(&mut self,
35                       matches: &getopts::Matches,
36                       sopts: &config::Options,
37                       descriptions: &diagnostics::registry::Registry,
38                       output: ErrorOutputType)
39                       -> Compilation {
40         self.0.early_callback(matches, sopts, descriptions, output)
41     }
42     fn no_input(&mut self,
43                 matches: &getopts::Matches,
44                 sopts: &config::Options,
45                 odir: &Option<PathBuf>,
46                 ofile: &Option<PathBuf>,
47                 descriptions: &diagnostics::registry::Registry)
48                 -> Option<(Input, Option<PathBuf>)> {
49         self.0.no_input(matches, sopts, odir, ofile, descriptions)
50     }
51     fn late_callback(&mut self,
52                      matches: &getopts::Matches,
53                      sess: &Session,
54                      input: &Input,
55                      odir: &Option<PathBuf>,
56                      ofile: &Option<PathBuf>)
57                      -> Compilation {
58         self.0.late_callback(matches, sess, input, odir, ofile)
59     }
60     fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> driver::CompileController<'a> {
61         let mut control = self.0.build_controller(sess, matches);
62
63         let old = std::mem::replace(&mut control.after_parse.callback, box |_| {});
64         control.after_parse.callback = Box::new(move |state| {
65             {
66                 let mut registry = rustc_plugin::registry::Registry::new(state.session, state.krate.as_ref().expect("at this compilation stage the krate must be parsed"));
67                 registry.args_hidden = Some(Vec::new());
68                 clippy_lints::register_plugins(&mut registry);
69
70                 let rustc_plugin::registry::Registry { early_lint_passes, late_lint_passes, lint_groups, llvm_passes, attributes, mir_passes, .. } = registry;
71                 let sess = &state.session;
72                 let mut ls = sess.lint_store.borrow_mut();
73                 for pass in early_lint_passes {
74                     ls.register_early_pass(Some(sess), true, pass);
75                 }
76                 for pass in late_lint_passes {
77                     ls.register_late_pass(Some(sess), true, pass);
78                 }
79
80                 for (name, to) in lint_groups {
81                     ls.register_group(Some(sess), true, name, to);
82                 }
83
84                 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
85                 sess.mir_passes.borrow_mut().extend(mir_passes);
86                 sess.plugin_attributes.borrow_mut().extend(attributes);
87             }
88             old(state);
89         });
90
91         control
92     }
93 }
94
95 use std::path::Path;
96
97 pub fn main() {
98     use std::env;
99
100     if env::var("CLIPPY_DOGFOOD").map(|_| true).unwrap_or(false) {
101         panic!("yummy");
102     }
103
104     let dep_path = env::current_dir().expect("current dir is not readable").join("target").join("debug").join("deps");
105
106     let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
107     let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
108     let sys_root = match (home, toolchain) {
109         (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
110         _ => option_env!("SYSROOT").map(|s| s.to_owned())
111                                    .or(Command::new("rustc").arg("--print")
112                                                             .arg("sysroot")
113                                                             .output().ok()
114                                                             .and_then(|out| String::from_utf8(out.stdout).ok())
115                                                             .map(|s| s.trim().to_owned())
116                                                             )
117                 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust"),
118     };
119
120     if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
121         let args = wrap_args(std::env::args().skip(2), dep_path, sys_root);
122         let path = std::env::current_exe().expect("current executable path invalid");
123         let exit_status = std::process::Command::new("cargo")
124             .args(&args)
125             .env("RUSTC", path)
126             .spawn().expect("could not run cargo")
127             .wait().expect("failed to wait for cargo?");
128
129         if let Some(code) = exit_status.code() {
130             std::process::exit(code);
131         }
132     } else {
133         let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
134             env::args().collect()
135         } else {
136             env::args().chain(Some("--sysroot".to_owned())).chain(Some(sys_root)).collect()
137         };
138         let (result, _) = rustc_driver::run_compiler(&args, &mut ClippyCompilerCalls::new());
139
140         if let Err(err_count) = result {
141             if err_count > 0 {
142                 std::process::exit(1);
143             }
144         }
145     }
146 }
147
148 fn wrap_args<P, I>(old_args: I, dep_path: P, sysroot: String) -> Vec<String>
149     where P: AsRef<Path>, I: Iterator<Item=String> {
150
151     let mut args = vec!["rustc".to_owned()];
152
153     let mut found_dashes = false;
154     for arg in old_args {
155         found_dashes |= arg == "--";
156         args.push(arg);
157     }
158     if !found_dashes {
159         args.push("--".to_owned());
160     }
161     args.push("-L".to_owned());
162     args.push(dep_path.as_ref().to_string_lossy().into_owned());
163     args.push(String::from("--sysroot"));
164     args.push(sysroot);
165     args.push("-Zno-trans".to_owned());
166     args
167 }