]>
Commit | Line | Data |
---|---|---|
c34b1796 | 1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
1a4d82fc JJ |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | //! Implementation of lint checking. | |
12 | //! | |
13 | //! The lint checking is mostly consolidated into one pass which runs just | |
14 | //! before translation to LLVM bytecode. Throughout compilation, lint warnings | |
15 | //! can be added via the `add_lint` method on the Session structure. This | |
16 | //! requires a span and an id of the node that the lint is being added to. The | |
17 | //! lint isn't actually emitted at that time because it is unknown what the | |
18 | //! actual lint level at that location is. | |
19 | //! | |
20 | //! To actually emit lint warnings/errors, a separate pass is used just before | |
21 | //! translation. A context keeps track of the current state of all lint levels. | |
22 | //! Upon entering a node of the ast which can modify the lint settings, the | |
23 | //! previous lint state is pushed onto a stack and the ast is then recursed | |
24 | //! upon. As the ast is traversed, this keeps track of the current lint level | |
25 | //! for all lint attributes. | |
26 | use self::TargetLint::*; | |
27 | ||
28 | use middle::privacy::ExportedItems; | |
29 | use middle::ty::{self, Ty}; | |
30 | use session::{early_error, Session}; | |
31 | use session::config::UnstableFeatures; | |
32 | use lint::{Level, LevelSource, Lint, LintId, LintArray, LintPass, LintPassObject}; | |
33 | use lint::{Default, CommandLine, Node, Allow, Warn, Deny, Forbid, ReleaseChannel}; | |
34 | use lint::builtin; | |
35 | use util::nodemap::FnvHashMap; | |
36 | ||
37 | use std::cell::RefCell; | |
38 | use std::mem; | |
39 | use syntax::ast_util::IdVisitingOperation; | |
40 | use syntax::attr::AttrMetaMethods; | |
41 | use syntax::attr; | |
42 | use syntax::codemap::Span; | |
43 | use syntax::visit::{Visitor, FnKind}; | |
44 | use syntax::parse::token::InternedString; | |
45 | use syntax::{ast, ast_util, visit}; | |
46 | ||
47 | /// Information about the registered lints. | |
48 | /// | |
49 | /// This is basically the subset of `Context` that we can | |
50 | /// build early in the compile pipeline. | |
51 | pub struct LintStore { | |
52 | /// Registered lints. The bool is true if the lint was | |
53 | /// added by a plugin. | |
54 | lints: Vec<(&'static Lint, bool)>, | |
55 | ||
56 | /// Trait objects for each lint pass. | |
57 | /// This is only `None` while iterating over the objects. See the definition | |
58 | /// of run_lints. | |
59 | passes: Option<Vec<LintPassObject>>, | |
60 | ||
61 | /// Lints indexed by name. | |
62 | by_name: FnvHashMap<String, TargetLint>, | |
63 | ||
64 | /// Current levels of each lint, and where they were set. | |
65 | levels: FnvHashMap<LintId, LevelSource>, | |
66 | ||
67 | /// Map of registered lint groups to what lints they expand to. The bool | |
68 | /// is true if the lint group was added by a plugin. | |
69 | lint_groups: FnvHashMap<&'static str, (Vec<LintId>, bool)>, | |
70 | } | |
71 | ||
72 | /// The targed of the `by_name` map, which accounts for renaming/deprecation. | |
73 | enum TargetLint { | |
74 | /// A direct lint target | |
75 | Id(LintId), | |
76 | ||
77 | /// Temporary renaming, used for easing migration pain; see #16545 | |
78 | Renamed(String, LintId), | |
79 | } | |
80 | ||
81 | impl LintStore { | |
82 | fn get_level_source(&self, lint: LintId) -> LevelSource { | |
83 | match self.levels.get(&lint) { | |
84 | Some(&s) => s, | |
85 | None => (Allow, Default), | |
86 | } | |
87 | } | |
88 | ||
89 | fn set_level(&mut self, lint: LintId, lvlsrc: LevelSource) { | |
90 | if lvlsrc.0 == Allow { | |
91 | self.levels.remove(&lint); | |
92 | } else { | |
93 | self.levels.insert(lint, lvlsrc); | |
94 | } | |
95 | } | |
96 | ||
97 | pub fn new() -> LintStore { | |
98 | LintStore { | |
99 | lints: vec!(), | |
100 | passes: Some(vec!()), | |
85aaf69f SL |
101 | by_name: FnvHashMap(), |
102 | levels: FnvHashMap(), | |
103 | lint_groups: FnvHashMap(), | |
1a4d82fc JJ |
104 | } |
105 | } | |
106 | ||
107 | pub fn get_lints<'t>(&'t self) -> &'t [(&'static Lint, bool)] { | |
c34b1796 | 108 | &self.lints |
1a4d82fc JJ |
109 | } |
110 | ||
111 | pub fn get_lint_groups<'t>(&'t self) -> Vec<(&'static str, Vec<LintId>, bool)> { | |
112 | self.lint_groups.iter().map(|(k, v)| (*k, | |
113 | v.0.clone(), | |
114 | v.1)).collect() | |
115 | } | |
116 | ||
117 | pub fn register_pass(&mut self, sess: Option<&Session>, | |
118 | from_plugin: bool, pass: LintPassObject) { | |
85aaf69f | 119 | for &lint in pass.get_lints() { |
1a4d82fc JJ |
120 | self.lints.push((*lint, from_plugin)); |
121 | ||
122 | let id = LintId::of(*lint); | |
123 | if self.by_name.insert(lint.name_lower(), Id(id)).is_some() { | |
124 | let msg = format!("duplicate specification of lint {}", lint.name_lower()); | |
125 | match (sess, from_plugin) { | |
126 | // We load builtin lints first, so a duplicate is a compiler bug. | |
127 | // Use early_error when handling -W help with no crate. | |
85aaf69f SL |
128 | (None, _) => early_error(&msg[..]), |
129 | (Some(sess), false) => sess.bug(&msg[..]), | |
1a4d82fc JJ |
130 | |
131 | // A duplicate name from a plugin is a user error. | |
85aaf69f | 132 | (Some(sess), true) => sess.err(&msg[..]), |
1a4d82fc JJ |
133 | } |
134 | } | |
135 | ||
136 | if lint.default_level != Allow { | |
137 | self.levels.insert(id, (lint.default_level, Default)); | |
138 | } | |
139 | } | |
140 | self.passes.as_mut().unwrap().push(pass); | |
141 | } | |
142 | ||
143 | pub fn register_group(&mut self, sess: Option<&Session>, | |
144 | from_plugin: bool, name: &'static str, | |
145 | to: Vec<LintId>) { | |
146 | let new = self.lint_groups.insert(name, (to, from_plugin)).is_none(); | |
147 | ||
148 | if !new { | |
149 | let msg = format!("duplicate specification of lint group {}", name); | |
150 | match (sess, from_plugin) { | |
151 | // We load builtin lints first, so a duplicate is a compiler bug. | |
152 | // Use early_error when handling -W help with no crate. | |
85aaf69f SL |
153 | (None, _) => early_error(&msg[..]), |
154 | (Some(sess), false) => sess.bug(&msg[..]), | |
1a4d82fc JJ |
155 | |
156 | // A duplicate name from a plugin is a user error. | |
85aaf69f | 157 | (Some(sess), true) => sess.err(&msg[..]), |
1a4d82fc JJ |
158 | } |
159 | } | |
160 | } | |
161 | ||
c34b1796 | 162 | pub fn register_renamed(&mut self, old_name: &str, new_name: &str) { |
1a4d82fc JJ |
163 | let target = match self.by_name.get(new_name) { |
164 | Some(&Id(lint_id)) => lint_id.clone(), | |
165 | _ => panic!("invalid lint renaming of {} to {}", old_name, new_name) | |
166 | }; | |
167 | self.by_name.insert(old_name.to_string(), Renamed(new_name.to_string(), target)); | |
168 | } | |
169 | ||
1a4d82fc JJ |
170 | #[allow(unused_variables)] |
171 | fn find_lint(&self, lint_name: &str, sess: &Session, span: Option<Span>) | |
172 | -> Option<LintId> | |
173 | { | |
174 | match self.by_name.get(lint_name) { | |
175 | Some(&Id(lint_id)) => Some(lint_id), | |
176 | Some(&Renamed(ref new_name, lint_id)) => { | |
177 | let warning = format!("lint {} has been renamed to {}", | |
178 | lint_name, new_name); | |
179 | match span { | |
85aaf69f SL |
180 | Some(span) => sess.span_warn(span, &warning[..]), |
181 | None => sess.warn(&warning[..]), | |
1a4d82fc JJ |
182 | }; |
183 | Some(lint_id) | |
184 | } | |
185 | None => None | |
186 | } | |
187 | } | |
188 | ||
189 | pub fn process_command_line(&mut self, sess: &Session) { | |
85aaf69f SL |
190 | for &(ref lint_name, level) in &sess.opts.lint_opts { |
191 | match self.find_lint(&lint_name[..], sess, None) { | |
1a4d82fc JJ |
192 | Some(lint_id) => self.set_level(lint_id, (level, CommandLine)), |
193 | None => { | |
194 | match self.lint_groups.iter().map(|(&x, pair)| (x, pair.0.clone())) | |
195 | .collect::<FnvHashMap<&'static str, | |
196 | Vec<LintId>>>() | |
85aaf69f | 197 | .get(&lint_name[..]) { |
1a4d82fc JJ |
198 | Some(v) => { |
199 | v.iter() | |
200 | .map(|lint_id: &LintId| | |
201 | self.set_level(*lint_id, (level, CommandLine))) | |
202 | .collect::<Vec<()>>(); | |
203 | } | |
204 | None => sess.err(&format!("unknown {} flag: {}", | |
c34b1796 | 205 | level.as_str(), lint_name)), |
1a4d82fc JJ |
206 | } |
207 | } | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | fn maybe_stage_features(&mut self, sess: &Session) { | |
213 | let lvl = match sess.opts.unstable_features { | |
214 | UnstableFeatures::Default => return, | |
c34b1796 | 215 | UnstableFeatures::Disallow => Forbid, |
1a4d82fc JJ |
216 | UnstableFeatures::Cheat => Allow |
217 | }; | |
218 | match self.by_name.get("unstable_features") { | |
219 | Some(&Id(lint_id)) => if self.get_level_source(lint_id).0 != Forbid { | |
220 | self.set_level(lint_id, (lvl, ReleaseChannel)) | |
221 | }, | |
222 | Some(&Renamed(_, lint_id)) => if self.get_level_source(lint_id).0 != Forbid { | |
223 | self.set_level(lint_id, (lvl, ReleaseChannel)) | |
224 | }, | |
225 | None => unreachable!() | |
226 | } | |
1a4d82fc JJ |
227 | } |
228 | } | |
229 | ||
230 | /// Context for lint checking. | |
231 | pub struct Context<'a, 'tcx: 'a> { | |
232 | /// Type context we're checking in. | |
233 | pub tcx: &'a ty::ctxt<'tcx>, | |
234 | ||
235 | /// The crate being checked. | |
236 | pub krate: &'a ast::Crate, | |
237 | ||
238 | /// Items exported from the crate being checked. | |
239 | pub exported_items: &'a ExportedItems, | |
240 | ||
241 | /// The store of registered lints. | |
242 | lints: LintStore, | |
243 | ||
244 | /// When recursing into an attributed node of the ast which modifies lint | |
245 | /// levels, this stack keeps track of the previous lint levels of whatever | |
246 | /// was modified. | |
247 | level_stack: Vec<(LintId, LevelSource)>, | |
248 | ||
249 | /// Level of lints for certain NodeIds, stored here because the body of | |
250 | /// the lint needs to run in trans. | |
251 | node_levels: RefCell<FnvHashMap<(ast::NodeId, LintId), LevelSource>>, | |
252 | } | |
253 | ||
254 | /// Convenience macro for calling a `LintPass` method on every pass in the context. | |
255 | macro_rules! run_lints { ($cx:expr, $f:ident, $($args:expr),*) => ({ | |
256 | // Move the vector of passes out of `$cx` so that we can | |
257 | // iterate over it mutably while passing `$cx` to the methods. | |
258 | let mut passes = $cx.lints.passes.take().unwrap(); | |
85aaf69f | 259 | for obj in &mut passes { |
1a4d82fc JJ |
260 | obj.$f($cx, $($args),*); |
261 | } | |
262 | $cx.lints.passes = Some(passes); | |
263 | }) } | |
264 | ||
265 | /// Parse the lint attributes into a vector, with `Err`s for malformed lint | |
266 | /// attributes. Writing this as an iterator is an enormous mess. | |
267 | pub fn gather_attrs(attrs: &[ast::Attribute]) | |
268 | -> Vec<Result<(InternedString, Level, Span), Span>> { | |
269 | let mut out = vec!(); | |
85aaf69f SL |
270 | for attr in attrs { |
271 | let level = match Level::from_str(&attr.name()) { | |
1a4d82fc JJ |
272 | None => continue, |
273 | Some(lvl) => lvl, | |
274 | }; | |
275 | ||
276 | attr::mark_used(attr); | |
277 | ||
278 | let meta = &attr.node.value; | |
279 | let metas = match meta.node { | |
280 | ast::MetaList(_, ref metas) => metas, | |
281 | _ => { | |
282 | out.push(Err(meta.span)); | |
283 | continue; | |
284 | } | |
285 | }; | |
286 | ||
85aaf69f | 287 | for meta in metas { |
1a4d82fc JJ |
288 | out.push(match meta.node { |
289 | ast::MetaWord(ref lint_name) => Ok((lint_name.clone(), level, meta.span)), | |
290 | _ => Err(meta.span), | |
291 | }); | |
292 | } | |
293 | } | |
294 | out | |
295 | } | |
296 | ||
297 | /// Emit a lint as a warning or an error (or not at all) | |
298 | /// according to `level`. | |
299 | /// | |
300 | /// This lives outside of `Context` so it can be used by checks | |
301 | /// in trans that run after the main lint pass is finished. Most | |
302 | /// lints elsewhere in the compiler should call | |
303 | /// `Session::add_lint()` instead. | |
304 | pub fn raw_emit_lint(sess: &Session, lint: &'static Lint, | |
305 | lvlsrc: LevelSource, span: Option<Span>, msg: &str) { | |
306 | let (mut level, source) = lvlsrc; | |
307 | if level == Allow { return } | |
308 | ||
309 | let name = lint.name_lower(); | |
310 | let mut def = None; | |
311 | let mut note = None; | |
312 | let msg = match source { | |
313 | Default => { | |
314 | format!("{}, #[{}({})] on by default", msg, | |
315 | level.as_str(), name) | |
316 | }, | |
317 | CommandLine => { | |
318 | format!("{} [-{} {}]", msg, | |
319 | match level { | |
320 | Warn => 'W', Deny => 'D', Forbid => 'F', | |
321 | Allow => panic!() | |
322 | }, name.replace("_", "-")) | |
323 | }, | |
324 | Node(src) => { | |
325 | def = Some(src); | |
326 | msg.to_string() | |
327 | } | |
328 | ReleaseChannel => { | |
329 | let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)"); | |
330 | note = Some(format!("this feature may not be used in the {} release channel", | |
331 | release_channel)); | |
332 | msg.to_string() | |
333 | } | |
334 | }; | |
335 | ||
336 | // For purposes of printing, we can treat forbid as deny. | |
337 | if level == Forbid { level = Deny; } | |
338 | ||
339 | match (level, span) { | |
85aaf69f SL |
340 | (Warn, Some(sp)) => sess.span_warn(sp, &msg[..]), |
341 | (Warn, None) => sess.warn(&msg[..]), | |
342 | (Deny, Some(sp)) => sess.span_err(sp, &msg[..]), | |
343 | (Deny, None) => sess.err(&msg[..]), | |
1a4d82fc JJ |
344 | _ => sess.bug("impossible level in raw_emit_lint"), |
345 | } | |
346 | ||
85aaf69f SL |
347 | if let Some(note) = note { |
348 | sess.note(¬e[..]); | |
1a4d82fc JJ |
349 | } |
350 | ||
85aaf69f | 351 | if let Some(span) = def { |
1a4d82fc JJ |
352 | sess.span_note(span, "lint level defined here"); |
353 | } | |
354 | } | |
355 | ||
356 | impl<'a, 'tcx> Context<'a, 'tcx> { | |
357 | fn new(tcx: &'a ty::ctxt<'tcx>, | |
358 | krate: &'a ast::Crate, | |
359 | exported_items: &'a ExportedItems) -> Context<'a, 'tcx> { | |
360 | // We want to own the lint store, so move it out of the session. | |
361 | let lint_store = mem::replace(&mut *tcx.sess.lint_store.borrow_mut(), | |
362 | LintStore::new()); | |
363 | ||
364 | Context { | |
365 | tcx: tcx, | |
366 | krate: krate, | |
367 | exported_items: exported_items, | |
368 | lints: lint_store, | |
369 | level_stack: vec![], | |
85aaf69f | 370 | node_levels: RefCell::new(FnvHashMap()), |
1a4d82fc JJ |
371 | } |
372 | } | |
373 | ||
374 | /// Get the overall compiler `Session` object. | |
375 | pub fn sess(&'a self) -> &'a Session { | |
376 | &self.tcx.sess | |
377 | } | |
378 | ||
379 | /// Get the level of `lint` at the current position of the lint | |
380 | /// traversal. | |
381 | pub fn current_level(&self, lint: &'static Lint) -> Level { | |
382 | self.lints.levels.get(&LintId::of(lint)).map_or(Allow, |&(lvl, _)| lvl) | |
383 | } | |
384 | ||
385 | fn lookup_and_emit(&self, lint: &'static Lint, span: Option<Span>, msg: &str) { | |
386 | let (level, src) = match self.lints.levels.get(&LintId::of(lint)) { | |
387 | None => return, | |
388 | Some(&(Warn, src)) => { | |
389 | let lint_id = LintId::of(builtin::WARNINGS); | |
390 | (self.lints.get_level_source(lint_id).0, src) | |
391 | } | |
392 | Some(&pair) => pair, | |
393 | }; | |
394 | ||
395 | raw_emit_lint(&self.tcx.sess, lint, (level, src), span, msg); | |
396 | } | |
397 | ||
398 | /// Emit a lint at the appropriate level, with no associated span. | |
399 | pub fn lint(&self, lint: &'static Lint, msg: &str) { | |
400 | self.lookup_and_emit(lint, None, msg); | |
401 | } | |
402 | ||
403 | /// Emit a lint at the appropriate level, for a particular span. | |
404 | pub fn span_lint(&self, lint: &'static Lint, span: Span, msg: &str) { | |
405 | self.lookup_and_emit(lint, Some(span), msg); | |
406 | } | |
407 | ||
408 | /// Merge the lints specified by any lint attributes into the | |
409 | /// current lint context, call the provided function, then reset the | |
410 | /// lints in effect to their previous state. | |
411 | fn with_lint_attrs<F>(&mut self, | |
412 | attrs: &[ast::Attribute], | |
413 | f: F) where | |
414 | F: FnOnce(&mut Context), | |
415 | { | |
416 | // Parse all of the lint attributes, and then add them all to the | |
417 | // current dictionary of lint information. Along the way, keep a history | |
418 | // of what we changed so we can roll everything back after invoking the | |
419 | // specified closure | |
85aaf69f | 420 | let mut pushed = 0; |
1a4d82fc | 421 | |
85aaf69f | 422 | for result in gather_attrs(attrs) { |
1a4d82fc JJ |
423 | let v = match result { |
424 | Err(span) => { | |
425 | self.tcx.sess.span_err(span, "malformed lint attribute"); | |
426 | continue; | |
427 | } | |
428 | Ok((lint_name, level, span)) => { | |
85aaf69f | 429 | match self.lints.find_lint(&lint_name, &self.tcx.sess, Some(span)) { |
1a4d82fc JJ |
430 | Some(lint_id) => vec![(lint_id, level, span)], |
431 | None => { | |
85aaf69f | 432 | match self.lints.lint_groups.get(&lint_name[..]) { |
1a4d82fc JJ |
433 | Some(&(ref v, _)) => v.iter() |
434 | .map(|lint_id: &LintId| | |
435 | (*lint_id, level, span)) | |
436 | .collect(), | |
437 | None => { | |
438 | self.span_lint(builtin::UNKNOWN_LINTS, span, | |
85aaf69f SL |
439 | &format!("unknown `{}` attribute: `{}`", |
440 | level.as_str(), lint_name)); | |
1a4d82fc JJ |
441 | continue; |
442 | } | |
443 | } | |
444 | } | |
445 | } | |
446 | } | |
447 | }; | |
448 | ||
85aaf69f | 449 | for (lint_id, level, span) in v { |
1a4d82fc JJ |
450 | let now = self.lints.get_level_source(lint_id).0; |
451 | if now == Forbid && level != Forbid { | |
452 | let lint_name = lint_id.as_str(); | |
453 | self.tcx.sess.span_err(span, | |
454 | &format!("{}({}) overruled by outer forbid({})", | |
455 | level.as_str(), lint_name, | |
c34b1796 | 456 | lint_name)); |
1a4d82fc JJ |
457 | } else if now != level { |
458 | let src = self.lints.get_level_source(lint_id).1; | |
459 | self.level_stack.push((lint_id, (now, src))); | |
460 | pushed += 1; | |
461 | self.lints.set_level(lint_id, (level, Node(span))); | |
462 | } | |
463 | } | |
464 | } | |
465 | ||
466 | run_lints!(self, enter_lint_attrs, attrs); | |
467 | f(self); | |
468 | run_lints!(self, exit_lint_attrs, attrs); | |
469 | ||
470 | // rollback | |
85aaf69f | 471 | for _ in 0..pushed { |
1a4d82fc JJ |
472 | let (lint, lvlsrc) = self.level_stack.pop().unwrap(); |
473 | self.lints.set_level(lint, lvlsrc); | |
474 | } | |
475 | } | |
476 | ||
477 | fn visit_ids<F>(&mut self, f: F) where | |
478 | F: FnOnce(&mut ast_util::IdVisitor<Context>) | |
479 | { | |
480 | let mut v = ast_util::IdVisitor { | |
481 | operation: self, | |
482 | pass_through_items: false, | |
483 | visited_outermost: false, | |
484 | }; | |
485 | f(&mut v); | |
486 | } | |
487 | } | |
488 | ||
489 | impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { | |
490 | fn visit_item(&mut self, it: &ast::Item) { | |
c34b1796 | 491 | self.with_lint_attrs(&it.attrs, |cx| { |
1a4d82fc JJ |
492 | run_lints!(cx, check_item, it); |
493 | cx.visit_ids(|v| v.visit_item(it)); | |
494 | visit::walk_item(cx, it); | |
495 | }) | |
496 | } | |
497 | ||
498 | fn visit_foreign_item(&mut self, it: &ast::ForeignItem) { | |
c34b1796 | 499 | self.with_lint_attrs(&it.attrs, |cx| { |
1a4d82fc JJ |
500 | run_lints!(cx, check_foreign_item, it); |
501 | visit::walk_foreign_item(cx, it); | |
502 | }) | |
503 | } | |
504 | ||
1a4d82fc JJ |
505 | fn visit_pat(&mut self, p: &ast::Pat) { |
506 | run_lints!(self, check_pat, p); | |
507 | visit::walk_pat(self, p); | |
508 | } | |
509 | ||
510 | fn visit_expr(&mut self, e: &ast::Expr) { | |
511 | run_lints!(self, check_expr, e); | |
512 | visit::walk_expr(self, e); | |
513 | } | |
514 | ||
515 | fn visit_stmt(&mut self, s: &ast::Stmt) { | |
516 | run_lints!(self, check_stmt, s); | |
517 | visit::walk_stmt(self, s); | |
518 | } | |
519 | ||
520 | fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v ast::FnDecl, | |
521 | body: &'v ast::Block, span: Span, id: ast::NodeId) { | |
c34b1796 AL |
522 | run_lints!(self, check_fn, fk, decl, body, span, id); |
523 | visit::walk_fn(self, fk, decl, body, span); | |
1a4d82fc JJ |
524 | } |
525 | ||
526 | fn visit_struct_def(&mut self, | |
527 | s: &ast::StructDef, | |
528 | ident: ast::Ident, | |
529 | g: &ast::Generics, | |
530 | id: ast::NodeId) { | |
531 | run_lints!(self, check_struct_def, s, ident, g, id); | |
532 | visit::walk_struct_def(self, s); | |
533 | run_lints!(self, check_struct_def_post, s, ident, g, id); | |
534 | } | |
535 | ||
536 | fn visit_struct_field(&mut self, s: &ast::StructField) { | |
c34b1796 | 537 | self.with_lint_attrs(&s.node.attrs, |cx| { |
1a4d82fc JJ |
538 | run_lints!(cx, check_struct_field, s); |
539 | visit::walk_struct_field(cx, s); | |
540 | }) | |
541 | } | |
542 | ||
543 | fn visit_variant(&mut self, v: &ast::Variant, g: &ast::Generics) { | |
c34b1796 | 544 | self.with_lint_attrs(&v.node.attrs, |cx| { |
1a4d82fc JJ |
545 | run_lints!(cx, check_variant, v, g); |
546 | visit::walk_variant(cx, v, g); | |
547 | run_lints!(cx, check_variant_post, v, g); | |
548 | }) | |
549 | } | |
550 | ||
1a4d82fc JJ |
551 | fn visit_ty(&mut self, t: &ast::Ty) { |
552 | run_lints!(self, check_ty, t); | |
c34b1796 | 553 | visit::walk_ty(self, t); |
1a4d82fc JJ |
554 | } |
555 | ||
556 | fn visit_ident(&mut self, sp: Span, id: ast::Ident) { | |
557 | run_lints!(self, check_ident, sp, id); | |
558 | } | |
559 | ||
560 | fn visit_mod(&mut self, m: &ast::Mod, s: Span, n: ast::NodeId) { | |
561 | run_lints!(self, check_mod, m, s, n); | |
562 | visit::walk_mod(self, m); | |
563 | } | |
564 | ||
565 | fn visit_local(&mut self, l: &ast::Local) { | |
566 | run_lints!(self, check_local, l); | |
567 | visit::walk_local(self, l); | |
568 | } | |
569 | ||
570 | fn visit_block(&mut self, b: &ast::Block) { | |
571 | run_lints!(self, check_block, b); | |
572 | visit::walk_block(self, b); | |
573 | } | |
574 | ||
575 | fn visit_arm(&mut self, a: &ast::Arm) { | |
576 | run_lints!(self, check_arm, a); | |
577 | visit::walk_arm(self, a); | |
578 | } | |
579 | ||
580 | fn visit_decl(&mut self, d: &ast::Decl) { | |
581 | run_lints!(self, check_decl, d); | |
582 | visit::walk_decl(self, d); | |
583 | } | |
584 | ||
585 | fn visit_expr_post(&mut self, e: &ast::Expr) { | |
586 | run_lints!(self, check_expr_post, e); | |
587 | } | |
588 | ||
589 | fn visit_generics(&mut self, g: &ast::Generics) { | |
590 | run_lints!(self, check_generics, g); | |
591 | visit::walk_generics(self, g); | |
592 | } | |
593 | ||
c34b1796 AL |
594 | fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) { |
595 | self.with_lint_attrs(&trait_item.attrs, |cx| { | |
596 | run_lints!(cx, check_trait_item, trait_item); | |
597 | cx.visit_ids(|v| v.visit_trait_item(trait_item)); | |
598 | visit::walk_trait_item(cx, trait_item); | |
599 | }); | |
600 | } | |
601 | ||
602 | fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) { | |
603 | self.with_lint_attrs(&impl_item.attrs, |cx| { | |
604 | run_lints!(cx, check_impl_item, impl_item); | |
605 | cx.visit_ids(|v| v.visit_impl_item(impl_item)); | |
606 | visit::walk_impl_item(cx, impl_item); | |
607 | }); | |
1a4d82fc JJ |
608 | } |
609 | ||
610 | fn visit_opt_lifetime_ref(&mut self, sp: Span, lt: &Option<ast::Lifetime>) { | |
611 | run_lints!(self, check_opt_lifetime_ref, sp, lt); | |
612 | } | |
613 | ||
614 | fn visit_lifetime_ref(&mut self, lt: &ast::Lifetime) { | |
615 | run_lints!(self, check_lifetime_ref, lt); | |
616 | } | |
617 | ||
618 | fn visit_lifetime_def(&mut self, lt: &ast::LifetimeDef) { | |
619 | run_lints!(self, check_lifetime_def, lt); | |
620 | } | |
621 | ||
622 | fn visit_explicit_self(&mut self, es: &ast::ExplicitSelf) { | |
623 | run_lints!(self, check_explicit_self, es); | |
624 | visit::walk_explicit_self(self, es); | |
625 | } | |
626 | ||
627 | fn visit_mac(&mut self, mac: &ast::Mac) { | |
628 | run_lints!(self, check_mac, mac); | |
629 | visit::walk_mac(self, mac); | |
630 | } | |
631 | ||
632 | fn visit_path(&mut self, p: &ast::Path, id: ast::NodeId) { | |
633 | run_lints!(self, check_path, p, id); | |
634 | visit::walk_path(self, p); | |
635 | } | |
636 | ||
637 | fn visit_attribute(&mut self, attr: &ast::Attribute) { | |
638 | run_lints!(self, check_attribute, attr); | |
639 | } | |
640 | } | |
641 | ||
642 | // Output any lints that were previously added to the session. | |
643 | impl<'a, 'tcx> IdVisitingOperation for Context<'a, 'tcx> { | |
644 | fn visit_id(&mut self, id: ast::NodeId) { | |
645 | match self.tcx.sess.lints.borrow_mut().remove(&id) { | |
646 | None => {} | |
647 | Some(lints) => { | |
85aaf69f SL |
648 | for (lint_id, span, msg) in lints { |
649 | self.span_lint(lint_id.lint, span, &msg[..]) | |
1a4d82fc JJ |
650 | } |
651 | } | |
652 | } | |
653 | } | |
654 | } | |
655 | ||
656 | // This lint pass is defined here because it touches parts of the `Context` | |
657 | // that we don't want to expose. It records the lint level at certain AST | |
658 | // nodes, so that the variant size difference check in trans can call | |
659 | // `raw_emit_lint`. | |
660 | ||
c34b1796 | 661 | pub struct GatherNodeLevels; |
1a4d82fc JJ |
662 | |
663 | impl LintPass for GatherNodeLevels { | |
664 | fn get_lints(&self) -> LintArray { | |
665 | lint_array!() | |
666 | } | |
667 | ||
668 | fn check_item(&mut self, cx: &Context, it: &ast::Item) { | |
669 | match it.node { | |
670 | ast::ItemEnum(..) => { | |
671 | let lint_id = LintId::of(builtin::VARIANT_SIZE_DIFFERENCES); | |
672 | let lvlsrc = cx.lints.get_level_source(lint_id); | |
673 | match lvlsrc { | |
674 | (lvl, _) if lvl != Allow => { | |
675 | cx.node_levels.borrow_mut() | |
676 | .insert((it.id, lint_id), lvlsrc); | |
677 | }, | |
678 | _ => { } | |
679 | } | |
680 | }, | |
681 | _ => { } | |
682 | } | |
683 | } | |
684 | } | |
685 | ||
686 | /// Perform lint checking on a crate. | |
687 | /// | |
688 | /// Consumes the `lint_store` field of the `Session`. | |
689 | pub fn check_crate(tcx: &ty::ctxt, | |
690 | exported_items: &ExportedItems) { | |
691 | ||
692 | // If this is a feature-staged build of rustc then flip several lints to 'forbid' | |
693 | tcx.sess.lint_store.borrow_mut().maybe_stage_features(&tcx.sess); | |
694 | ||
695 | let krate = tcx.map.krate(); | |
696 | let mut cx = Context::new(tcx, krate, exported_items); | |
697 | ||
698 | // Visit the whole crate. | |
c34b1796 | 699 | cx.with_lint_attrs(&krate.attrs, |cx| { |
1a4d82fc JJ |
700 | cx.visit_id(ast::CRATE_NODE_ID); |
701 | cx.visit_ids(|v| { | |
702 | v.visited_outermost = true; | |
703 | visit::walk_crate(v, krate); | |
704 | }); | |
705 | ||
706 | // since the root module isn't visited as an item (because it isn't an | |
707 | // item), warn for it here. | |
708 | run_lints!(cx, check_crate, krate); | |
709 | ||
710 | visit::walk_crate(cx, krate); | |
711 | }); | |
712 | ||
713 | // If we missed any lints added to the session, then there's a bug somewhere | |
714 | // in the iteration code. | |
85aaf69f SL |
715 | for (id, v) in &*tcx.sess.lints.borrow() { |
716 | for &(lint, span, ref msg) in v { | |
1a4d82fc | 717 | tcx.sess.span_bug(span, |
85aaf69f SL |
718 | &format!("unprocessed lint {} at {}: {}", |
719 | lint.as_str(), tcx.map.node_to_string(*id), *msg)) | |
1a4d82fc JJ |
720 | } |
721 | } | |
722 | ||
1a4d82fc JJ |
723 | *tcx.node_lint_levels.borrow_mut() = cx.node_levels.into_inner(); |
724 | } |