]> git.lizzy.rs Git - rust.git/blob - src/librustc/lint/levels.rs
8a899a35ecb547a7e27e5fc59549a6df067a17f3
[rust.git] / src / librustc / lint / levels.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
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 use std::cmp;
12
13 use errors::DiagnosticBuilder;
14 use hir::HirId;
15 use ich::StableHashingContext;
16 use lint::builtin;
17 use lint::context::CheckLintNameResult;
18 use lint::{self, Lint, LintId, Level, LintSource};
19 use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
20                                            StableHasher, StableHasherResult};
21 use session::Session;
22 use syntax::ast;
23 use syntax::attr;
24 use syntax::codemap::MultiSpan;
25 use syntax::symbol::Symbol;
26 use util::nodemap::FxHashMap;
27
28 pub struct LintLevelSets {
29     list: Vec<LintSet>,
30     lint_cap: Level,
31 }
32
33 enum LintSet {
34     CommandLine {
35         // -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
36         // flag.
37         specs: FxHashMap<LintId, (Level, LintSource)>,
38     },
39
40     Node {
41         specs: FxHashMap<LintId, (Level, LintSource)>,
42         parent: u32,
43     },
44 }
45
46 impl LintLevelSets {
47     pub fn new(sess: &Session) -> LintLevelSets {
48         let mut me = LintLevelSets {
49             list: Vec::new(),
50             lint_cap: Level::Forbid,
51         };
52         me.process_command_line(sess);
53         return me
54     }
55
56     pub fn builder(sess: &Session) -> LintLevelsBuilder {
57         LintLevelsBuilder::new(sess, LintLevelSets::new(sess))
58     }
59
60     fn process_command_line(&mut self, sess: &Session) {
61         let store = sess.lint_store.borrow();
62         let mut specs = FxHashMap();
63         self.lint_cap = sess.opts.lint_cap.unwrap_or(Level::Forbid);
64
65         for &(ref lint_name, level) in &sess.opts.lint_opts {
66             store.check_lint_name_cmdline(sess, &lint_name, level);
67
68             // If the cap is less than this specified level, e.g. if we've got
69             // `--cap-lints allow` but we've also got `-D foo` then we ignore
70             // this specification as the lint cap will set it to allow anyway.
71             let level = cmp::min(level, self.lint_cap);
72
73             let lint_flag_val = Symbol::intern(lint_name);
74             let ids = match store.find_lints(&lint_name) {
75                 Ok(ids) => ids,
76                 Err(_) => continue, // errors handled in check_lint_name_cmdline above
77             };
78             for id in ids {
79                 let src = LintSource::CommandLine(lint_flag_val);
80                 specs.insert(id, (level, src));
81             }
82         }
83
84         self.list.push(LintSet::CommandLine {
85             specs: specs,
86         });
87     }
88
89     fn get_lint_level(&self,
90                       lint: &'static Lint,
91                       idx: u32,
92                       aux: Option<&FxHashMap<LintId, (Level, LintSource)>>,
93                       sess: &Session)
94         -> (Level, LintSource)
95     {
96         let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
97
98         // If `level` is none then we actually assume the default level for this
99         // lint.
100         let mut level = level.unwrap_or(lint.default_level(sess));
101
102         // If we're about to issue a warning, check at the last minute for any
103         // directives against the warnings "lint". If, for example, there's an
104         // `allow(warnings)` in scope then we want to respect that instead.
105         if level == Level::Warn {
106             let (warnings_level, warnings_src) =
107                 self.get_lint_id_level(LintId::of(lint::builtin::WARNINGS),
108                                        idx,
109                                        aux);
110             if let Some(configured_warning_level) = warnings_level {
111                 if configured_warning_level != Level::Warn {
112                     level = configured_warning_level;
113                     src = warnings_src;
114                 }
115             }
116         }
117
118         // Ensure that we never exceed the `--cap-lints` argument.
119         level = cmp::min(level, self.lint_cap);
120
121         return (level, src)
122     }
123
124     fn get_lint_id_level(&self,
125                          id: LintId,
126                          mut idx: u32,
127                          aux: Option<&FxHashMap<LintId, (Level, LintSource)>>)
128         -> (Option<Level>, LintSource)
129     {
130         if let Some(specs) = aux {
131             if let Some(&(level, src)) = specs.get(&id) {
132                 return (Some(level), src)
133             }
134         }
135         loop {
136             match self.list[idx as usize] {
137                 LintSet::CommandLine { ref specs } => {
138                     if let Some(&(level, src)) = specs.get(&id) {
139                         return (Some(level), src)
140                     }
141                     return (None, LintSource::Default)
142                 }
143                 LintSet::Node { ref specs, parent } => {
144                     if let Some(&(level, src)) = specs.get(&id) {
145                         return (Some(level), src)
146                     }
147                     idx = parent;
148                 }
149             }
150         }
151     }
152 }
153
154 pub struct LintLevelsBuilder<'a> {
155     sess: &'a Session,
156     sets: LintLevelSets,
157     id_to_set: FxHashMap<HirId, u32>,
158     cur: u32,
159     warn_about_weird_lints: bool,
160 }
161
162 pub struct BuilderPush {
163     prev: u32,
164 }
165
166 impl<'a> LintLevelsBuilder<'a> {
167     pub fn new(sess: &'a Session, sets: LintLevelSets) -> LintLevelsBuilder<'a> {
168         assert_eq!(sets.list.len(), 1);
169         LintLevelsBuilder {
170             sess,
171             sets,
172             cur: 0,
173             id_to_set: FxHashMap(),
174             warn_about_weird_lints: sess.buffered_lints.borrow().is_some(),
175         }
176     }
177
178     /// Pushes a list of AST lint attributes onto this context.
179     ///
180     /// This function will return a `BuilderPush` object which should be be
181     /// passed to `pop` when this scope for the attributes provided is exited.
182     ///
183     /// This function will perform a number of tasks:
184     ///
185     /// * It'll validate all lint-related attributes in `attrs`
186     /// * It'll mark all lint-related attriutes as used
187     /// * Lint levels will be updated based on the attributes provided
188     /// * Lint attributes are validated, e.g. a #[forbid] can't be switched to
189     ///   #[allow]
190     ///
191     /// Don't forget to call `pop`!
192     pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush {
193         let mut specs = FxHashMap();
194         let store = self.sess.lint_store.borrow();
195         let sess = self.sess;
196         let bad_attr = |span| {
197             span_err!(sess, span, E0452,
198                       "malformed lint attribute");
199         };
200         for attr in attrs {
201             let level = match attr.name().and_then(|name| Level::from_str(&name.as_str())) {
202                 None => continue,
203                 Some(lvl) => lvl,
204             };
205
206             let meta = unwrap_or!(attr.meta(), continue);
207             attr::mark_used(attr);
208
209             let metas = if let Some(metas) = meta.meta_item_list() {
210                 metas
211             } else {
212                 bad_attr(meta.span);
213                 continue
214             };
215
216             for li in metas {
217                 let word = match li.word() {
218                     Some(word) => word,
219                     None => {
220                         bad_attr(li.span);
221                         continue
222                     }
223                 };
224                 let name = word.name();
225                 match store.check_lint_name(&name.as_str()) {
226                     CheckLintNameResult::Ok(ids) => {
227                         let src = LintSource::Node(name, li.span);
228                         for id in ids {
229                             specs.insert(*id, (level, src));
230                         }
231                     }
232
233                     _ if !self.warn_about_weird_lints => {}
234
235                     CheckLintNameResult::Warning(ref msg) => {
236                         let lint = builtin::RENAMED_AND_REMOVED_LINTS;
237                         let (level, src) = self.sets.get_lint_level(lint,
238                                                                     self.cur,
239                                                                     Some(&specs),
240                                                                     &sess);
241                         lint::struct_lint_level(self.sess,
242                                                 lint,
243                                                 level,
244                                                 src,
245                                                 Some(li.span.into()),
246                                                 msg)
247                             .emit();
248                     }
249                     CheckLintNameResult::NoLint => {
250                         let lint = builtin::UNKNOWN_LINTS;
251                         let (level, src) = self.sets.get_lint_level(lint,
252                                                                     self.cur,
253                                                                     Some(&specs),
254                                                                     self.sess);
255                         let msg = format!("unknown lint: `{}`", name);
256                         let mut db = lint::struct_lint_level(self.sess,
257                                                 lint,
258                                                 level,
259                                                 src,
260                                                 Some(li.span.into()),
261                                                 &msg);
262                         if name.as_str().chars().any(|c| c.is_uppercase()) {
263                             let name_lower = name.as_str().to_lowercase();
264                             if let CheckLintNameResult::NoLint =
265                                     store.check_lint_name(&name_lower) {
266                                 db.emit();
267                             } else {
268                                 db.span_suggestion(
269                                     li.span,
270                                     "lowercase the lint name",
271                                     name_lower
272                                 ).emit();
273                             }
274                         } else {
275                             db.emit();
276                         }
277                     }
278                 }
279             }
280         }
281
282         for (id, &(level, ref src)) in specs.iter() {
283             if level == Level::Forbid {
284                 continue
285             }
286             let forbid_src = match self.sets.get_lint_id_level(*id, self.cur, None) {
287                 (Some(Level::Forbid), src) => src,
288                 _ => continue,
289             };
290             let forbidden_lint_name = match forbid_src {
291                 LintSource::Default => id.to_string(),
292                 LintSource::Node(name, _) => name.to_string(),
293                 LintSource::CommandLine(name) => name.to_string(),
294             };
295             let (lint_attr_name, lint_attr_span) = match *src {
296                 LintSource::Node(name, span) => (name, span),
297                 _ => continue,
298             };
299             let mut diag_builder = struct_span_err!(self.sess,
300                                                     lint_attr_span,
301                                                     E0453,
302                                                     "{}({}) overruled by outer forbid({})",
303                                                     level.as_str(),
304                                                     lint_attr_name,
305                                                     forbidden_lint_name);
306             diag_builder.span_label(lint_attr_span, "overruled by previous forbid");
307             match forbid_src {
308                 LintSource::Default => &mut diag_builder,
309                 LintSource::Node(_, forbid_source_span) => {
310                     diag_builder.span_label(forbid_source_span,
311                                             "`forbid` level set here")
312                 },
313                 LintSource::CommandLine(_) => {
314                     diag_builder.note("`forbid` lint level was set on command line")
315                 }
316             }.emit();
317             // don't set a separate error for every lint in the group
318             break
319         }
320
321         let prev = self.cur;
322         if specs.len() > 0 {
323             self.cur = self.sets.list.len() as u32;
324             self.sets.list.push(LintSet::Node {
325                 specs: specs,
326                 parent: prev,
327             });
328         }
329
330         BuilderPush {
331             prev: prev,
332         }
333     }
334
335     /// Called after `push` when the scope of a set of attributes are exited.
336     pub fn pop(&mut self, push: BuilderPush) {
337         self.cur = push.prev;
338     }
339
340     /// Used to emit a lint-related diagnostic based on the current state of
341     /// this lint context.
342     pub fn struct_lint(&self,
343                        lint: &'static Lint,
344                        span: Option<MultiSpan>,
345                        msg: &str)
346         -> DiagnosticBuilder<'a>
347     {
348         let (level, src) = self.sets.get_lint_level(lint, self.cur, None, self.sess);
349         lint::struct_lint_level(self.sess, lint, level, src, span, msg)
350     }
351
352     /// Registers the ID provided with the current set of lints stored in
353     /// this context.
354     pub fn register_id(&mut self, id: HirId) {
355         self.id_to_set.insert(id, self.cur);
356     }
357
358     pub fn build(self) -> LintLevelSets {
359         self.sets
360     }
361
362     pub fn build_map(self) -> LintLevelMap {
363         LintLevelMap {
364             sets: self.sets,
365             id_to_set: self.id_to_set,
366         }
367     }
368 }
369
370 pub struct LintLevelMap {
371     sets: LintLevelSets,
372     id_to_set: FxHashMap<HirId, u32>,
373 }
374
375 impl LintLevelMap {
376     /// If the `id` was previously registered with `register_id` when building
377     /// this `LintLevelMap` this returns the corresponding lint level and source
378     /// of the lint level for the lint provided.
379     ///
380     /// If the `id` was not previously registered, returns `None`. If `None` is
381     /// returned then the parent of `id` should be acquired and this function
382     /// should be called again.
383     pub fn level_and_source(&self, lint: &'static Lint, id: HirId, session: &Session)
384         -> Option<(Level, LintSource)>
385     {
386         self.id_to_set.get(&id).map(|idx| {
387             self.sets.get_lint_level(lint, *idx, None, session)
388         })
389     }
390
391     /// Returns if this `id` has lint level information.
392     pub fn lint_level_set(&self, id: HirId) -> Option<u32> {
393         self.id_to_set.get(&id).cloned()
394     }
395 }
396
397 impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
398     #[inline]
399     fn hash_stable<W: StableHasherResult>(&self,
400                                           hcx: &mut StableHashingContext<'a>,
401                                           hasher: &mut StableHasher<W>) {
402         let LintLevelMap {
403             ref sets,
404             ref id_to_set,
405         } = *self;
406
407         id_to_set.hash_stable(hcx, hasher);
408
409         let LintLevelSets {
410             ref list,
411             lint_cap,
412         } = *sets;
413
414         lint_cap.hash_stable(hcx, hasher);
415
416         hcx.while_hashing_spans(true, |hcx| {
417             list.len().hash_stable(hcx, hasher);
418
419             // We are working under the assumption here that the list of
420             // lint-sets is built in a deterministic order.
421             for lint_set in list {
422                 ::std::mem::discriminant(lint_set).hash_stable(hcx, hasher);
423
424                 match *lint_set {
425                     LintSet::CommandLine { ref specs } => {
426                         specs.hash_stable(hcx, hasher);
427                     }
428                     LintSet::Node { ref specs, parent } => {
429                         specs.hash_stable(hcx, hasher);
430                         parent.hash_stable(hcx, hasher);
431                     }
432                 }
433             }
434         })
435     }
436 }
437
438 impl<HCX> HashStable<HCX> for LintId {
439     #[inline]
440     fn hash_stable<W: StableHasherResult>(&self,
441                                           hcx: &mut HCX,
442                                           hasher: &mut StableHasher<W>) {
443         self.lint_name_raw().hash_stable(hcx, hasher);
444     }
445 }
446
447 impl<HCX> ToStableHashKey<HCX> for LintId {
448     type KeyType = &'static str;
449
450     #[inline]
451     fn to_stable_hash_key(&self, _: &HCX) -> &'static str {
452         self.lint_name_raw()
453     }
454 }