Skip to content

Commit 819f76c

Browse files
author
Keegan McAllister
committed
Store the registered lints in the Session
1 parent 442fbc4 commit 819f76c

File tree

8 files changed

+208
-128
lines changed

8 files changed

+208
-128
lines changed

src/librustc/driver/driver.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@ pub fn compile_input(sess: Session,
7979
&sess);
8080
let id = link::find_crate_id(krate.attrs.as_slice(),
8181
outputs.out_filestem.as_slice());
82-
let (expanded_crate, ast_map) =
83-
phase_2_configure_and_expand(&sess, krate, &id);
82+
let (expanded_crate, ast_map)
83+
= match phase_2_configure_and_expand(&sess, krate, &id) {
84+
None => return,
85+
Some(p) => p,
86+
};
87+
8488
(outputs, expanded_crate, ast_map)
8589
};
8690
write_out_deps(&sess, input, &outputs, &expanded_crate);
@@ -173,10 +177,12 @@ pub fn phase_1_parse_input(sess: &Session, cfg: ast::CrateConfig, input: &Input)
173177
/// syntax expansion, secondary `cfg` expansion, synthesis of a test
174178
/// harness if one is to be provided and injection of a dependency on the
175179
/// standard library and prelude.
180+
///
181+
/// Returns `None` if we're aborting after handling -W help.
176182
pub fn phase_2_configure_and_expand(sess: &Session,
177183
mut krate: ast::Crate,
178184
crate_id: &CrateId)
179-
-> (ast::Crate, syntax::ast_map::Map) {
185+
-> Option<(ast::Crate, syntax::ast_map::Map)> {
180186
let time_passes = sess.time_passes();
181187

182188
*sess.crate_types.borrow_mut() = collect_crate_types(sess, krate.attrs.as_slice());
@@ -212,6 +218,17 @@ pub fn phase_2_configure_and_expand(sess: &Session,
212218

213219
let Registry { syntax_exts, .. } = registry;
214220

221+
// Process command line flags for lints.
222+
// Do this here because we will have lint plugins eventually.
223+
if sess.opts.describe_lints {
224+
super::describe_lints(&*sess.lint_store.borrow());
225+
return None;
226+
}
227+
sess.lint_store.borrow_mut().process_command_line(sess);
228+
229+
// Abort if there are errors from lint processing or a plugin registrar.
230+
sess.abort_if_errors();
231+
215232
krate = time(time_passes, "expansion", (krate, macros, syntax_exts),
216233
|(krate, macros, syntax_exts)| {
217234
// Windows dlls do not have rpaths, so they don't know how to find their
@@ -254,7 +271,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
254271
krate.encode(&mut json).unwrap();
255272
}
256273

257-
(krate, map)
274+
Some((krate, map))
258275
}
259276

260277
pub struct CrateAnalysis {
@@ -631,9 +648,11 @@ pub fn pretty_print_input(sess: Session,
631648

632649
let (krate, ast_map, is_expanded) = match ppm {
633650
PpmExpanded | PpmExpandedIdentified | PpmTyped | PpmFlowGraph(_) => {
634-
let (krate, ast_map) = phase_2_configure_and_expand(&sess,
635-
krate,
636-
&id);
651+
let (krate, ast_map)
652+
= match phase_2_configure_and_expand(&sess, krate, &id) {
653+
None => return,
654+
Some(p) => p,
655+
};
637656
(krate, Some(ast_map), true)
638657
}
639658
_ => (krate, None, false)

src/librustc/driver/mod.rs

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use syntax::diagnostic;
1313
use back::link;
1414
use driver::driver::{Input, FileInput, StrInput};
1515
use driver::session::{Session, build_session};
16+
use lint::Lint;
1617
use lint;
1718
use metadata;
1819

@@ -48,15 +49,18 @@ fn run_compiler(args: &[String]) {
4849
Some(matches) => matches,
4950
None => return
5051
};
51-
5252
let sopts = config::build_session_options(&matches);
53-
if sopts.describe_lints {
54-
describe_lints();
55-
return;
56-
}
5753

5854
let (input, input_file_path) = match matches.free.len() {
59-
0u => early_error("no input filename given"),
55+
0u => {
56+
if sopts.describe_lints {
57+
let mut ls = lint::LintStore::new();
58+
ls.register_builtin(None);
59+
describe_lints(&ls);
60+
return;
61+
}
62+
early_error("no input filename given");
63+
}
6064
1u => {
6165
let ifile = matches.free.get(0).as_slice();
6266
if ifile == "-" {
@@ -128,43 +132,56 @@ Additional help:
128132
config::optgroups().as_slice()));
129133
}
130134

131-
fn describe_lints() {
135+
fn describe_lints(lint_store: &lint::LintStore) {
132136
println!("
133137
Available lint options:
134138
-W <foo> Warn about <foo>
135139
-A <foo> Allow <foo>
136140
-D <foo> Deny <foo>
137141
-F <foo> Forbid <foo> (deny, and deny all overrides)
142+
138143
");
139144

140-
let mut builtin_specs = lint::builtin_lint_specs();
141-
builtin_specs.sort_by(|x, y| {
142-
match x.default_level.cmp(&y.default_level) {
143-
Equal => x.name.cmp(&y.name),
144-
r => r,
145-
}
146-
});
145+
fn sort_lints(lints: Vec<(&'static Lint, bool)>) -> Vec<&'static Lint> {
146+
let mut lints: Vec<_> = lints.move_iter().map(|(x, _)| x).collect();
147+
lints.sort_by(|x: &&Lint, y: &&Lint| {
148+
match x.default_level.cmp(&y.default_level) {
149+
Equal => x.name.cmp(&y.name),
150+
r => r,
151+
}
152+
});
153+
lints
154+
}
147155

148-
// FIXME: What if someone uses combining characters or East Asian fullwidth
149-
// characters in a lint name?!?!?
150-
let max_name_len = builtin_specs.iter()
156+
let (_plugin, builtin) = lint_store.get_lints().partitioned(|&(_, p)| p);
157+
// let plugin = sort_lints(plugin);
158+
let builtin = sort_lints(builtin);
159+
160+
// FIXME (#7043): We should use the width in character cells rather than
161+
// the number of codepoints.
162+
let max_name_len = builtin.iter()
151163
.map(|&s| s.name.char_len())
152164
.max().unwrap_or(0);
153165
let padded = |x: &str| {
154-
format!("{}{}", " ".repeat(max_name_len - x.char_len()), x)
166+
" ".repeat(max_name_len - x.char_len()).append(x)
155167
};
156168

157-
println!("\nAvailable lint checks:\n");
169+
println!("Lint checks provided by rustc:\n");
158170
println!(" {} {:7.7s} {}", padded("name"), "default", "meaning");
159171
println!(" {} {:7.7s} {}", padded("----"), "-------", "-------");
160-
println!("");
161172

162-
for spec in builtin_specs.move_iter() {
163-
let name = spec.name.replace("_", "-");
164-
println!(" {} {:7.7s} {}",
165-
padded(name.as_slice()), spec.default_level.as_str(), spec.desc);
166-
}
167-
println!("");
173+
let print_lints = |lints: Vec<&Lint>| {
174+
for lint in lints.move_iter() {
175+
let name = lint.name.replace("_", "-");
176+
println!(" {} {:7.7s} {}",
177+
padded(name.as_slice()), lint.default_level.as_str(), lint.desc);
178+
}
179+
println!("\n");
180+
};
181+
182+
print_lints(builtin);
183+
184+
// Describe lint plugins here once they exist.
168185
}
169186

170187
fn describe_debug_flags() {

src/librustc/driver/session.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub struct Session {
4343
// expected to be absolute. `None` means that there is no source file.
4444
pub local_crate_source_file: Option<Path>,
4545
pub working_dir: Path,
46+
pub lint_store: RefCell<lint::LintStore>,
4647
pub lints: RefCell<NodeMap<Vec<(lint::LintId, codemap::Span, String)>>>,
4748
pub node_id: Cell<ast::NodeId>,
4849
pub crate_types: RefCell<Vec<config::CrateType>>,
@@ -226,7 +227,7 @@ pub fn build_session_(sopts: config::Options,
226227
}
227228
);
228229

229-
Session {
230+
let sess = Session {
230231
targ_cfg: target_cfg,
231232
opts: sopts,
232233
cstore: CStore::new(token::get_ident_interner()),
@@ -238,12 +239,16 @@ pub fn build_session_(sopts: config::Options,
238239
default_sysroot: default_sysroot,
239240
local_crate_source_file: local_crate_source_file,
240241
working_dir: os::getcwd(),
242+
lint_store: RefCell::new(lint::LintStore::new()),
241243
lints: RefCell::new(NodeMap::new()),
242244
node_id: Cell::new(1),
243245
crate_types: RefCell::new(Vec::new()),
244246
features: front::feature_gate::Features::new(),
245247
recursion_limit: Cell::new(64),
246-
}
248+
};
249+
250+
sess.lint_store.borrow_mut().register_builtin(Some(&sess));
251+
sess
247252
}
248253

249254
// Seems out of place, but it uses session, so I'm putting it here

src/librustc/lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ impl LintPass for GatherNodeLevels {
15171517
match it.node {
15181518
ast::ItemEnum(..) => {
15191519
let lint_id = lint::LintId::of(variant_size_difference);
1520-
match cx.get_level_source(lint_id) {
1520+
match cx.lints.get_level_source(lint_id) {
15211521
lvlsrc @ (lvl, _) if lvl != lint::Allow => {
15221522
cx.insert_node_level(it.id, lint_id, lvlsrc);
15231523
},

0 commit comments

Comments
 (0)