|
8 | 8 | #[allow(unused_extern_crates)] |
9 | 9 | extern crate rustc_driver; |
10 | 10 | #[allow(unused_extern_crates)] |
| 11 | +extern crate rustc_interface; |
| 12 | +#[allow(unused_extern_crates)] |
11 | 13 | extern crate rustc_plugin; |
12 | | -use self::rustc_driver::{driver::CompileController, Compilation}; |
13 | 14 |
|
14 | | -use std::convert::TryInto; |
| 15 | +use rustc_interface::interface; |
15 | 16 | use std::path::Path; |
16 | 17 | use std::process::{exit, Command}; |
17 | 18 |
|
@@ -60,10 +61,62 @@ fn test_arg_value() { |
60 | 61 | } |
61 | 62 |
|
62 | 63 | #[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 |
| 73 | + .parse() |
| 74 | + .expect( |
| 75 | + "at this compilation stage \ |
| 76 | + the crate must be parsed", |
| 77 | + ) |
| 78 | + .peek() |
| 79 | + .span, |
| 80 | + ); |
| 81 | + registry.args_hidden = Some(Vec::new()); |
| 82 | + |
| 83 | + let conf = clippy_lints::read_conf(®istry); |
| 84 | + clippy_lints::register_plugins(&mut registry, &conf); |
| 85 | + |
| 86 | + let rustc_plugin::registry::Registry { |
| 87 | + early_lint_passes, |
| 88 | + late_lint_passes, |
| 89 | + lint_groups, |
| 90 | + llvm_passes, |
| 91 | + attributes, |
| 92 | + .. |
| 93 | + } = registry; |
| 94 | + let mut ls = sess.lint_store.borrow_mut(); |
| 95 | + for pass in early_lint_passes { |
| 96 | + ls.register_early_pass(Some(sess), true, false, pass); |
| 97 | + } |
| 98 | + for pass in late_lint_passes { |
| 99 | + ls.register_late_pass(Some(sess), true, pass); |
| 100 | + } |
| 101 | + |
| 102 | + for (name, (to, deprecated_name)) in lint_groups { |
| 103 | + ls.register_group(Some(sess), true, name, deprecated_name, to); |
| 104 | + } |
| 105 | + clippy_lints::register_pre_expansion_lints(sess, &mut ls, &conf); |
| 106 | + clippy_lints::register_renamed(&mut ls); |
| 107 | + |
| 108 | + sess.plugin_llvm_passes.borrow_mut().extend(llvm_passes); |
| 109 | + sess.plugin_attributes.borrow_mut().extend(attributes); |
| 110 | + |
| 111 | + // Continue execution |
| 112 | + true |
| 113 | + } |
| 114 | +} |
| 115 | + |
63 | 116 | pub fn main() { |
64 | 117 | rustc_driver::init_rustc_env_logger(); |
65 | 118 | exit( |
66 | | - rustc_driver::run(move || { |
| 119 | + rustc_driver::report_ices_to_stderr_if_any(move || { |
67 | 120 | use std::env; |
68 | 121 |
|
69 | 122 | if std::env::args().any(|a| a == "--version" || a == "-V") { |
@@ -144,58 +197,14 @@ pub fn main() { |
144 | 197 | } |
145 | 198 | } |
146 | 199 |
|
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(®istry); |
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 | | - |
| 200 | + let mut clippy = ClippyCallbacks; |
| 201 | + let mut default = rustc_driver::DefaultCallbacks; |
| 202 | + let callbacks: &mut (dyn rustc_driver::Callbacks + Send) = |
| 203 | + if clippy_enabled { &mut clippy } else { &mut default }; |
195 | 204 | let args = args; |
196 | | - rustc_driver::run_compiler(&args, Box::new(controller), None, None) |
| 205 | + rustc_driver::run_compiler(&args, callbacks, None, None) |
197 | 206 | }) |
198 | | - .try_into() |
199 | | - .expect("exit code too large"), |
| 207 | + .and_then(|result| result) |
| 208 | + .is_err() as i32, |
200 | 209 | ) |
201 | 210 | } |
0 commit comments