Skip to content

Commit 2f274d1

Browse files
author
Keegan McAllister
committed
Implement lint plugins
1 parent 51d438e commit 2f274d1

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

src/librustc/driver/driver.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,18 @@ pub fn phase_2_configure_and_expand(sess: &Session,
216216
}
217217
});
218218

219-
let Registry { syntax_exts, .. } = registry;
219+
let Registry { syntax_exts, lint_passes, .. } = registry;
220220

221-
// Process command line flags for lints.
222-
// Do this here because we will have lint plugins eventually.
221+
{
222+
let mut ls = sess.lint_store.borrow_mut();
223+
for pass in lint_passes.move_iter() {
224+
ls.register_pass(Some(sess), true, pass);
225+
}
226+
}
227+
228+
// Lint plugins are registered; now we can process command line flags.
223229
if sess.opts.describe_lints {
224-
super::describe_lints(&*sess.lint_store.borrow());
230+
super::describe_lints(&*sess.lint_store.borrow(), true);
225231
return None;
226232
}
227233
sess.lint_store.borrow_mut().process_command_line(sess);

src/librustc/driver/mod.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn run_compiler(args: &[String]) {
5656
if sopts.describe_lints {
5757
let mut ls = lint::LintStore::new();
5858
ls.register_builtin(None);
59-
describe_lints(&ls);
59+
describe_lints(&ls, false);
6060
return;
6161
}
6262
early_error("no input filename given");
@@ -132,7 +132,7 @@ Additional help:
132132
config::optgroups().as_slice()));
133133
}
134134

135-
fn describe_lints(lint_store: &lint::LintStore) {
135+
fn describe_lints(lint_store: &lint::LintStore, loaded_plugins: bool) {
136136
println!("
137137
Available lint options:
138138
-W <foo> Warn about <foo>
@@ -154,13 +154,13 @@ Available lint options:
154154
lints
155155
}
156156

157-
let (_plugin, builtin) = lint_store.get_lints().partitioned(|&(_, p)| p);
158-
// let plugin = sort_lints(plugin);
157+
let (plugin, builtin) = lint_store.get_lints().partitioned(|&(_, p)| p);
158+
let plugin = sort_lints(plugin);
159159
let builtin = sort_lints(builtin);
160160

161161
// FIXME (#7043): We should use the width in character cells rather than
162162
// the number of codepoints.
163-
let max_name_len = builtin.iter()
163+
let max_name_len = plugin.iter().chain(builtin.iter())
164164
.map(|&s| s.name.char_len())
165165
.max().unwrap_or(0);
166166
let padded = |x: &str| {
@@ -182,7 +182,18 @@ Available lint options:
182182

183183
print_lints(builtin);
184184

185-
// Describe lint plugins here once they exist.
185+
match (loaded_plugins, plugin.len()) {
186+
(false, 0) => {
187+
println!("Compiler plugins can provide additional lints. To see a listing of these, \
188+
re-run `rustc -W help` with a crate filename.");
189+
}
190+
(false, _) => fail!("didn't load lint plugins but got them anyway!"),
191+
(true, 0) => println!("This crate does not load any lint plugins."),
192+
(true, _) => {
193+
println!("Lint checks provided by plugins loaded by this crate:\n");
194+
print_lints(plugin);
195+
}
196+
}
186197
}
187198

188199
fn describe_debug_flags() {

src/librustc/lint/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! Most lints can be written as `LintPass` instances. These run just before
2020
//! translation to LLVM bytecode. The `LintPass`es built into rustc are defined
2121
//! within `builtin.rs`, which has further comments on how to add such a lint.
22+
//! rustc can also load user-defined lint plugins via the plugin mechanism.
2223
//!
2324
//! Some of rustc's lints are defined elsewhere in the compiler and work by
2425
//! calling `add_lint()` on the overall `Session` object. This works when

src/librustc/plugin/registry.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
//! Used by plugin crates to tell `rustc` about the plugins they provide.
1212
13+
use lint::LintPassObject;
14+
1315
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
1416
use syntax::ext::base::{IdentTT, ItemDecorator, ItemModifier, BasicMacroExpander};
1517
use syntax::ext::base::{MacroExpanderFn};
@@ -31,6 +33,9 @@ pub struct Registry {
3133

3234
#[doc(hidden)]
3335
pub syntax_exts: Vec<NamedSyntaxExtension>,
36+
37+
#[doc(hidden)]
38+
pub lint_passes: Vec<LintPassObject>,
3439
}
3540

3641
impl Registry {
@@ -39,6 +44,7 @@ impl Registry {
3944
Registry {
4045
krate_span: krate.span,
4146
syntax_exts: vec!(),
47+
lint_passes: vec!(),
4248
}
4349
}
4450

@@ -67,4 +73,9 @@ impl Registry {
6773
span: None,
6874
}, None));
6975
}
76+
77+
/// Register a compiler lint pass.
78+
pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
79+
self.lint_passes.push(lint_pass);
80+
}
7081
}

0 commit comments

Comments
 (0)