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