2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
5 extern crate clippy_lints;
8 extern crate rustc_driver;
9 extern crate rustc_errors;
10 extern crate rustc_plugin;
13 use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
14 use rustc::session::{config, Session};
15 use rustc::session::config::{Input, ErrorOutputType};
16 use std::path::PathBuf;
17 use std::process::Command;
19 use clippy_lints::utils::cargo;
21 struct ClippyCompilerCalls(RustcDefaultCalls);
23 impl std::default::Default for ClippyCompilerCalls {
24 fn default() -> Self {
29 impl ClippyCompilerCalls {
31 ClippyCompilerCalls(RustcDefaultCalls)
35 impl<'a> CompilerCalls<'a> for ClippyCompilerCalls {
36 fn early_callback(&mut self,
37 matches: &getopts::Matches,
38 sopts: &config::Options,
39 descriptions: &rustc_errors::registry::Registry,
40 output: ErrorOutputType)
42 self.0.early_callback(matches, sopts, descriptions, output)
44 fn no_input(&mut self,
45 matches: &getopts::Matches,
46 sopts: &config::Options,
47 odir: &Option<PathBuf>,
48 ofile: &Option<PathBuf>,
49 descriptions: &rustc_errors::registry::Registry)
50 -> Option<(Input, Option<PathBuf>)> {
51 self.0.no_input(matches, sopts, odir, ofile, descriptions)
53 fn late_callback(&mut self,
54 matches: &getopts::Matches,
57 odir: &Option<PathBuf>,
58 ofile: &Option<PathBuf>)
60 self.0.late_callback(matches, sess, input, odir, ofile)
62 fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> driver::CompileController<'a> {
63 let mut control = self.0.build_controller(sess, matches);
65 let old = std::mem::replace(&mut control.after_parse.callback, box |_| {});
66 control.after_parse.callback = Box::new(move |state| {
68 let mut registry = rustc_plugin::registry::Registry::new(state.session, state.krate.as_ref().expect("at this compilation stage the krate must be parsed"));
69 registry.args_hidden = Some(Vec::new());
70 clippy_lints::register_plugins(&mut registry);
72 let rustc_plugin::registry::Registry { early_lint_passes,
79 let sess = &state.session;
80 let mut ls = sess.lint_store.borrow_mut();
81 for pass in early_lint_passes {
82 ls.register_early_pass(Some(sess), true, pass);
84 for pass in late_lint_passes {
85 ls.register_late_pass(Some(sess), true, pass);
88 for (name, to) in lint_groups {
89 ls.register_group(Some(sess), true, name, to);
92 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
93 sess.mir_passes.borrow_mut().extend(mir_passes);
94 sess.plugin_attributes.borrow_mut().extend(attributes);
108 if env::var("CLIPPY_DOGFOOD").map(|_| true).unwrap_or(false) {
112 let dep_path = env::current_dir().expect("current dir is not readable").join("target").join("debug").join("deps");
114 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
115 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
116 let sys_root = if let (Some(home), Some(toolchain)) = (home, toolchain) {
117 format!("{}/toolchains/{}", home, toolchain)
119 option_env!("SYSROOT")
120 .map(|s| s.to_owned())
121 .or(Command::new("rustc")
126 .and_then(|out| String::from_utf8(out.stdout).ok())
127 .map(|s| s.trim().to_owned()))
128 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust")
131 if let Some("clippy") = std::env::args().nth(1).as_ref().map(AsRef::as_ref) {
132 let manifest_path = std::env::args().skip(2).find(|val| val.starts_with("--manifest-path="));
133 let mut metadata = cargo::metadata(manifest_path).expect("could not obtain cargo metadata");
134 assert_eq!(metadata.version, 1);
135 for target in metadata.packages.remove(0).targets {
136 let args = std::env::args().skip(2);
137 if let Some(first) = target.kind.get(0) {
138 if target.kind.len() > 1 || first.ends_with("lib") {
139 if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args), &dep_path, &sys_root) {
140 std::process::exit(code);
142 } else if first == "bin" {
143 if let Err(code) = process(vec!["--bin".to_owned(), target.name].into_iter().chain(args), &dep_path, &sys_root) {
144 std::process::exit(code);
148 panic!("badly formatted cargo metadata: target::kind is an empty array");
152 let args: Vec<String> = if env::args().any(|s| s == "--sysroot") {
153 env::args().collect()
155 env::args().chain(Some("--sysroot".to_owned())).chain(Some(sys_root)).collect()
157 let (result, _) = rustc_driver::run_compiler(&args, &mut ClippyCompilerCalls::new());
159 if let Err(err_count) = result {
161 std::process::exit(1);
167 fn process<P, I>(old_args: I, dep_path: P, sysroot: &str) -> Result<(), i32>
168 where P: AsRef<Path>,
169 I: Iterator<Item = String>
172 let mut args = vec!["rustc".to_owned()];
174 let mut found_dashes = false;
175 for arg in old_args {
176 found_dashes |= arg == "--";
180 args.push("--".to_owned());
182 args.push("-L".to_owned());
183 args.push(dep_path.as_ref().to_string_lossy().into_owned());
184 args.push(String::from("--sysroot"));
185 args.push(sysroot.to_owned());
186 args.push("-Zno-trans".to_owned());
188 let path = std::env::current_exe().expect("current executable path invalid");
189 let exit_status = std::process::Command::new("cargo")
192 .spawn().expect("could not run cargo")
193 .wait().expect("failed to wait for cargo?");
195 if exit_status.success() {
198 Err(exit_status.code().unwrap_or(-1))