]> git.lizzy.rs Git - rust.git/blob - src/driver.rs
Reintroduce `extern crate` for non-Cargo dependencies.
[rust.git] / src / driver.rs
1 // error-pattern:yummy
2 #![feature(box_syntax)]
3 #![feature(rustc_private)]
4 #![feature(tool_lints)]
5 #![allow(unknown_lints, clippy::missing_docs_in_private_items)]
6
7 // FIXME: switch to something more ergonomic here, once available.
8 // (currently there is no way to opt into sysroot crates w/o `extern crate`)
9 #[allow(unused_extern_crates)]
10 extern crate rustc_driver;
11 #[allow(unused_extern_crates)]
12 extern crate rustc_plugin;
13 use self::rustc_driver::{driver::CompileController, Compilation};
14
15 use std::path::Path;
16 use std::process::{exit, Command};
17
18 #[allow(clippy::print_stdout)]
19 fn show_version() {
20     println!(env!("CARGO_PKG_VERSION"));
21 }
22
23 pub fn main() {
24     exit(rustc_driver::run(move || {
25         use std::env;
26
27         if std::env::args().any(|a| a == "--version" || a == "-V") {
28             show_version();
29             exit(0);
30         }
31
32         let sys_root = option_env!("SYSROOT")
33             .map(String::from)
34             .or_else(|| std::env::var("SYSROOT").ok())
35             .or_else(|| {
36                 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
37                 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
38                 home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
39             })
40             .or_else(|| {
41                 Command::new("rustc")
42                     .arg("--print")
43                     .arg("sysroot")
44                     .output()
45                     .ok()
46                     .and_then(|out| String::from_utf8(out.stdout).ok())
47                     .map(|s| s.trim().to_owned())
48             })
49             .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
50
51         // Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
52         // We're invoking the compiler programmatically, so we ignore this/
53         let mut orig_args: Vec<String> = env::args().collect();
54         if orig_args.len() <= 1 {
55             std::process::exit(1);
56         }
57         if Path::new(&orig_args[1]).file_stem() == Some("rustc".as_ref()) {
58             // we still want to be able to invoke it normally though
59             orig_args.remove(1);
60         }
61         // this conditional check for the --sysroot flag is there so users can call
62         // `clippy_driver` directly
63         // without having to pass --sysroot or anything
64         let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
65             orig_args.clone()
66         } else {
67             orig_args
68                 .clone()
69                 .into_iter()
70                 .chain(Some("--sysroot".to_owned()))
71                 .chain(Some(sys_root))
72                 .collect()
73         };
74
75         // this check ensures that dependencies are built but not linted and the final
76         // crate is
77         // linted but not built
78         let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
79             || orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
80
81         if clippy_enabled {
82             args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
83             if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
84                 args.extend(
85                     extra_args
86                         .split("__CLIPPY_HACKERY__")
87                         .filter_map(|s| if s.is_empty() {
88                             None
89                         } else {
90                             Some(s.to_string())
91                         })
92                 );
93             }
94         }
95
96         let mut controller = CompileController::basic();
97         if clippy_enabled {
98             controller.after_parse.callback = Box::new(move |state| {
99                 let mut registry = rustc_plugin::registry::Registry::new(
100                     state.session,
101                     state
102                         .krate
103                         .as_ref()
104                         .expect(
105                             "at this compilation stage \
106                             the crate must be parsed",
107                         )
108                         .span,
109                 );
110                 registry.args_hidden = Some(Vec::new());
111
112                 let conf = clippy_lints::read_conf(&registry);
113                 clippy_lints::register_plugins(&mut registry, &conf);
114
115                 let rustc_plugin::registry::Registry {
116                     early_lint_passes,
117                     late_lint_passes,
118                     lint_groups,
119                     llvm_passes,
120                     attributes,
121                     ..
122                 } = registry;
123                 let sess = &state.session;
124                 let mut ls = sess.lint_store.borrow_mut();
125                 for pass in early_lint_passes {
126                     ls.register_early_pass(Some(sess), true, pass);
127                 }
128                 for pass in late_lint_passes {
129                     ls.register_late_pass(Some(sess), true, pass);
130                 }
131
132                 for (name, (to, deprecated_name)) in lint_groups {
133                     ls.register_group(Some(sess), true, name, deprecated_name, to);
134                 }
135                 clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
136
137                 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
138                 sess.plugin_attributes.borrow_mut().extend(attributes);
139             });
140         }
141         controller.compilation_done.stop = Compilation::Stop;
142
143         let args = args;
144         rustc_driver::run_compiler(&args, Box::new(controller), None, None)
145     }) as i32)
146 }