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