Skip to content

Commit 0d4a19c

Browse files
committed
Use the new rustc interface
1 parent 9308df7 commit 0d4a19c

File tree

1 file changed

+61
-55
lines changed

1 file changed

+61
-55
lines changed

src/driver.rs

+61-55
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
extern crate rustc_driver;
1010
#[allow(unused_extern_crates)]
1111
extern crate rustc_plugin;
12-
use self::rustc_driver::{driver::CompileController, Compilation};
12+
#[allow(unused_extern_crates)]
13+
extern crate rustc_interface;
1314

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

@@ -60,10 +61,58 @@ fn test_arg_value() {
6061
}
6162

6263
#[allow(clippy::too_many_lines)]
64+
65+
struct ClippyCallbacks;
66+
67+
impl rustc_driver::Callbacks for ClippyCallbacks {
68+
fn after_parsing(&mut self, compiler: &interface::Compiler) -> bool {
69+
let sess = compiler.session();
70+
let mut registry = rustc_plugin::registry::Registry::new(
71+
sess,
72+
compiler.parse().expect(
73+
"at this compilation stage \
74+
the crate must be parsed",
75+
).peek().span,
76+
);
77+
registry.args_hidden = Some(Vec::new());
78+
79+
let conf = clippy_lints::read_conf(&registry);
80+
clippy_lints::register_plugins(&mut registry, &conf);
81+
82+
let rustc_plugin::registry::Registry {
83+
early_lint_passes,
84+
late_lint_passes,
85+
lint_groups,
86+
llvm_passes,
87+
attributes,
88+
..
89+
} = registry;
90+
let mut ls = sess.lint_store.borrow_mut();
91+
for pass in early_lint_passes {
92+
ls.register_early_pass(Some(sess), true, false, pass);
93+
}
94+
for pass in late_lint_passes {
95+
ls.register_late_pass(Some(sess), true, pass);
96+
}
97+
98+
for (name, (to, deprecated_name)) in lint_groups {
99+
ls.register_group(Some(sess), true, name, deprecated_name, to);
100+
}
101+
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
102+
clippy_lints::register_renamed(&mut ls);
103+
104+
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
105+
sess.plugin_attributes.borrow_mut().extend(attributes);
106+
107+
// Continue execution
108+
true
109+
}
110+
}
111+
63112
pub fn main() {
64113
rustc_driver::init_rustc_env_logger();
65114
exit(
66-
rustc_driver::run(move || {
115+
rustc_driver::report_ices_to_stderr_if_any(move || {
67116
use std::env;
68117

69118
if std::env::args().any(|a| a == "--version" || a == "-V") {
@@ -144,58 +193,15 @@ pub fn main() {
144193
}
145194
}
146195

147-
let mut controller = CompileController::basic();
148-
if clippy_enabled {
149-
controller.after_parse.callback = Box::new(move |state| {
150-
let mut registry = rustc_plugin::registry::Registry::new(
151-
state.session,
152-
state
153-
.krate
154-
.as_ref()
155-
.expect(
156-
"at this compilation stage \
157-
the crate must be parsed",
158-
)
159-
.span,
160-
);
161-
registry.args_hidden = Some(Vec::new());
162-
163-
let conf = clippy_lints::read_conf(&registry);
164-
clippy_lints::register_plugins(&mut registry, &conf);
165-
166-
let rustc_plugin::registry::Registry {
167-
early_lint_passes,
168-
late_lint_passes,
169-
lint_groups,
170-
llvm_passes,
171-
attributes,
172-
..
173-
} = registry;
174-
let sess = &state.session;
175-
let mut ls = sess.lint_store.borrow_mut();
176-
for pass in early_lint_passes {
177-
ls.register_early_pass(Some(sess), true, false, pass);
178-
}
179-
for pass in late_lint_passes {
180-
ls.register_late_pass(Some(sess), true, pass);
181-
}
182-
183-
for (name, (to, deprecated_name)) in lint_groups {
184-
ls.register_group(Some(sess), true, name, deprecated_name, to);
185-
}
186-
clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf);
187-
clippy_lints::register_renamed(&mut ls);
188-
189-
sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes);
190-
sess.plugin_attributes.borrow_mut().extend(attributes);
191-
});
192-
}
193-
controller.compilation_done.stop = Compilation::Stop;
194-
196+
let mut clippy = ClippyCallbacks;
197+
let mut default = rustc_driver::DefaultCallbacks;
198+
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) = if clippy_enabled {
199+
&mut clippy
200+
} else {
201+
&mut default
202+
};
195203
let args = args;
196-
rustc_driver::run_compiler(&args, Box::new(controller), None, None)
197-
})
198-
.try_into()
199-
.expect("exit code too large"),
204+
rustc_driver::run_compiler(&args, callbacks, None, None)
205+
}).and_then(|result| result).is_err() as i32
200206
)
201207
}

0 commit comments

Comments
 (0)