Skip to content

Commit e28a1ae

Browse files
committed
Use the new rustc interface
1 parent 5176a5c commit e28a1ae

File tree

1 file changed

+61
-55
lines changed

1 file changed

+61
-55
lines changed

src/driver.rs

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,69 @@
1010
extern crate rustc_driver;
1111
#[allow(unused_extern_crates)]
1212
extern crate rustc_plugin;
13-
use self::rustc_driver::{driver::CompileController, Compilation};
13+
#[allow(unused_extern_crates)]
14+
extern crate rustc_interface;
1415

15-
use std::convert::TryInto;
16+
use rustc_interface::interface;
1617
use std::path::Path;
1718
use std::process::{exit, Command};
1819

1920
fn show_version() {
2021
println!(env!("CARGO_PKG_VERSION"));
2122
}
2223

24+
25+
struct ClippyCallbacks;
26+
27+
impl rustc_driver::Callbacks for ClippyCallbacks {
28+
fn after_parsing(&mut self, compiler: &interface::Compiler<'_>) -> bool {
29+
let sess = compiler.session();
30+
let mut registry = rustc_plugin::registry::Registry::new(
31+
sess,
32+
compiler.parse().expect(
33+
"at this compilation stage \
34+
the crate must be parsed",
35+
).peek().span,
36+
);
37+
registry.args_hidden = Some(Vec::new());
38+
39+
let conf = clippy_lints::read_conf(&registry);
40+
clippy_lints::register_plugins(&mut registry, &conf);
41+
42+
let rustc_plugin::registry::Registry {
43+
early_lint_passes,
44+
late_lint_passes,
45+
lint_groups,
46+
llvm_passes,
47+
attributes,
48+
..
49+
} = registry;
50+
let mut ls = sess.lint_store.borrow_mut();
51+
for pass in early_lint_passes {
52+
ls.register_early_pass(Some(sess), true, false, pass);
53+
}
54+
for pass in late_lint_passes {
55+
ls.register_late_pass(Some(sess), true, pass);
56+
}
57+
58+
for (name, (to, deprecated_name)) in lint_groups {
59+
ls.register_group(Some(sess), true, name, deprecated_name, to);
60+
}
61+
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
62+
clippy_lints::register_renamed(&mut ls);
63+
64+
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
65+
sess.plugin_attributes.borrow_mut().extend(attributes);
66+
67+
// Continue execution
68+
true
69+
}
70+
}
71+
2372
pub fn main() {
2473
rustc_driver::init_rustc_env_logger();
2574
exit(
26-
rustc_driver::run(move || {
75+
rustc_driver::report_ices_to_stderr_if_any(move || {
2776
use std::env;
2877

2978
if std::env::args().any(|a| a == "--version" || a == "-V") {
@@ -93,58 +142,15 @@ pub fn main() {
93142
}
94143
}
95144

96-
let mut controller = CompileController::basic();
97-
if clippy_enabled {
98-
controller.after_parse.callback = Box::new(move |state| {
99-
let mut registry = rustc_plugin::registry::Registry::new(
100-
state.session,
101-
state
102-
.krate
103-
.as_ref()
104-
.expect(
105-
"at this compilation stage \
106-
the crate must be parsed",
107-
)
108-
.span,
109-
);
110-
registry.args_hidden = Some(Vec::new());
111-
112-
let conf = clippy_lints::read_conf(&registry);
113-
clippy_lints::register_plugins(&mut registry, &conf);
114-
115-
let rustc_plugin::registry::Registry {
116-
early_lint_passes,
117-
late_lint_passes,
118-
lint_groups,
119-
llvm_passes,
120-
attributes,
121-
..
122-
} = registry;
123-
let sess = &state.session;
124-
let mut ls = sess.lint_store.borrow_mut();
125-
for pass in early_lint_passes {
126-
ls.register_early_pass(Some(sess), true, false, pass);
127-
}
128-
for pass in late_lint_passes {
129-
ls.register_late_pass(Some(sess), true, pass);
130-
}
131-
132-
for (name, (to, deprecated_name)) in lint_groups {
133-
ls.register_group(Some(sess), true, name, deprecated_name, to);
134-
}
135-
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
136-
clippy_lints::register_renamed(&mut ls);
137-
138-
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
139-
sess.plugin_attributes.borrow_mut().extend(attributes);
140-
});
141-
}
142-
controller.compilation_done.stop = Compilation::Stop;
143-
145+
let mut clippy = ClippyCallbacks;
146+
let mut default = rustc_driver::DefaultCallbacks;
147+
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) = if clippy_enabled {
148+
&mut clippy
149+
} else {
150+
&mut default
151+
};
144152
let args = args;
145-
rustc_driver::run_compiler(&args, Box::new(controller), None, None)
146-
})
147-
.try_into()
148-
.expect("exit code too large"),
153+
rustc_driver::run_compiler(&args, callbacks, None, None)
154+
}).and_then(|result| result).is_err() as i32
149155
)
150156
}

0 commit comments

Comments
 (0)