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