|
10 | 10 | extern crate rustc_driver;
|
11 | 11 | #[allow(unused_extern_crates)]
|
12 | 12 | extern crate rustc_plugin;
|
13 |
| -use self::rustc_driver::{driver::CompileController, Compilation}; |
| 13 | +#[allow(unused_extern_crates)] |
| 14 | +extern crate rustc_interface; |
14 | 15 |
|
15 |
| -use std::convert::TryInto; |
| 16 | +use rustc_interface::interface; |
16 | 17 | use std::path::Path;
|
17 | 18 | use std::process::{exit, Command};
|
18 | 19 |
|
19 | 20 | fn show_version() {
|
20 | 21 | println!(env!("CARGO_PKG_VERSION"));
|
21 | 22 | }
|
22 | 23 |
|
| 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(®istry); |
| 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 | + |
23 | 72 | pub fn main() {
|
24 | 73 | rustc_driver::init_rustc_env_logger();
|
25 | 74 | exit(
|
26 |
| - rustc_driver::run(move || { |
| 75 | + rustc_driver::report_ices_to_stderr_if_any(move || { |
27 | 76 | use std::env;
|
28 | 77 |
|
29 | 78 | if std::env::args().any(|a| a == "--version" || a == "-V") {
|
@@ -93,58 +142,15 @@ pub fn main() {
|
93 | 142 | }
|
94 | 143 | }
|
95 | 144 |
|
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(®istry); |
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 | + }; |
144 | 152 | 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 |
149 | 155 | )
|
150 | 156 | }
|
0 commit comments