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