2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
5 extern crate rustc_driver;
9 extern crate rustc_plugin;
10 extern crate clippy_lints;
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;
19 struct ClippyCompilerCalls(RustcDefaultCalls);
21 impl std::default::Default for ClippyCompilerCalls {
22 fn default() -> Self {
27 impl ClippyCompilerCalls {
29 ClippyCompilerCalls(RustcDefaultCalls)
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)
40 self.0.early_callback(matches, sopts, descriptions, output)
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)
51 fn late_callback(&mut self,
52 matches: &getopts::Matches,
55 odir: &Option<PathBuf>,
56 ofile: &Option<PathBuf>)
58 self.0.late_callback(matches, sess, input, odir, ofile)
60 fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> driver::CompileController<'a> {
61 let mut control = self.0.build_controller(sess, matches);
63 let old = std::mem::replace(&mut control.after_parse.callback, box |_| {});
64 control.after_parse.callback = Box::new(move |state| {
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);
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);
76 for pass in late_lint_passes {
77 ls.register_late_pass(Some(sess), true, pass);
80 for (name, to) in lint_groups {
81 ls.register_group(Some(sess), true, name, to);
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);
100 if env::var("CLIPPY_DOGFOOD").map(|_| true).unwrap_or(false) {
104 let dep_path = env::current_dir().expect("current dir is not readable").join("target").join("debug").join("deps");
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")
114 .and_then(|out| String::from_utf8(out.stdout).ok())
115 .map(|s| s.trim().to_owned())
117 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust"),
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")
126 .spawn().expect("could not run cargo")
127 .wait().expect("failed to wait for cargo?");
129 if let Some(code) = exit_status.code() {
130 std::process::exit(code);
133 let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
134 env::args().collect()
136 env::args().chain(Some("--sysroot".to_owned())).chain(Some(sys_root)).collect()
138 let (result, _) = rustc_driver::run_compiler(&args, &mut ClippyCompilerCalls::new());
140 if let Err(err_count) = result {
142 std::process::exit(1);
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> {
151 let mut args = vec!["rustc".to_owned()];
153 let mut found_dashes = false;
154 for arg in old_args {
155 found_dashes |= arg == "--";
159 args.push("--".to_owned());
161 args.push("-L".to_owned());
162 args.push(dep_path.as_ref().to_string_lossy().into_owned());
163 args.push(String::from("--sysroot"));
165 args.push("-Zno-trans".to_owned());