| 
9 | 9 | extern crate rustc_driver;  | 
10 | 10 | #[allow(unused_extern_crates)]  | 
11 | 11 | extern crate rustc_plugin;  | 
12 |  | -use self::rustc_driver::{driver::CompileController, Compilation};  | 
 | 12 | +#[allow(unused_extern_crates)]  | 
 | 13 | +extern crate rustc_interface;  | 
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,58 @@ 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.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(®istry);  | 
 | 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 | + | 
63 | 112 | pub fn main() {  | 
64 | 113 |     rustc_driver::init_rustc_env_logger();  | 
65 | 114 |     exit(  | 
66 |  | -        rustc_driver::run(move || {  | 
 | 115 | +        rustc_driver::report_ices_to_stderr_if_any(move || {  | 
67 | 116 |             use std::env;  | 
68 | 117 | 
 
  | 
69 | 118 |             if std::env::args().any(|a| a == "--version" || a == "-V") {  | 
@@ -144,58 +193,15 @@ pub fn main() {  | 
144 | 193 |                 }  | 
145 | 194 |             }  | 
146 | 195 | 
 
  | 
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 |  | - | 
 | 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 | +            };  | 
195 | 203 |             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  | 
200 | 206 |     )  | 
201 | 207 | }  | 
0 commit comments