Skip to content

Commit

Permalink
Implement lint plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan McAllister committed Jun 24, 2014
1 parent 51d438e commit 2f274d1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/librustc/driver/driver.rs
Expand Up @@ -216,12 +216,18 @@ pub fn phase_2_configure_and_expand(sess: &Session,
}
});

let Registry { syntax_exts, .. } = registry;
let Registry { syntax_exts, lint_passes, .. } = registry;

// Process command line flags for lints.
// Do this here because we will have lint plugins eventually.
{
let mut ls = sess.lint_store.borrow_mut();
for pass in lint_passes.move_iter() {
ls.register_pass(Some(sess), true, pass);
}
}

// Lint plugins are registered; now we can process command line flags.
if sess.opts.describe_lints {
super::describe_lints(&*sess.lint_store.borrow());
super::describe_lints(&*sess.lint_store.borrow(), true);
return None;
}
sess.lint_store.borrow_mut().process_command_line(sess);
Expand Down
23 changes: 17 additions & 6 deletions src/librustc/driver/mod.rs
Expand Up @@ -56,7 +56,7 @@ fn run_compiler(args: &[String]) {
if sopts.describe_lints {
let mut ls = lint::LintStore::new();
ls.register_builtin(None);
describe_lints(&ls);
describe_lints(&ls, false);
return;
}
early_error("no input filename given");
Expand Down Expand Up @@ -132,7 +132,7 @@ Additional help:
config::optgroups().as_slice()));
}

fn describe_lints(lint_store: &lint::LintStore) {
fn describe_lints(lint_store: &lint::LintStore, loaded_plugins: bool) {
println!("
Available lint options:
-W <foo> Warn about <foo>
Expand All @@ -154,13 +154,13 @@ Available lint options:
lints
}

let (_plugin, builtin) = lint_store.get_lints().partitioned(|&(_, p)| p);
// let plugin = sort_lints(plugin);
let (plugin, builtin) = lint_store.get_lints().partitioned(|&(_, p)| p);
let plugin = sort_lints(plugin);
let builtin = sort_lints(builtin);

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

print_lints(builtin);

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

fn describe_debug_flags() {
Expand Down
1 change: 1 addition & 0 deletions src/librustc/lint/mod.rs
Expand Up @@ -19,6 +19,7 @@
//! Most lints can be written as `LintPass` instances. These run just before
//! translation to LLVM bytecode. The `LintPass`es built into rustc are defined
//! within `builtin.rs`, which has further comments on how to add such a lint.
//! rustc can also load user-defined lint plugins via the plugin mechanism.
//!
//! Some of rustc's lints are defined elsewhere in the compiler and work by
//! calling `add_lint()` on the overall `Session` object. This works when
Expand Down
11 changes: 11 additions & 0 deletions src/librustc/plugin/registry.rs
Expand Up @@ -10,6 +10,8 @@

//! Used by plugin crates to tell `rustc` about the plugins they provide.

use lint::LintPassObject;

use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
use syntax::ext::base::{IdentTT, ItemDecorator, ItemModifier, BasicMacroExpander};
use syntax::ext::base::{MacroExpanderFn};
Expand All @@ -31,6 +33,9 @@ pub struct Registry {

#[doc(hidden)]
pub syntax_exts: Vec<NamedSyntaxExtension>,

#[doc(hidden)]
pub lint_passes: Vec<LintPassObject>,
}

impl Registry {
Expand All @@ -39,6 +44,7 @@ impl Registry {
Registry {
krate_span: krate.span,
syntax_exts: vec!(),
lint_passes: vec!(),
}
}

Expand Down Expand Up @@ -67,4 +73,9 @@ impl Registry {
span: None,
}, None));
}

/// Register a compiler lint pass.
pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
self.lint_passes.push(lint_pass);
}
}

0 comments on commit 2f274d1

Please sign in to comment.