]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/diagnostics/plugin.rs
rollup merge of #20607: nrc/kinds
[rust.git] / src / libsyntax / diagnostics / plugin.rs
1 // Copyright 2014 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::cell::RefCell;
12 use std::collections::HashMap;
13 use ast;
14 use ast::{Ident, Name, TokenTree};
15 use codemap::Span;
16 use ext::base::{ExtCtxt, MacExpr, MacResult, MacItems};
17 use ext::build::AstBuilder;
18 use parse::token;
19 use ptr::P;
20
21 thread_local! {
22     static REGISTERED_DIAGNOSTICS: RefCell<HashMap<Name, Option<Name>>> = {
23         RefCell::new(HashMap::new())
24     }
25 }
26 thread_local! {
27     static USED_DIAGNOSTICS: RefCell<HashMap<Name, Span>> = {
28         RefCell::new(HashMap::new())
29     }
30 }
31
32 fn with_registered_diagnostics<T, F>(f: F) -> T where
33     F: FnOnce(&mut HashMap<Name, Option<Name>>) -> T,
34 {
35     REGISTERED_DIAGNOSTICS.with(move |slot| {
36         f(&mut *slot.borrow_mut())
37     })
38 }
39
40 fn with_used_diagnostics<T, F>(f: F) -> T where
41     F: FnOnce(&mut HashMap<Name, Span>) -> T,
42 {
43     USED_DIAGNOSTICS.with(move |slot| {
44         f(&mut *slot.borrow_mut())
45     })
46 }
47
48 pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
49                                    span: Span,
50                                    token_tree: &[TokenTree])
51                                    -> Box<MacResult+'cx> {
52     let code = match token_tree {
53         [ast::TtToken(_, token::Ident(code, _))] => code,
54         _ => unreachable!()
55     };
56     with_used_diagnostics(|diagnostics| {
57         match diagnostics.insert(code.name, span) {
58             Some(previous_span) => {
59                 ecx.span_warn(span, format!(
60                     "diagnostic code {} already used", token::get_ident(code).get()
61                 ).index(&FullRange));
62                 ecx.span_note(previous_span, "previous invocation");
63             },
64             None => ()
65         }
66         ()
67     });
68     MacExpr::new(quote_expr!(ecx, ()))
69 }
70
71 pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
72                                        span: Span,
73                                        token_tree: &[TokenTree])
74                                        -> Box<MacResult+'cx> {
75     let (code, description) = match token_tree {
76         [ast::TtToken(_, token::Ident(ref code, _))] => {
77             (code, None)
78         },
79         [ast::TtToken(_, token::Ident(ref code, _)),
80          ast::TtToken(_, token::Comma),
81          ast::TtToken(_, token::Literal(token::StrRaw(description, _), None))] => {
82             (code, Some(description))
83         }
84         _ => unreachable!()
85     };
86     with_registered_diagnostics(|diagnostics| {
87         if diagnostics.insert(code.name, description).is_some() {
88             ecx.span_err(span, format!(
89                 "diagnostic code {} already registered", token::get_ident(*code).get()
90             ).index(&FullRange));
91         }
92     });
93     let sym = Ident::new(token::gensym((
94         "__register_diagnostic_".to_string() + token::get_ident(*code).get()
95     ).index(&FullRange)));
96     MacItems::new(vec![quote_item!(ecx, mod $sym {}).unwrap()].into_iter())
97 }
98
99 pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
100                                           span: Span,
101                                           token_tree: &[TokenTree])
102                                           -> Box<MacResult+'cx> {
103     let name = match token_tree {
104         [ast::TtToken(_, token::Ident(ref name, _))] => name,
105         _ => unreachable!()
106     };
107
108     let (count, expr) =
109         with_registered_diagnostics(|diagnostics| {
110             let descriptions: Vec<P<ast::Expr>> =
111                 diagnostics.iter().filter_map(|(code, description)| {
112                     description.map(|description| {
113                         ecx.expr_tuple(span, vec![
114                             ecx.expr_str(span, token::get_name(*code)),
115                             ecx.expr_str(span, token::get_name(description))])
116                     })
117                 }).collect();
118             (descriptions.len(), ecx.expr_vec(span, descriptions))
119         });
120
121     MacItems::new(vec![quote_item!(ecx,
122         pub static $name: [(&'static str, &'static str); $count] = $expr;
123     ).unwrap()].into_iter())
124 }