Skip to content

Commit 6289344

Browse files
authored
Merge pull request #3087 from rust-lang-nursery/run
Use the compilers exit code computation instead of rolling our own
2 parents 9ce826a + caa59e2 commit 6289344

File tree

1 file changed

+107
-109
lines changed

1 file changed

+107
-109
lines changed

src/driver.rs

Lines changed: 107 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -13,125 +13,123 @@ fn show_version() {
1313
}
1414

1515
pub fn main() {
16-
use std::env;
16+
exit(rustc_driver::run(move || {
17+
use std::env;
1718

18-
if std::env::args().any(|a| a == "--version" || a == "-V") {
19-
show_version();
20-
return;
21-
}
19+
if std::env::args().any(|a| a == "--version" || a == "-V") {
20+
show_version();
21+
exit(0);
22+
}
2223

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");
24+
let sys_root = option_env!("SYSROOT")
25+
.map(String::from)
26+
.or_else(|| std::env::var("SYSROOT").ok())
27+
.or_else(|| {
28+
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
29+
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
30+
home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
31+
})
32+
.or_else(|| {
33+
Command::new("rustc")
34+
.arg("--print")
35+
.arg("sysroot")
36+
.output()
37+
.ok()
38+
.and_then(|out| String::from_utf8(out.stdout).ok())
39+
.map(|s| s.trim().to_owned())
40+
})
41+
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
4142

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-
};
43+
// Setting RUSTC_WRAPPER causes Cargo to pass 'rustc' as the first argument.
44+
// We're invoking the compiler programmatically, so we ignore this/
45+
let mut orig_args: Vec<String> = env::args().collect();
46+
if orig_args.len() <= 1 {
47+
std::process::exit(1);
48+
}
49+
if orig_args[1] == "rustc" {
50+
// we still want to be able to invoke it normally though
51+
orig_args.remove(1);
52+
}
53+
// this conditional check for the --sysroot flag is there so users can call
54+
// `clippy_driver` directly
55+
// without having to pass --sysroot or anything
56+
let mut args: Vec<String> = if orig_args.iter().any(|s| s == "--sysroot") {
57+
orig_args.clone()
58+
} else {
59+
orig_args
60+
.clone()
61+
.into_iter()
62+
.chain(Some("--sysroot".to_owned()))
63+
.chain(Some(sys_root))
64+
.collect()
65+
};
6566

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");
67+
// this check ensures that dependencies are built but not linted and the final
68+
// crate is
69+
// linted but not built
70+
let clippy_enabled = env::var("CLIPPY_TESTS").ok().map_or(false, |val| val == "true")
71+
|| orig_args.iter().any(|s| s == "--emit=dep-info,metadata");
7172

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-
);
73+
if clippy_enabled {
74+
args.extend_from_slice(&["--cfg".to_owned(), r#"feature="cargo-clippy""#.to_owned()]);
75+
if let Ok(extra_args) = env::var("CLIPPY_ARGS") {
76+
args.extend(
77+
extra_args
78+
.split("__CLIPPY_HACKERY__")
79+
.filter(|s| !s.is_empty())
80+
.map(str::to_owned),
81+
);
82+
}
8183
}
82-
}
8384

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());
85+
let mut controller = CompileController::basic();
86+
if clippy_enabled {
87+
controller.after_parse.callback = Box::new(move |state| {
88+
let mut registry = rustc_plugin::registry::Registry::new(
89+
state.session,
90+
state
91+
.krate
92+
.as_ref()
93+
.expect(
94+
"at this compilation stage \
95+
the crate must be parsed",
96+
)
97+
.span,
98+
);
99+
registry.args_hidden = Some(Vec::new());
99100

100-
let conf = clippy_lints::read_conf(&registry);
101-
clippy_lints::register_plugins(&mut registry, &conf);
101+
let conf = clippy_lints::read_conf(&registry);
102+
clippy_lints::register_plugins(&mut registry, &conf);
102103

103-
let rustc_plugin::registry::Registry {
104-
early_lint_passes,
105-
late_lint_passes,
106-
lint_groups,
107-
llvm_passes,
108-
attributes,
109-
..
110-
} = registry;
111-
let sess = &state.session;
112-
let mut ls = sess.lint_store.borrow_mut();
113-
for pass in early_lint_passes {
114-
ls.register_early_pass(Some(sess), true, pass);
115-
}
116-
for pass in late_lint_passes {
117-
ls.register_late_pass(Some(sess), true, pass);
118-
}
104+
let rustc_plugin::registry::Registry {
105+
early_lint_passes,
106+
late_lint_passes,
107+
lint_groups,
108+
llvm_passes,
109+
attributes,
110+
..
111+
} = registry;
112+
let sess = &state.session;
113+
let mut ls = sess.lint_store.borrow_mut();
114+
for pass in early_lint_passes {
115+
ls.register_early_pass(Some(sess), true, pass);
116+
}
117+
for pass in late_lint_passes {
118+
ls.register_late_pass(Some(sess), true, pass);
119+
}
119120

120-
for (name, to) in lint_groups {
121-
ls.register_group(Some(sess), true, name, to);
122-
}
123-
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
121+
for (name, to) in lint_groups {
122+
ls.register_group(Some(sess), true, name, to);
123+
}
124+
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
124125

125-
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
126-
sess.plugin_attributes.borrow_mut().extend(attributes);
127-
});
128-
}
129-
controller.compilation_done.stop = Compilation::Stop;
126+
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
127+
sess.plugin_attributes.borrow_mut().extend(attributes);
128+
});
129+
}
130+
controller.compilation_done.stop = Compilation::Stop;
130131

131-
if rustc_driver::run_compiler(&args, Box::new(controller), None, None)
132-
.0
133-
.is_err()
134-
{
135-
exit(101);
136-
}
132+
let args = args;
133+
rustc_driver::run_compiler(&args, Box::new(controller), None, None)
134+
}) as i32)
137135
}

0 commit comments

Comments
 (0)