]> git.lizzy.rs Git - rust.git/blob - src/driver.rs
remove unused allow() attributes, NFC
[rust.git] / src / driver.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10
11 // error-pattern:yummy
12 #![feature(box_syntax)]
13 #![feature(rustc_private)]
14
15 #![feature(try_from)]
16 #![allow(clippy::missing_docs_in_private_items)]
17
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};
25
26 use std::convert::TryInto;
27 use std::path::Path;
28 use std::process::{exit, Command};
29
30 fn show_version() {
31     println!(env!("CARGO_PKG_VERSION"));
32 }
33
34 pub fn main() {
35     rustc_driver::init_rustc_env_logger();
36     exit(rustc_driver::run(move || {
37         use std::env;
38
39         if std::env::args().any(|a| a == "--version" || a == "-V") {
40             show_version();
41             exit(0);
42         }
43
44         let sys_root = option_env!("SYSROOT")
45             .map(String::from)
46             .or_else(|| std::env::var("SYSROOT").ok())
47             .or_else(|| {
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)))
51             })
52             .or_else(|| {
53                 Command::new("rustc")
54                     .arg("--print")
55                     .arg("sysroot")
56                     .output()
57                     .ok()
58                     .and_then(|out| String::from_utf8(out.stdout).ok())
59                     .map(|s| s.trim().to_owned())
60             })
61             .expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
62
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);
68         }
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
71             orig_args.remove(1);
72         }
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") {
77             orig_args.clone()
78         } else {
79             orig_args
80                 .clone()
81                 .into_iter()
82                 .chain(Some("--sysroot".to_owned()))
83                 .chain(Some(sys_root))
84                 .collect()
85         };
86
87         // this check ensures that dependencies are built but not linted and the final
88         // crate is
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");
92
93         if clippy_enabled {
94             args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
95             if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
96                 args.extend(
97                     extra_args
98                         .split("__CLIPPY_HACKERY__")
99                         .filter_map(|s| if s.is_empty() {
100                             None
101                         } else {
102                             Some(s.to_string())
103                         })
104                 );
105             }
106         }
107
108         let mut controller = CompileController::basic();
109         if clippy_enabled {
110             controller.after_parse.callback = Box::new(move |state| {
111                 let mut registry = rustc_plugin::registry::Registry::new(
112                     state.session,
113                     state
114                         .krate
115                         .as_ref()
116                         .expect(
117                             "at this compilation stage \
118                             the crate must be parsed",
119                         )
120                         .span,
121                 );
122                 registry.args_hidden = Some(Vec::new());
123
124                 let conf = clippy_lints::read_conf(&registry);
125                 clippy_lints::register_plugins(&mut registry, &conf);
126
127                 let rustc_plugin::registry::Registry {
128                     early_lint_passes,
129                     late_lint_passes,
130                     lint_groups,
131                     llvm_passes,
132                     attributes,
133                     ..
134                 } = 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);
139                 }
140                 for pass in late_lint_passes {
141                     ls.register_late_pass(Some(sess), true, pass);
142                 }
143
144                 for (name, (to, deprecated_name)) in lint_groups {
145                     ls.register_group(Some(sess), true, name, deprecated_name, to);
146                 }
147                 clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
148
149                 sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
150                 sess.plugin_attributes.borrow_mut().extend(attributes);
151             });
152         }
153         controller.compilation_done.stop = Compilation::Stop;
154
155         let args = args;
156         rustc_driver::run_compiler(&args, Box::new(controller), None, None)
157     }).try_into().expect("exit code too large"))
158 }