2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![feature(slice_patterns)]
6 extern crate rustc_driver;
10 extern crate rustc_plugin;
11 extern crate clippy_lints;
12 extern crate rustc_serialize;
14 use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
15 use rustc::session::{config, Session};
16 use rustc::session::config::{Input, ErrorOutputType};
17 use syntax::diagnostics;
18 use std::path::PathBuf;
19 use std::process::Command;
21 use clippy_lints::utils::cargo;
23 struct ClippyCompilerCalls(RustcDefaultCalls);
25 impl std::default::Default for ClippyCompilerCalls {
26 fn default() -> Self {
31 impl ClippyCompilerCalls {
33 ClippyCompilerCalls(RustcDefaultCalls)
37 impl<'a> CompilerCalls<'a> for ClippyCompilerCalls {
38 fn early_callback(&mut self,
39 matches: &getopts::Matches,
40 sopts: &config::Options,
41 descriptions: &diagnostics::registry::Registry,
42 output: ErrorOutputType)
44 self.0.early_callback(matches, sopts, descriptions, output)
46 fn no_input(&mut self,
47 matches: &getopts::Matches,
48 sopts: &config::Options,
49 odir: &Option<PathBuf>,
50 ofile: &Option<PathBuf>,
51 descriptions: &diagnostics::registry::Registry)
52 -> Option<(Input, Option<PathBuf>)> {
53 self.0.no_input(matches, sopts, odir, ofile, descriptions)
55 fn late_callback(&mut self,
56 matches: &getopts::Matches,
59 odir: &Option<PathBuf>,
60 ofile: &Option<PathBuf>)
62 self.0.late_callback(matches, sess, input, odir, ofile)
64 fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> driver::CompileController<'a> {
65 let mut control = self.0.build_controller(sess, matches);
67 let old = std::mem::replace(&mut control.after_parse.callback, box |_| {});
68 control.after_parse.callback = Box::new(move |state| {
70 let mut registry = rustc_plugin::registry::Registry::new(state.session, state.krate.as_ref().expect("at this compilation stage the krate must be parsed"));
71 registry.args_hidden = Some(Vec::new());
72 clippy_lints::register_plugins(&mut registry);
74 let rustc_plugin::registry::Registry { early_lint_passes, late_lint_passes, lint_groups, llvm_passes, attributes, mir_passes, .. } = registry;
75 let sess = &state.session;
76 let mut ls = sess.lint_store.borrow_mut();
77 for pass in early_lint_passes {
78 ls.register_early_pass(Some(sess), true, pass);
80 for pass in late_lint_passes {
81 ls.register_late_pass(Some(sess), true, pass);
84 for (name, to) in lint_groups {
85 ls.register_group(Some(sess), true, name, to);
88 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
89 sess.mir_passes.borrow_mut().extend(mir_passes);
90 sess.plugin_attributes.borrow_mut().extend(attributes);
104 if env::var("CLIPPY_DOGFOOD").map(|_| true).unwrap_or(false) {
108 let dep_path = env::current_dir().expect("current dir is not readable").join("target").join("debug").join("deps");
110 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
111 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
112 let sys_root = match (home, toolchain) {
113 (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
114 _ => option_env!("SYSROOT").map(|s| s.to_owned())
115 .or(Command::new("rustc").arg("--print")
118 .and_then(|out| String::from_utf8(out.stdout).ok())
119 .map(|s| s.trim().to_owned())
121 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust"),
124 if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
125 let mut metadata = cargo::metadata().expect("could not obtain cargo metadata");
126 assert_eq!(metadata.version, 1);
127 for target in metadata.packages.remove(0).targets {
128 let args = std::env::args().skip(2);
129 if let Some(first) = target.kind.get(0) {
130 if target.kind.len() > 1 || first.ends_with("lib") {
131 process(std::iter::once("--lib".to_owned()).chain(args), &dep_path, &sys_root);
132 } else if first == "bin" {
133 process(vec!["--bin".to_owned(), target.name].into_iter().chain(args), &dep_path, &sys_root);
136 panic!("badly formatted cargo metadata: target::kind is an empty array");
140 let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
141 env::args().collect()
143 env::args().chain(Some("--sysroot".to_owned())).chain(Some(sys_root)).collect()
145 let (result, _) = rustc_driver::run_compiler(&args, &mut ClippyCompilerCalls::new());
147 if let Err(err_count) = result {
149 std::process::exit(1);
155 fn process<P, I>(old_args: I, dep_path: P, sysroot: &str)
156 where P: AsRef<Path>, I: Iterator<Item=String> {
158 let mut args = vec!["rustc".to_owned()];
160 let mut found_dashes = false;
161 for arg in old_args {
162 found_dashes |= arg == "--";
166 args.push("--".to_owned());
168 args.push("-L".to_owned());
169 args.push(dep_path.as_ref().to_string_lossy().into_owned());
170 args.push(String::from("--sysroot"));
171 args.push(sysroot.to_owned());
172 args.push("-Zno-trans".to_owned());
174 let path = std::env::current_exe().expect("current executable path invalid");
175 let exit_status = std::process::Command::new("cargo")
178 .spawn().expect("could not run cargo")
179 .wait().expect("failed to wait for cargo?");
181 if let Some(code) = exit_status.code() {
182 std::process::exit(code);