1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 // error-pattern:yummy
12 #![feature(box_syntax)]
13 #![feature(rustc_private)]
16 #![allow(clippy::missing_docs_in_private_items)]
18 // FIXME: switch to something more ergonomic here, once available.
19 // (currently there is no way to opt into sysroot crates w/o `extern crate`)
20 #[allow(unused_extern_crates)]
21 extern crate rustc_driver;
22 #[allow(unused_extern_crates)]
23 extern crate rustc_plugin;
24 use self::rustc_driver::{driver::CompileController, Compilation};
26 use std::convert::TryInto;
28 use std::process::{exit, Command};
31 println!(env!("CARGO_PKG_VERSION"));
35 rustc_driver::init_rustc_env_logger();
36 exit(rustc_driver::run(move || {
39 if std::env::args().any(|a| a == "--version" || a == "-V") {
44 let sys_root = option_env!("SYSROOT")
46 .or_else(|| std::env::var("SYSROOT").ok())
48 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
49 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
50 home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
58 .and_then(|out| String::from_utf8(out.stdout).ok())
59 .map(|s| s.trim().to_owned())
61 .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
63 // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
64 // We're invoking the compiler programmatically, so we ignore this/
65 let mut orig_args: Vec<String> = env::args().collect();
66 if orig_args.len() <= 1 {
67 std::process::exit(1);
69 if Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref()) {
70 // we still want to be able to invoke it normally though
73 // this conditional check for the --sysroot flag is there so users can call
74 // `clippy_driver` directly
75 // without having to pass --sysroot or anything
76 let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
82 .chain(Some("--sysroot".to_owned()))
83 .chain(Some(sys_root))
87 // this check ensures that dependencies are built but not linted and the final
89 // linted but not built
90 let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
91 || orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
94 args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
95 if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
98 .split("__CLIPPY_HACKERY__")
99 .filter_map(|s| if s.is_empty() {
108 let mut controller = CompileController::basic();
110 controller.after_parse.callback = Box::new(move |state| {
111 let mut registry = rustc_plugin::registry::Registry::new(
117 "at this compilation stage \
118 the crate must be parsed",
122 registry.args_hidden = Some(Vec::new());
124 let conf = clippy_lints::read_conf(®istry);
125 clippy_lints::register_plugins(&mut registry, &conf);
127 let rustc_plugin::registry::Registry {
135 let sess = &state.session;
136 let mut ls = sess.lint_store.borrow_mut();
137 for pass in early_lint_passes {
138 ls.register_early_pass(Some(sess), true, pass);
140 for pass in late_lint_passes {
141 ls.register_late_pass(Some(sess), true, pass);
144 for (name, (to, deprecated_name)) in lint_groups {
145 ls.register_group(Some(sess), true, name, deprecated_name, to);
147 clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
149 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
150 sess.plugin_attributes.borrow_mut().extend(attributes);
153 controller.compilation_done.stop = Compilation::Stop;
156 rustc_driver::run_compiler(&args, Box::new(controller), None, None)
157 }).try_into().expect("exit code too large"))